getLogs API changes to return txHash, make contract arg optional. (#81)

* getLogs API changes to return txHash, make contract arg optional.

* Populate log index.

* Add test for txn hash in GetLogs request.

* Convert tx string to common.Hash after fetching.

Co-authored-by: Arijit Das <arijitad.in@gmail.com>
This commit was merged in pull request #81.
This commit is contained in:
Ashwin Phatak
2021-07-26 15:43:38 +05:30
committed by GitHub
co-authored by Arijit Das
parent afc63ac960
commit 70f7face75
6 changed files with 43 additions and 16 deletions
+10 -2
View File
@@ -21,8 +21,13 @@ type GetStorageAt struct {
}
type LogResponse struct {
Topics []common.Hash `json:"topics"`
Data hexutil.Bytes `json:"data"`
Topics []common.Hash `json:"topics"`
Data hexutil.Bytes `json:"data"`
Transaction TransactionResp `json:"transaction"`
}
type TransactionResp struct {
Hash common.Hash `json:"hash"`
}
type GetLogs struct {
@@ -44,6 +49,9 @@ func (c *Client) GetLogs(ctx context.Context, hash common.Hash, address common.A
getLogs(blockHash: "%s", contract: "%s") {
data
topics
transaction {
hash
}
}
}
`, hash.String(), address.String())
+10 -3
View File
@@ -1005,15 +1005,16 @@ func (r *Resolver) GetStorageAt(ctx context.Context, args struct {
func (r *Resolver) GetLogs(ctx context.Context, args struct {
BlockHash common.Hash
Contract common.Address
Contract *common.Address
}) (*[]*Log, error) {
ret := make([]*Log, 0, 10)
receiptCIDs, receiptsBytes, err := r.backend.IPLDRetriever.RetrieveReceiptsByBlockHash(args.BlockHash)
receiptCIDs, receiptsBytes, txs, err := r.backend.IPLDRetriever.RetrieveReceiptsByBlockHash(args.BlockHash)
if err != nil {
return nil, err
}
var logIndexInBlock uint = 0
receipts := make(types.Receipts, len(receiptsBytes))
for index, receiptBytes := range receiptsBytes {
receiptCID := receiptCIDs[index]
@@ -1024,12 +1025,18 @@ func (r *Resolver) GetLogs(ctx context.Context, args struct {
receipts[index] = receipt
for _, log := range receipt.Logs {
if log.Address == args.Contract {
log.Index = logIndexInBlock
logIndexInBlock++
if args.Contract == nil || *args.Contract == log.Address {
ret = append(ret, &Log{
backend: r.backend,
log: log,
cid: receiptCID,
ipldBlock: receiptBytes,
transaction: &Transaction{
hash: txs[index],
},
})
}
}
+3 -2
View File
@@ -161,8 +161,9 @@ var _ = Describe("GraphQL", func() {
expectedLogs := []graphql.LogResponse{
{
Topics: test_helpers.MockLog1.Topics,
Data: hexutil.Bytes(test_helpers.MockLog1.Data),
Topics: test_helpers.MockLog1.Topics,
Data: hexutil.Bytes(test_helpers.MockLog1.Data),
Transaction: graphql.TransactionResp{Hash: test_helpers.MockTransactions[0].Hash()},
},
}
Expect(logs).To(Equal(expectedLogs))
+1 -1
View File
@@ -295,6 +295,6 @@ const schema string = `
getStorageAt(blockHash: Bytes32!, contract: Address!, slot: Bytes32!): StorageResult
# Get contract logs by block hash and contract address.
getLogs(blockHash: Bytes32!, contract: Address!): [Log!]
getLogs(blockHash: Bytes32!, contract: Address): [Log!]
}
`