Compare commits
5
Commits
v4.0.6-alpha
..
v4
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d1b423830c | ||
|
|
859df44546 | ||
|
|
aecb98129c | ||
|
|
aababba8d0 | ||
|
|
c16fa94850 |
+7
-4
@@ -59,7 +59,8 @@ It can operate at three levels:
|
||||
}
|
||||
|
||||
func validateTrie() {
|
||||
v, err := newValidator()
|
||||
numWorkers := viper.GetUint("validator.workers")
|
||||
v, err := newValidator(numWorkers)
|
||||
if err != nil {
|
||||
logWithCommand.Fatal(err)
|
||||
}
|
||||
@@ -104,20 +105,20 @@ func validateTrie() {
|
||||
logWithCommand.Debugf("groupcache stats %+v", stats)
|
||||
}
|
||||
|
||||
func newValidator() (*validator.Validator, error) {
|
||||
func newValidator(workers uint) (*validator.Validator, error) {
|
||||
ipfsPath := viper.GetString("ipfs.path")
|
||||
if ipfsPath == "" {
|
||||
db, err := validator.NewDB()
|
||||
if err != nil {
|
||||
logWithCommand.Fatal(err)
|
||||
}
|
||||
return validator.NewPGIPFSValidator(db), nil
|
||||
return validator.NewPGIPFSValidator(db, workers), nil
|
||||
}
|
||||
bs, err := validator.InitIPFSBlockService(ipfsPath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return validator.NewIPFSValidator(bs), nil
|
||||
return validator.NewIPFSValidator(bs, workers), nil
|
||||
}
|
||||
|
||||
func init() {
|
||||
@@ -128,10 +129,12 @@ func init() {
|
||||
validateTrieCmd.PersistentFlags().String("storage-root", "", "Root of the storage trie we wish to validate; for storage validation")
|
||||
validateTrieCmd.PersistentFlags().String("address", "", "Contract address for the storage trie we wish to validate; for storage validation")
|
||||
validateTrieCmd.PersistentFlags().String("ipfs-path", "", "Path to IPFS repository; if provided operations move through the IPFS repo otherwise Postgres connection params are expected in the provided config")
|
||||
validateTrieCmd.PersistentFlags().Int("workers", 4, "number of concurrent workers to use")
|
||||
|
||||
viper.BindPFlag("validator.stateRoot", validateTrieCmd.PersistentFlags().Lookup("state-root"))
|
||||
viper.BindPFlag("validator.type", validateTrieCmd.PersistentFlags().Lookup("type"))
|
||||
viper.BindPFlag("validator.storageRoot", validateTrieCmd.PersistentFlags().Lookup("storage-root"))
|
||||
viper.BindPFlag("validator.address", validateTrieCmd.PersistentFlags().Lookup("address"))
|
||||
viper.BindPFlag("validator.workers", validateTrieCmd.PersistentFlags().Lookup("workers"))
|
||||
viper.BindPFlag("ipfs.path", validateTrieCmd.PersistentFlags().Lookup("ipfs-path"))
|
||||
}
|
||||
|
||||
@@ -3,14 +3,14 @@ module github.com/vulcanize/eth-ipfs-state-validator/v4
|
||||
go 1.18
|
||||
|
||||
require (
|
||||
github.com/ethereum/go-ethereum v1.10.21
|
||||
github.com/ethereum/go-ethereum v1.10.23
|
||||
github.com/ipfs/go-blockservice v0.4.0
|
||||
github.com/ipfs/go-cid v0.2.0
|
||||
github.com/ipfs/go-ipfs-blockstore v1.2.0
|
||||
github.com/ipfs/go-ipfs-ds-help v1.1.0
|
||||
github.com/ipfs/kubo v0.14.0
|
||||
github.com/jmoiron/sqlx v1.3.5
|
||||
github.com/lib/pq v1.10.5
|
||||
github.com/lib/pq v1.10.6
|
||||
github.com/mailgun/groupcache/v2 v2.3.0
|
||||
github.com/multiformats/go-multihash v0.2.0
|
||||
github.com/onsi/ginkgo v1.16.5
|
||||
@@ -18,7 +18,7 @@ require (
|
||||
github.com/sirupsen/logrus v1.8.1
|
||||
github.com/spf13/cobra v1.4.0
|
||||
github.com/spf13/viper v1.11.0
|
||||
github.com/vulcanize/ipfs-ethdb/v4 v4.0.5-alpha
|
||||
github.com/vulcanize/ipfs-ethdb/v4 v4.0.7-alpha
|
||||
)
|
||||
|
||||
require (
|
||||
@@ -202,6 +202,7 @@ require (
|
||||
github.com/tidwall/pretty v1.2.0 // indirect
|
||||
github.com/tklauser/go-sysconf v0.3.5 // indirect
|
||||
github.com/tklauser/numcpus v0.2.2 // indirect
|
||||
github.com/vulcanize/go-eth-state-node-iterator v1.1.4
|
||||
github.com/wI2L/jsondiff v0.2.0 // indirect
|
||||
github.com/whyrusleeping/base32 v0.0.0-20170828182744-c30ac30633cc // indirect
|
||||
github.com/whyrusleeping/cbor-gen v0.0.0-20210219115102-f37d292932f2 // indirect
|
||||
@@ -219,7 +220,7 @@ require (
|
||||
go.uber.org/multierr v1.8.0 // indirect
|
||||
go.uber.org/zap v1.21.0 // indirect
|
||||
go4.org v0.0.0-20200411211856-f5505b9728dd // indirect
|
||||
golang.org/x/crypto v0.0.0-20220525230936-793ad666bf5e // indirect
|
||||
golang.org/x/crypto v0.0.0-20220829220503-c86fa9a7ed90 // indirect
|
||||
golang.org/x/mod v0.6.0-dev.0.20220106191415-9b9b3d81d5e3 // indirect
|
||||
golang.org/x/net v0.0.0-20220607020251-c690dde0001d // indirect
|
||||
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect
|
||||
@@ -235,4 +236,4 @@ require (
|
||||
lukechampine.com/blake3 v1.1.7 // indirect
|
||||
)
|
||||
|
||||
replace github.com/ethereum/go-ethereum v1.10.21 => github.com/vulcanize/go-ethereum v1.10.21-statediff-4.1.2-alpha
|
||||
replace github.com/ethereum/go-ethereum v1.10.23 => github.com/vulcanize/go-ethereum v1.10.23-statediff-4.2.0-alpha
|
||||
|
||||
@@ -732,8 +732,8 @@ github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
|
||||
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
|
||||
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
|
||||
github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
|
||||
github.com/lib/pq v1.10.5 h1:J+gdV2cUmX7ZqL2B0lFcW0m+egaHC2V3lpO8nWxyYiQ=
|
||||
github.com/lib/pq v1.10.5/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
|
||||
github.com/lib/pq v1.10.6 h1:jbk+ZieJ0D7EVGJYpL9QTz7/YW6UHbmdnZWYyK5cdBs=
|
||||
github.com/lib/pq v1.10.6/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
|
||||
github.com/libp2p/go-addr-util v0.0.1/go.mod h1:4ac6O7n9rIAKB1dnd+s8IbbMXkt+oBpzX4/+RACcnlQ=
|
||||
github.com/libp2p/go-addr-util v0.0.2/go.mod h1:Ecd6Fb3yIuLzq4bD7VcywcVSBtefcAwnUISBM3WG15E=
|
||||
github.com/libp2p/go-addr-util v0.1.0/go.mod h1:6I3ZYuFr2O/9D+SoyM0zEw0EF3YkldtTX406BpdQMqw=
|
||||
@@ -1519,10 +1519,12 @@ github.com/urfave/cli v1.22.2/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtX
|
||||
github.com/urfave/cli/v2 v2.0.0/go.mod h1:SE9GqnLQmjVa0iPEY0f1w3ygNIYcIJ0OKPMoW2caLfQ=
|
||||
github.com/viant/assertly v0.4.8/go.mod h1:aGifi++jvCrUaklKEKT0BU95igDNaqkvz+49uaYMPRU=
|
||||
github.com/viant/toolbox v0.24.0/go.mod h1:OxMCG57V0PXuIP2HNQrtJf2CjqdmbrOx5EkMILuUhzM=
|
||||
github.com/vulcanize/go-ethereum v1.10.21-statediff-4.1.2-alpha h1:ct+8FGuQnHA6SOGOQoAMINWdeexuSF40+IjF48J094A=
|
||||
github.com/vulcanize/go-ethereum v1.10.21-statediff-4.1.2-alpha/go.mod h1:dNJkmCSbaasX0zfQM6pm1g3rWlW3EGhLOEZMScyrRAs=
|
||||
github.com/vulcanize/ipfs-ethdb/v4 v4.0.5-alpha h1:NFRwWeMB3Q+QqLM9qdcHvfvWBxOk0lPwhOqXJpkIg30=
|
||||
github.com/vulcanize/ipfs-ethdb/v4 v4.0.5-alpha/go.mod h1:WvYj0m0cLPAtoytTbcbE2nZ3Hg9iuuF+lY14dBVRWZQ=
|
||||
github.com/vulcanize/go-eth-state-node-iterator v1.1.4 h1:2jIbgZ/yxrNZt5zmsXtvUPSxT0sP5k1PR5/O1GN/Ma0=
|
||||
github.com/vulcanize/go-eth-state-node-iterator v1.1.4/go.mod h1:kFzlSi7rUB9mJWZf08xIEiiGuXnnUEn48It+WMHzxPc=
|
||||
github.com/vulcanize/go-ethereum v1.10.23-statediff-4.2.0-alpha h1:iy1uhdazPIlUJcCu6Kal+hFQiZblTDwSe2i644o167Y=
|
||||
github.com/vulcanize/go-ethereum v1.10.23-statediff-4.2.0-alpha/go.mod h1:lKBVBWksSwBDR/5D9CAxaGQzDPIS3ueWb6idy7X1Shg=
|
||||
github.com/vulcanize/ipfs-ethdb/v4 v4.0.7-alpha h1:UdOYaVmK/QaFyUDg44eEcEaNivP6059hmK6Qeh+3Mg8=
|
||||
github.com/vulcanize/ipfs-ethdb/v4 v4.0.7-alpha/go.mod h1:gK23M2S0428yKsJoiUzBMTMVRt6OdOuFEFPcnO/ksGA=
|
||||
github.com/wI2L/jsondiff v0.2.0 h1:dE00WemBa1uCjrzQUUTE/17I6m5qAaN0EMFOg2Ynr/k=
|
||||
github.com/wI2L/jsondiff v0.2.0/go.mod h1:axTcwtBkY4TsKuV+RgoMhHyHKKFRI6nnjRLi8LLYQnA=
|
||||
github.com/wangjia184/sortedset v0.0.0-20160527075905-f5d03557ba30/go.mod h1:YkocrP2K2tcw938x9gCOmT5G5eCD6jsTz0SZuyAqwIE=
|
||||
@@ -1677,8 +1679,8 @@ golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97/go.mod h1:GvvjBRRGRdwPK5y
|
||||
golang.org/x/crypto v0.0.0-20210813211128-0a44fdfbc16e/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
|
||||
golang.org/x/crypto v0.0.0-20211108221036-ceb1ce70b4fa/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
|
||||
golang.org/x/crypto v0.0.0-20220518034528-6f7dac969898/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
|
||||
golang.org/x/crypto v0.0.0-20220525230936-793ad666bf5e h1:T8NU3HyQ8ClP4SEE+KbFlg6n0NhuTsN4MyznaarGsZM=
|
||||
golang.org/x/crypto v0.0.0-20220525230936-793ad666bf5e/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
|
||||
golang.org/x/crypto v0.0.0-20220829220503-c86fa9a7ed90 h1:Y/gsMcFOcR+6S6f3YeMKl5g+dZMEWqcz5Czj/GWYbkM=
|
||||
golang.org/x/crypto v0.0.0-20220829220503-c86fa9a7ed90/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
|
||||
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
|
||||
golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
|
||||
golang.org/x/exp v0.0.0-20190510132918-efd6b22b2522/go.mod h1:ZjyILWgesfNpC6sMxTJOJm9Kp84zZh5NQWvqDGG3Qr8=
|
||||
|
||||
+94
-25
@@ -17,17 +17,23 @@
|
||||
package validator
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
"github.com/ethereum/go-ethereum/core/state"
|
||||
"github.com/ethereum/go-ethereum/core/types"
|
||||
"github.com/ethereum/go-ethereum/crypto"
|
||||
"github.com/ethereum/go-ethereum/ethdb"
|
||||
"github.com/ethereum/go-ethereum/rlp"
|
||||
"github.com/ethereum/go-ethereum/trie"
|
||||
"github.com/ipfs/go-blockservice"
|
||||
"github.com/jmoiron/sqlx"
|
||||
"github.com/mailgun/groupcache/v2"
|
||||
|
||||
nodeiter "github.com/vulcanize/go-eth-state-node-iterator"
|
||||
ipfsethdb "github.com/vulcanize/ipfs-ethdb/v4"
|
||||
pgipfsethdb "github.com/vulcanize/ipfs-ethdb/v4/postgres"
|
||||
)
|
||||
@@ -38,10 +44,14 @@ type Validator struct {
|
||||
trieDB *trie.Database
|
||||
stateDatabase state.Database
|
||||
db *pgipfsethdb.Database
|
||||
|
||||
iterWorkers uint
|
||||
}
|
||||
|
||||
var emptyCodeHash = crypto.Keccak256(nil)
|
||||
|
||||
// NewPGIPFSValidator returns a new trie validator ontop of a connection pool for an IPFS backing Postgres database
|
||||
func NewPGIPFSValidator(db *sqlx.DB) *Validator {
|
||||
func NewPGIPFSValidator(db *sqlx.DB, workers uint) *Validator {
|
||||
kvs := pgipfsethdb.NewKeyValueStore(db, pgipfsethdb.CacheConfig{
|
||||
Name: "kv",
|
||||
Size: 16 * 1000 * 1000, // 16MB
|
||||
@@ -54,11 +64,15 @@ func NewPGIPFSValidator(db *sqlx.DB) *Validator {
|
||||
ExpiryDuration: time.Hour * 8, // 8 hours
|
||||
})
|
||||
|
||||
if workers == 0 {
|
||||
workers = 1
|
||||
}
|
||||
return &Validator{
|
||||
kvs: kvs,
|
||||
trieDB: trie.NewDatabase(kvs),
|
||||
stateDatabase: state.NewDatabase(database),
|
||||
db: database.(*pgipfsethdb.Database),
|
||||
iterWorkers: workers,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -67,13 +81,17 @@ func (v *Validator) GetCacheStats() groupcache.Stats {
|
||||
}
|
||||
|
||||
// NewIPFSValidator returns a new trie validator ontop of an IPFS blockservice
|
||||
func NewIPFSValidator(bs blockservice.BlockService) *Validator {
|
||||
func NewIPFSValidator(bs blockservice.BlockService, workers uint) *Validator {
|
||||
kvs := ipfsethdb.NewKeyValueStore(bs)
|
||||
database := ipfsethdb.NewDatabase(bs)
|
||||
if workers == 0 {
|
||||
workers = 1
|
||||
}
|
||||
return &Validator{
|
||||
kvs: kvs,
|
||||
trieDB: trie.NewDatabase(kvs),
|
||||
stateDatabase: state.NewDatabase(database),
|
||||
iterWorkers: workers,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -88,21 +106,82 @@ func NewValidator(kvs ethdb.KeyValueStore, database ethdb.Database) *Validator {
|
||||
}
|
||||
}
|
||||
|
||||
// Traverses each iterator in a separate goroutine.
|
||||
// If storage = true, also traverse storage tries for each leaf.
|
||||
func (v *Validator) iterateAsync(iters []trie.NodeIterator, storage bool) error {
|
||||
var wg sync.WaitGroup
|
||||
errors := make(chan error)
|
||||
for _, it := range iters {
|
||||
wg.Add(1)
|
||||
go func(it trie.NodeIterator) {
|
||||
defer wg.Done()
|
||||
for it.Next(true) {
|
||||
// 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
|
||||
// missing node). If we are able to iterate through the entire trie without error
|
||||
// then the trie is complete.
|
||||
|
||||
// If storage is not requested, or the state trie node is an internal entry, leave as is
|
||||
if !storage || !it.Leaf() {
|
||||
continue
|
||||
}
|
||||
// Otherwise we've reached an account node, initiate data iteration
|
||||
var account types.StateAccount
|
||||
if err := rlp.Decode(bytes.NewReader(it.LeafBlob()), &account); err != nil {
|
||||
errors <- err
|
||||
break
|
||||
}
|
||||
dataTrie, err := v.stateDatabase.OpenStorageTrie(common.BytesToHash(it.LeafKey()), account.Root)
|
||||
if err != nil {
|
||||
errors <- err
|
||||
break
|
||||
}
|
||||
dataIt := dataTrie.NodeIterator(nil)
|
||||
if !bytes.Equal(account.CodeHash, emptyCodeHash) {
|
||||
addrHash := common.BytesToHash(it.LeafKey())
|
||||
_, err := v.stateDatabase.ContractCode(addrHash, common.BytesToHash(account.CodeHash))
|
||||
if err != nil {
|
||||
errors <- fmt.Errorf("code %x: %v", account.CodeHash, err)
|
||||
break
|
||||
}
|
||||
}
|
||||
for dataIt.Next(true) {
|
||||
}
|
||||
if err = dataIt.Error(); err != nil {
|
||||
errors <- err
|
||||
break
|
||||
}
|
||||
|
||||
}
|
||||
if it.Error() != nil {
|
||||
errors <- it.Error()
|
||||
}
|
||||
}(it)
|
||||
}
|
||||
|
||||
done := make(chan struct{})
|
||||
go func() {
|
||||
wg.Wait()
|
||||
done <- struct{}{}
|
||||
}()
|
||||
var err error
|
||||
select {
|
||||
case err = <-errors:
|
||||
case <-done:
|
||||
close(errors)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// ValidateTrie returns an error if the state and storage tries for the provided state root cannot be confirmed as complete
|
||||
// This does consider child storage tries
|
||||
func (v *Validator) ValidateTrie(stateRoot common.Hash) error {
|
||||
// Generate the state.NodeIterator for this root
|
||||
stateDB, err := state.New(stateRoot, v.stateDatabase, nil)
|
||||
t, err := v.stateDatabase.OpenTrie(stateRoot)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
it := state.NewNodeIterator(stateDB)
|
||||
for it.Next() {
|
||||
// iterate through entire state trie and descendent storage tries
|
||||
// it.Next() will return false when we have either completed iteration of the entire trie or have ran into an error (e.g. a missing node)
|
||||
// if we are able to iterate through the entire trie without error then the trie is complete
|
||||
}
|
||||
return it.Error
|
||||
iters := nodeiter.SubtrieIterators(t, v.iterWorkers)
|
||||
return v.iterateAsync(iters, true)
|
||||
}
|
||||
|
||||
// ValidateStateTrie returns an error if the state trie for the provided state root cannot be confirmed as complete
|
||||
@@ -113,13 +192,8 @@ func (v *Validator) ValidateStateTrie(stateRoot common.Hash) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
it := t.NodeIterator(nil)
|
||||
for it.Next(true) {
|
||||
// iterate through entire state trie
|
||||
// it.Next() will return false when we have either completed iteration of the entire trie or have ran into an error (e.g. a missing node)
|
||||
// if we are able to iterate through the entire trie without error then the trie is complete
|
||||
}
|
||||
return it.Error()
|
||||
iters := nodeiter.SubtrieIterators(t, v.iterWorkers)
|
||||
return v.iterateAsync(iters, false)
|
||||
}
|
||||
|
||||
// ValidateStorageTrie returns an error if the storage trie for the provided storage root and contract address cannot be confirmed as complete
|
||||
@@ -130,13 +204,8 @@ func (v *Validator) ValidateStorageTrie(address common.Address, storageRoot comm
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
it := t.NodeIterator(nil)
|
||||
for it.Next(true) {
|
||||
// iterate through entire storage trie
|
||||
// it.Next() will return false when we have either completed iteration of the entire trie or have ran into an error (e.g. a missing node)
|
||||
// if we are able to iterate through the entire trie without error then the trie is complete
|
||||
}
|
||||
return it.Error()
|
||||
iters := nodeiter.SubtrieIterators(t, v.iterWorkers)
|
||||
return v.iterateAsync(iters, false)
|
||||
}
|
||||
|
||||
// Close implements io.Closer
|
||||
|
||||
@@ -200,7 +200,7 @@ var _ = Describe("PG-IPFS Validator", func() {
|
||||
BeforeEach(func() {
|
||||
db, err = pgipfsethdb.TestDB()
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
v = validator.NewPGIPFSValidator(db)
|
||||
v = validator.NewPGIPFSValidator(db, 4)
|
||||
})
|
||||
AfterEach(func() {
|
||||
v.Close()
|
||||
|
||||
Reference in New Issue
Block a user