mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-26 06:36:43 +00:00
eth_getTdByNumber method (#1552)
* get td by number method * fix ci * fixing lint
This commit is contained in:
parent
4200afdbda
commit
e41a830bc7
5 changed files with 43 additions and 0 deletions
|
|
@ -285,6 +285,13 @@ func (b *EthAPIBackend) GetTd(ctx context.Context, hash common.Hash) *big.Int {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (b *EthAPIBackend) GetTdByNumber(ctx context.Context, blockNr rpc.BlockNumber) *big.Int {
|
||||
if header, err := b.HeaderByNumber(ctx, blockNr); header != nil && err == nil {
|
||||
return b.eth.blockchain.GetTd(header.Hash(), uint64(blockNr.Int64()))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *EthAPIBackend) GetEVM(ctx context.Context, msg *core.Message, state *state.StateDB, header *types.Header, vmConfig *vm.Config, blockCtx *vm.BlockContext) *vm.EVM {
|
||||
if vmConfig == nil {
|
||||
vmConfig = b.eth.blockchain.GetVMConfig()
|
||||
|
|
|
|||
|
|
@ -769,6 +769,32 @@ func (api *BlockChainAPI) GetBlockReceipts(ctx context.Context, blockNrOrHash rp
|
|||
return result, nil
|
||||
}
|
||||
|
||||
// GetTdByHash returns a map containing the total difficulty (hex-encoded) for the given block hash.
|
||||
func (api *BlockChainAPI) GetTdByHash(ctx context.Context, hash common.Hash) map[string]interface{} {
|
||||
td := api.b.GetTd(ctx, hash)
|
||||
if td == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
resp := make(map[string]interface{}, 2)
|
||||
resp["blockHash"] = hash.Hex()
|
||||
resp["totalDifficulty"] = hexutil.EncodeBig(td)
|
||||
return resp
|
||||
}
|
||||
|
||||
// GetTdByNumber returns a map containing the total difficulty (hex-encoded) for the given block number.
|
||||
func (api *BlockChainAPI) GetTdByNumber(ctx context.Context, blockNr rpc.BlockNumber) map[string]interface{} {
|
||||
td := api.b.GetTdByNumber(ctx, blockNr)
|
||||
if td == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
resp := make(map[string]interface{}, 2)
|
||||
resp["blockNumber"] = hexutil.EncodeUint64(uint64(blockNr.Int64()))
|
||||
resp["totalDifficulty"] = hexutil.EncodeBig(td)
|
||||
return resp
|
||||
}
|
||||
|
||||
// OverrideAccount indicates the overriding fields of account during the execution
|
||||
// of a message call.
|
||||
// Note, state and stateDiff can't be specified at the same time. If state is
|
||||
|
|
|
|||
|
|
@ -568,6 +568,10 @@ func (b testBackend) GetTd(ctx context.Context, hash common.Hash) *big.Int {
|
|||
}
|
||||
return big.NewInt(1)
|
||||
}
|
||||
func (b testBackend) GetTdByNumber(ctx context.Context, blockNr rpc.BlockNumber) *big.Int {
|
||||
panic("not implemented")
|
||||
}
|
||||
|
||||
func (b testBackend) GetEVM(ctx context.Context, msg *core.Message, state *state.StateDB, header *types.Header, vmConfig *vm.Config, blockContext *vm.BlockContext) *vm.EVM {
|
||||
if vmConfig == nil {
|
||||
vmConfig = b.chain.GetVMConfig()
|
||||
|
|
|
|||
|
|
@ -70,6 +70,8 @@ type Backend interface {
|
|||
Pending() (*types.Block, types.Receipts, *state.StateDB)
|
||||
GetReceipts(ctx context.Context, hash common.Hash) (types.Receipts, error)
|
||||
GetEVM(ctx context.Context, msg *core.Message, state *state.StateDB, header *types.Header, vmConfig *vm.Config, blockCtx *vm.BlockContext) *vm.EVM
|
||||
GetTd(ctx context.Context, hash common.Hash) *big.Int
|
||||
GetTdByNumber(ctx context.Context, blockNr rpc.BlockNumber) *big.Int
|
||||
SubscribeChainEvent(ch chan<- core.ChainEvent) event.Subscription
|
||||
SubscribeChainHeadEvent(ch chan<- core.ChainHeadEvent) event.Subscription
|
||||
|
||||
|
|
|
|||
|
|
@ -470,3 +470,7 @@ func (b *backendMock) PurgeWhitelistedMilestone() {}
|
|||
func (b backendMock) PeerStats() interface{} {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b backendMock) GetTdByNumber(ctx context.Context, blockNr rpc.BlockNumber) *big.Int {
|
||||
panic("not implemented")
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue