From 907f15f6407b4a52047484ed3239eb828ba9857c Mon Sep 17 00:00:00 2001 From: i-norden Date: Thu, 2 Mar 2023 17:59:13 -0600 Subject: [PATCH] mh_key => cid; leaf_cid => cid; refactor GetReceipts --- pkg/eth/backend.go | 12 +++++------- pkg/eth/cid_retriever.go | 28 +++++++++++++++------------- pkg/eth/ipld_retriever.go | 29 ++++++++++++++--------------- pkg/eth/types.go | 2 +- 4 files changed, 35 insertions(+), 36 deletions(-) diff --git a/pkg/eth/backend.go b/pkg/eth/backend.go index c5f554ac..cecd29a0 100644 --- a/pkg/eth/backend.go +++ b/pkg/eth/backend.go @@ -23,7 +23,6 @@ import ( "errors" "fmt" "math/big" - "strconv" "time" validator "github.com/cerc-io/eth-ipfs-state-validator/v4/pkg" @@ -68,8 +67,8 @@ const ( FROM canonical_header_hash($1) AS block_hash WHERE block_hash IS NOT NULL` RetrieveCanonicalHeaderByNumber = `SELECT cid, data FROM eth.header_cids - INNER JOIN ipld.blocks ON ( - header_cids.mh_key = blocks.key + INNER JOIN public.blocks ON ( + header_cids.cid = blocks.key AND header_cids.block_number = blocks.block_number ) WHERE block_hash = (SELECT canonical_header_hash($1))` @@ -77,7 +76,7 @@ const ( WHERE header_cids.block_hash = $1` RetrieveRPCTransaction = `SELECT blocks.data, header_id, transaction_cids.block_number, index FROM ipld.blocks, eth.transaction_cids - WHERE blocks.key = transaction_cids.mh_key + WHERE blocks.key = transaction_cids.cid AND blocks.block_number = transaction_cids.block_number AND transaction_cids.tx_hash = $1 AND transaction_cids.header_id = (SELECT canonical_header_hash(transaction_cids.block_number))` @@ -94,7 +93,7 @@ const ( AND header_cids.block_hash = (SELECT canonical_header_hash(header_cids.block_number)) ORDER BY header_cids.block_number DESC LIMIT 1` - RetrieveCodeByMhKey = `SELECT data FROM ipld.blocks WHERE key = $1` + RetrieveCodeByMhKey = `SELECT data FROM public.blocks WHERE key = $1` ) const ( @@ -582,11 +581,10 @@ func (b *Backend) GetReceipts(ctx context.Context, hash common.Hash) (types.Rece } }() - headerCID, err := b.Retriever.RetrieveHeaderCIDByHash(tx, hash) + blockNumber, err := b.Retriever.RetrieveBlockNumberByHash(tx, hash) if err != nil { return nil, err } - blockNumber, _ := strconv.ParseUint(string(headerCID.BlockNumber), 10, 64) return b.GetReceiptsByBlockHashAndNumber(tx, hash, blockNumber) } diff --git a/pkg/eth/cid_retriever.go b/pkg/eth/cid_retriever.go index d6ff7ac1..7548f4c8 100644 --- a/pkg/eth/cid_retriever.go +++ b/pkg/eth/cid_retriever.go @@ -19,6 +19,7 @@ package eth import ( "fmt" "math/big" + "strconv" "github.com/cerc-io/ipld-eth-server/v4/pkg/log" "github.com/ethereum/go-ethereum/common" @@ -202,14 +203,14 @@ func (ecr *CIDRetriever) RetrieveFilteredGQLLogs(tx *sqlx.Tx, rctFilter ReceiptF args := make([]interface{}, 0, 4) id := 1 pgStr := `SELECT CAST(eth.log_cids.block_number as Text), eth.log_cids.header_id as block_hash, - eth.log_cids.leaf_cid, eth.log_cids.index, eth.log_cids.rct_id, eth.log_cids.address, + eth.log_cids.cid, eth.log_cids.index, eth.log_cids.rct_id, eth.log_cids.address, eth.log_cids.topic0, eth.log_cids.topic1, eth.log_cids.topic2, eth.log_cids.topic3, eth.log_cids.log_data, - data, eth.receipt_cids.leaf_cid as cid, eth.receipt_cids.post_status, eth.receipt_cids.tx_id AS tx_hash + data, eth.receipt_cids.cid, eth.receipt_cids.post_status, eth.receipt_cids.tx_id AS tx_hash FROM eth.log_cids, eth.receipt_cids, ipld.blocks WHERE eth.log_cids.rct_id = receipt_cids.tx_id AND eth.log_cids.header_id = receipt_cids.header_id AND eth.log_cids.block_number = receipt_cids.block_number - AND log_cids.leaf_mh_key = blocks.key + AND log_cids.cid = blocks.key AND log_cids.block_number = blocks.block_number AND receipt_cids.header_id = $1` @@ -239,10 +240,10 @@ func (ecr *CIDRetriever) RetrieveFilteredGQLLogs(tx *sqlx.Tx, rctFilter ReceiptF func (ecr *CIDRetriever) RetrieveFilteredLog(tx *sqlx.Tx, rctFilter ReceiptFilter, blockNumber int64, blockHash *common.Hash) ([]LogResult, error) { log.Debug("retrieving log cids for receipt ids") args := make([]interface{}, 0, 4) - pgStr := `SELECT CAST(eth.log_cids.block_number as Text), eth.log_cids.leaf_cid, eth.log_cids.index, eth.log_cids.rct_id, + pgStr := `SELECT CAST(eth.log_cids.block_number as Text), eth.log_cids.cid, eth.log_cids.index, eth.log_cids.rct_id, eth.log_cids.address, eth.log_cids.topic0, eth.log_cids.topic1, eth.log_cids.topic2, eth.log_cids.topic3, eth.log_cids.log_data, eth.transaction_cids.tx_hash, eth.transaction_cids.index as txn_index, - eth.receipt_cids.leaf_cid as cid, eth.receipt_cids.post_status, header_cids.block_hash + eth.receipt_cids.cid as cid, eth.receipt_cids.post_status, header_cids.block_hash FROM eth.log_cids, eth.receipt_cids, eth.transaction_cids, eth.header_cids WHERE eth.log_cids.rct_id = receipt_cids.tx_id AND eth.log_cids.header_id = eth.receipt_cids.header_id @@ -285,14 +286,15 @@ func hasTopics(topics [][]string) bool { return false } -// RetrieveHeaderCIDByHash returns the header for the given block hash -func (ecr *CIDRetriever) RetrieveHeaderCIDByHash(tx *sqlx.Tx, blockHash common.Hash) (models.HeaderModel, error) { - log.Debug("retrieving header cids for block hash ", blockHash.String()) - pgStr := `SELECT block_hash, CAST(block_number as Text), parent_hash, cid, mh_key, CAST(td as Text), - state_root, uncle_root, tx_root, receipt_root, bloom, timestamp FROM eth.header_cids - WHERE block_hash = $1` - var headerCID models.HeaderModel - return headerCID, tx.Get(&headerCID, pgStr, blockHash.String()) +// RetrieveBlockNumberByHash returns the block number for the given block hash +func (ecr *CIDRetriever) RetrieveBlockNumberByHash(tx *sqlx.Tx, blockHash common.Hash) (uint64, error) { + log.Debug("retrieving block number for block hash ", blockHash.String()) + pgStr := `SELECT CAST(block_number as TEXT) FROM eth.header_cids WHERE block_hash = $1` + var blockNumberStr string + if err := tx.Get(&blockNumberStr, pgStr, blockHash.String()); err != nil { + return 0, err + } + return strconv.ParseUint(blockNumberStr, 10, 64) } // RetrieveHeaderAndTxCIDsByBlockNumber retrieves header CIDs and their associated tx CIDs by block number diff --git a/pkg/eth/ipld_retriever.go b/pkg/eth/ipld_retriever.go index 5d1075d3..eb849ca1 100644 --- a/pkg/eth/ipld_retriever.go +++ b/pkg/eth/ipld_retriever.go @@ -34,7 +34,7 @@ const ( RetrieveHeaderByHashPgStr = `SELECT cid, data FROM eth.header_cids INNER JOIN ipld.blocks ON ( - header_cids.mh_key = blocks.key + header_cids.cid = blocks.key AND header_cids.block_number = blocks.block_number ) WHERE block_hash = $1` @@ -45,7 +45,7 @@ const ( AND uncle_cids.block_number = header_cids.block_number ) INNER JOIN ipld.blocks ON ( - uncle_cids.mh_key = blocks.key + uncle_cids.cid = blocks.key AND uncle_cids.block_number = blocks.block_number ) WHERE header_cids.block_hash = $1 @@ -58,7 +58,7 @@ const ( AND uncle_cids.block_number = header_cids.block_number ) INNER JOIN ipld.blocks ON ( - uncle_cids.mh_key = blocks.key + uncle_cids.cid = blocks.key AND uncle_cids.block_number = blocks.block_number ) WHERE header_cids.block_hash = $1 @@ -70,7 +70,7 @@ const ( AND transaction_cids.block_number = header_cids.block_number ) INNER JOIN ipld.blocks ON ( - transaction_cids.mh_key = blocks.key + transaction_cids.cid = blocks.key AND transaction_cids.block_number = blocks.block_number ) WHERE block_hash = $1 @@ -83,12 +83,12 @@ const ( AND transaction_cids.block_number = header_cids.block_number ) INNER JOIN ipld.blocks ON ( - transaction_cids.mh_key = blocks.key + transaction_cids.cid = blocks.key AND transaction_cids.block_number = blocks.block_number ) WHERE block_hash = $1 ORDER BY eth.transaction_cids.index ASC` - RetrieveReceiptsPgStr = `SELECT receipt_cids.leaf_cid, data, eth.transaction_cids.tx_hash + RetrieveReceiptsPgStr = `SELECT receipt_cids.cid, data, eth.transaction_cids.tx_hash FROM eth.receipt_cids INNER JOIN eth.transaction_cids ON ( receipt_cids.tx_id = transaction_cids.tx_hash @@ -100,13 +100,13 @@ const ( AND transaction_cids.block_number = header_cids.block_number ) INNER JOIN ipld.blocks ON ( - receipt_cids.leaf_mh_key = blocks.key + receipt_cids.cid = blocks.key AND receipt_cids.block_number = blocks.block_number ) WHERE block_hash = $1 AND header_cids.block_number = $2 ORDER BY eth.transaction_cids.index ASC` - RetrieveReceiptsByBlockHashPgStr = `SELECT receipt_cids.leaf_cid, data, eth.transaction_cids.tx_hash + RetrieveReceiptsByBlockHashPgStr = `SELECT receipt_cids.cid, data, eth.transaction_cids.tx_hash FROM eth.receipt_cids INNER JOIN eth.transaction_cids ON ( receipt_cids.tx_id = transaction_cids.tx_hash @@ -118,12 +118,12 @@ const ( AND transaction_cids.block_number = header_cids.block_number ) INNER JOIN ipld.blocks ON ( - receipt_cids.leaf_mh_key = blocks.key + receipt_cids.cid = blocks.key AND receipt_cids.block_number = blocks.block_number ) WHERE block_hash = $1 ORDER BY eth.transaction_cids.index ASC` - RetrieveAccountByLeafKeyAndBlockHashPgStr = `SELECT state_cids.cid, state_cids.mh_key, state_cids.block_number, state_cids.node_type + RetrieveAccountByLeafKeyAndBlockHashPgStr = `SELECT state_cids.cid, state_cids.block_number, state_cids.node_type FROM eth.state_cids INNER JOIN eth.header_cids ON ( state_cids.header_id = header_cids.block_hash @@ -136,13 +136,13 @@ const ( AND header_cids.block_hash = (SELECT canonical_header_hash(header_cids.block_number)) ORDER BY header_cids.block_number DESC LIMIT 1` - RetrieveStorageLeafByAddressHashAndLeafKeyAndBlockHashPgStr = `SELECT cid, mh_key, block_number, node_type, state_leaf_removed FROM get_storage_at_by_hash($1, $2, $3)` + RetrieveStorageLeafByAddressHashAndLeafKeyAndBlockHashPgStr = `SELECT cid, block_number, node_type, state_leaf_removed FROM get_storage_at_by_hash($1, $2, $3)` ) var EmptyNodeValue = make([]byte, common.HashLength) type rctIpldResult struct { - LeafCID string `db:"leaf_cid"` + LeafCID string `db:"cid"` Data []byte `db:"data"` TxHash string `db:"tx_hash"` } @@ -296,7 +296,6 @@ func (r *IPLDRetriever) RetrieveReceiptsByBlockHash(tx *sqlx.Tx, hash common.Has type nodeInfo struct { CID string `db:"cid"` - MhKey string `db:"mh_key"` BlockNumber string `db:"block_number"` Data []byte `db:"data"` NodeType int `db:"node_type"` @@ -320,7 +319,7 @@ func (r *IPLDRetriever) RetrieveAccountByAddressAndBlockHash(address common.Addr if err != nil { return "", nil, err } - accountResult.Data, err = shared.FetchIPLD(r.db, accountResult.MhKey, blockNumber) + accountResult.Data, err = shared.FetchIPLD(r.db, accountResult.CID, blockNumber) if err != nil { return "", nil, err } @@ -351,7 +350,7 @@ func (r *IPLDRetriever) RetrieveStorageAtByAddressAndStorageSlotAndBlockHash(add if err != nil { return "", nil, nil, err } - storageResult.Data, err = shared.FetchIPLD(r.db, storageResult.MhKey, blockNumber) + storageResult.Data, err = shared.FetchIPLD(r.db, storageResult.CID, blockNumber) if err != nil { return "", nil, nil, err } diff --git a/pkg/eth/types.go b/pkg/eth/types.go index 1efa9084..d20e21ed 100644 --- a/pkg/eth/types.go +++ b/pkg/eth/types.go @@ -249,7 +249,7 @@ type ConvertedPayload struct { // LogResult represent a log. type LogResult struct { - LeafCID string `db:"leaf_cid"` + LeafCID string `db:"cid"` ReceiptID string `db:"rct_id"` Address string `db:"address"` Index int64 `db:"index"`