Compare commits

..
Author SHA1 Message Date
Ashwin Phatak c8813448e9 Merge pull request #22 from deep-stack/pm-v4-schema-updates
Update to use v4 schema
2022-05-12 16:42:01 +05:30
prathamesh bce89d43da Update module path for v4 2022-05-11 19:09:31 +05:30
prathamesh 54f5f4cd9c Upgrade dependencies 2022-05-11 19:05:10 +05:30
prathamesh a42ad0441b Update tests to use v4 schema 2022-05-11 18:58:22 +05:30
Ashwin Phatak bd01384525 Merge pull request #21 from deep-stack/pm-v3-upgrades
Upgrade dependencies and update module path for v3
2022-05-04 10:18:28 +05:30
prathamesh c64a6e59f8 Upgrade dependencies and update module path for v3 2022-05-03 11:53:29 +05:30
Ian Norden 8a46648418 Merge pull request #20 from vulcanize/v0.1.0
bump go version
2021-12-29 14:03:10 -06:00
i-norden 6e5bfa6bc5 bump go version 2021-12-29 14:01:38 -06:00
Ian Norden e66ac139fa Merge pull request #19 from vulcanize/release-v0.0.3
v0.0.3
2021-12-29 13:54:15 -06:00
i-norden 5203c506b0 type assertion needed 2021-12-29 13:44:32 -06:00
i-norden 0926e1f53f bump ipfs-ethdb and go-ethereum versions 2021-12-29 13:44:21 -06:00
Ian Norden eea7ef859d Merge pull request #15 from vulcanize/security_update
Security update
2021-10-11 10:22:42 -05:00
i-norden f7ce7a48dd fix unit tests after updating deps 2021-10-11 10:18:45 -05:00
i-norden 3fb74dfbf6 update deps 2021-10-11 10:09:03 -05:00
7 changed files with 1235 additions and 364 deletions
+1 -1
View File
@@ -24,7 +24,7 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper" "github.com/spf13/viper"
validator "github.com/vulcanize/eth-ipfs-state-validator/pkg" validator "github.com/vulcanize/eth-ipfs-state-validator/v4/pkg"
) )
// validateTrieCmd represents the validateTrie command // validateTrieCmd represents the validateTrie command
+16 -16
View File
@@ -1,23 +1,23 @@
module github.com/vulcanize/eth-ipfs-state-validator module github.com/vulcanize/eth-ipfs-state-validator/v4
go 1.13 go 1.15
require ( require (
github.com/ethereum/go-ethereum v1.9.15 github.com/ethereum/go-ethereum v1.10.17
github.com/ipfs/go-blockservice v0.1.3 github.com/ipfs/go-blockservice v0.1.7
github.com/ipfs/go-cid v0.0.7 github.com/ipfs/go-cid v0.0.7
github.com/ipfs/go-filestore v1.0.0 //indirect github.com/ipfs/go-filestore v1.0.0 //indirect
github.com/ipfs/go-ipfs v0.7.0 github.com/ipfs/go-ipfs v0.10.0
github.com/ipfs/go-ipfs-blockstore v1.0.0 github.com/ipfs/go-ipfs-blockstore v1.0.1
github.com/ipfs/go-ipfs-ds-help v1.0.0 github.com/ipfs/go-ipfs-ds-help v1.0.0
github.com/jmoiron/sqlx v1.2.0 github.com/jmoiron/sqlx v1.3.5
github.com/lib/pq v1.5.2 github.com/lib/pq v1.10.5
github.com/mailgun/groupcache/v2 v2.2.1 github.com/mailgun/groupcache/v2 v2.3.0
github.com/multiformats/go-multihash v0.0.14 github.com/multiformats/go-multihash v0.1.0
github.com/onsi/ginkgo v1.14.0 github.com/onsi/ginkgo v1.16.5
github.com/onsi/gomega v1.10.1 github.com/onsi/gomega v1.19.0
github.com/sirupsen/logrus v1.6.0 github.com/sirupsen/logrus v1.8.1
github.com/spf13/cobra v1.0.0 github.com/spf13/cobra v1.4.0
github.com/spf13/viper v1.7.0 github.com/spf13/viper v1.11.0
github.com/vulcanize/ipfs-ethdb v0.0.3 github.com/vulcanize/ipfs-ethdb/v4 v4.0.0-alpha
) )
+1176 -317
View File
File diff suppressed because it is too large Load Diff
+1 -1
View File
@@ -18,7 +18,7 @@ package main
import ( import (
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"github.com/vulcanize/eth-ipfs-state-validator/cmd" "github.com/vulcanize/eth-ipfs-state-validator/v4/cmd"
) )
func main() { func main() {
+4 -4
View File
@@ -21,22 +21,22 @@ import (
"github.com/ipfs/go-blockservice" "github.com/ipfs/go-blockservice"
"github.com/ipfs/go-cid" "github.com/ipfs/go-cid"
"github.com/ipfs/go-ipfs-blockstore" blockstore "github.com/ipfs/go-ipfs-blockstore"
"github.com/ipfs/go-ipfs-ds-help" dshelp "github.com/ipfs/go-ipfs-ds-help"
"github.com/ipfs/go-ipfs/core" "github.com/ipfs/go-ipfs/core"
"github.com/ipfs/go-ipfs/repo/fsrepo" "github.com/ipfs/go-ipfs/repo/fsrepo"
"github.com/jmoiron/sqlx" "github.com/jmoiron/sqlx"
) )
// PublishRaw derives a cid from raw bytes and provided codec and multihash type, and writes it to the db tx // PublishRaw derives a cid from raw bytes and provided codec and multihash type, and writes it to the db tx
func PublishRaw(tx *sqlx.Tx, codec, mh uint64, raw []byte) (string, error) { func PublishRaw(tx *sqlx.Tx, codec, mh uint64, raw []byte, blockNumber uint64) (string, error) {
c, err := RawdataToCid(codec, raw, mh) c, err := RawdataToCid(codec, raw, mh)
if err != nil { if err != nil {
return "", err return "", err
} }
dbKey := dshelp.MultihashToDsKey(c.Hash()) dbKey := dshelp.MultihashToDsKey(c.Hash())
prefixedKey := blockstore.BlockPrefix.String() + dbKey.String() prefixedKey := blockstore.BlockPrefix.String() + dbKey.String()
_, err = tx.Exec(`INSERT INTO public.blocks (key, data) VALUES ($1, $2) ON CONFLICT (key) DO NOTHING`, prefixedKey, raw) _, err = tx.Exec(`INSERT INTO public.blocks (key, data, block_number) VALUES ($1, $2, $3) ON CONFLICT DO NOTHING`, prefixedKey, raw, blockNumber)
return c.String(), err return c.String(), err
} }
+11 -3
View File
@@ -28,8 +28,8 @@ import (
"github.com/jmoiron/sqlx" "github.com/jmoiron/sqlx"
"github.com/mailgun/groupcache/v2" "github.com/mailgun/groupcache/v2"
ipfsethdb "github.com/vulcanize/ipfs-ethdb" ipfsethdb "github.com/vulcanize/ipfs-ethdb/v4"
pgipfsethdb "github.com/vulcanize/ipfs-ethdb/postgres" pgipfsethdb "github.com/vulcanize/ipfs-ethdb/v4/postgres"
) )
// 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
@@ -58,7 +58,7 @@ func NewPGIPFSValidator(db *sqlx.DB) *Validator {
kvs: kvs, kvs: kvs,
trieDB: trie.NewDatabase(kvs), trieDB: trie.NewDatabase(kvs),
stateDatabase: state.NewDatabase(database), stateDatabase: state.NewDatabase(database),
db: database, db: database.(*pgipfsethdb.Database),
} }
} }
@@ -138,3 +138,11 @@ func (v *Validator) ValidateStorageTrie(address common.Address, storageRoot comm
} }
return it.Error() return it.Error()
} }
// Close implements io.Closer
// it deregisters the groupcache name
func (v *Validator) Close() error {
groupcache.DeregisterGroup("kv")
groupcache.DeregisterGroup("db")
return nil
}
+26 -22
View File
@@ -21,28 +21,29 @@ import (
"math/big" "math/big"
"github.com/ethereum/go-ethereum/common" "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/crypto"
"github.com/ethereum/go-ethereum/rlp" "github.com/ethereum/go-ethereum/rlp"
"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"
. "github.com/onsi/ginkgo" . "github.com/onsi/ginkgo"
. "github.com/onsi/gomega" . "github.com/onsi/gomega"
"github.com/vulcanize/eth-ipfs-state-validator/pkg" validator "github.com/vulcanize/eth-ipfs-state-validator/v4/pkg"
"github.com/vulcanize/ipfs-ethdb/postgres" pgipfsethdb "github.com/vulcanize/ipfs-ethdb/v4/postgres"
) )
var ( var (
blockNumber = uint64(1)
contractAddr = common.HexToAddress("0xaE9BEa628c4Ce503DcFD7E305CaB4e29E7476592") contractAddr = common.HexToAddress("0xaE9BEa628c4Ce503DcFD7E305CaB4e29E7476592")
slot0StorageValue = common.Hex2Bytes("94703c4b2bd70c169f5717101caee543299fc946c7") slot0StorageValue = common.Hex2Bytes("94703c4b2bd70c169f5717101caee543299fc946c7")
slot1StorageValue = common.Hex2Bytes("01") slot1StorageValue = common.Hex2Bytes("01")
nullCodeHash = crypto.Keccak256Hash([]byte{}) nullCodeHash = crypto.Keccak256Hash([]byte{})
emptyRootNode, _ = rlp.EncodeToBytes([]byte{}) emptyRootNode, _ = rlp.EncodeToBytes(&[]byte{})
emptyContractRoot = crypto.Keccak256Hash(emptyRootNode) emptyContractRoot = crypto.Keccak256Hash(emptyRootNode)
stateBranchRootNode, _ = rlp.EncodeToBytes([]interface{}{ stateBranchRootNode, _ = rlp.EncodeToBytes(&[]interface{}{
crypto.Keccak256(bankAccountLeafNode), crypto.Keccak256(bankAccountLeafNode),
[]byte{}, []byte{},
[]byte{}, []byte{},
@@ -65,62 +66,62 @@ var (
mockCode = []byte{1, 2, 3, 4, 5} mockCode = []byte{1, 2, 3, 4, 5}
codeHash = crypto.Keccak256Hash(mockCode) codeHash = crypto.Keccak256Hash(mockCode)
contractAccount, _ = rlp.EncodeToBytes(state.Account{ contractAccount, _ = rlp.EncodeToBytes(&types.StateAccount{
Nonce: 1, Nonce: 1,
Balance: big.NewInt(0), Balance: big.NewInt(0),
CodeHash: codeHash.Bytes(), CodeHash: codeHash.Bytes(),
Root: crypto.Keccak256Hash(storageBranchRootNode), Root: crypto.Keccak256Hash(storageBranchRootNode),
}) })
contractAccountLeafNode, _ = rlp.EncodeToBytes([]interface{}{ contractAccountLeafNode, _ = rlp.EncodeToBytes(&[]interface{}{
common.Hex2Bytes("3114658a74d9cc9f7acf2c5cd696c3494d7c344d78bfec3add0d91ec4e8d1c45"), common.Hex2Bytes("3114658a74d9cc9f7acf2c5cd696c3494d7c344d78bfec3add0d91ec4e8d1c45"),
contractAccount, contractAccount,
}) })
minerAccount, _ = rlp.EncodeToBytes(state.Account{ minerAccount, _ = rlp.EncodeToBytes(&types.StateAccount{
Nonce: 0, Nonce: 0,
Balance: big.NewInt(1000), Balance: big.NewInt(1000),
CodeHash: nullCodeHash.Bytes(), CodeHash: nullCodeHash.Bytes(),
Root: emptyContractRoot, Root: emptyContractRoot,
}) })
minerAccountLeafNode, _ = rlp.EncodeToBytes([]interface{}{ minerAccountLeafNode, _ = rlp.EncodeToBytes(&[]interface{}{
common.Hex2Bytes("3380c7b7ae81a58eb98d9c78de4a1fd7fd9535fc953ed2be602daaa41767312a"), common.Hex2Bytes("3380c7b7ae81a58eb98d9c78de4a1fd7fd9535fc953ed2be602daaa41767312a"),
minerAccount, minerAccount,
}) })
account1, _ = rlp.EncodeToBytes(state.Account{ account1, _ = rlp.EncodeToBytes(&types.StateAccount{
Nonce: 2, Nonce: 2,
Balance: big.NewInt(1000), Balance: big.NewInt(1000),
CodeHash: nullCodeHash.Bytes(), CodeHash: nullCodeHash.Bytes(),
Root: emptyContractRoot, Root: emptyContractRoot,
}) })
account1LeafNode, _ = rlp.EncodeToBytes([]interface{}{ account1LeafNode, _ = rlp.EncodeToBytes(&[]interface{}{
common.Hex2Bytes("3926db69aaced518e9b9f0f434a473e7174109c943548bb8f23be41ca76d9ad2"), common.Hex2Bytes("3926db69aaced518e9b9f0f434a473e7174109c943548bb8f23be41ca76d9ad2"),
account1, account1,
}) })
account2, _ = rlp.EncodeToBytes(state.Account{ account2, _ = rlp.EncodeToBytes(&types.StateAccount{
Nonce: 0, Nonce: 0,
Balance: big.NewInt(1000), Balance: big.NewInt(1000),
CodeHash: nullCodeHash.Bytes(), CodeHash: nullCodeHash.Bytes(),
Root: emptyContractRoot, Root: emptyContractRoot,
}) })
account2LeafNode, _ = rlp.EncodeToBytes([]interface{}{ account2LeafNode, _ = rlp.EncodeToBytes(&[]interface{}{
common.Hex2Bytes("3957f3e2f04a0764c3a0491b175f69926da61efbcc8f61fa1455fd2d2b4cdd45"), common.Hex2Bytes("3957f3e2f04a0764c3a0491b175f69926da61efbcc8f61fa1455fd2d2b4cdd45"),
account2, account2,
}) })
bankAccount, _ = rlp.EncodeToBytes(state.Account{ bankAccount, _ = rlp.EncodeToBytes(&types.StateAccount{
Nonce: 2, Nonce: 2,
Balance: big.NewInt(1000), Balance: big.NewInt(1000),
CodeHash: nullCodeHash.Bytes(), CodeHash: nullCodeHash.Bytes(),
Root: emptyContractRoot, Root: emptyContractRoot,
}) })
bankAccountLeafNode, _ = rlp.EncodeToBytes([]interface{}{ bankAccountLeafNode, _ = rlp.EncodeToBytes(&[]interface{}{
common.Hex2Bytes("30bf49f440a1cd0527e4d06e2765654c0f56452257516d793a9b8d604dcfdf2a"), common.Hex2Bytes("30bf49f440a1cd0527e4d06e2765654c0f56452257516d793a9b8d604dcfdf2a"),
bankAccount, bankAccount,
}) })
storageBranchRootNode, _ = rlp.EncodeToBytes([]interface{}{ storageBranchRootNode, _ = rlp.EncodeToBytes(&[]interface{}{
[]byte{}, []byte{},
[]byte{}, []byte{},
crypto.Keccak256(slot0StorageLeafNode), crypto.Keccak256(slot0StorageLeafNode),
@@ -141,11 +142,11 @@ var (
}) })
storageRoot = crypto.Keccak256Hash(storageBranchRootNode) storageRoot = crypto.Keccak256Hash(storageBranchRootNode)
slot0StorageLeafNode, _ = rlp.EncodeToBytes([]interface{}{ slot0StorageLeafNode, _ = rlp.EncodeToBytes(&[]interface{}{
common.Hex2Bytes("390decd9548b62a8d60345a988386fc84ba6bc95484008f6362f93160ef3e563"), common.Hex2Bytes("390decd9548b62a8d60345a988386fc84ba6bc95484008f6362f93160ef3e563"),
slot0StorageValue, slot0StorageValue,
}) })
slot1StorageLeafNode, _ = rlp.EncodeToBytes([]interface{}{ slot1StorageLeafNode, _ = rlp.EncodeToBytes(&[]interface{}{
common.Hex2Bytes("310e2d527612073b26eecdfd717e6a320cf44b4afac2b0732d9fcbe2b7fa0cf6"), common.Hex2Bytes("310e2d527612073b26eecdfd717e6a320cf44b4afac2b0732d9fcbe2b7fa0cf6"),
slot1StorageValue, slot1StorageValue,
}) })
@@ -201,6 +202,9 @@ var _ = Describe("PG-IPFS Validator", func() {
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
v = validator.NewPGIPFSValidator(db) v = validator.NewPGIPFSValidator(db)
}) })
AfterEach(func() {
v.Close()
})
Describe("ValidateTrie", func() { Describe("ValidateTrie", func() {
AfterEach(func() { AfterEach(func() {
err = validator.ResetTestDB(db) err = validator.ResetTestDB(db)
@@ -236,7 +240,7 @@ var _ = Describe("PG-IPFS Validator", func() {
loadTrie(trieStateNodes, trieStorageNodes) loadTrie(trieStateNodes, trieStorageNodes)
err = v.ValidateTrie(stateRoot) err = v.ValidateTrie(stateRoot)
Expect(err).To(HaveOccurred()) Expect(err).To(HaveOccurred())
subStr := fmt.Sprintf("code %s: sql: no rows in result set", codeHash.Hex()[2:]) subStr := fmt.Sprintf("code %s: not found", codeHash.Hex()[2:])
Expect(err.Error()).To(ContainSubstring(subStr)) Expect(err.Error()).To(ContainSubstring(subStr))
}) })
It("Returns no error if the entire state (state trie and storage tries) can be validated", func() { It("Returns no error if the entire state (state trie and storage tries) can be validated", func() {
@@ -299,11 +303,11 @@ func loadTrie(stateNodes, storageNodes [][]byte) {
tx, err := db.Beginx() tx, err := db.Beginx()
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
for _, node := range stateNodes { for _, node := range stateNodes {
_, err := validator.PublishRaw(tx, cid.EthStateTrie, multihash.KECCAK_256, node) _, err := validator.PublishRaw(tx, cid.EthStateTrie, multihash.KECCAK_256, node, blockNumber)
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
} }
for _, node := range storageNodes { for _, node := range storageNodes {
_, err := validator.PublishRaw(tx, cid.EthStorageTrie, multihash.KECCAK_256, node) _, err := validator.PublishRaw(tx, cid.EthStorageTrie, multihash.KECCAK_256, node, blockNumber)
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
} }
err = tx.Commit() err = tx.Commit()