mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-19 10:22:23 +00:00
Add query operator, for a range of blocks
This commit is contained in:
parent
4ecd3de5fc
commit
76e45cf2c1
1 changed files with 37 additions and 1 deletions
|
|
@ -751,6 +751,41 @@ func (q *Query) Block(ctx context.Context, args BlockArgs) (*Block, error) {
|
|||
return block, nil
|
||||
}
|
||||
|
||||
type BlocksArgs struct {
|
||||
From int32
|
||||
To *int32
|
||||
}
|
||||
|
||||
func (q *Query) Blocks(ctx context.Context, args BlocksArgs) ([]*Block, error) {
|
||||
be, err := getBackend(q.node)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
from := rpc.BlockNumber(args.From)
|
||||
|
||||
var to rpc.BlockNumber
|
||||
if args.To != nil {
|
||||
to = rpc.BlockNumber(*args.To)
|
||||
} else {
|
||||
to = rpc.BlockNumber(be.CurrentBlock().Number().Int64())
|
||||
}
|
||||
|
||||
if to < from {
|
||||
return []*Block{}, nil
|
||||
}
|
||||
|
||||
ret := make([]*Block, 0, to-from+1)
|
||||
for i := from; i <= to; i++ {
|
||||
num := i
|
||||
ret = append(ret, &Block{
|
||||
node: q.node,
|
||||
num: &num,
|
||||
})
|
||||
}
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
type AccountArgs struct {
|
||||
Address Address
|
||||
BlockNumber *int32
|
||||
|
|
@ -861,8 +896,9 @@ func NewHandler(n *node.Node) (http.Handler, error) {
|
|||
}
|
||||
|
||||
type Query {
|
||||
block(number: Int, hash: Bytes32): Block
|
||||
account(address: Address!, blockNumber: Int): Account!
|
||||
block(number: Int, hash: Bytes32): Block
|
||||
blocks(from: Int!, to: Int): [Block!]!
|
||||
transaction(hash: Bytes32!): Transaction
|
||||
}
|
||||
`
|
||||
|
|
|
|||
Loading…
Reference in a new issue