eth-ipfs-state-validator/pkg/validator.go

149 lines
5.3 KiB
Go
Raw Normal View History

2020-06-25 20:11:50 +00:00
// VulcanizeDB
// Copyright © 2020 Vulcanize
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package validator
import (
"time"
2020-06-25 20:11:50 +00:00
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/state"
2020-06-26 18:11:30 +00:00
"github.com/ethereum/go-ethereum/crypto"
2020-06-25 20:11:50 +00:00
"github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/trie"
2020-07-13 00:57:47 +00:00
"github.com/ipfs/go-blockservice"
2020-06-25 20:11:50 +00:00
"github.com/jmoiron/sqlx"
"github.com/mailgun/groupcache/v2"
2020-06-25 20:11:50 +00:00
ipfsethdb "github.com/vulcanize/ipfs-ethdb/v3"
pgipfsethdb "github.com/vulcanize/ipfs-ethdb/v3/postgres"
2020-06-25 20:11:50 +00:00
)
// Validator is used for validating Ethereum state and storage tries on PG-IPFS
type Validator struct {
kvs ethdb.KeyValueStore
trieDB *trie.Database
stateDatabase state.Database
db *pgipfsethdb.Database
2020-06-25 20:11:50 +00:00
}
2020-07-13 00:57:47 +00:00
// NewPGIPFSValidator returns a new trie validator ontop of a connection pool for an IPFS backing Postgres database
func NewPGIPFSValidator(db *sqlx.DB) *Validator {
kvs := pgipfsethdb.NewKeyValueStore(db, pgipfsethdb.CacheConfig{
Name: "kv",
Size: 16 * 1000 * 1000, // 16MB
ExpiryDuration: time.Hour * 8, // 8 hours
})
database := pgipfsethdb.NewDatabase(db, pgipfsethdb.CacheConfig{
Name: "db",
Size: 16 * 1000 * 1000, // 16MB
ExpiryDuration: time.Hour * 8, // 8 hours
})
2020-07-13 00:57:47 +00:00
return &Validator{
kvs: kvs,
trieDB: trie.NewDatabase(kvs),
stateDatabase: state.NewDatabase(database),
2021-12-29 19:44:32 +00:00
db: database.(*pgipfsethdb.Database),
2020-07-13 00:57:47 +00:00
}
}
func (v *Validator) GetCacheStats() groupcache.Stats {
return v.db.GetCacheStats()
}
2020-07-13 00:57:47 +00:00
// NewIPFSValidator returns a new trie validator ontop of an IPFS blockservice
func NewIPFSValidator(bs blockservice.BlockService) *Validator {
kvs := ipfsethdb.NewKeyValueStore(bs)
database := ipfsethdb.NewDatabase(bs)
return &Validator{
kvs: kvs,
trieDB: trie.NewDatabase(kvs),
stateDatabase: state.NewDatabase(database),
}
}
2020-06-25 20:11:50 +00:00
// NewValidator returns a new trie validator
2020-06-26 18:11:30 +00:00
// Validating the completeness of a modified merkle patricia tries requires traversing the entire trie and verifying that
// every node is present, this is an expensive operation
2020-07-13 00:57:47 +00:00
func NewValidator(kvs ethdb.KeyValueStore, database ethdb.Database) *Validator {
2020-06-25 20:11:50 +00:00
return &Validator{
kvs: kvs,
trieDB: trie.NewDatabase(kvs),
stateDatabase: state.NewDatabase(database),
}
}
2020-06-26 18:11:30 +00:00
// 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 {
2020-06-25 20:11:50 +00:00
// Generate the state.NodeIterator for this root
stateDB, err := state.New(stateRoot, v.stateDatabase, nil)
2020-06-25 20:11:50 +00:00
if err != nil {
2020-06-26 18:11:30 +00:00
return err
2020-06-25 20:11:50 +00:00
}
it := state.NewNodeIterator(stateDB)
for it.Next() {
2020-06-26 18:11:30 +00:00
// 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
}
// ValidateStateTrie returns an error if the state trie for the provided state root cannot be confirmed as complete
// This does not consider child storage tries
func (v *Validator) ValidateStateTrie(stateRoot common.Hash) error {
// Generate the trie.NodeIterator for this root
t, err := v.stateDatabase.OpenTrie(stateRoot)
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)
2020-06-25 20:11:50 +00:00
// if we are able to iterate through the entire trie without error then the trie is complete
}
2020-06-26 18:11:30 +00:00
return it.Error()
}
// ValidateStorageTrie returns an error if the storage trie for the provided storage root and contract address cannot be confirmed as complete
func (v *Validator) ValidateStorageTrie(address common.Address, storageRoot common.Hash) error {
// Generate the state.NodeIterator for this root
addrHash := crypto.Keccak256Hash(address.Bytes())
t, err := v.stateDatabase.OpenStorageTrie(addrHash, storageRoot)
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
2020-06-25 20:11:50 +00:00
}
2020-06-26 18:11:30 +00:00
return it.Error()
2020-06-25 20:11:50 +00:00
}
2021-10-11 15:18:45 +00:00
// Close implements io.Closer
// it deregisters the groupcache name
func (v *Validator) Close() error {
groupcache.DeregisterGroup("kv")
groupcache.DeregisterGroup("db")
return nil
}