update code

This commit is contained in:
Roy Crihfield 2024-04-15 19:42:58 +08:00
parent ae01d5793b
commit 9d9332fed1
3 changed files with 34 additions and 24 deletions

View File

@ -19,7 +19,7 @@ package validator
import ( import (
"context" "context"
"github.com/ipfs/go-blockservice" "github.com/ipfs/boxo/blockservice"
"github.com/ipfs/kubo/core" "github.com/ipfs/kubo/core"
"github.com/ipfs/kubo/repo/fsrepo" "github.com/ipfs/kubo/repo/fsrepo"
) )

View File

@ -22,14 +22,12 @@ import (
"fmt" "fmt"
"time" "time"
"github.com/spf13/viper"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/rlp" "github.com/ethereum/go-ethereum/rlp"
"github.com/ipfs/go-blockservice" "github.com/ipfs/boxo/blockservice"
"github.com/jmoiron/sqlx" "github.com/jmoiron/sqlx"
"github.com/mailgun/groupcache/v2" "github.com/mailgun/groupcache/v2"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
@ -41,12 +39,13 @@ import (
pgipfsethdb "github.com/cerc-io/ipfs-ethdb/v5/postgres/v0" pgipfsethdb "github.com/cerc-io/ipfs-ethdb/v5/postgres/v0"
"github.com/cerc-io/ipld-eth-statedb/trie_by_cid/state" "github.com/cerc-io/ipld-eth-statedb/trie_by_cid/state"
"github.com/cerc-io/ipld-eth-statedb/trie_by_cid/trie" "github.com/cerc-io/ipld-eth-statedb/trie_by_cid/trie"
"github.com/cerc-io/ipld-eth-statedb/trie_by_cid/triedb"
) )
// Validator is used for validating Ethereum state and storage tries on PG-IPFS // Validator is used for validating Ethereum state and storage tries on PG-IPFS
type Validator struct { type Validator struct {
kvs ethdb.KeyValueStore kvs ethdb.KeyValueStore
trieDB *trie.Database trieDB *triedb.Database
stateDatabase state.Database stateDatabase state.Database
db *pgipfsethdb.Database db *pgipfsethdb.Database
@ -91,7 +90,7 @@ func NewPGIPFSValidator(db *sqlx.DB, par Params) *Validator {
normalizeParams(&par) normalizeParams(&par)
return &Validator{ return &Validator{
kvs: kvs, kvs: kvs,
trieDB: trie.NewDatabase(NewKVSDatabaseWithAncient(kvs)), trieDB: triedb.NewDatabase(NewKVSDatabaseWithAncient(kvs), nil),
stateDatabase: state.NewDatabase(database), stateDatabase: state.NewDatabase(database),
db: database.(*pgipfsethdb.Database), db: database.(*pgipfsethdb.Database),
params: par, params: par,
@ -109,7 +108,7 @@ func NewIPFSValidator(bs blockservice.BlockService, par Params) *Validator {
normalizeParams(&par) normalizeParams(&par)
return &Validator{ return &Validator{
kvs: kvs, kvs: kvs,
trieDB: trie.NewDatabase(NewKVSDatabaseWithAncient(kvs)), trieDB: triedb.NewDatabase(NewKVSDatabaseWithAncient(kvs), nil),
stateDatabase: state.NewDatabase(database), stateDatabase: state.NewDatabase(database),
params: par, params: par,
} }
@ -121,7 +120,7 @@ func NewIPFSValidator(bs blockservice.BlockService, par Params) *Validator {
func NewValidator(kvs ethdb.KeyValueStore, database ethdb.Database) *Validator { func NewValidator(kvs ethdb.KeyValueStore, database ethdb.Database) *Validator {
return &Validator{ return &Validator{
kvs: kvs, kvs: kvs,
trieDB: trie.NewDatabase(NewKVSDatabaseWithAncient(kvs)), trieDB: triedb.NewDatabase(NewKVSDatabaseWithAncient(kvs), nil),
stateDatabase: state.NewDatabase(database), stateDatabase: state.NewDatabase(database),
} }
} }
@ -143,7 +142,7 @@ func (v *Validator) ValidateTrie(stateRoot common.Hash) error {
if err != nil { if err != nil {
return err return err
} }
iterate := func(it trie.NodeIterator) error { return v.iterate(it, true) } iterate := func(it trie.NodeIterator) error { return v.iterate(stateRoot, it, true) }
return iterateTracked(t, fmt.Sprintf(v.params.RecoveryFormat, fullTraversal), v.params.Workers, iterate) return iterateTracked(t, fmt.Sprintf(v.params.RecoveryFormat, fullTraversal), v.params.Workers, iterate)
} }
@ -155,7 +154,7 @@ func (v *Validator) ValidateStateTrie(stateRoot common.Hash) error {
if err != nil { if err != nil {
return err return err
} }
iterate := func(it trie.NodeIterator) error { return v.iterate(it, false) } iterate := func(it trie.NodeIterator) error { return v.iterate(stateRoot, it, false) }
return iterateTracked(t, fmt.Sprintf(v.params.RecoveryFormat, stateTraversal), v.params.Workers, iterate) return iterateTracked(t, fmt.Sprintf(v.params.RecoveryFormat, stateTraversal), v.params.Workers, iterate)
} }
@ -163,12 +162,16 @@ func (v *Validator) ValidateStateTrie(stateRoot common.Hash) error {
func (v *Validator) ValidateStorageTrie(stateRoot common.Hash, address common.Address, storageRoot common.Hash) error { func (v *Validator) ValidateStorageTrie(stateRoot common.Hash, address common.Address, storageRoot common.Hash) error {
// Generate the state.NodeIterator for this root // Generate the state.NodeIterator for this root
addrHash := crypto.Keccak256Hash(address.Bytes()) addrHash := crypto.Keccak256Hash(address.Bytes())
t, err := v.stateDatabase.OpenStorageTrie(stateRoot, addrHash, storageRoot) st, err := v.stateDatabase.OpenTrie(stateRoot)
if err != nil { if err != nil {
return err return err
} }
iterate := func(it trie.NodeIterator) error { return v.iterate(it, false) } storage, err := v.stateDatabase.OpenStorageTrie(stateRoot, addrHash, storageRoot, st)
return iterateTracked(t, fmt.Sprintf(v.params.RecoveryFormat, storageTraversal), v.params.Workers, iterate) if err != nil {
return err
}
iterate := func(it trie.NodeIterator) error { return v.iterate(stateRoot, it, false) }
return iterateTracked(storage, fmt.Sprintf(v.params.RecoveryFormat, storageTraversal), v.params.Workers, iterate)
} }
// Close implements io.Closer // Close implements io.Closer
@ -181,7 +184,7 @@ func (v *Validator) Close() error {
// Traverses one iterator fully // Traverses one iterator fully
// If storage = true, also traverse storage tries for each leaf. // If storage = true, also traverse storage tries for each leaf.
func (v *Validator) iterate(it trie.NodeIterator, storage bool) error { func (v *Validator) iterate(stateRoot common.Hash, it trie.NodeIterator, storage bool) error {
// Iterate through entire state trie. it.Next() will return false when we have // Iterate through entire state trie. it.Next() will return false when we have
// either completed iteration of the entire trie or run into an error (e.g. a // either completed iteration of the entire trie or run into an error (e.g. a
// missing node). If we are able to iterate through the entire trie without error // missing node). If we are able to iterate through the entire trie without error
@ -197,13 +200,17 @@ func (v *Validator) iterate(it trie.NodeIterator, storage bool) error {
if err := rlp.Decode(bytes.NewReader(it.LeafBlob()), &account); err != nil { if err := rlp.Decode(bytes.NewReader(it.LeafBlob()), &account); err != nil {
return err return err
} }
dataTrie, err := v.stateDatabase.OpenStorageTrie(common.HexToHash(viper.GetString("validator.stateRoot")), common.BytesToHash(it.LeafKey()), account.Root) dataTrie, err := v.stateDatabase.OpenStorageTrie(stateRoot, common.BytesToHash(it.LeafKey()), account.Root, nil)
if err != nil { if err != nil {
return err return err
} }
dataIt := dataTrie.NodeIterator(nil) dataIt, err := dataTrie.NodeIterator(nil)
if err != nil {
return err
}
if !bytes.Equal(account.CodeHash, emptyCodeHash) { if !bytes.Equal(account.CodeHash, emptyCodeHash) {
_, err := v.stateDatabase.ContractCode(common.BytesToHash(account.CodeHash)) _, err := v.stateDatabase.ContractCode(common.Address{}, common.BytesToHash(account.CodeHash))
if err != nil { if err != nil {
return fmt.Errorf("code hash %x: %w (path %x)", account.CodeHash, err, iterutils.HexToKeyBytes(it.Path())) return fmt.Errorf("code hash %x: %w (path %x)", account.CodeHash, err, iterutils.HexToKeyBytes(it.Path()))
} }
@ -238,7 +245,10 @@ func iterateTracked(tree state.Trie, recoveryFile string, iterCount uint, fn fun
} }
if iters == nil { // nothing restored if iters == nil { // nothing restored
iters = iterutils.SubtrieIterators(tree.NodeIterator, iterCount) iters, err = iterutils.SubtrieIterators(tree.NodeIterator, iterCount)
if err != nil {
return err
}
for i, it := range iters { for i, it := range iters {
iters[i] = tracker.Tracked(it) iters[i] = tracker.Tracked(it)
} }

View File

@ -17,7 +17,6 @@
package validator_test package validator_test
import ( import (
"math/big"
"os" "os"
"path/filepath" "path/filepath"
@ -25,6 +24,7 @@ import (
"github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/rlp" "github.com/ethereum/go-ethereum/rlp"
"github.com/holiman/uint256"
cid "github.com/ipfs/go-cid/_rsrch/cidiface" cid "github.com/ipfs/go-cid/_rsrch/cidiface"
"github.com/jmoiron/sqlx" "github.com/jmoiron/sqlx"
"github.com/multiformats/go-multihash" "github.com/multiformats/go-multihash"
@ -69,7 +69,7 @@ var (
codePath = common.Hex2Bytes("6114658a74d9cc9f7acf2c5cd696c3494d7c344d78bfec3add0d91ec4e8d1c45") codePath = common.Hex2Bytes("6114658a74d9cc9f7acf2c5cd696c3494d7c344d78bfec3add0d91ec4e8d1c45")
contractAccount, _ = rlp.EncodeToBytes(&types.StateAccount{ contractAccount, _ = rlp.EncodeToBytes(&types.StateAccount{
Nonce: 1, Nonce: 1,
Balance: big.NewInt(0), Balance: uint256.NewInt(0),
CodeHash: codeHash.Bytes(), CodeHash: codeHash.Bytes(),
Root: crypto.Keccak256Hash(storageBranchRootNode), Root: crypto.Keccak256Hash(storageBranchRootNode),
}) })
@ -80,7 +80,7 @@ var (
minerAccount, _ = rlp.EncodeToBytes(&types.StateAccount{ minerAccount, _ = rlp.EncodeToBytes(&types.StateAccount{
Nonce: 0, Nonce: 0,
Balance: big.NewInt(1000), Balance: uint256.NewInt(1000),
CodeHash: nullCodeHash.Bytes(), CodeHash: nullCodeHash.Bytes(),
Root: emptyContractRoot, Root: emptyContractRoot,
}) })
@ -91,7 +91,7 @@ var (
account1, _ = rlp.EncodeToBytes(&types.StateAccount{ account1, _ = rlp.EncodeToBytes(&types.StateAccount{
Nonce: 2, Nonce: 2,
Balance: big.NewInt(1000), Balance: uint256.NewInt(1000),
CodeHash: nullCodeHash.Bytes(), CodeHash: nullCodeHash.Bytes(),
Root: emptyContractRoot, Root: emptyContractRoot,
}) })
@ -102,7 +102,7 @@ var (
account2, _ = rlp.EncodeToBytes(&types.StateAccount{ account2, _ = rlp.EncodeToBytes(&types.StateAccount{
Nonce: 0, Nonce: 0,
Balance: big.NewInt(1000), Balance: uint256.NewInt(1000),
CodeHash: nullCodeHash.Bytes(), CodeHash: nullCodeHash.Bytes(),
Root: emptyContractRoot, Root: emptyContractRoot,
}) })
@ -113,7 +113,7 @@ var (
bankAccount, _ = rlp.EncodeToBytes(&types.StateAccount{ bankAccount, _ = rlp.EncodeToBytes(&types.StateAccount{
Nonce: 2, Nonce: 2,
Balance: big.NewInt(1000), Balance: uint256.NewInt(1000),
CodeHash: nullCodeHash.Bytes(), CodeHash: nullCodeHash.Bytes(),
Root: emptyContractRoot, Root: emptyContractRoot,
}) })