mh_key => cid; leaf_cid => cid; refactor GetReceipts

This commit is contained in:
i-norden 2023-03-02 17:59:13 -06:00
parent 87013110f9
commit 907f15f640
4 changed files with 35 additions and 36 deletions

View File

@ -23,7 +23,6 @@ import (
"errors" "errors"
"fmt" "fmt"
"math/big" "math/big"
"strconv"
"time" "time"
validator "github.com/cerc-io/eth-ipfs-state-validator/v4/pkg" validator "github.com/cerc-io/eth-ipfs-state-validator/v4/pkg"
@ -68,8 +67,8 @@ const (
FROM canonical_header_hash($1) AS block_hash FROM canonical_header_hash($1) AS block_hash
WHERE block_hash IS NOT NULL` WHERE block_hash IS NOT NULL`
RetrieveCanonicalHeaderByNumber = `SELECT cid, data FROM eth.header_cids RetrieveCanonicalHeaderByNumber = `SELECT cid, data FROM eth.header_cids
INNER JOIN ipld.blocks ON ( INNER JOIN public.blocks ON (
header_cids.mh_key = blocks.key header_cids.cid = blocks.key
AND header_cids.block_number = blocks.block_number AND header_cids.block_number = blocks.block_number
) )
WHERE block_hash = (SELECT canonical_header_hash($1))` WHERE block_hash = (SELECT canonical_header_hash($1))`
@ -77,7 +76,7 @@ const (
WHERE header_cids.block_hash = $1` WHERE header_cids.block_hash = $1`
RetrieveRPCTransaction = `SELECT blocks.data, header_id, transaction_cids.block_number, index RetrieveRPCTransaction = `SELECT blocks.data, header_id, transaction_cids.block_number, index
FROM ipld.blocks, eth.transaction_cids 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 blocks.block_number = transaction_cids.block_number
AND transaction_cids.tx_hash = $1 AND transaction_cids.tx_hash = $1
AND transaction_cids.header_id = (SELECT canonical_header_hash(transaction_cids.block_number))` 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)) AND header_cids.block_hash = (SELECT canonical_header_hash(header_cids.block_number))
ORDER BY header_cids.block_number DESC ORDER BY header_cids.block_number DESC
LIMIT 1` LIMIT 1`
RetrieveCodeByMhKey = `SELECT data FROM ipld.blocks WHERE key = $1` RetrieveCodeByMhKey = `SELECT data FROM public.blocks WHERE key = $1`
) )
const ( 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 { if err != nil {
return nil, err return nil, err
} }
blockNumber, _ := strconv.ParseUint(string(headerCID.BlockNumber), 10, 64)
return b.GetReceiptsByBlockHashAndNumber(tx, hash, blockNumber) return b.GetReceiptsByBlockHashAndNumber(tx, hash, blockNumber)
} }

View File

@ -19,6 +19,7 @@ package eth
import ( import (
"fmt" "fmt"
"math/big" "math/big"
"strconv"
"github.com/cerc-io/ipld-eth-server/v4/pkg/log" "github.com/cerc-io/ipld-eth-server/v4/pkg/log"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
@ -202,14 +203,14 @@ func (ecr *CIDRetriever) RetrieveFilteredGQLLogs(tx *sqlx.Tx, rctFilter ReceiptF
args := make([]interface{}, 0, 4) args := make([]interface{}, 0, 4)
id := 1 id := 1
pgStr := `SELECT CAST(eth.log_cids.block_number as Text), eth.log_cids.header_id as block_hash, 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, 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 FROM eth.log_cids, eth.receipt_cids, ipld.blocks
WHERE eth.log_cids.rct_id = receipt_cids.tx_id WHERE eth.log_cids.rct_id = receipt_cids.tx_id
AND eth.log_cids.header_id = receipt_cids.header_id AND eth.log_cids.header_id = receipt_cids.header_id
AND eth.log_cids.block_number = receipt_cids.block_number 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 log_cids.block_number = blocks.block_number
AND receipt_cids.header_id = $1` 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) { func (ecr *CIDRetriever) RetrieveFilteredLog(tx *sqlx.Tx, rctFilter ReceiptFilter, blockNumber int64, blockHash *common.Hash) ([]LogResult, error) {
log.Debug("retrieving log cids for receipt ids") log.Debug("retrieving log cids for receipt ids")
args := make([]interface{}, 0, 4) 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.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.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 FROM eth.log_cids, eth.receipt_cids, eth.transaction_cids, eth.header_cids
WHERE eth.log_cids.rct_id = receipt_cids.tx_id WHERE eth.log_cids.rct_id = receipt_cids.tx_id
AND eth.log_cids.header_id = eth.receipt_cids.header_id AND eth.log_cids.header_id = eth.receipt_cids.header_id
@ -285,14 +286,15 @@ func hasTopics(topics [][]string) bool {
return false return false
} }
// RetrieveHeaderCIDByHash returns the header for the given block hash // RetrieveBlockNumberByHash returns the block number for the given block hash
func (ecr *CIDRetriever) RetrieveHeaderCIDByHash(tx *sqlx.Tx, blockHash common.Hash) (models.HeaderModel, error) { func (ecr *CIDRetriever) RetrieveBlockNumberByHash(tx *sqlx.Tx, blockHash common.Hash) (uint64, error) {
log.Debug("retrieving header cids for block hash ", blockHash.String()) log.Debug("retrieving block number for block hash ", blockHash.String())
pgStr := `SELECT block_hash, CAST(block_number as Text), parent_hash, cid, mh_key, CAST(td as Text), pgStr := `SELECT CAST(block_number as TEXT) FROM eth.header_cids WHERE block_hash = $1`
state_root, uncle_root, tx_root, receipt_root, bloom, timestamp FROM eth.header_cids var blockNumberStr string
WHERE block_hash = $1` if err := tx.Get(&blockNumberStr, pgStr, blockHash.String()); err != nil {
var headerCID models.HeaderModel return 0, err
return headerCID, tx.Get(&headerCID, pgStr, blockHash.String()) }
return strconv.ParseUint(blockNumberStr, 10, 64)
} }
// RetrieveHeaderAndTxCIDsByBlockNumber retrieves header CIDs and their associated tx CIDs by block number // RetrieveHeaderAndTxCIDsByBlockNumber retrieves header CIDs and their associated tx CIDs by block number

View File

@ -34,7 +34,7 @@ const (
RetrieveHeaderByHashPgStr = `SELECT cid, data RetrieveHeaderByHashPgStr = `SELECT cid, data
FROM eth.header_cids FROM eth.header_cids
INNER JOIN ipld.blocks ON ( INNER JOIN ipld.blocks ON (
header_cids.mh_key = blocks.key header_cids.cid = blocks.key
AND header_cids.block_number = blocks.block_number AND header_cids.block_number = blocks.block_number
) )
WHERE block_hash = $1` WHERE block_hash = $1`
@ -45,7 +45,7 @@ const (
AND uncle_cids.block_number = header_cids.block_number AND uncle_cids.block_number = header_cids.block_number
) )
INNER JOIN ipld.blocks ON ( INNER JOIN ipld.blocks ON (
uncle_cids.mh_key = blocks.key uncle_cids.cid = blocks.key
AND uncle_cids.block_number = blocks.block_number AND uncle_cids.block_number = blocks.block_number
) )
WHERE header_cids.block_hash = $1 WHERE header_cids.block_hash = $1
@ -58,7 +58,7 @@ const (
AND uncle_cids.block_number = header_cids.block_number AND uncle_cids.block_number = header_cids.block_number
) )
INNER JOIN ipld.blocks ON ( INNER JOIN ipld.blocks ON (
uncle_cids.mh_key = blocks.key uncle_cids.cid = blocks.key
AND uncle_cids.block_number = blocks.block_number AND uncle_cids.block_number = blocks.block_number
) )
WHERE header_cids.block_hash = $1 WHERE header_cids.block_hash = $1
@ -70,7 +70,7 @@ const (
AND transaction_cids.block_number = header_cids.block_number AND transaction_cids.block_number = header_cids.block_number
) )
INNER JOIN ipld.blocks ON ( INNER JOIN ipld.blocks ON (
transaction_cids.mh_key = blocks.key transaction_cids.cid = blocks.key
AND transaction_cids.block_number = blocks.block_number AND transaction_cids.block_number = blocks.block_number
) )
WHERE block_hash = $1 WHERE block_hash = $1
@ -83,12 +83,12 @@ const (
AND transaction_cids.block_number = header_cids.block_number AND transaction_cids.block_number = header_cids.block_number
) )
INNER JOIN ipld.blocks ON ( INNER JOIN ipld.blocks ON (
transaction_cids.mh_key = blocks.key transaction_cids.cid = blocks.key
AND transaction_cids.block_number = blocks.block_number AND transaction_cids.block_number = blocks.block_number
) )
WHERE block_hash = $1 WHERE block_hash = $1
ORDER BY eth.transaction_cids.index ASC` 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 FROM eth.receipt_cids
INNER JOIN eth.transaction_cids ON ( INNER JOIN eth.transaction_cids ON (
receipt_cids.tx_id = transaction_cids.tx_hash receipt_cids.tx_id = transaction_cids.tx_hash
@ -100,13 +100,13 @@ const (
AND transaction_cids.block_number = header_cids.block_number AND transaction_cids.block_number = header_cids.block_number
) )
INNER JOIN ipld.blocks ON ( 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 AND receipt_cids.block_number = blocks.block_number
) )
WHERE block_hash = $1 WHERE block_hash = $1
AND header_cids.block_number = $2 AND header_cids.block_number = $2
ORDER BY eth.transaction_cids.index ASC` 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 FROM eth.receipt_cids
INNER JOIN eth.transaction_cids ON ( INNER JOIN eth.transaction_cids ON (
receipt_cids.tx_id = transaction_cids.tx_hash receipt_cids.tx_id = transaction_cids.tx_hash
@ -118,12 +118,12 @@ const (
AND transaction_cids.block_number = header_cids.block_number AND transaction_cids.block_number = header_cids.block_number
) )
INNER JOIN ipld.blocks ON ( 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 AND receipt_cids.block_number = blocks.block_number
) )
WHERE block_hash = $1 WHERE block_hash = $1
ORDER BY eth.transaction_cids.index ASC` 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 FROM eth.state_cids
INNER JOIN eth.header_cids ON ( INNER JOIN eth.header_cids ON (
state_cids.header_id = header_cids.block_hash 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)) AND header_cids.block_hash = (SELECT canonical_header_hash(header_cids.block_number))
ORDER BY header_cids.block_number DESC ORDER BY header_cids.block_number DESC
LIMIT 1` 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) var EmptyNodeValue = make([]byte, common.HashLength)
type rctIpldResult struct { type rctIpldResult struct {
LeafCID string `db:"leaf_cid"` LeafCID string `db:"cid"`
Data []byte `db:"data"` Data []byte `db:"data"`
TxHash string `db:"tx_hash"` TxHash string `db:"tx_hash"`
} }
@ -296,7 +296,6 @@ func (r *IPLDRetriever) RetrieveReceiptsByBlockHash(tx *sqlx.Tx, hash common.Has
type nodeInfo struct { type nodeInfo struct {
CID string `db:"cid"` CID string `db:"cid"`
MhKey string `db:"mh_key"`
BlockNumber string `db:"block_number"` BlockNumber string `db:"block_number"`
Data []byte `db:"data"` Data []byte `db:"data"`
NodeType int `db:"node_type"` NodeType int `db:"node_type"`
@ -320,7 +319,7 @@ func (r *IPLDRetriever) RetrieveAccountByAddressAndBlockHash(address common.Addr
if err != nil { if err != nil {
return "", nil, err 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 { if err != nil {
return "", nil, err return "", nil, err
} }
@ -351,7 +350,7 @@ func (r *IPLDRetriever) RetrieveStorageAtByAddressAndStorageSlotAndBlockHash(add
if err != nil { if err != nil {
return "", nil, nil, err 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 { if err != nil {
return "", nil, nil, err return "", nil, nil, err
} }

View File

@ -249,7 +249,7 @@ type ConvertedPayload struct {
// LogResult represent a log. // LogResult represent a log.
type LogResult struct { type LogResult struct {
LeafCID string `db:"leaf_cid"` LeafCID string `db:"cid"`
ReceiptID string `db:"rct_id"` ReceiptID string `db:"rct_id"`
Address string `db:"address"` Address string `db:"address"`
Index int64 `db:"index"` Index int64 `db:"index"`