Add query ethTransactionCidByTxHash

This commit is contained in:
nabarun 2022-05-30 12:01:27 +05:30
parent d7ab0e0863
commit 5e65111d12
3 changed files with 79 additions and 5 deletions

View File

@ -627,6 +627,7 @@ func (ecr *CIDRetriever) RetrieveTxCIDsByHeaderID(tx *sqlx.Tx, headerID string,
return txCIDs, tx.Select(&txCIDs, pgStr, headerID, blockNumber)
}
// RetrieveTxCIDsByBlockNumber retrieves all tx CIDs for the given blockNumber
func (ecr *CIDRetriever) RetrieveTxCIDsByBlockNumber(tx *sqlx.Tx, blockNumber int64) ([]models.TxModel, error) {
log.Debug("retrieving tx cids for block number ", blockNumber)
pgStr := `SELECT CAST(block_number as Text), header_id, index, tx_hash, cid, mh_key,
@ -652,6 +653,7 @@ func (ecr *CIDRetriever) RetrieveReceiptCIDsByTxIDs(tx *sqlx.Tx, txHashes []stri
return rctCIDs, tx.Select(&rctCIDs, pgStr, pq.Array(txHashes))
}
// RetrieveHeaderAndTxCIDsByBlockNumber retrieves header CIDs and their associated tx CIDs by block number
func (ecr *CIDRetriever) RetrieveHeaderAndTxCIDsByBlockNumber(blockNumber int64) ([]models.HeaderModel, [][]models.TxModel, error) {
log.Debug("retrieving header cids and tx cids for block number ", blockNumber)
@ -712,6 +714,7 @@ func (ecr *CIDRetriever) RetrieveHeaderAndTxCIDsByBlockNumber(blockNumber int64)
return headerCIDs, allTxCIDs, nil
}
// RetrieveHeaderAndTxCIDsByBlockHash retrieves header CID and their associated tx CIDs by block hash
func (ecr *CIDRetriever) RetrieveHeaderAndTxCIDsByBlockHash(blockHash common.Hash) (models.HeaderModel, []models.TxModel, error) {
log.Debug("retrieving header cid and tx cids for block hash ", blockHash.String())
@ -751,3 +754,32 @@ func (ecr *CIDRetriever) RetrieveHeaderAndTxCIDsByBlockHash(blockHash common.Has
return headerCID, txCIDs, nil
}
// RetrieveTxCIDByHash returns the tx for the given tx hash
func (ecr *CIDRetriever) RetrieveTxCIDByHash(txHash string) (models.TxModel, error) {
log.Debug("retrieving tx cid for tx hash ", txHash)
// Begin new db tx
tx, err := ecr.db.Beginx()
if err != nil {
return models.TxModel{}, err
}
defer func() {
if p := recover(); p != nil {
shared.Rollback(tx)
panic(p)
} else if err != nil {
shared.Rollback(tx)
} else {
err = tx.Commit()
}
}()
pgStr := `SELECT CAST(block_number as Text), header_id, index, tx_hash, cid, mh_key,
dst, src, tx_data, tx_type, value
FROM eth.transaction_cids
WHERE tx_hash = $1
ORDER BY index`
var txCID models.TxModel
return txCID, tx.Get(&txCID, pgStr, txHash)
}

View File

@ -1131,6 +1131,7 @@ type EthTransactionCid struct {
index int32
src string
dst string
ipfsBlock IPFSBlock
}
func (t EthTransactionCid) Cid(ctx context.Context) string {
@ -1153,6 +1154,10 @@ func (t EthTransactionCid) Dst(ctx context.Context) string {
return t.dst
}
func (t EthTransactionCid) BlockByMhKey(ctx context.Context) IPFSBlock {
return t.ipfsBlock
}
type EthTransactionCidsConnection struct {
nodes []*EthTransactionCid
}
@ -1337,3 +1342,36 @@ func (r *Resolver) AllEthHeaderCids(ctx context.Context, args struct {
nodes: resultNodes,
}, nil
}
func (r *Resolver) EthTransactionCidByTxHash(ctx context.Context, args struct {
TxHash string
}) (*EthTransactionCid, error) {
txCID, err := r.backend.Retriever.RetrieveTxCIDByHash(args.TxHash)
if err != nil {
return nil, err
}
// Begin tx
tx, err := r.backend.DB.Beginx()
if err != nil {
return nil, err
}
txIPLDs, err := r.backend.Fetcher.FetchTrxs(tx, []models.TxModel{txCID})
if err != nil {
return nil, err
}
return &EthTransactionCid{
cid: txCID.CID,
txHash: txCID.TxHash,
index: int32(txCID.Index),
src: txCID.Src,
dst: txCID.Dst,
ipfsBlock: IPFSBlock{
key: txIPLDs[0].Key,
data: hexutil.Bytes(txIPLDs[0].Data).String(),
},
}, nil
}

View File

@ -294,6 +294,7 @@ const schema string = `
index: Int!
src: String!
dst: String!
blockByMhKey: IPFSBlock!
}
type EthTransactionCidsConnection {
@ -351,5 +352,8 @@ const schema string = `
# PostGraphile alternative to get headers with transactions using block number or block hash.
allEthHeaderCids(condition: EthHeaderCidCondition): EthHeaderCidsConnection
# PostGraphile alternative to get transactions using transaction hash.
ethTransactionCidByTxHash(txHash: String!): EthTransactionCid
}
`