From 649fd54a9fec1a01f84185a9c5dbdeaa61a40442 Mon Sep 17 00:00:00 2001 From: Ian Norden Date: Fri, 3 Apr 2020 15:15:06 -0500 Subject: [PATCH] store receipt contract hash instead of contract --- ...0032_add_log_contracts_to_receipt_cids.sql | 7 ------- db/migrations/00032_update_receipt_cids.sql | 19 +++++++++++++++++++ db/migrations/00033_add_times_validated.sql | 4 ++-- environments/superNodeBTC.toml | 1 + environments/superNodeETH.toml | 1 + pkg/super_node/eth/backend.go | 5 ++--- pkg/super_node/eth/cleaner_test.go | 12 ++++++------ pkg/super_node/eth/converter.go | 7 ++++++- pkg/super_node/eth/indexer.go | 9 +++++---- pkg/super_node/eth/mocks/test_data.go | 14 +++++++------- pkg/super_node/eth/models.go | 2 +- pkg/super_node/eth/publisher.go | 2 +- pkg/super_node/eth/retriever.go | 6 +++--- pkg/watcher/eth/converter.go | 8 +++++++- 14 files changed, 61 insertions(+), 36 deletions(-) delete mode 100644 db/migrations/00032_add_log_contracts_to_receipt_cids.sql create mode 100644 db/migrations/00032_update_receipt_cids.sql diff --git a/db/migrations/00032_add_log_contracts_to_receipt_cids.sql b/db/migrations/00032_add_log_contracts_to_receipt_cids.sql deleted file mode 100644 index 7638755e..00000000 --- a/db/migrations/00032_add_log_contracts_to_receipt_cids.sql +++ /dev/null @@ -1,7 +0,0 @@ --- +goose Up -ALTER TABLE eth.receipt_cids -ADD COLUMN log_contracts VARCHAR(66)[]; - --- +goose Down -ALTER TABLE eth.receipt_cids -DROP COLUMN log_contracts; \ No newline at end of file diff --git a/db/migrations/00032_update_receipt_cids.sql b/db/migrations/00032_update_receipt_cids.sql new file mode 100644 index 00000000..a4ca85f3 --- /dev/null +++ b/db/migrations/00032_update_receipt_cids.sql @@ -0,0 +1,19 @@ +-- +goose Up +ALTER TABLE eth.receipt_cids +ADD COLUMN log_contracts VARCHAR(66)[]; + +ALTER TABLE eth.receipt_cids +RENAME COLUMN contract TO contract_hash; + +ALTER TABLE eth.receipt_cids +ADD CONSTRAINT receipt_cids_tx_id_key UNIQUE (tx_id); + +-- +goose Down +ALTER TABLE eth.receipt_cids +DROP CONSTRAINT receipt_cids_tx_id_key; + +ALTER TABLE eth.receipt_cids +RENAME COLUMN contract_hash TO contract; + +ALTER TABLE eth.receipt_cids +DROP COLUMN log_contracts; \ No newline at end of file diff --git a/db/migrations/00033_add_times_validated.sql b/db/migrations/00033_add_times_validated.sql index 2502b195..eb8cbd27 100644 --- a/db/migrations/00033_add_times_validated.sql +++ b/db/migrations/00033_add_times_validated.sql @@ -1,9 +1,9 @@ -- +goose Up ALTER TABLE eth.header_cids -ADD COLUMN times_validated INTEGER NOT NULL DEFAULT 0; +ADD COLUMN times_validated INTEGER NOT NULL DEFAULT 1; ALTER TABLE btc.header_cids -ADD COLUMN times_validated INTEGER NOT NULL DEFAULT 0; +ADD COLUMN times_validated INTEGER NOT NULL DEFAULT 1; -- +goose Down ALTER TABLE btc.header_cids diff --git a/environments/superNodeBTC.toml b/environments/superNodeBTC.toml index 99171a9d..d6ee3f6f 100644 --- a/environments/superNodeBTC.toml +++ b/environments/superNodeBTC.toml @@ -16,6 +16,7 @@ batchSize = 1 # $RESYNC_BATCH_SIZE batchNumber = 50 # $RESYNC_BATCH_NUMBER clearOldCache = false # $RESYNC_CLEAR_OLD_CACHE + resetValidation = false # $RESYNC_RESET_VALIDATION [superNode] chain = "bitcoin" # $SUPERNODE_CHAIN diff --git a/environments/superNodeETH.toml b/environments/superNodeETH.toml index 69e3b32d..65c339ec 100644 --- a/environments/superNodeETH.toml +++ b/environments/superNodeETH.toml @@ -16,6 +16,7 @@ batchSize = 5 # $RESYNC_BATCH_SIZE batchNumber = 50 # $RESYNC_BATCH_NUMBER clearOldCache = true # $RESYNC_CLEAR_OLD_CACHE + resetValidation = true # $RESYNC_RESET_VALIDATION [superNode] chain = "ethereum" # $SUPERNODE_CHAIN diff --git a/pkg/super_node/eth/backend.go b/pkg/super_node/eth/backend.go index 6c8d4a9b..1365c47c 100644 --- a/pkg/super_node/eth/backend.go +++ b/pkg/super_node/eth/backend.go @@ -22,15 +22,14 @@ import ( "fmt" "math/big" - "github.com/ethereum/go-ethereum/common/hexutil" - "github.com/vulcanize/vulcanizedb/pkg/ipfs" - "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/common/hexutil" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/rlp" "github.com/ethereum/go-ethereum/rpc" "github.com/sirupsen/logrus" + "github.com/vulcanize/vulcanizedb/pkg/ipfs" "github.com/vulcanize/vulcanizedb/pkg/postgres" ) diff --git a/pkg/super_node/eth/cleaner_test.go b/pkg/super_node/eth/cleaner_test.go index 0c3c5b88..3ced56ac 100644 --- a/pkg/super_node/eth/cleaner_test.go +++ b/pkg/super_node/eth/cleaner_test.go @@ -89,12 +89,12 @@ var ( rct2Contract = common.HexToAddress("0x010c") receiptModels1 = map[common.Hash]eth2.ReceiptModel{ tx1Hash: { - CID: rct1CID, - Contract: rct1Contract.String(), + CID: rct1CID, + ContractHash: crypto.Keccak256Hash(rct1Contract.Bytes()).String(), }, tx2Hash: { - CID: rct2CID, - Contract: rct2Contract.String(), + CID: rct2CID, + ContractHash: crypto.Keccak256Hash(rct2Contract.Bytes()).String(), }, } @@ -170,8 +170,8 @@ var ( rct3CID = "mockRct3CID" receiptModels2 = map[common.Hash]eth2.ReceiptModel{ tx3Hash: { - CID: rct3CID, - Contract: rct1Contract.String(), + CID: rct3CID, + ContractHash: crypto.Keccak256Hash(rct1Contract.Bytes()).String(), }, } diff --git a/pkg/super_node/eth/converter.go b/pkg/super_node/eth/converter.go index ae5bee85..537bc9a2 100644 --- a/pkg/super_node/eth/converter.go +++ b/pkg/super_node/eth/converter.go @@ -104,12 +104,17 @@ func (pc *PayloadConverter) Convert(payload shared.RawChainData) (shared.Convert for addr := range mappedContracts { logContracts = append(logContracts, addr) } + contract := shared.HandleNullAddr(receipt.ContractAddress) + var contractHash string + if contract != "" { + contractHash = crypto.Keccak256Hash(common.HexToAddress(contract).Bytes()).String() + } rctMeta := ReceiptModel{ Topic0s: topicSets[0], Topic1s: topicSets[1], Topic2s: topicSets[2], Topic3s: topicSets[3], - Contract: shared.HandleNullAddr(receipt.ContractAddress), + ContractHash: contractHash, LogContracts: logContracts, } // receipt and rctMeta will have same indexes diff --git a/pkg/super_node/eth/indexer.go b/pkg/super_node/eth/indexer.go index 533d0443..f19efa9e 100644 --- a/pkg/super_node/eth/indexer.go +++ b/pkg/super_node/eth/indexer.go @@ -127,8 +127,9 @@ func (in *CIDIndexer) indexTransactionAndReceiptCIDs(tx *sqlx.Tx, payload *CIDPa } func (in *CIDIndexer) indexReceiptCID(tx *sqlx.Tx, cidMeta ReceiptModel, txID int64) error { - _, err := tx.Exec(`INSERT INTO eth.receipt_cids (tx_id, cid, contract, topic0s, topic1s, topic2s, topic3s, log_contracts) VALUES ($1, $2, $3, $4, $5, $6, $7, $8)`, - txID, cidMeta.CID, cidMeta.Contract, cidMeta.Topic0s, cidMeta.Topic1s, cidMeta.Topic2s, cidMeta.Topic3s, cidMeta.LogContracts) + _, err := tx.Exec(`INSERT INTO eth.receipt_cids (tx_id, cid, contract_hash, topic0s, topic1s, topic2s, topic3s, log_contracts) VALUES ($1, $2, $3, $4, $5, $6, $7, $8) + ON CONFLICT (tx_id) DO UPDATE SET (cid, contract_hash, topic0s, topic1s, topic2s, topic3s, log_contracts) = ($2, $3, $4, $5, $6, $7, $8)`, + txID, cidMeta.CID, cidMeta.ContractHash, cidMeta.Topic0s, cidMeta.Topic1s, cidMeta.Topic2s, cidMeta.Topic3s, cidMeta.LogContracts) return err } @@ -166,7 +167,7 @@ func (in *CIDIndexer) indexStateAndStorageCIDs(tx *sqlx.Tx, payload *CIDPayload, func (in *CIDIndexer) indexStateAccount(tx *sqlx.Tx, stateAccount StateAccountModel, stateID int64) error { _, err := tx.Exec(`INSERT INTO eth.state_accounts (state_id, balance, nonce, code_hash, storage_root) VALUES ($1, $2, $3, $4, $5) - ON CONFLICT (state_id) DO UPDATE SET (balance, nonce, code_hash, storage_root) = ($2, $3, $4, $5)`, + ON CONFLICT (state_id) DO UPDATE SET (balance, nonce, code_hash, storage_root) = ($2, $3, $4, $5)`, stateID, stateAccount.Balance, stateAccount.Nonce, stateAccount.CodeHash, stateAccount.StorageRoot) return err } @@ -177,7 +178,7 @@ func (in *CIDIndexer) indexStorageCID(tx *sqlx.Tx, storageCID StorageNodeModel, storageKey = storageCID.StorageKey } _, err := tx.Exec(`INSERT INTO eth.storage_cids (state_id, storage_leaf_key, cid, storage_path, node_type) VALUES ($1, $2, $3, $4, $5) - ON CONFLICT (state_id, storage_path) DO UPDATE SET (storage_leaf_key, cid, node_type) = ($2, $3, $5)`, + ON CONFLICT (state_id, storage_path) DO UPDATE SET (storage_leaf_key, cid, node_type) = ($2, $3, $5)`, stateID, storageKey, storageCID.CID, storageCID.Path, storageCID.NodeType) return err } diff --git a/pkg/super_node/eth/mocks/test_data.go b/pkg/super_node/eth/mocks/test_data.go index 0c578446..c0439435 100644 --- a/pkg/super_node/eth/mocks/test_data.go +++ b/pkg/super_node/eth/mocks/test_data.go @@ -61,7 +61,7 @@ var ( Address = common.HexToAddress("0xaE9BEa628c4Ce503DcFD7E305CaB4e29E7476592") AnotherAddress = common.HexToAddress("0xaE9BEa628c4Ce503DcFD7E305CaB4e29E7476593") ContractAddress = crypto.CreateAddress(SenderAddr, MockTransactions[2].Nonce()) - NullAddr = common.HexToAddress("0x0000000000000000000000000000000000000000") + ContractHash = crypto.Keccak256Hash(ContractAddress.Bytes()).String() mockTopic11 = common.HexToHash("0x04") mockTopic12 = common.HexToHash("0x06") mockTopic21 = common.HexToHash("0x05") @@ -141,7 +141,7 @@ var ( Topic1s: []string{ mockTopic12.String(), }, - Contract: "", + ContractHash: "", LogContracts: []string{ Address.String(), }, @@ -154,14 +154,14 @@ var ( Topic1s: []string{ mockTopic22.String(), }, - Contract: "", + ContractHash: "", LogContracts: []string{ AnotherAddress.String(), }, }, { CID: "", - Contract: ContractAddress.String(), + ContractHash: ContractHash, LogContracts: []string{}, }, } @@ -174,7 +174,7 @@ var ( Topic1s: []string{ mockTopic12.String(), }, - Contract: "", + ContractHash: "", LogContracts: []string{ Address.String(), }, @@ -187,14 +187,14 @@ var ( Topic1s: []string{ mockTopic22.String(), }, - Contract: "", + ContractHash: "", LogContracts: []string{ AnotherAddress.String(), }, }, { CID: Rct3CID.String(), - Contract: ContractAddress.String(), + ContractHash: ContractHash, LogContracts: []string{}, }, } diff --git a/pkg/super_node/eth/models.go b/pkg/super_node/eth/models.go index 52740799..c960b91b 100644 --- a/pkg/super_node/eth/models.go +++ b/pkg/super_node/eth/models.go @@ -63,7 +63,7 @@ type ReceiptModel struct { ID int64 `db:"id"` TxID int64 `db:"tx_id"` CID string `db:"cid"` - Contract string `db:"contract"` + ContractHash string `db:"contract_hash"` LogContracts pq.StringArray `db:"log_contracts"` Topic0s pq.StringArray `db:"topic0s"` Topic1s pq.StringArray `db:"topic1s"` diff --git a/pkg/super_node/eth/publisher.go b/pkg/super_node/eth/publisher.go index 21a9c01a..67211cc3 100644 --- a/pkg/super_node/eth/publisher.go +++ b/pkg/super_node/eth/publisher.go @@ -189,7 +189,7 @@ func (pub *IPLDPublisher) publishReceipts(receipts []*ipld.EthReceipt, receiptTr } rctCids[rct.TxHash] = ReceiptModel{ CID: cid, - Contract: receiptMeta[i].Contract, + ContractHash: receiptMeta[i].ContractHash, Topic0s: receiptMeta[i].Topic0s, Topic1s: receiptMeta[i].Topic1s, Topic2s: receiptMeta[i].Topic2s, diff --git a/pkg/super_node/eth/retriever.go b/pkg/super_node/eth/retriever.go index c537a808..bfe3f5d1 100644 --- a/pkg/super_node/eth/retriever.go +++ b/pkg/super_node/eth/retriever.go @@ -215,7 +215,7 @@ func (ecr *CIDRetriever) RetrieveRctCIDsByHeaderID(tx *sqlx.Tx, rctFilter Receip log.Debug("retrieving receipt cids for header id ", headerID) args := make([]interface{}, 0, 4) pgStr := `SELECT receipt_cids.id, receipt_cids.tx_id, receipt_cids.cid, - receipt_cids.contract, receipt_cids.topic0s, receipt_cids.topic1s, + receipt_cids.contract_hash, receipt_cids.topic0s, receipt_cids.topic1s, receipt_cids.topic2s, receipt_cids.topic3s, receipt_cids.log_contracts FROM eth.receipt_cids, eth.transaction_cids, eth.header_cids WHERE receipt_cids.tx_id = transaction_cids.id @@ -295,7 +295,7 @@ func (ecr *CIDRetriever) RetrieveRctCIDs(tx *sqlx.Tx, rctFilter ReceiptFilter, b log.Debug("retrieving receipt cids for block ", blockNumber) args := make([]interface{}, 0, 5) pgStr := `SELECT receipt_cids.id, receipt_cids.tx_id, receipt_cids.cid, - receipt_cids.contract, receipt_cids.topic0s, receipt_cids.topic1s, + receipt_cids.contract_hash, receipt_cids.topic0s, receipt_cids.topic1s, receipt_cids.topic2s, receipt_cids.topic3s, receipt_cids.log_contracts FROM eth.receipt_cids, eth.transaction_cids, eth.header_cids WHERE receipt_cids.tx_id = transaction_cids.id @@ -587,7 +587,7 @@ func (ecr *CIDRetriever) RetrieveTxCIDsByHeaderID(tx *sqlx.Tx, headerID int64) ( func (ecr *CIDRetriever) RetrieveReceiptCIDsByTxIDs(tx *sqlx.Tx, txIDs []int64) ([]ReceiptModel, error) { log.Debugf("retrieving receipt cids for tx ids %v", txIDs) pgStr := `SELECT receipt_cids.id, receipt_cids.tx_id, receipt_cids.cid, - receipt_cids.contract, receipt_cids.topic0s, receipt_cids.topic1s, + receipt_cids.contract_hash, receipt_cids.topic0s, receipt_cids.topic1s, receipt_cids.topic2s, receipt_cids.topic3s, receipt_cids.log_contracts FROM eth.receipt_cids, eth.transaction_cids WHERE tx_id = ANY($1::INTEGER[]) diff --git a/pkg/watcher/eth/converter.go b/pkg/watcher/eth/converter.go index 6e290d82..b08dbb31 100644 --- a/pkg/watcher/eth/converter.go +++ b/pkg/watcher/eth/converter.go @@ -18,6 +18,7 @@ package eth import ( "fmt" + "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/types" @@ -127,6 +128,11 @@ func (pc *WatcherConverter) Convert(ethIPLDs eth.IPLDs) (*eth.CIDPayload, error) for addr := range mappedContracts { logContracts = append(logContracts, addr) } + contract := shared.HandleNullAddr(receipt.ContractAddress) + var contractHash string + if contract != "" { + contractHash = crypto.Keccak256Hash(common.Hex2Bytes(contract)).String() + } // Rct data cids.ReceiptCIDs[matchedTx.Hash()] = eth.ReceiptModel{ CID: ethIPLDs.Receipts[i].CID, @@ -134,7 +140,7 @@ func (pc *WatcherConverter) Convert(ethIPLDs eth.IPLDs) (*eth.CIDPayload, error) Topic1s: topicSets[1], Topic2s: topicSets[2], Topic3s: topicSets[3], - Contract: receipt.ContractAddress.Hex(), + ContractHash: contractHash, LogContracts: logContracts, } }