diff --git a/statediff/builder.go b/statediff/builder.go index c9757b794269..49b920ee1872 100644 --- a/statediff/builder.go +++ b/statediff/builder.go @@ -419,18 +419,6 @@ func (sdb *builder) deletedOrUpdatedState(a, b trie.NodeIterator, diffPathsAtB m if err != nil { return nil, err } - // if this node's path did not show up in diffPathsAtB - // that means the node at this path was deleted (or moved) in B - // emit an empty "removed" diff to signify as such - if _, ok := diffPathsAtB[common.Bytes2Hex(node.Path)]; !ok { - if err := output(StateNode{ - Path: node.Path, - NodeValue: []byte{}, - NodeType: Removed, - }); err != nil { - return nil, err - } - } switch node.NodeType { case Leaf: // map all different accounts at A to their leafkey @@ -449,7 +437,32 @@ func (sdb *builder) deletedOrUpdatedState(a, b trie.NodeIterator, diffPathsAtB m LeafKey: leafKey, Account: &account, } + // if this node's path did not show up in diffPathsAtB + // that means the node at this path was deleted (or moved) in B + // emit an empty "removed" diff to signify as such + if _, ok := diffPathsAtB[common.Bytes2Hex(node.Path)]; !ok { + if err := output(StateNode{ + Path: node.Path, + NodeValue: []byte{}, + NodeType: Removed, + LeafKey: leafKey, + }); err != nil { + return nil, err + } + } case Extension, Branch: + // if this node's path did not show up in diffPathsAtB + // that means the node at this path was deleted (or moved) in B + // emit an empty "removed" diff to signify as such + if _, ok := diffPathsAtB[common.Bytes2Hex(node.Path)]; !ok { + if err := output(StateNode{ + Path: node.Path, + NodeValue: []byte{}, + NodeType: Removed, + }); err != nil { + return nil, err + } + } // fall through, we did everything we need to do with these node types default: return nil, fmt.Errorf("unexpected node type %s", node.NodeType) @@ -703,6 +716,7 @@ func (sdb *builder) deletedOrUpdatedStorage(a, b trie.NodeIterator, diffPathsAtB NodeType: Removed, Path: node.Path, NodeValue: []byte{}, + LeafKey: leafKey, }); err != nil { return err } diff --git a/statediff/builder_test.go b/statediff/builder_test.go index 8ad0d9822e84..d62bdc76605d 100644 --- a/statediff/builder_test.go +++ b/statediff/builder_test.go @@ -1375,11 +1375,13 @@ func TestBuilderWithRemovedAccountAndStorage(t *testing.T) { { Path: []byte{'\x0b'}, NodeType: sdtypes.Removed, + LeafKey: slot1StorageKey.Bytes(), NodeValue: []byte{}, }, { Path: []byte{'\x0c'}, NodeType: sdtypes.Removed, + LeafKey: slot3StorageKey.Bytes(), NodeValue: []byte{}, }, }, @@ -1434,11 +1436,13 @@ func TestBuilderWithRemovedAccountAndStorage(t *testing.T) { { Path: []byte{'\x02'}, NodeType: sdtypes.Removed, + LeafKey: slot0StorageKey.Bytes(), NodeValue: []byte{}, }, { Path: []byte{'\x04'}, NodeType: sdtypes.Removed, + LeafKey: slot2StorageKey.Bytes(), NodeValue: []byte{}, }, }, @@ -1474,6 +1478,7 @@ func TestBuilderWithRemovedAccountAndStorage(t *testing.T) { { Path: []byte{'\x06'}, NodeType: sdtypes.Removed, + LeafKey: contractLeafKey, NodeValue: []byte{}, }, { @@ -1571,11 +1576,13 @@ func TestBuilderWithRemovedAccountAndStorageWithoutIntermediateNodes(t *testing. { Path: []byte{'\x0b'}, NodeType: sdtypes.Removed, + LeafKey: slot1StorageKey.Bytes(), NodeValue: []byte{}, }, { Path: []byte{'\x0c'}, NodeType: sdtypes.Removed, + LeafKey: slot3StorageKey.Bytes(), NodeValue: []byte{}, }, }, @@ -1618,17 +1625,19 @@ func TestBuilderWithRemovedAccountAndStorageWithoutIntermediateNodes(t *testing. { Path: []byte{}, NodeType: sdtypes.Leaf, - NodeValue: slot0StorageLeafRootNode, LeafKey: slot0StorageKey.Bytes(), + NodeValue: slot0StorageLeafRootNode, }, { Path: []byte{'\x02'}, NodeType: sdtypes.Removed, + LeafKey: slot0StorageKey.Bytes(), NodeValue: []byte{}, }, { Path: []byte{'\x04'}, NodeType: sdtypes.Removed, + LeafKey: slot2StorageKey.Bytes(), NodeValue: []byte{}, }, }, @@ -1658,6 +1667,7 @@ func TestBuilderWithRemovedAccountAndStorageWithoutIntermediateNodes(t *testing. { Path: []byte{'\x06'}, NodeType: sdtypes.Removed, + LeafKey: contractLeafKey, NodeValue: []byte{}, }, { @@ -1881,11 +1891,13 @@ func TestBuilderWithMovedAccount(t *testing.T) { { Path: []byte{'\x01'}, NodeType: sdtypes.Removed, + LeafKey: contractLeafKey, NodeValue: []byte{}, }, { Path: []byte{'\x00'}, NodeType: sdtypes.Removed, + LeafKey: testhelpers.BankLeafKey, NodeValue: []byte{}, }, }, @@ -2003,11 +2015,13 @@ func TestBuilderWithMovedAccountOnlyLeafs(t *testing.T) { { Path: []byte{'\x01'}, NodeType: sdtypes.Removed, + LeafKey: contractLeafKey, NodeValue: []byte{}, }, { Path: []byte{'\x00'}, NodeType: sdtypes.Removed, + LeafKey: testhelpers.BankLeafKey, NodeValue: []byte{}, }, }, diff --git a/statediff/indexer/postgres/errors.go b/statediff/indexer/postgres/errors.go index f368f900b2e5..effa74aa125e 100644 --- a/statediff/indexer/postgres/errors.go +++ b/statediff/indexer/postgres/errors.go @@ -21,29 +21,14 @@ import ( ) const ( - BeginTransactionFailedMsg = "failed to begin transaction" - DbConnectionFailedMsg = "db connection failed" - DeleteQueryFailedMsg = "delete query failed" - InsertQueryFailedMsg = "insert query failed" - SettingNodeFailedMsg = "unable to set db node" + DbConnectionFailedMsg = "db connection failed" + SettingNodeFailedMsg = "unable to set db node" ) -func ErrBeginTransactionFailed(beginErr error) error { - return formatError(BeginTransactionFailedMsg, beginErr.Error()) -} - func ErrDBConnectionFailed(connectErr error) error { return formatError(DbConnectionFailedMsg, connectErr.Error()) } -func ErrDBDeleteFailed(deleteErr error) error { - return formatError(DeleteQueryFailedMsg, deleteErr.Error()) -} - -func ErrDBInsertFailed(insertErr error) error { - return formatError(InsertQueryFailedMsg, insertErr.Error()) -} - func ErrUnableToSetNode(setErr error) error { return formatError(SettingNodeFailedMsg, setErr.Error()) } diff --git a/statediff/indexer/postgres/postgres_test.go b/statediff/indexer/postgres/postgres_test.go index f86055352ad6..d245b78bbc9c 100644 --- a/statediff/indexer/postgres/postgres_test.go +++ b/statediff/indexer/postgres/postgres_test.go @@ -31,7 +31,13 @@ import ( "github.com/ethereum/go-ethereum/statediff/indexer/shared" ) -var DBParams postgres.ConnectionParams +var DBParams = postgres.ConnectionParams{ + Name: "vulcanize_testing", + Password: "", + Port: 5432, + Hostname: "localhost", + User: "postgres", +} func expectContainsSubstring(t *testing.T, full string, sub string) { if !strings.Contains(full, sub) { @@ -49,7 +55,7 @@ func TestPostgresDB(t *testing.T) { sqlxdb, err = sqlx.Connect("postgres", pgConfig) if err != nil { - t.Fatal(err) + t.Fatalf("failed to connect to db with connection string: %s err: %v", pgConfig, err) } if sqlxdb == nil { t.Fatal("DB is nil")