Skip to content

Commit

Permalink
update bucket enumer, delete keys after migrate
Browse files Browse the repository at this point in the history
  • Loading branch information
weiihann committed Oct 17, 2024
1 parent 066aed1 commit 3db5746
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 15 deletions.
2 changes: 1 addition & 1 deletion cmd/juno/dbcmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ func dbSize(cmd *cobra.Command, args []string) error {
totalSize += bucketItem.Size
totalCount += bucketItem.Count

if utils.AnyOf(b, db.StateTrie, db.ContractStorage, db.Class, db.ContractNonce, db.ContractDeploymentHeight) {
if utils.AnyOf(b, db.StateTrie, db.ContractStorage, db.Class, db.Contract) {
withoutHistorySize += bucketItem.Size
withHistorySize += bucketItem.Size

Expand Down
1 change: 0 additions & 1 deletion db/buckets.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ const (
Temporary // used temporarily for migrations
SchemaIntermediateState // used for db schema metadata
Contract // Contract + ContractAddr -> Encoded(Contract)
ContractHistory // ContractHistory + ContractAddr + BlockHeight -> Encoded(Contract)
)

// Key flattens a prefix and series of byte arrays into a single []byte.
Expand Down
12 changes: 8 additions & 4 deletions db/buckets_enumer.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 36 additions & 9 deletions migration/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -724,15 +724,15 @@ func MigrateContractFields(txn db.Transaction, _ *utils.Network) error {
return err
}

if err := collectContractNonces(it, contracts); err != nil {
if err := collectContractNonces(txn, contracts); err != nil {
return err
}

if err := collectContractClassHashes(it, contracts); err != nil {
if err := collectContractClassHashes(txn, contracts); err != nil {
return err
}

if err := collectContractDeploymentHeights(it, contracts); err != nil {
if err := collectContractDeploymentHeights(txn, contracts); err != nil {
return err
}

Expand All @@ -743,7 +743,12 @@ func MigrateContractFields(txn db.Transaction, _ *utils.Network) error {
return it.Close()
}

func collectContractNonces(it db.Iterator, contracts map[felt.Felt]*core.StateContract) error {
func collectContractNonces(txn db.Transaction, contracts map[felt.Felt]*core.StateContract) error {
it, err := txn.NewIterator()
if err != nil {
return err
}

noncePrefix := db.ContractNonce.Key()
for it.Seek(noncePrefix); it.Valid(); it.Next() {
key := it.Key()
Expand All @@ -767,12 +772,21 @@ func collectContractNonces(it db.Iterator, contracts map[felt.Felt]*core.StateCo
Nonce: new(felt.Felt).SetBytes(value),
}
contracts[*addrFelt] = contract

if err := txn.Delete(key); err != nil {
return err
}
}

return nil
return it.Close()
}

func collectContractClassHashes(it db.Iterator, contracts map[felt.Felt]*core.StateContract) error {
func collectContractClassHashes(txn db.Transaction, contracts map[felt.Felt]*core.StateContract) error {
it, err := txn.NewIterator()
if err != nil {
return err
}

classHashPrefix := db.ContractClassHash.Key()
for it.Seek(classHashPrefix); it.Valid(); it.Next() {
key := it.Key()
Expand All @@ -797,12 +811,21 @@ func collectContractClassHashes(it db.Iterator, contracts map[felt.Felt]*core.St
}

contracts[*addrFelt].ClassHash = new(felt.Felt).SetBytes(value)

if err := txn.Delete(key); err != nil {
return err
}
}

return nil
return it.Close()
}

func collectContractDeploymentHeights(it db.Iterator, contracts map[felt.Felt]*core.StateContract) error {
func collectContractDeploymentHeights(txn db.Transaction, contracts map[felt.Felt]*core.StateContract) error {
it, err := txn.NewIterator()
if err != nil {
return err
}

deployHeightPrefix := db.ContractDeploymentHeight.Key()
for it.Seek(deployHeightPrefix); it.Valid(); it.Next() {
key := it.Key()
Expand All @@ -827,9 +850,13 @@ func collectContractDeploymentHeights(it db.Iterator, contracts map[felt.Felt]*c
}

contracts[*addrFelt].DeployHeight = binary.BigEndian.Uint64(value)

if err := txn.Delete(key); err != nil {
return err
}
}

return nil
return it.Close()
}

func storeUpdatedContracts(txn db.Transaction, contracts map[felt.Felt]*core.StateContract) error {
Expand Down

0 comments on commit 3db5746

Please sign in to comment.