forked from ten-protocol/go-ten
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient_api_eth.go
230 lines (202 loc) · 8.54 KB
/
client_api_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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package clientapi
import (
"context"
"errors"
"fmt"
"math/big"
"github.com/obscuronet/go-obscuro/go/common/host"
gethcommon "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/rpc"
"github.com/obscuronet/go-obscuro/go/common"
)
// EthereumAPI implements a subset of the Ethereum JSON RPC operations. All the method signatures are copied from the
// corresponding Geth implementations.
type EthereumAPI struct {
host host.Host
}
func NewEthereumAPI(host host.Host) *EthereumAPI {
return &EthereumAPI{
host: host,
}
}
// ChainId returns the Obscuro chain ID.
func (api *EthereumAPI) ChainId() (*hexutil.Big, error) { //nolint:stylecheck,revive
return (*hexutil.Big)(big.NewInt(api.host.Config().ObscuroChainID)), nil
}
// BlockNumber returns the height of the current head rollup.
func (api *EthereumAPI) BlockNumber() hexutil.Uint64 {
head := api.host.DB().GetCurrentRollupHead()
if head == nil {
return 0
}
number := head.Header.Number.Uint64()
return hexutil.Uint64(number)
}
// GetBalance returns the address's balance on the Obscuro network, encrypted with the viewing key corresponding to the
// `address` field and encoded as hex.
func (api *EthereumAPI) GetBalance(_ context.Context, encryptedParams common.EncryptedParamsGetBalance) (string, error) {
encryptedBalance, err := api.host.EnclaveClient().GetBalance(encryptedParams)
if err != nil {
return "", err
}
return gethcommon.Bytes2Hex(encryptedBalance), nil
}
// GetBlockByNumber returns the rollup with the given height as a block. No transactions are included.
func (api *EthereumAPI) GetBlockByNumber(ctx context.Context, number rpc.BlockNumber, _ bool) (map[string]interface{}, error) {
rollupHash, err := api.blockNumberToHash(number)
if err != nil {
return nil, fmt.Errorf("unable to fetch block number: %w", err)
}
return api.GetBlockByHash(ctx, *rollupHash, true)
}
// GetBlockByHash returns the rollup with the given hash as a block. No transactions are included.
func (api *EthereumAPI) GetBlockByHash(_ context.Context, hash gethcommon.Hash, _ bool) (map[string]interface{}, error) {
rollupHeaderWithHashes := api.host.DB().GetRollupHeader(hash)
if rollupHeaderWithHashes == nil {
return nil, nil //nolint:nilnil
}
return headerWithHashesToBlock(rollupHeaderWithHashes), nil
}
// GasPrice is a placeholder for an RPC method required by MetaMask/Remix.
func (api *EthereumAPI) GasPrice(context.Context) (*hexutil.Big, error) {
return (*hexutil.Big)(big.NewInt(1)), nil
}
// Call returns the result of executing the smart contract as a user, encrypted with the viewing key corresponding to
// the `from` field and encoded as hex.
func (api *EthereumAPI) Call(_ context.Context, encryptedParams common.EncryptedParamsCall) (string, error) {
encryptedResponse, err := api.host.EnclaveClient().ExecuteOffChainTransaction(encryptedParams)
if err != nil {
return "", err
}
return gethcommon.Bytes2Hex(encryptedResponse), nil
}
// GetTransactionReceipt returns the transaction receipt for the given transaction hash, encrypted with the viewing key
// corresponding to the original transaction submitter and encoded as hex, or nil if no matching transaction exists.
func (api *EthereumAPI) GetTransactionReceipt(_ context.Context, encryptedParams common.EncryptedParamsGetTxReceipt) (*string, error) {
encryptedResponse, err := api.host.EnclaveClient().GetTransactionReceipt(encryptedParams)
if err != nil {
return nil, err
}
if encryptedResponse == nil {
return nil, nil //nolint:nilnil
}
encryptedResponseHex := gethcommon.Bytes2Hex(encryptedResponse)
return &encryptedResponseHex, nil
}
// EstimateGas requests the enclave the gas estimation based on the callMsg supplied params (encrypted)
func (api *EthereumAPI) EstimateGas(_ context.Context, encryptedParams common.EncryptedParamsEstimateGas) (*string, error) {
encryptedResponse, err := api.host.EnclaveClient().EstimateGas(encryptedParams)
if err != nil {
return nil, err
}
encryptedResponseHex := gethcommon.Bytes2Hex(encryptedResponse)
return &encryptedResponseHex, nil
}
// SendRawTransaction sends the encrypted transaction.
func (api *EthereumAPI) SendRawTransaction(_ context.Context, encryptedParams common.EncryptedParamsSendRawTx) (string, error) {
encryptedResponse, err := api.host.SubmitAndBroadcastTx(encryptedParams)
if err != nil {
return "", err
}
return gethcommon.Bytes2Hex(encryptedResponse), nil
}
// GetCode returns the code stored at the given address in the state for the given rollup height or rollup hash.
func (api *EthereumAPI) GetCode(_ context.Context, address gethcommon.Address, blockNrOrHash rpc.BlockNumberOrHash) (hexutil.Bytes, error) {
// requested a number
if rollupNumber, ok := blockNrOrHash.Number(); ok {
rollupHash, err := api.blockNumberToHash(rollupNumber)
if err != nil {
return nil, fmt.Errorf("unable to fetch block number: %w", err)
}
return api.host.EnclaveClient().GetCode(address, rollupHash)
}
// requested a hash
if rollupHash, ok := blockNrOrHash.Hash(); ok {
return api.host.EnclaveClient().GetCode(address, &rollupHash)
}
return nil, errors.New("invalid arguments; neither rollup height nor rollup hash specified")
}
func (api *EthereumAPI) GetTransactionCount(_ context.Context, encryptedParams common.EncryptedParamsGetTxCount) (string, error) {
encryptedResponse, err := api.host.EnclaveClient().GetTransactionCount(encryptedParams)
if err != nil {
return "", err
}
if encryptedResponse == nil {
return "", err
}
return gethcommon.Bytes2Hex(encryptedResponse), nil
}
// GetTransactionByHash returns the transaction with the given hash, encrypted with the viewing key corresponding to the
// `from` field and encoded as hex, or nil if no matching transaction exists.
func (api *EthereumAPI) GetTransactionByHash(_ context.Context, encryptedParams common.EncryptedParamsGetTxByHash) (*string, error) {
encryptedResponse, err := api.host.EnclaveClient().GetTransaction(encryptedParams)
if err != nil {
return nil, err
}
if encryptedResponse == nil {
return nil, err
}
encryptedResponseHex := gethcommon.Bytes2Hex(encryptedResponse)
return &encryptedResponseHex, nil
}
// FeeHistory is a placeholder for an RPC method required by MetaMask/Remix.
func (api *EthereumAPI) FeeHistory(context.Context, rpc.DecimalOrHex, rpc.BlockNumber, []float64) (*FeeHistoryResult, error) {
// TODO - Return a non-dummy fee history.
return &FeeHistoryResult{
OldestBlock: (*hexutil.Big)(big.NewInt(0)),
Reward: [][]*hexutil.Big{},
BaseFee: []*hexutil.Big{},
GasUsedRatio: []float64{},
}, nil
}
// Maps an external rollup to a block.
func headerWithHashesToBlock(headerWithHashes *common.HeaderWithTxHashes) map[string]interface{} {
header := headerWithHashes.Header
return map[string]interface{}{
"number": (*hexutil.Big)(header.Number),
"hash": header.Hash(),
"parentHash": header.ParentHash,
"nonce": header.Nonce,
"logsBloom": header.Bloom,
"stateRoot": header.Root,
"receiptsRoot": header.ReceiptHash,
"miner": header.Agg,
"extraData": hexutil.Bytes(header.Extra),
"transactionsRoot": header.TxHash,
"transactions": headerWithHashes.TxHashes,
"sha3Uncles": header.UncleHash,
"difficulty": header.Difficulty,
"gasLimit": header.GasLimit,
"gasUsed": header.GasUsed,
"timestamp": header.Time,
"mixHash": header.MixDigest,
"baseFeePerGas": header.BaseFee,
}
}
// FeeHistoryResult is the structure returned by Geth `eth_feeHistory` API.
type FeeHistoryResult struct {
OldestBlock *hexutil.Big `json:"oldestBlock"`
Reward [][]*hexutil.Big `json:"reward,omitempty"`
BaseFee []*hexutil.Big `json:"baseFeePerGas,omitempty"`
GasUsedRatio []float64 `json:"gasUsedRatio"`
}
func (api *EthereumAPI) blockNumberToHash(blockNumber rpc.BlockNumber) (*gethcommon.Hash, error) {
// Predefined constants to support Geth's API
switch blockNumber {
case rpc.LatestBlockNumber:
hash := api.host.DB().GetCurrentRollupHead().Header.Hash()
return &hash, nil
case rpc.EarliestBlockNumber:
hash := api.host.DB().GetRollupHeader(gethcommon.BigToHash(big.NewInt(0))).Header.Hash()
return &hash, nil
case rpc.PendingBlockNumber:
// todo Dependent on the current pending rollup - leaving it for a different iteration as it will need more thought
return nil, nil //nolint
}
rollupHash := api.host.DB().GetRollupHash(big.NewInt(blockNumber.Int64()))
if rollupHash == nil {
return nil, fmt.Errorf("unable to fetch rollup at height: %d", blockNumber.Int64())
}
return rollupHash, nil
}