Optimize GQL and getBlock APIs #201
@ -713,16 +713,21 @@ func (ecr *CIDRetriever) RetrieveHeaderAndTxCIDsByBlockNumber(blockNumber int64)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// RetrieveHeaderAndTxCIDsByBlockHash retrieves header CID and their associated tx CIDs by block hash
|
// RetrieveHeaderAndTxCIDsByBlockHash retrieves header CID and their associated tx CIDs by block hash
|
||||||
func (ecr *CIDRetriever) RetrieveHeaderAndTxCIDsByBlockHash(blockHash common.Hash) (HeaderCIDRecord, error) {
|
func (ecr *CIDRetriever) RetrieveHeaderAndTxCIDsByBlockHash(blockHash common.Hash, blockNumber *big.Int) (HeaderCIDRecord, 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())
|
||||||
|
|
||||||
var headerCIDs []HeaderCIDRecord
|
var headerCIDs []HeaderCIDRecord
|
||||||
|
|
||||||
|
conditions := map[string]interface{}{"block_hash": blockHash.String()}
|
||||||
|
if blockNumber != nil {
|
||||||
|
conditions["header_cids.block_number"] = blockNumber.Int64()
|
||||||
|
}
|
||||||
|
|
||||||
// https://github.com/go-gorm/gorm/issues/4083#issuecomment-778883283
|
// https://github.com/go-gorm/gorm/issues/4083#issuecomment-778883283
|
||||||
// Will use join for TransactionCIDs once preload for 1:N is supported.
|
// Will use join for TransactionCIDs once preload for 1:N is supported.
|
||||||
err := ecr.gormDB.Preload("TransactionCIDs", func(tx *gorm.DB) *gorm.DB {
|
err := ecr.gormDB.Preload("TransactionCIDs", func(tx *gorm.DB) *gorm.DB {
|
||||||
return tx.Select("cid", "tx_hash", "index", "src", "dst", "header_id", "block_number")
|
return tx.Select("cid", "tx_hash", "index", "src", "dst", "header_id", "block_number")
|
||||||
}).Joins("IPLD").Find(&headerCIDs, "block_hash = ?", blockHash.String()).Error
|
}).Joins("IPLD").Find(&headerCIDs, conditions).Error
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("header cid retrieval error")
|
log.Error("header cid retrieval error")
|
||||||
|
@ -1272,7 +1272,7 @@ func (r *Resolver) AllEthHeaderCids(ctx context.Context, args struct {
|
|||||||
var headerCIDs []eth.HeaderCIDRecord
|
var headerCIDs []eth.HeaderCIDRecord
|
||||||
var err error
|
var err error
|
||||||
if args.Condition.BlockHash != nil {
|
if args.Condition.BlockHash != nil {
|
||||||
headerCID, err := r.backend.Retriever.RetrieveHeaderAndTxCIDsByBlockHash(common.HexToHash(*args.Condition.BlockHash))
|
headerCID, err := r.backend.Retriever.RetrieveHeaderAndTxCIDsByBlockHash(common.HexToHash(*args.Condition.BlockHash), args.Condition.BlockNumber.ToInt())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if !strings.Contains(err.Error(), "not found") {
|
if !strings.Contains(err.Error(), "not found") {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
Loading…
Reference in New Issue
Block a user