Add query ethTransactionCidByTxHash

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

View File

@ -593,6 +593,7 @@ func (ecr *CIDRetriever) RetrieveTxCIDsByHeaderID(tx *sqlx.Tx, headerID string)
return txCIDs, tx.Select(&txCIDs, pgStr, headerID) return txCIDs, tx.Select(&txCIDs, pgStr, headerID)
} }
// RetrieveTxCIDsByBlockNumber retrieves all tx CIDs for the given blockNumber
func (ecr *CIDRetriever) RetrieveTxCIDsByBlockNumber(tx *sqlx.Tx, blockNumber int64) ([]models.TxModel, error) { func (ecr *CIDRetriever) RetrieveTxCIDsByBlockNumber(tx *sqlx.Tx, blockNumber int64) ([]models.TxModel, error) {
log.Debug("retrieving tx cids for block number ", blockNumber) log.Debug("retrieving tx cids for block number ", blockNumber)
pgStr := `SELECT CAST(block_number as Text), header_id, index, tx_hash, cid, mh_key, pgStr := `SELECT CAST(block_number as Text), header_id, index, tx_hash, cid, mh_key,
@ -617,6 +618,7 @@ func (ecr *CIDRetriever) RetrieveReceiptCIDsByTxIDs(tx *sqlx.Tx, txHashes []stri
return rctCIDs, tx.Select(&rctCIDs, pgStr, pq.Array(txHashes)) 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) { func (ecr *CIDRetriever) RetrieveHeaderAndTxCIDsByBlockNumber(blockNumber int64) ([]models.HeaderModel, [][]models.TxModel, error) {
log.Debug("retrieving header cids and tx cids for block number ", blockNumber) log.Debug("retrieving header cids and tx cids for block number ", blockNumber)
@ -677,6 +679,7 @@ func (ecr *CIDRetriever) RetrieveHeaderAndTxCIDsByBlockNumber(blockNumber int64)
return headerCIDs, allTxCIDs, nil 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) { 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()) log.Debug("retrieving header cid and tx cids for block hash ", blockHash.String())
@ -715,3 +718,32 @@ func (ecr *CIDRetriever) RetrieveHeaderAndTxCIDsByBlockHash(blockHash common.Has
return headerCID, txCIDs, nil 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 index int32
src string src string
dst string dst string
ipfsBlock IPFSBlock
} }
func (t EthTransactionCid) Cid(ctx context.Context) string { func (t EthTransactionCid) Cid(ctx context.Context) string {
@ -1153,6 +1154,10 @@ func (t EthTransactionCid) Dst(ctx context.Context) string {
return t.dst return t.dst
} }
func (t EthTransactionCid) BlockByMhKey(ctx context.Context) IPFSBlock {
return t.ipfsBlock
}
type EthTransactionCidsConnection struct { type EthTransactionCidsConnection struct {
nodes []*EthTransactionCid nodes []*EthTransactionCid
} }
@ -1337,3 +1342,36 @@ func (r *Resolver) AllEthHeaderCids(ctx context.Context, args struct {
nodes: resultNodes, nodes: resultNodes,
}, nil }, 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! index: Int!
src: String! src: String!
dst: String! dst: String!
blockByMhKey: IPFSBlock!
} }
type EthTransactionCidsConnection { type EthTransactionCidsConnection {
@ -351,5 +352,8 @@ const schema string = `
# PostGraphile alternative to get headers with transactions using block number or block hash. # PostGraphile alternative to get headers with transactions using block number or block hash.
allEthHeaderCids(condition: EthHeaderCidCondition): EthHeaderCidsConnection allEthHeaderCids(condition: EthHeaderCidCondition): EthHeaderCidsConnection
# PostGraphile alternative to get transactions using transaction hash.
ethTransactionCidByTxHash(txHash: String!): EthTransactionCid
} }
` `