Skip to content

Commit

Permalink
Merge pull request #495 from SiaFoundation/nate/update-dependencies
Browse files Browse the repository at this point in the history
Update core and coreutils
  • Loading branch information
n8maninger authored Nov 1, 2024
2 parents 9a39669 + c9edf02 commit 8319b75
Show file tree
Hide file tree
Showing 20 changed files with 522 additions and 358 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ require (
github.com/hashicorp/golang-lru/v2 v2.0.7
github.com/mattn/go-sqlite3 v1.14.24
github.com/shopspring/decimal v1.4.0
go.sia.tech/core v0.4.8-0.20240926222149-2c8b541119dc
go.sia.tech/coreutils v0.4.1
go.sia.tech/core v0.6.1
go.sia.tech/coreutils v0.6.0
go.sia.tech/jape v0.12.1
go.sia.tech/web/hostd v0.49.0
go.uber.org/goleak v1.3.0
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsT
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
go.etcd.io/bbolt v1.3.11 h1:yGEzV1wPz2yVCLsD8ZAiGHhHVlczyC9d1rP43/VCRJ0=
go.etcd.io/bbolt v1.3.11/go.mod h1:dksAq7YMXoljX0xu6VF5DMZGbhYYoLUalEiSySYAS4I=
go.sia.tech/core v0.4.8-0.20240926222149-2c8b541119dc h1:+hCcYky+23HtiAnirXsq0U/NaCt1WuIu308lmfTtJNM=
go.sia.tech/core v0.4.8-0.20240926222149-2c8b541119dc/go.mod h1:j2Ke8ihV8or7d2VDrFZWcCkwSVHO0DNMQJAGs9Qop2M=
go.sia.tech/coreutils v0.4.1 h1:ExQ9g6EtnFe70ptNBG+OtZyFU3aBoEzE/06rtbN6f4c=
go.sia.tech/coreutils v0.4.1/go.mod h1:v60kPqZERsb1ZS0PVe4S8hr2ArNEwTdp7XTzErXnV2U=
go.sia.tech/core v0.6.1 h1:eaExM2E2eNr43su2XDkY5J24E3F54YGS7hcC3WtVjVk=
go.sia.tech/core v0.6.1/go.mod h1:P3C1BWa/7J4XgdzWuaYHBvLo2RzZ0UBaJM4TG1GWB2g=
go.sia.tech/coreutils v0.6.0 h1:r0IZt+aVdGG2uIHl7OtaWRYdVx4NQ7ezRoSGa0Ej8GY=
go.sia.tech/coreutils v0.6.0/go.mod h1:XlsnogeYU/Tdjzp/HUNAj5T7tZCdmeBHIBjymbPC+uQ=
go.sia.tech/jape v0.12.1 h1:xr+o9V8FO8ScRqbSaqYf9bjj1UJ2eipZuNcI1nYousU=
go.sia.tech/jape v0.12.1/go.mod h1:wU+h6Wh5olDjkPXjF0tbZ1GDgoZ6VTi4naFw91yyWC4=
go.sia.tech/mux v1.3.0 h1:hgR34IEkqvfBKUJkAzGi31OADeW2y7D6Bmy/Jcbop9c=
Expand Down
2 changes: 1 addition & 1 deletion host/contracts/integrity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func TestCheckIntegrity(t *testing.T) {
}

// mine enough for the wallet to have some funds
mineAndSync(t, cm, db, wm.Address(), 150)
mineAndSync(t, cm, db, wm.Address(), cm.Tip().Network.MaturityDelay+5)

rev, err := formContract(renterKey, hostKey, 50, 60, types.Siacoins(500), types.Siacoins(1000), c, node, node.ChainManager(), node.TPool())
if err != nil {
Expand Down
8 changes: 3 additions & 5 deletions host/contracts/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -355,14 +355,12 @@ func (cm *Manager) RenewV2Contract(renewal V2FormationTransactionSet, usage V2Us
fc := resolution.NewContract

// sanity checks
if finalRevision.FileMerkleRoot != (types.Hash256{}) {
return errors.New("existing contract must be cleared")
} else if finalRevision.Filesize != 0 {
return errors.New("existing contract must be cleared")
} else if finalRevision.RevisionNumber != types.MaxRevisionNumber {
if finalRevision.RevisionNumber != types.MaxRevisionNumber {
return errors.New("existing contract must be cleared")
} else if fc.Filesize != existing.Filesize {
return errors.New("renewal contract must have same file size as existing contract")
} else if fc.Capacity != existing.Capacity {
return errors.New("renewal contract must have same capacity as existing contract")
} else if fc.FileMerkleRoot != existing.FileMerkleRoot {
return errors.New("renewal root does not match existing roots")
}
Expand Down
37 changes: 21 additions & 16 deletions host/contracts/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"time"

rhp2 "go.sia.tech/core/rhp/v2"
rhp4 "go.sia.tech/core/rhp/v4"
"go.sia.tech/core/types"
"go.sia.tech/coreutils/chain"
"go.sia.tech/coreutils/syncer"
Expand All @@ -35,6 +36,7 @@ func formV2Contract(t *testing.T, cm *chain.Manager, c *contracts.Manager, w *wa
fc := types.V2FileContract{
RevisionNumber: 0,
Filesize: 0,
Capacity: 0,
FileMerkleRoot: types.Hash256{},
ProofHeight: cs.Index.Height + duration,
ExpirationHeight: cs.Index.Height + duration + 10,
Expand Down Expand Up @@ -227,7 +229,7 @@ func TestContractLifecycle(t *testing.T) {

network, genesis := testutil.V1Network()
node := testutil.NewHostNode(t, hostKey, network, genesis, log)
testutil.MineAndSync(t, node, node.Wallet.Address(), 150)
testutil.MineAndSync(t, node, node.Wallet.Address(), int(network.MaturityDelay+5))

cm := node.Chain
c := node.Contracts
Expand Down Expand Up @@ -287,7 +289,7 @@ func TestContractLifecycle(t *testing.T) {

network, genesis := testutil.V1Network()
node := testutil.NewHostNode(t, hostKey, network, genesis, log)
testutil.MineAndSync(t, node, node.Wallet.Address(), 150)
testutil.MineAndSync(t, node, node.Wallet.Address(), int(network.MaturityDelay+5))

rev := formContract(t, node.Chain, node.Contracts, node.Wallet, node.Syncer, renterKey, hostKey, types.Siacoins(10), types.Siacoins(20), 10, false)
assertContractStatus(t, node.Contracts, rev.Revision.ParentID, contracts.ContractStatusPending)
Expand Down Expand Up @@ -325,7 +327,7 @@ func TestContractLifecycle(t *testing.T) {
t.Fatal(err)
}

testutil.MineAndSync(t, node, node.Wallet.Address(), 150)
testutil.MineAndSync(t, node, node.Wallet.Address(), int(network.MaturityDelay+5))

renterFunds := types.Siacoins(500)
hostCollateral := types.Siacoins(1000)
Expand Down Expand Up @@ -437,7 +439,7 @@ func TestContractLifecycle(t *testing.T) {
t.Fatal(err)
}

testutil.MineAndSync(t, node, node.Wallet.Address(), 150)
testutil.MineAndSync(t, node, node.Wallet.Address(), int(network.MaturityDelay+5))

renterFunds := types.Siacoins(500)
hostCollateral := types.Siacoins(1000)
Expand Down Expand Up @@ -531,7 +533,7 @@ func TestContractLifecycle(t *testing.T) {
t.Fatal(err)
}

testutil.MineAndSync(t, node, node.Wallet.Address(), 150)
testutil.MineAndSync(t, node, node.Wallet.Address(), int(network.MaturityDelay+5))

renterFunds := types.Siacoins(500)
hostCollateral := types.Siacoins(1000)
Expand Down Expand Up @@ -610,7 +612,7 @@ func TestContractLifecycle(t *testing.T) {
t.Fatal(err)
}

testutil.MineAndSync(t, node, node.Wallet.Address(), 150)
testutil.MineAndSync(t, node, node.Wallet.Address(), int(network.MaturityDelay+5))

renterFunds := types.Siacoins(500)
hostCollateral := types.Siacoins(1000)
Expand Down Expand Up @@ -723,7 +725,7 @@ func TestV2ContractLifecycle(t *testing.T) {
}

// fund the wallet
testutil.MineAndSync(t, node, node.Wallet.Address(), 150)
testutil.MineAndSync(t, node, node.Wallet.Address(), int(network.MaturityDelay+5))

assertContractStatus := func(t *testing.T, contractID types.FileContractID, status contracts.V2ContractStatus) {
t.Helper()
Expand Down Expand Up @@ -851,8 +853,9 @@ func TestV2ContractLifecycle(t *testing.T) {
}
defer release()

fc.Filesize = rhp2.SectorSize
fc.FileMerkleRoot = rhp2.MetaRoot(roots)
fc.Filesize = rhp4.SectorSize
fc.Capacity = rhp4.SectorSize
fc.FileMerkleRoot = rhp4.MetaRoot(roots)
fc.RevisionNumber++
// transfer some funds from the renter to the host
cost, collateral := types.Siacoins(1), types.Siacoins(2)
Expand Down Expand Up @@ -910,8 +913,9 @@ func TestV2ContractLifecycle(t *testing.T) {
}
defer release()

fc.Filesize = rhp2.SectorSize
fc.FileMerkleRoot = rhp2.MetaRoot(roots)
fc.Filesize = rhp4.SectorSize
fc.Capacity = rhp4.SectorSize
fc.FileMerkleRoot = rhp4.MetaRoot(roots)
fc.RevisionNumber++
// transfer some funds from the renter to the host
cost, collateral := types.Siacoins(1), types.Siacoins(2)
Expand Down Expand Up @@ -969,8 +973,9 @@ func TestV2ContractLifecycle(t *testing.T) {
}
defer release()

fc.Filesize = rhp2.SectorSize
fc.FileMerkleRoot = rhp2.MetaRoot(roots)
fc.Filesize = rhp4.SectorSize
fc.Capacity = rhp4.SectorSize
fc.FileMerkleRoot = rhp4.MetaRoot(roots)
fc.RevisionNumber++
// transfer some funds from the renter to the host
cost, collateral := types.Siacoins(1), types.Siacoins(2)
Expand Down Expand Up @@ -1006,8 +1011,6 @@ func TestV2ContractLifecycle(t *testing.T) {
cs := cm.TipState()
final := fc
final.RevisionNumber = types.MaxRevisionNumber
final.FileMerkleRoot = types.Hash256{}
final.Filesize = 0
final.HostSignature = types.Signature{}
final.RenterSignature = types.Signature{}
final.RevisionNumber = types.MaxRevisionNumber
Expand All @@ -1018,6 +1021,7 @@ func TestV2ContractLifecycle(t *testing.T) {
NewContract: types.V2FileContract{
RevisionNumber: 0,
Filesize: fc.Filesize,
Capacity: fc.Capacity,
FileMerkleRoot: fc.FileMerkleRoot,
ProofHeight: final.ProofHeight + 10,
ExpirationHeight: final.ExpirationHeight + 10,
Expand Down Expand Up @@ -1124,6 +1128,7 @@ func TestV2ContractLifecycle(t *testing.T) {
fc := types.V2FileContract{
RevisionNumber: 0,
Filesize: 0,
Capacity: 0,
FileMerkleRoot: types.Hash256{},
ProofHeight: cs.Index.Height + duration,
ExpirationHeight: cs.Index.Height + duration + 10,
Expand Down Expand Up @@ -1198,7 +1203,7 @@ func TestSectorRoots(t *testing.T) {
t.Fatal(err)
}

testutil.MineAndSync(t, node, node.Wallet.Address(), 150)
testutil.MineAndSync(t, node, node.Wallet.Address(), int(network.MaturityDelay+5))

// create a fake volume so disk space is not used
id, err := node.Store.AddVolume("test", false)
Expand Down
107 changes: 21 additions & 86 deletions host/contracts/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
rhp2 "go.sia.tech/core/rhp/v2"
"go.sia.tech/core/types"
"go.sia.tech/coreutils/chain"
"go.sia.tech/coreutils/wallet"
"go.uber.org/zap"
)

Expand Down Expand Up @@ -58,12 +59,9 @@ type (
// An UpdateStateTx atomically updates the state of contracts in the contract
// store.
UpdateStateTx interface {
// ContractStateElements returns all state elements from the contract
// store
ContractStateElements() ([]types.StateElement, error)
// UpdateContractStateElements updates the state elements in the host
// UpdateContractElementProofs updates the state elements in the host
// contract store
UpdateContractStateElements([]types.StateElement) error
UpdateContractElementProofs(wallet.ProofUpdater) error
// ContractRelevant returns whether the contract with the provided id is
// relevant to the host
ContractRelevant(id types.FileContractID) (bool, error)
Expand All @@ -81,18 +79,18 @@ type (
// been confirmed to rejected
RejectContracts(height uint64) (v1, v2 []types.FileContractID, err error)

// ContractChainIndexElements returns all chain index elements from the
// contract store
ContractChainIndexElements() (elements []types.ChainIndexElement, err error)
// ApplyContractChainIndexElements adds or updates the merkle proof of
// AddContractChainIndexElement adds or updates the merkle proof of
// chain index state elements
ApplyContractChainIndexElements(elements []types.ChainIndexElement) error
AddContractChainIndexElement(elements types.ChainIndexElement) error
// RevertContractChainIndexElements removes chain index state elements
// that were reverted
RevertContractChainIndexElement(types.ChainIndex) error
// UpdateChainIndexElementProofs returns all chain index elements from the
// contract store
UpdateChainIndexElementProofs(wallet.ProofUpdater) error
// DeleteExpiredContractChainIndexElements deletes chain index state
// elements that are no long necessary
DeleteExpiredContractChainIndexElements(height uint64) error
DeleteExpiredChainIndexElements(height uint64) error
}
)

Expand Down Expand Up @@ -533,20 +531,6 @@ func buildContractState(tx UpdateStateTx, u stateUpdater, revert bool, log *zap.
func (cm *Manager) UpdateChainState(tx UpdateStateTx, reverted []chain.RevertUpdate, applied []chain.ApplyUpdate) error {
log := cm.log.Named("updateChainState")

chainElements, err := tx.ContractChainIndexElements()
if err != nil {
return fmt.Errorf("failed to get chain index state elements: %w", err)
}

v2ContractStateElements, err := tx.ContractStateElements()
if err != nil {
return fmt.Errorf("failed to get contract state elements: %w", err)
}
v2ContractElementMap := make(map[types.Hash256]*types.StateElement, len(v2ContractStateElements))
for _, ele := range v2ContractStateElements {
v2ContractElementMap[ele.ID] = &ele
}

for _, cru := range reverted {
revertedIndex := types.ChainIndex{
ID: cru.Block.ID(),
Expand All @@ -557,32 +541,12 @@ func (cm *Manager) UpdateChainState(tx UpdateStateTx, reverted []chain.RevertUpd
state := buildContractState(tx, cru, true, log.Named("revert").With(zap.Stringer("index", revertedIndex)))
if err := tx.RevertContracts(revertedIndex, state); err != nil {
return fmt.Errorf("failed to revert contracts: %w", err)
}

// delete reverted contract state elements from the map
for _, reverted := range state.ConfirmedV2 {
delete(v2ContractElementMap, reverted.ID)
}
// update remaining contract state elements
for key := range v2ContractElementMap {
cru.UpdateElementProof(v2ContractElementMap[key])
}

// revert contract chain index element
if err := tx.RevertContractChainIndexElement(revertedIndex); err != nil {
} else if err := tx.RevertContractChainIndexElement(revertedIndex); err != nil {
return fmt.Errorf("failed to revert chain index state element: %w", err)
}

// update chain state elements
if len(chainElements) > 0 {
last := chainElements[len(chainElements)-1]
if last.ChainIndex != revertedIndex {
panic(fmt.Errorf("unexpected chain index: %v != %v", last.ChainIndex, revertedIndex)) // developer error
}
chainElements = chainElements[:len(chainElements)-1]
for i := range chainElements {
cru.UpdateElementProof(&chainElements[i].StateElement)
}
} else if err := tx.UpdateChainIndexElementProofs(cru); err != nil {
return fmt.Errorf("failed to update chain index elements: %w", err)
} else if err := tx.UpdateContractElementProofs(cru); err != nil {
return fmt.Errorf("failed to update contract element proofs: %w", err)
}
}

Expand All @@ -593,23 +557,12 @@ func (cm *Manager) UpdateChainState(tx UpdateStateTx, reverted []chain.RevertUpd
return fmt.Errorf("failed to revert contracts: %w", err)
}

// update existing contract state elements
for id := range v2ContractElementMap {
cau.UpdateElementProof(v2ContractElementMap[id])
}
// add new contract state elements
for _, applied := range state.ConfirmedV2 {
v2ContractElementMap[applied.ID] = &applied.StateElement
}

// update existing chain index elements proofs
for i := range chainElements {
cau.UpdateElementProof(&chainElements[i].StateElement)
}
// add new chain index element
chainElements = append(chainElements, cau.ChainIndexElement())
if len(chainElements) > chainIndexBuffer {
chainElements = chainElements[len(chainElements)-chainIndexBuffer:]
if err := tx.UpdateChainIndexElementProofs(cau); err != nil {
return fmt.Errorf("failed to update chain index elements: %w", err)
} else if err := tx.UpdateContractElementProofs(cau); err != nil {
return fmt.Errorf("failed to update contract element proofs: %w", err)
} else if err := tx.AddContractChainIndexElement(cau.ChainIndexElement()); err != nil {
return fmt.Errorf("failed to add chain index state element: %w", err)
}

// reject any contracts that have not been confirmed after the reject buffer
Expand All @@ -632,28 +585,10 @@ func (cm *Manager) UpdateChainState(tx UpdateStateTx, reverted []chain.RevertUpd
// delete any chain index elements outside of the proof window buffer
if cau.State.Index.Height > chainIndexBuffer {
minHeight := cau.State.Index.Height - chainIndexBuffer
if err := tx.DeleteExpiredContractChainIndexElements(minHeight); err != nil {
if err := tx.DeleteExpiredChainIndexElements(minHeight); err != nil {
return fmt.Errorf("failed to delete expired chain index elements: %w", err)
}
}
}

// update chain index state elements
if len(chainElements) > 0 {
if err := tx.ApplyContractChainIndexElements(chainElements); err != nil {
return fmt.Errorf("failed to update chain index state elements: %w", err)
}
}

// update contract state elements
if len(v2ContractElementMap) > 0 {
contractStateElements := make([]types.StateElement, 0, len(v2ContractElementMap))
for _, ele := range v2ContractElementMap {
contractStateElements = append(contractStateElements, *ele)
}
if err := tx.UpdateContractStateElements(contractStateElements); err != nil {
return fmt.Errorf("failed to update contract state elements: %w", err)
}
}
return nil
}
Loading

0 comments on commit 8319b75

Please sign in to comment.