Skip to content

Commit

Permalink
contracts,sqlite: revert contract revision changes
Browse files Browse the repository at this point in the history
  • Loading branch information
n8maninger committed Oct 5, 2023
1 parent cf5057c commit e27d43f
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 27 deletions.
4 changes: 2 additions & 2 deletions host/contracts/contracts.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,10 @@ type (
once sync.Once
done func() // done is called when the updater is closed.

sectors uint64
contractID types.FileContractID
sectorActions []SectorChange
sectorRoots []types.Hash256
oldRoots []types.Hash256
}
)

Expand Down Expand Up @@ -331,7 +331,7 @@ func (cu *ContractUpdater) Commit(revision SignedRevision, usage Usage) error {

start := time.Now()
// revise the contract
err := cu.store.ReviseContract(revision, usage, cu.sectorActions)
err := cu.store.ReviseContract(revision, cu.oldRoots, usage, cu.sectorActions)
if err == nil {
// clear the committed sector actions
cu.sectorActions = cu.sectorActions[:0]
Expand Down
4 changes: 2 additions & 2 deletions host/contracts/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -466,8 +466,8 @@ func (cm *ContractManager) ReviseContract(contractID types.FileContractID) (*Con

rootsCache: cm.rootsCache,
contractID: contractID,
sectors: uint64(len(roots)),
sectorRoots: roots,
sectorRoots: roots, // roots is already a deep copy
oldRoots: append([]types.Hash256(nil), roots...),

done: done, // decrements the threadgroup counter after the updater is closed
}, nil
Expand Down
2 changes: 1 addition & 1 deletion host/contracts/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1013,7 +1013,7 @@ func TestSectorRoots(t *testing.T) {
defer release()

// use the database method directly to avoid the sector cache
err = db.ReviseContract(rev, contracts.Usage{}, []contracts.SectorChange{
err = db.ReviseContract(rev, roots, contracts.Usage{}, []contracts.SectorChange{
{Action: contracts.SectorActionAppend, Root: root},
})
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion host/contracts/persist.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ type (
ContractAction(height uint64, contractFn func(types.FileContractID, uint64, string)) error
// ReviseContract atomically updates a contract and its associated
// sector roots.
ReviseContract(revision SignedRevision, usage Usage, sectorChanges []SectorChange) error
ReviseContract(revision SignedRevision, oldRoots []types.Hash256, usage Usage, sectorChanges []SectorChange) error
// UpdateContractState atomically updates the contract manager's state.
UpdateContractState(modules.ConsensusChangeID, uint64, func(UpdateStateTransaction) error) error
// ExpireContractSectors removes sector roots for any contracts that are
Expand Down
17 changes: 2 additions & 15 deletions persist/sqlite/contracts.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,17 +259,8 @@ func (s *Store) RenewContract(renewal contracts.SignedRevision, clearing contrac
})
}

func contractSectorRoots(tx txn, contractID int64) (uint64, error) {
var index uint64
err := tx.QueryRow(`SELECT COUNT(*) FROM contract_sector_roots WHERE contract_id=$1`, contractID).Scan(&index)
if errors.Is(err, sql.ErrNoRows) {
return 0, nil
}
return index, err
}

// ReviseContract atomically updates a contract's revision and sectors
func (s *Store) ReviseContract(revision contracts.SignedRevision, usage contracts.Usage, sectorChanges []contracts.SectorChange) error {
func (s *Store) ReviseContract(revision contracts.SignedRevision, oldRoots []types.Hash256, usage contracts.Usage, sectorChanges []contracts.SectorChange) error {
return s.transaction(func(tx txn) error {
// revise the contract
contractID, err := reviseContract(tx, revision)
Expand All @@ -286,11 +277,7 @@ func (s *Store) ReviseContract(revision contracts.SignedRevision, usage contract
}

// update the sector roots
sectors, err := contractSectorRoots(tx, contractID)
if err != nil {
return fmt.Errorf("failed to get sector index: %w", err)
}

sectors := uint64(len(oldRoots))
for _, change := range sectorChanges {
switch change.Action {
case contracts.SectorActionAppend:
Expand Down
7 changes: 4 additions & 3 deletions persist/sqlite/contracts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func rootsEqual(a, b []types.Hash256) error {
return nil
}

func runRevision(db *Store, revision contracts.SignedRevision, changes []contracts.SectorChange) error {
func runRevision(db *Store, revision contracts.SignedRevision, roots []types.Hash256, changes []contracts.SectorChange) error {
for _, change := range changes {
switch change.Action {
// store a sector in the database for the append or update actions
Expand All @@ -52,7 +52,7 @@ func runRevision(db *Store, revision contracts.SignedRevision, changes []contrac
}
}

return db.ReviseContract(revision, contracts.Usage{}, changes)
return db.ReviseContract(revision, roots, contracts.Usage{}, changes)
}

func TestReviseContract(t *testing.T) {
Expand Down Expand Up @@ -261,6 +261,7 @@ func TestReviseContract(t *testing.T) {
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
oldRoots := append([]types.Hash256(nil), roots...)
// update the expected roots
for i, change := range test.changes {
switch change.Action {
Expand Down Expand Up @@ -301,7 +302,7 @@ func TestReviseContract(t *testing.T) {
}
}

if err := runRevision(db, contract, test.changes); err != nil {
if err := runRevision(db, contract, oldRoots, test.changes); err != nil {
if test.errors {
t.Log("received error:", err)
return
Expand Down
7 changes: 4 additions & 3 deletions persist/sqlite/volumes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -652,7 +652,7 @@ func TestPrune(t *testing.T) {
Action: contracts.SectorActionAppend,
})
}
err = db.ReviseContract(c, contracts.Usage{}, changes)
err = db.ReviseContract(c, []types.Hash256{}, contracts.Usage{}, changes)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -776,11 +776,12 @@ func TestPrune(t *testing.T) {
changes = []contracts.SectorChange{
{Action: contracts.SectorActionTrim, A: uint64(len(contractSectors) / 2)},
}
if err := db.ReviseContract(c, contracts.Usage{}, changes); err != nil {
if err := db.ReviseContract(c, contractSectors, contracts.Usage{}, changes); err != nil {
t.Fatal(err)
}
contractSectors = contractSectors[:len(contractSectors)/2]

if err := checkConsistency(contractSectors[:len(contractSectors)/2], nil, nil, roots[50:]); err != nil {
if err := checkConsistency(contractSectors, nil, nil, roots[50:]); err != nil {
t.Fatal(err)
}

Expand Down

0 comments on commit e27d43f

Please sign in to comment.