Review changes

This commit is contained in:
nikugogoi 2022-06-06 15:36:29 +05:30
parent c19cc5c44d
commit 31e9a7dc5e
4 changed files with 64 additions and 58 deletions

View File

@ -45,16 +45,16 @@ type CIDRetriever struct {
gormDB *gorm.DB gormDB *gorm.DB
} }
type IPLD struct { type IPLDModelRecord struct {
models.IPLDModel models.IPLDModel
} }
// TableName overrides the table name used by IPLD // TableName overrides the table name used by IPLD
func (IPLD) TableName() string { func (IPLDModelRecord) TableName() string {
return "public.blocks" return "public.blocks"
} }
type HeaderCid struct { type HeaderCidRecord struct {
CID string `gorm:"column:cid"` CID string `gorm:"column:cid"`
BlockHash string `gorm:"primaryKey"` BlockHash string `gorm:"primaryKey"`
BlockNumber string BlockNumber string
@ -67,16 +67,19 @@ type HeaderCid struct {
UncleRoot string UncleRoot string
Bloom []byte Bloom []byte
MhKey string MhKey string
TransactionCids []TransactionCid `gorm:"foreignKey:HeaderID;references:BlockHash"`
IPLD IPLD `gorm:"foreignKey:MhKey;references:Key"` // gorm doesn't check if foreign key exists in database.
// It is required to eager load relations using preload.
TransactionCids []TransactionCidRecord `gorm:"foreignKey:HeaderID;references:BlockHash"`
IPLD IPLDModelRecord `gorm:"foreignKey:MhKey;references:Key"`
} }
// TableName overrides the table name used by HeaderCid // TableName overrides the table name used by HeaderCid
func (HeaderCid) TableName() string { func (HeaderCidRecord) TableName() string {
return "eth.header_cids" return "eth.header_cids"
} }
type TransactionCid struct { type TransactionCidRecord struct {
CID string `gorm:"column:cid"` CID string `gorm:"column:cid"`
TxHash string `gorm:"primaryKey"` TxHash string `gorm:"primaryKey"`
HeaderID string `gorm:"column:header_id"` HeaderID string `gorm:"column:header_id"`
@ -84,11 +87,11 @@ type TransactionCid struct {
Src string Src string
Dst string Dst string
MhKey string MhKey string
IPLD IPLD `gorm:"foreignKey:MhKey;references:Key"` IPLD IPLDModelRecord `gorm:"foreignKey:MhKey;references:Key"`
} }
// TableName overrides the table name used by TransactionCid // TableName overrides the table name used by TransactionCid
func (TransactionCid) TableName() string { func (TransactionCidRecord) TableName() string {
return "eth.transaction_cids" return "eth.transaction_cids"
} }
@ -97,6 +100,7 @@ func NewCIDRetriever(db *sqlx.DB) *CIDRetriever {
gormDB, err := gorm.Open(postgres.New(postgres.Config{ gormDB, err := gorm.Open(postgres.New(postgres.Config{
Conn: db, Conn: db,
}), &gorm.Config{}) }), &gorm.Config{})
if err != nil { if err != nil {
log.Error(err) log.Error(err)
return nil return nil
@ -227,7 +231,7 @@ func (ecr *CIDRetriever) RetrieveHeaderCIDs(tx *sqlx.Tx, blockNumber int64) ([]m
log.Debug("retrieving header cids for block ", blockNumber) log.Debug("retrieving header cids for block ", blockNumber)
headers := make([]models.HeaderModel, 0) headers := make([]models.HeaderModel, 0)
pgStr := `SELECT CAST(block_number as Text), block_hash,parent_hash,cid,mh_key,CAST(td as Text),node_id, pgStr := `SELECT CAST(block_number as Text), block_hash,parent_hash,cid,mh_key,CAST(td as Text),node_id,
CAST(reward as Text), state_root,uncle_root,tx_root,receipt_root,bloom,timestamp,times_validated, CAST(reward as Text), state_root, uncle_root, tx_root, receipt_root, bloom, timestamp, times_validated,
coinbase FROM eth.header_cids coinbase FROM eth.header_cids
WHERE block_number = $1` WHERE block_number = $1`
return headers, tx.Select(&headers, pgStr, blockNumber) return headers, tx.Select(&headers, pgStr, blockNumber)
@ -636,7 +640,7 @@ func (ecr *CIDRetriever) RetrieveBlockByNumber(blockNumber int64) (models.Header
func (ecr *CIDRetriever) RetrieveHeaderCIDByHash(tx *sqlx.Tx, blockHash common.Hash) (models.HeaderModel, error) { func (ecr *CIDRetriever) RetrieveHeaderCIDByHash(tx *sqlx.Tx, blockHash common.Hash) (models.HeaderModel, error) {
log.Debug("retrieving header cids for block hash ", blockHash.String()) log.Debug("retrieving header cids 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 block_hash, CAST(block_number as Text), parent_hash, cid, mh_key, CAST(td as Text),
state_root,uncle_root,tx_root,receipt_root,bloom,timestamp FROM eth.header_cids state_root, uncle_root, tx_root, receipt_root, bloom, timestamp FROM eth.header_cids
WHERE block_hash = $1` WHERE block_hash = $1`
var headerCID models.HeaderModel var headerCID models.HeaderModel
return headerCID, tx.Get(&headerCID, pgStr, blockHash.String()) return headerCID, tx.Get(&headerCID, pgStr, blockHash.String())
@ -666,16 +670,17 @@ func (ecr *CIDRetriever) RetrieveReceiptCIDsByTxIDs(tx *sqlx.Tx, txHashes []stri
} }
// RetrieveHeaderAndTxCIDsByBlockNumber retrieves header CIDs and their associated tx CIDs by block number // RetrieveHeaderAndTxCIDsByBlockNumber retrieves header CIDs and their associated tx CIDs by block number
func (ecr *CIDRetriever) RetrieveHeaderAndTxCIDsByBlockNumber(blockNumber int64) ([]HeaderCid, error) { func (ecr *CIDRetriever) RetrieveHeaderAndTxCIDsByBlockNumber(blockNumber int64) ([]HeaderCidRecord, 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)
var headerCIDs []HeaderCid var headerCIDs []HeaderCidRecord
// 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") return tx.Select("cid", "tx_hash", "index", "src", "dst", "header_id")
}).Joins("IPLD").Find(&headerCIDs, "block_number = ?", blockNumber).Error }).Joins("IPLD").Find(&headerCIDs, "block_number = ?", blockNumber).Error
if err != nil { if err != nil {
log.Error("header cid retrieval error") log.Error("header cid retrieval error")
return nil, err return nil, err
@ -685,16 +690,17 @@ 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) (HeaderCid, error) { func (ecr *CIDRetriever) RetrieveHeaderAndTxCIDsByBlockHash(blockHash common.Hash) (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 headerCID HeaderCid var headerCID HeaderCidRecord
// 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") return tx.Select("cid", "tx_hash", "index", "src", "dst", "header_id")
}).Joins("IPLD").First(&headerCID, "block_hash = ?", blockHash.String()).Error }).Joins("IPLD").First(&headerCID, "block_hash = ?", blockHash.String()).Error
if err != nil { if err != nil {
log.Error("header cid retrieval error") log.Error("header cid retrieval error")
return headerCID, err return headerCID, err
@ -704,10 +710,10 @@ func (ecr *CIDRetriever) RetrieveHeaderAndTxCIDsByBlockHash(blockHash common.Has
} }
// RetrieveTxCIDByHash returns the tx for the given tx hash // RetrieveTxCIDByHash returns the tx for the given tx hash
func (ecr *CIDRetriever) RetrieveTxCIDByHash(txHash string) (TransactionCid, error) { func (ecr *CIDRetriever) RetrieveTxCIDByHash(txHash string) (TransactionCidRecord, error) {
log.Debug("retrieving tx cid for tx hash ", txHash) log.Debug("retrieving tx cid for tx hash ", txHash)
var txCID TransactionCid var txCID TransactionCidRecord
err := ecr.gormDB.Joins("IPLD").First(&txCID, "tx_hash = ?", txHash).Error err := ecr.gormDB.Joins("IPLD").First(&txCID, "tx_hash = ?", txHash).Error
if err != nil { if err != nil {

View File

@ -23,12 +23,12 @@ type GetStorageAt struct {
type LogResponse struct { type LogResponse struct {
Topics []common.Hash `json:"topics"` Topics []common.Hash `json:"topics"`
Data hexutil.Bytes `json:"data"` Data hexutil.Bytes `json:"data"`
Transaction TransactionResp `json:"transaction"` Transaction TransactionResponse `json:"transaction"`
ReceiptCID string `json:"receiptCID"` ReceiptCID string `json:"receiptCID"`
Status int32 `json:"status"` Status int32 `json:"status"`
} }
type TransactionResp struct { type TransactionResponse struct {
Hash common.Hash `json:"hash"` Hash common.Hash `json:"hash"`
} }
@ -36,29 +36,29 @@ type GetLogs struct {
Responses []LogResponse `json:"getLogs"` Responses []LogResponse `json:"getLogs"`
} }
type IPFSBlockResp struct { type IPFSBlockResponse struct {
Key string `json:"key"` Key string `json:"key"`
Data string `json:"data"` Data string `json:"data"`
} }
type EthTransactionCidResp struct { type EthTransactionCidResponse struct {
Cid string `json:"cid"` Cid string `json:"cid"`
TxHash string `json:"txHash"` TxHash string `json:"txHash"`
Index int32 `json:"index"` Index int32 `json:"index"`
Src string `json:"src"` Src string `json:"src"`
Dst string `json:"dst"` Dst string `json:"dst"`
BlockByMhKey IPFSBlockResp `json:"blockByMhKey"` BlockByMhKey IPFSBlockResponse `json:"blockByMhKey"`
} }
type EthTransactionCidByTxHash struct { type EthTransactionCidByTxHash struct {
Response EthTransactionCidResp `json:"ethTransactionCidByTxHash"` Response EthTransactionCidResponse `json:"ethTransactionCidByTxHash"`
} }
type EthTransactionCidsByHeaderIdResp struct { type EthTransactionCidsByHeaderIdResponse struct {
Nodes []EthTransactionCidResp `json:"nodes"` Nodes []EthTransactionCidResponse `json:"nodes"`
} }
type EthHeaderCidResp struct { type EthHeaderCidResponse struct {
Cid string `json:"cid"` Cid string `json:"cid"`
BlockNumber BigInt `json:"blockNumber"` BlockNumber BigInt `json:"blockNumber"`
BlockHash string `json:"blockHash"` BlockHash string `json:"blockHash"`
@ -70,16 +70,16 @@ type EthHeaderCidResp struct {
ReceiptRoot string `json:"receiptRoot"` ReceiptRoot string `json:"receiptRoot"`
UncleRoot string `json:"uncleRoot"` UncleRoot string `json:"uncleRoot"`
Bloom string `json:"bloom"` Bloom string `json:"bloom"`
EthTransactionCidsByHeaderId EthTransactionCidsByHeaderIdResp `json:"ethTransactionCidsByHeaderId"` EthTransactionCidsByHeaderId EthTransactionCidsByHeaderIdResponse `json:"ethTransactionCidsByHeaderId"`
BlockByMhKey IPFSBlockResp `json:"blockByMhKey"` BlockByMhKey IPFSBlockResponse `json:"blockByMhKey"`
} }
type AllEthHeaderCidsResp struct { type AllEthHeaderCidsResponse struct {
Nodes []EthHeaderCidResp `json:"nodes"` Nodes []EthHeaderCidResponse `json:"nodes"`
} }
type AllEthHeaderCids struct { type AllEthHeaderCids struct {
Response AllEthHeaderCidsResp `json:"allEthHeaderCids"` Response AllEthHeaderCidsResponse `json:"allEthHeaderCids"`
} }
type Client struct { type Client struct {
@ -164,7 +164,7 @@ func (c *Client) GetStorageAt(ctx context.Context, hash common.Hash, address com
return &storageAt.Response, nil return &storageAt.Response, nil
} }
func (c *Client) AllEthHeaderCids(ctx context.Context, condition EthHeaderCidCondition) (*AllEthHeaderCidsResp, error) { func (c *Client) AllEthHeaderCids(ctx context.Context, condition EthHeaderCidCondition) (*AllEthHeaderCidsResponse, error) {
var params string var params string
if condition.BlockHash != nil { if condition.BlockHash != nil {
params = fmt.Sprintf(`blockHash: "%s"`, *condition.BlockHash) params = fmt.Sprintf(`blockHash: "%s"`, *condition.BlockHash)
@ -228,7 +228,7 @@ func (c *Client) AllEthHeaderCids(ctx context.Context, condition EthHeaderCidCon
return &allEthHeaderCids.Response, nil return &allEthHeaderCids.Response, nil
} }
func (c *Client) EthTransactionCidByTxHash(ctx context.Context, txHash string) (*EthTransactionCidResp, error) { func (c *Client) EthTransactionCidByTxHash(ctx context.Context, txHash string) (*EthTransactionCidResponse, error) {
getTxQuery := fmt.Sprintf(` getTxQuery := fmt.Sprintf(`
query{ query{
ethTransactionCidByTxHash(txHash: "%s") { ethTransactionCidByTxHash(txHash: "%s") {

View File

@ -1263,7 +1263,7 @@ type EthHeaderCidCondition struct {
func (r *Resolver) AllEthHeaderCids(ctx context.Context, args struct { func (r *Resolver) AllEthHeaderCids(ctx context.Context, args struct {
Condition *EthHeaderCidCondition Condition *EthHeaderCidCondition
}) (*EthHeaderCidsConnection, error) { }) (*EthHeaderCidsConnection, error) {
var headerCIDs []eth.HeaderCid 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))

View File

@ -181,7 +181,7 @@ var _ = Describe("GraphQL", func() {
{ {
Topics: test_helpers.MockLog1.Topics, Topics: test_helpers.MockLog1.Topics,
Data: hexutil.Bytes(test_helpers.MockLog1.Data), Data: hexutil.Bytes(test_helpers.MockLog1.Data),
Transaction: graphql.TransactionResp{Hash: test_helpers.MockTransactions[0].Hash()}, Transaction: graphql.TransactionResponse{Hash: test_helpers.MockTransactions[0].Hash()},
ReceiptCID: test_helpers.Rct1CID.String(), ReceiptCID: test_helpers.Rct1CID.String(),
Status: int32(test_helpers.MockReceipts[0].Status), Status: int32(test_helpers.MockReceipts[0].Status),
}, },
@ -198,7 +198,7 @@ var _ = Describe("GraphQL", func() {
{ {
Topics: test_helpers.MockLog6.Topics, Topics: test_helpers.MockLog6.Topics,
Data: hexutil.Bytes(test_helpers.MockLog6.Data), Data: hexutil.Bytes(test_helpers.MockLog6.Data),
Transaction: graphql.TransactionResp{Hash: test_helpers.MockTransactions[3].Hash()}, Transaction: graphql.TransactionResponse{Hash: test_helpers.MockTransactions[3].Hash()},
ReceiptCID: test_helpers.Rct4CID.String(), ReceiptCID: test_helpers.Rct4CID.String(),
Status: int32(test_helpers.MockReceipts[3].Status), Status: int32(test_helpers.MockReceipts[3].Status),
}, },
@ -297,7 +297,7 @@ var _ = Describe("GraphQL", func() {
}) })
}) })
func compareEthHeaderCid(ethHeaderCid graphql.EthHeaderCidResp, headerCID eth.HeaderCid) { func compareEthHeaderCid(ethHeaderCid graphql.EthHeaderCidResponse, headerCID eth.HeaderCidRecord) {
blockNumber, err := strconv.ParseInt(headerCID.BlockNumber, 10, 64) blockNumber, err := strconv.ParseInt(headerCID.BlockNumber, 10, 64)
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
@ -325,7 +325,7 @@ func compareEthHeaderCid(ethHeaderCid graphql.EthHeaderCidResp, headerCID eth.He
Expect(ethHeaderCid.BlockByMhKey.Key).To(Equal(headerCID.IPLD.Key)) Expect(ethHeaderCid.BlockByMhKey.Key).To(Equal(headerCID.IPLD.Key))
} }
func compareEthTxCid(ethTxCid graphql.EthTransactionCidResp, txCID eth.TransactionCid) { func compareEthTxCid(ethTxCid graphql.EthTransactionCidResponse, txCID eth.TransactionCidRecord) {
Expect(ethTxCid.Cid).To(Equal(txCID.CID)) Expect(ethTxCid.Cid).To(Equal(txCID.CID))
Expect(ethTxCid.TxHash).To(Equal(txCID.TxHash)) Expect(ethTxCid.TxHash).To(Equal(txCID.TxHash))
Expect(ethTxCid.Index).To(Equal(int32(txCID.Index))) Expect(ethTxCid.Index).To(Equal(int32(txCID.Index)))