Avoid NULL result from canonical block hash query

This commit is contained in:
Prathamesh Musale 2022-07-07 16:14:43 +05:30
parent 4b40bb8b92
commit 3e2c47e896
3 changed files with 15 additions and 6 deletions

View File

@ -56,6 +56,7 @@ var (
errNegativeBlockNumber = errors.New("negative block number not supported")
errHeaderHashNotFound = errors.New("header for hash not found")
errHeaderNotFound = errors.New("header not found")
errTxHashNotFound = errors.New("transaction for hash not found")
errTxHashInMultipleBlocks = errors.New("transaction for hash found in more than one canonical block")
// errMissingSignature is returned if a block's extra-data section doesn't seem
@ -63,8 +64,10 @@ var (
)
const (
RetrieveCanonicalBlockHashByNumber = `SELECT canonical_header_hash($1)`
RetrieveCanonicalHeaderByNumber = `SELECT cid, data FROM eth.header_cids
RetrieveCanonicalBlockHashByNumber = `SELECT block_hash
FROM canonical_header_hash($1) AS block_hash
WHERE block_hash IS NOT NULL`
RetrieveCanonicalHeaderByNumber = `SELECT cid, data FROM eth.header_cids
INNER JOIN public.blocks ON (
header_cids.mh_key = blocks.key
AND header_cids.block_number = blocks.block_number
@ -526,12 +529,14 @@ func (b *Backend) GetTransaction(ctx context.Context, txHash common.Hash) (*type
Index uint64 `db:"index"`
}
var res = make([]txRes, 0)
if err := b.DB.Get(&res, RetrieveRPCTransaction, txHash.String()); err != nil {
if err := b.DB.Select(&res, RetrieveRPCTransaction, txHash.String()); err != nil {
return nil, common.Hash{}, 0, 0, err
}
// a transaction can be part of a only one canonical block
if len(res) > 1 {
if len(res) == 0 {
return nil, common.Hash{}, 0, 0, errTxHashNotFound
} else if len(res) > 1 {
// a transaction can be part of a only one canonical block
return nil, common.Hash{}, 0, 0, errTxHashInMultipleBlocks
}

View File

@ -737,7 +737,7 @@ func (ecr *CIDRetriever) RetrieveTxCIDByHash(txHash string) (TransactionCIDRecor
var txCID TransactionCIDRecord
err := ecr.gormDB.Joins("IPLD").First(&txCID, "tx_hash = ?", txHash).Error
err := ecr.gormDB.Joins("IPLD").Find(&txCID, "tx_hash = ? AND transaction_cids.header_id = (SELECT canonical_header_hash(transaction_cids.block_number))", txHash).Error
if err != nil {
log.Error("header cid retrieval error")
return txCID, err

View File

@ -301,6 +301,7 @@ var (
MockRctMetaPostPublish = []models.ReceiptModel{
{
BlockNumber: "1",
HeaderID: MockBlock.Hash().String(),
LeafCID: Rct1CID.String(),
LeafMhKey: Rct1MhKey,
Contract: "",
@ -308,6 +309,7 @@ var (
},
{
BlockNumber: "1",
HeaderID: MockBlock.Hash().String(),
LeafCID: Rct2CID.String(),
LeafMhKey: Rct2MhKey,
Contract: "",
@ -315,6 +317,7 @@ var (
},
{
BlockNumber: "1",
HeaderID: MockBlock.Hash().String(),
LeafCID: Rct3CID.String(),
LeafMhKey: Rct3MhKey,
Contract: ContractAddress.String(),
@ -322,6 +325,7 @@ var (
},
{
BlockNumber: "1",
HeaderID: MockBlock.Hash().String(),
LeafCID: Rct4CID.String(),
LeafMhKey: Rct4MhKey,
Contract: "",