-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Initial GraphQL interface implementation #6821
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
3730e5b
Initial GraphQL interface implementation
SLoeuillet 4aae90b
Initial GraphQL interface implementation
SLoeuillet 7345012
graphql: change some field types, make it possible to input block num…
SLoeuillet e13a68e
graphql: makes it behave more like Geth, and note remaining discrepan…
SLoeuillet b2c5f7d
graphql: implement chainID resolver
SLoeuillet 00208e9
graphql: update sample queries
SLoeuillet 5ffa991
graphql: fix linter
SLoeuillet 5cae304
merge devel
SLoeuillet 388de7e
Enable graphql in CI
SLoeuillet 53a1ad7
Rename test file
SLoeuillet 9d1004c
Update go.sum
SLoeuillet 42f1e64
Merge branch 'devel' into graphql
AskAlexSharov 4985ecc
Merge branch 'devel' into graphql
AskAlexSharov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| package commands | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "math/big" | ||
|
|
||
| "github.com/ledgerwatch/erigon-lib/common" | ||
| "github.com/ledgerwatch/erigon-lib/kv" | ||
| "github.com/ledgerwatch/erigon/common/hexutil" | ||
| "github.com/ledgerwatch/erigon/core/rawdb" | ||
| "github.com/ledgerwatch/erigon/core/types" | ||
| "github.com/ledgerwatch/erigon/rpc" | ||
| "github.com/ledgerwatch/erigon/turbo/adapter/ethapi" | ||
| "github.com/ledgerwatch/erigon/turbo/rpchelper" | ||
| ) | ||
|
|
||
| type GraphQLAPI interface { | ||
| GetBlockDetails(ctx context.Context, number rpc.BlockNumber) (map[string]interface{}, error) | ||
| GetChainID(ctx context.Context) (*big.Int, error) | ||
| } | ||
|
|
||
| type GraphQLAPIImpl struct { | ||
| *BaseAPI | ||
| db kv.RoDB | ||
| } | ||
|
|
||
| func NewGraphQLAPI(base *BaseAPI, db kv.RoDB) *GraphQLAPIImpl { | ||
| return &GraphQLAPIImpl{ | ||
| BaseAPI: base, | ||
| db: db, | ||
| } | ||
| } | ||
|
|
||
| func (api *GraphQLAPIImpl) GetChainID(ctx context.Context) (*big.Int, error) { | ||
| tx, err := api.db.BeginRo(ctx) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| defer tx.Rollback() | ||
|
|
||
| response, err := api.chainConfig(tx) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| return response.ChainID, nil | ||
| } | ||
|
|
||
| func (api *GraphQLAPIImpl) GetBlockDetails(ctx context.Context, blockNumber rpc.BlockNumber) (map[string]interface{}, error) { | ||
| tx, err := api.db.BeginRo(ctx) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| defer tx.Rollback() | ||
|
|
||
| block, senders, err := api.getBlockWithSenders(ctx, blockNumber, tx) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| if block == nil { | ||
| return nil, nil | ||
| } | ||
|
|
||
| getBlockRes, err := api.delegateGetBlockByNumber(tx, block, blockNumber, false) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| chainConfig, err := api.chainConfig(tx) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| receipts, err := api.getReceipts(ctx, tx, chainConfig, block, senders) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("getReceipts error: %w", err) | ||
| } | ||
| result := make([]map[string]interface{}, 0, len(receipts)) | ||
| for _, receipt := range receipts { | ||
| txn := block.Transactions()[receipt.TransactionIndex] | ||
| result = append(result, marshalReceipt(receipt, txn, chainConfig, block.HeaderNoCopy(), txn.Hash(), true)) | ||
| } | ||
|
|
||
| response := map[string]interface{}{} | ||
| response["block"] = getBlockRes | ||
| response["receipts"] = result | ||
|
|
||
| return response, nil | ||
| } | ||
|
|
||
| func (api *GraphQLAPIImpl) getBlockWithSenders(ctx context.Context, number rpc.BlockNumber, tx kv.Tx) (*types.Block, []common.Address, error) { | ||
| if number == rpc.PendingBlockNumber { | ||
| return api.pendingBlock(), nil, nil | ||
| } | ||
|
|
||
| blockHeight, blockHash, _, err := rpchelper.GetBlockNumber(rpc.BlockNumberOrHashWithNumber(number), tx, api.filters) | ||
| if err != nil { | ||
| return nil, nil, err | ||
| } | ||
|
|
||
| block, senders, err := api._blockReader.BlockWithSenders(ctx, tx, blockHash, blockHeight) | ||
| return block, senders, err | ||
| } | ||
|
|
||
| func (api *GraphQLAPIImpl) delegateGetBlockByNumber(tx kv.Tx, b *types.Block, number rpc.BlockNumber, inclTx bool) (map[string]interface{}, error) { | ||
| td, err := rawdb.ReadTd(tx, b.Hash(), b.NumberU64()) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| additionalFields := make(map[string]interface{}) | ||
| response, err := ethapi.RPCMarshalBlock(b, inclTx, inclTx, additionalFields) | ||
| if !inclTx { | ||
| delete(response, "transactions") // workaround for https://github.com/ledgerwatch/erigon/issues/4989#issuecomment-1218415666 | ||
| } | ||
| response["totalDifficulty"] = (*hexutil.Big)(td) | ||
| response["transactionCount"] = b.Transactions().Len() | ||
|
|
||
| if err == nil && number == rpc.PendingBlockNumber { | ||
| // Pending blocks need to nil out a few fields | ||
| for _, field := range []string{"hash", "nonce", "miner"} { | ||
| response[field] = nil | ||
| } | ||
| } | ||
|
|
||
| return response, err | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.