-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy patheth.go
76 lines (69 loc) · 2.13 KB
/
eth.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package main
import (
"context"
"errors"
"mergemock/rpc"
"mergemock/types"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/core"
ethTypes "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/node"
gethRpc "github.com/ethereum/go-ethereum/rpc"
)
type EthBackend struct {
chain *core.BlockChain
}
func NewEthBackend(chain *core.BlockChain) *EthBackend {
return &EthBackend{
chain: chain,
}
}
func (b *EthBackend) Register(srv *rpc.Server) error {
srv.RegisterName("eth", b)
return node.RegisterApis([]rpc.API{
{
Namespace: "eth",
Version: "1.0",
Service: b,
Public: true,
Authenticated: false,
},
}, []string{"eth"}, srv, false)
}
// Based on https://github.com/ethereum/go-ethereum/blob/16701c51697e28986feebd122c6a491e4d9ac0e7/internal/ethapi/api.go#L1200
func (b *EthBackend) rpcMarshalBlock(ctx context.Context, block *ethTypes.Block, inclTx bool, fullTx bool) (map[string]interface{}, error) {
fields, err := types.RPCMarshalBlock(block, inclTx, fullTx, b.chain.Config())
if err != nil {
return nil, err
}
if inclTx {
fields["totalDifficulty"] = (*hexutil.Big)(b.chain.GetTd(block.Hash(), block.NumberU64()))
}
return fields, err
}
func (b *EthBackend) GetBlockByHash(ctx context.Context, hash common.Hash, fullTx bool) (map[string]interface{}, error) {
block := b.chain.GetBlockByHash(hash)
if block == nil {
return nil, errors.New("unknown block")
}
return b.rpcMarshalBlock(ctx, block, true, fullTx)
}
func (b *EthBackend) GetBlockByNumber(ctx context.Context, number gethRpc.BlockNumber, fullTx bool) (map[string]interface{}, error) {
switch number {
case gethRpc.PendingBlockNumber:
return nil, errors.New("not implemented")
case gethRpc.LatestBlockNumber:
block := b.chain.CurrentBlock()
if block == nil {
block = b.chain.Genesis()
}
return b.rpcMarshalBlock(ctx, block, true, fullTx)
default:
block := b.chain.GetBlockByNumber(uint64(number))
if block == nil {
return nil, errors.New("unknown block")
}
return b.rpcMarshalBlock(ctx, block, true, fullTx)
}
}