Skip to content

Commit

Permalink
Add migration for p2p hash
Browse files Browse the repository at this point in the history
  • Loading branch information
kirugan committed Aug 9, 2024
1 parent 0fe951e commit 28d57db
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 10 deletions.
12 changes: 6 additions & 6 deletions blockchain/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ func (b *Blockchain) StateUpdateByNumber(number uint64) (*core.StateUpdate, erro
var update *core.StateUpdate
return update, b.database.View(func(txn db.Transaction) error {
var err error
update, err = stateUpdateByNumber(txn, number)
update, err = StateUpdateByNumber(txn, number)
return err
})
}
Expand Down Expand Up @@ -372,7 +372,7 @@ func (b *Blockchain) Store(block *core.Block, blockCommitments *core.BlockCommit
}

// todo only for <0.13.2 blocks
if err := storeP2PHash(txn, block, stateUpdate.StateDiff); err != nil {
if err := StoreP2PHash(txn, block, stateUpdate.StateDiff); err != nil {
return err
}

Expand All @@ -398,7 +398,7 @@ func (b *Blockchain) VerifyBlock(block *core.Block) error {
})
}

func storeP2PHash(txn db.Transaction, block *core.Block, stateDiff *core.StateDiff) error {
func StoreP2PHash(txn db.Transaction, block *core.Block, stateDiff *core.StateDiff) error {
originalParentHash := block.ParentHash
if block.Number > 0 {
prevP2PHash, err := blockP2PHashByNumber(txn, block.Number-1)
Expand Down Expand Up @@ -647,7 +647,7 @@ func storeStateUpdate(txn db.Transaction, blockNumber uint64, update *core.State
return txn.Set(db.StateUpdatesByBlockNumber.Key(numBytes), updateBytes)
}

func stateUpdateByNumber(txn db.Transaction, blockNumber uint64) (*core.StateUpdate, error) {
func StateUpdateByNumber(txn db.Transaction, blockNumber uint64) (*core.StateUpdate, error) {
numBytes := core.MarshalBlockNumber(blockNumber)

var update *core.StateUpdate
Expand All @@ -664,7 +664,7 @@ func stateUpdateByHash(txn db.Transaction, hash *felt.Felt) (*core.StateUpdate,
var update *core.StateUpdate
return update, txn.Get(db.BlockHeaderNumbersByHash.Key(hash.Marshal()), func(val []byte) error {
var err error
update, err = stateUpdateByNumber(txn, binary.BigEndian.Uint64(val))
update, err = StateUpdateByNumber(txn, binary.BigEndian.Uint64(val))
return err
})
}
Expand Down Expand Up @@ -882,7 +882,7 @@ func (b *Blockchain) revertHead(txn db.Transaction) error {
}
numBytes := core.MarshalBlockNumber(blockNumber)

stateUpdate, err := stateUpdateByNumber(txn, blockNumber)
stateUpdate, err := StateUpdateByNumber(txn, blockNumber)
if err != nil {
return err
}
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.

2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ require (
github.com/davidlazar/go-crypto v0.0.0-20200604182044-b73af7476f6c // indirect
github.com/deckarep/golang-set/v2 v2.6.0 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.3.0 // indirect
github.com/dmarkham/enumer v1.5.10 // indirect
github.com/docker/go-units v0.5.0 // indirect
github.com/elastic/gosigar v0.14.2 // indirect
github.com/ethereum/c-kzg-4844 v1.0.0 // indirect
Expand Down Expand Up @@ -134,6 +135,7 @@ require (
github.com/onsi/ginkgo/v2 v2.15.0 // indirect
github.com/opencontainers/runtime-spec v1.2.0 // indirect
github.com/opentracing/opentracing-go v1.2.0 // indirect
github.com/pascaldekloe/name v1.0.0 // indirect
github.com/pbnjay/memory v0.0.0-20210728143218-7b4eea64cf58 // indirect
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
github.com/pion/datachannel v1.5.6 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ github.com/decred/dcrd/crypto/blake256 v1.0.1 h1:7PltbUIQB7u/FfZ39+DGa/ShuMyJ5il
github.com/decred/dcrd/crypto/blake256 v1.0.1/go.mod h1:2OfgNZ5wDpcsFmHmCK5gZTPcCXqlm2ArzUIkw9czNJo=
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.3.0 h1:rpfIENRNNilwHwZeG5+P150SMrnNEcHYvcCuK6dPZSg=
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.3.0/go.mod h1:v57UDF4pDQJcEfFUCRop3lJL149eHGSe9Jvczhzjo/0=
github.com/dmarkham/enumer v1.5.10 h1:ygL0L6quiTiH1jpp68DyvsWaea6MaZLZrTTkIS++R0M=
github.com/dmarkham/enumer v1.5.10/go.mod h1:e4VILe2b1nYK3JKJpRmNdl5xbDQvELc6tQ8b+GsGk6E=
github.com/docker/go-units v0.4.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk=
github.com/docker/go-units v0.5.0 h1:69rxXcBk27SvSaaxTtLh/8llcHD8vYHT7WSdRZ/jvr4=
github.com/docker/go-units v0.5.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk=
Expand Down Expand Up @@ -406,6 +408,8 @@ github.com/opencontainers/runtime-spec v1.2.0/go.mod h1:jwyrGlmzljRJv/Fgzds9SsS/
github.com/opentracing/opentracing-go v1.2.0 h1:uEJPy/1a5RIPAJ0Ov+OIO8OxWu77jEv+1B0VhjKrZUs=
github.com/opentracing/opentracing-go v1.2.0/go.mod h1:GxEUsuufX4nBwe+T+Wl9TAgYrxe9dPLANfrWvHYVTgc=
github.com/openzipkin/zipkin-go v0.1.1/go.mod h1:NtoC/o8u3JlF1lSlyPNswIbeQH9bJTmOf0Erfk+hxe8=
github.com/pascaldekloe/name v1.0.0 h1:n7LKFgHixETzxpRv2R77YgPUFo85QHGZKrdaYm7eY5U=
github.com/pascaldekloe/name v1.0.0/go.mod h1:Z//MfYJnH4jVpQ9wkclwu2I2MkHmXTlT9wR5UZScttM=
github.com/pbnjay/memory v0.0.0-20210728143218-7b4eea64cf58 h1:onHthvaw9LFnH4t2DcNVpwGmV9E1BkGknEliJkfwQj0=
github.com/pbnjay/memory v0.0.0-20210728143218-7b4eea64cf58/go.mod h1:DXv8WO4yhMYhSNPKjeNKa5WY9YCIEBRbNzFFPJbWO6Y=
github.com/pelletier/go-toml/v2 v2.2.2 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6Wq+LM=
Expand Down
24 changes: 24 additions & 0 deletions migration/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,30 @@ var defaultMigrations = []Migration{
NewBucketMover(db.Temporary, db.ContractStorage),
NewBucketMigrator(db.StateUpdatesByBlockNumber, changeStateDiffStruct).WithBatchSize(100), //nolint:mnd
NewBucketMigrator(db.Class, migrateCairo1CompiledClass).WithBatchSize(1_000), //nolint:mnd
MigrationFunc(calculateP2PHash),
}

func calculateP2PHash(txn db.Transaction, network *utils.Network) error {
blockchain.RegisterCoreTypesToEncoder()
for blockNumber := uint64(0); ; blockNumber++ {
block, err := blockchain.BlockByNumber(txn, blockNumber)
if err != nil {
if errors.Is(err, db.ErrKeyNotFound) {
return nil
}
return err
}

stateUpdate, err := blockchain.StateUpdateByNumber(txn, block.Number)
if err != nil {
return err
}

err = blockchain.StoreP2PHash(txn, block, stateUpdate.StateDiff)
if err != nil {
return err
}
}
}

var ErrCallWithNewTransaction = errors.New("call with new transaction")
Expand Down

0 comments on commit 28d57db

Please sign in to comment.