Merge changes to implement postgraphile GraphQL queries #166

Merged
nikugogoi merged 29 commits from ng-watcher-queries-v4 into sharding 2022-06-08 06:34:08 +00:00
4 changed files with 64 additions and 58 deletions
Showing only changes of commit 31e9a7dc5e - Show all commits

View File

@ -45,16 +45,16 @@ type CIDRetriever struct {
gormDB *gorm.DB
}
type IPLD struct {
type IPLDModelRecord struct {
models.IPLDModel
}
// TableName overrides the table name used by IPLD
func (IPLD) TableName() string {
func (IPLDModelRecord) TableName() string {
return "public.blocks"
}
type HeaderCid struct {
type HeaderCidRecord struct {
CID string `gorm:"column:cid"`
BlockHash string `gorm:"primaryKey"`
BlockNumber string
@ -67,16 +67,19 @@ type HeaderCid struct {
UncleRoot string
Bloom []byte
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
func (HeaderCid) TableName() string {
func (HeaderCidRecord) TableName() string {
return "eth.header_cids"
}
type TransactionCid struct {
type TransactionCidRecord struct {
CID string `gorm:"column:cid"`
TxHash string `gorm:"primaryKey"`
HeaderID string `gorm:"column:header_id"`
@ -84,11 +87,11 @@ type TransactionCid struct {
Src string
Dst 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
func (TransactionCid) TableName() string {
func (TransactionCidRecord) TableName() string {
return "eth.transaction_cids"
}
@ -97,6 +100,7 @@ func NewCIDRetriever(db *sqlx.DB) *CIDRetriever {
gormDB, err := gorm.Open(postgres.New(postgres.Config{
Conn: db,
}), &gorm.Config{})
if err != nil {
log.Error(err)
return nil
@ -227,7 +231,7 @@ func (ecr *CIDRetriever) RetrieveHeaderCIDs(tx *sqlx.Tx, blockNumber int64) ([]m
log.Debug("retrieving header cids for block ", blockNumber)
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,
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
WHERE block_number = $1`
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) {
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),
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`
var headerCID models.HeaderModel
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
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)
var headerCIDs []HeaderCid
var headerCIDs []HeaderCidRecord
// https://github.com/go-gorm/gorm/issues/4083#issuecomment-778883283
// Will use join for TransactionCids once preload for 1:N is supported.
err := ecr.gormDB.Preload("TransactionCids", func(tx *gorm.DB) *gorm.DB {
return tx.Select("cid", "tx_hash", "index", "src", "dst", "header_id")
}).Joins("IPLD").Find(&headerCIDs, "block_number = ?", blockNumber).Error
if err != nil {
log.Error("header cid retrieval error")
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
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())
var headerCID HeaderCid
var headerCID HeaderCidRecord
// https://github.com/go-gorm/gorm/issues/4083#issuecomment-778883283
// Will use join for TransactionCids once preload for 1:N is supported.
err := ecr.gormDB.Preload("TransactionCids", func(tx *gorm.DB) *gorm.DB {
return tx.Select("cid", "tx_hash", "index", "src", "dst", "header_id")
}).Joins("IPLD").First(&headerCID, "block_hash = ?", blockHash.String()).Error
if err != nil {
log.Error("header cid retrieval error")
return headerCID, err
@ -704,10 +710,10 @@ func (ecr *CIDRetriever) RetrieveHeaderAndTxCIDsByBlockHash(blockHash common.Has
}
// 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)
var txCID TransactionCid
var txCID TransactionCidRecord
err := ecr.gormDB.Joins("IPLD").First(&txCID, "tx_hash = ?", txHash).Error
if err != nil {

View File

@ -21,14 +21,14 @@ type GetStorageAt struct {
}
type LogResponse struct {
Topics []common.Hash `json:"topics"`
Data hexutil.Bytes `json:"data"`
Transaction TransactionResp `json:"transaction"`
ReceiptCID string `json:"receiptCID"`
Status int32 `json:"status"`
Topics []common.Hash `json:"topics"`
Data hexutil.Bytes `json:"data"`
Transaction TransactionResponse `json:"transaction"`
ReceiptCID string `json:"receiptCID"`
Status int32 `json:"status"`
}
type TransactionResp struct {
type TransactionResponse struct {
Hash common.Hash `json:"hash"`
}
@ -36,50 +36,50 @@ type GetLogs struct {
Responses []LogResponse `json:"getLogs"`
}
type IPFSBlockResp struct {
type IPFSBlockResponse struct {
Key string `json:"key"`
Data string `json:"data"`
}
type EthTransactionCidResp struct {
Cid string `json:"cid"`
TxHash string `json:"txHash"`
Index int32 `json:"index"`
Src string `json:"src"`
Dst string `json:"dst"`
BlockByMhKey IPFSBlockResp `json:"blockByMhKey"`
type EthTransactionCidResponse struct {
Cid string `json:"cid"`
TxHash string `json:"txHash"`
Index int32 `json:"index"`
Src string `json:"src"`
Dst string `json:"dst"`
BlockByMhKey IPFSBlockResponse `json:"blockByMhKey"`
}
type EthTransactionCidByTxHash struct {
Response EthTransactionCidResp `json:"ethTransactionCidByTxHash"`
Response EthTransactionCidResponse `json:"ethTransactionCidByTxHash"`
}
type EthTransactionCidsByHeaderIdResp struct {
Nodes []EthTransactionCidResp `json:"nodes"`
type EthTransactionCidsByHeaderIdResponse struct {
Nodes []EthTransactionCidResponse `json:"nodes"`
}
type EthHeaderCidResp struct {
Cid string `json:"cid"`
BlockNumber BigInt `json:"blockNumber"`
BlockHash string `json:"blockHash"`
ParentHash string `json:"parentHash"`
Timestamp BigInt `json:"timestamp"`
StateRoot string `json:"stateRoot"`
Td BigInt `json:"td"`
TxRoot string `json:"txRoot"`
ReceiptRoot string `json:"receiptRoot"`
UncleRoot string `json:"uncleRoot"`
Bloom string `json:"bloom"`
EthTransactionCidsByHeaderId EthTransactionCidsByHeaderIdResp `json:"ethTransactionCidsByHeaderId"`
BlockByMhKey IPFSBlockResp `json:"blockByMhKey"`
type EthHeaderCidResponse struct {
Cid string `json:"cid"`
BlockNumber BigInt `json:"blockNumber"`
BlockHash string `json:"blockHash"`
ParentHash string `json:"parentHash"`
Timestamp BigInt `json:"timestamp"`
StateRoot string `json:"stateRoot"`
Td BigInt `json:"td"`
TxRoot string `json:"txRoot"`
ReceiptRoot string `json:"receiptRoot"`
UncleRoot string `json:"uncleRoot"`
Bloom string `json:"bloom"`
EthTransactionCidsByHeaderId EthTransactionCidsByHeaderIdResponse `json:"ethTransactionCidsByHeaderId"`
BlockByMhKey IPFSBlockResponse `json:"blockByMhKey"`
}
type AllEthHeaderCidsResp struct {
Nodes []EthHeaderCidResp `json:"nodes"`
type AllEthHeaderCidsResponse struct {
Nodes []EthHeaderCidResponse `json:"nodes"`
}
type AllEthHeaderCids struct {
Response AllEthHeaderCidsResp `json:"allEthHeaderCids"`
Response AllEthHeaderCidsResponse `json:"allEthHeaderCids"`
}
type Client struct {
@ -164,7 +164,7 @@ func (c *Client) GetStorageAt(ctx context.Context, hash common.Hash, address com
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
if condition.BlockHash != nil {
params = fmt.Sprintf(`blockHash: "%s"`, *condition.BlockHash)
@ -228,7 +228,7 @@ func (c *Client) AllEthHeaderCids(ctx context.Context, condition EthHeaderCidCon
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(`
query{
ethTransactionCidByTxHash(txHash: "%s") {

View File

@ -1263,7 +1263,7 @@ type EthHeaderCidCondition struct {
func (r *Resolver) AllEthHeaderCids(ctx context.Context, args struct {
Condition *EthHeaderCidCondition
}) (*EthHeaderCidsConnection, error) {
var headerCIDs []eth.HeaderCid
var headerCIDs []eth.HeaderCidRecord
var err error
if args.Condition.BlockHash != nil {
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,
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(),
Status: int32(test_helpers.MockReceipts[0].Status),
},
@ -198,7 +198,7 @@ var _ = Describe("GraphQL", func() {
{
Topics: test_helpers.MockLog6.Topics,
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(),
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)
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))
}
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.TxHash).To(Equal(txCID.TxHash))
Expect(ethTxCid.Index).To(Equal(int32(txCID.Index)))