/
api_multi.go
353 lines (303 loc) · 10.8 KB
/
api_multi.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
package eth
import (
"errors"
"fmt"
"math/big"
"github.com/FiboChain/fbc/app/rpc/monitor"
rpctypes "github.com/FiboChain/fbc/app/rpc/types"
sdk "github.com/FiboChain/fbc/libs/cosmos-sdk/types"
sdkerrors "github.com/FiboChain/fbc/libs/cosmos-sdk/types/errors"
"github.com/FiboChain/fbc/libs/cosmos-sdk/x/auth"
authexported "github.com/FiboChain/fbc/libs/cosmos-sdk/x/auth/exported"
ctypes "github.com/FiboChain/fbc/libs/tendermint/rpc/core/types"
evmtypes "github.com/FiboChain/fbc/x/evm/types"
"github.com/FiboChain/fbc/x/evm/watcher"
"github.com/FiboChain/fbc/x/token"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
ethtypes "github.com/ethereum/go-ethereum/core/types"
"github.com/spf13/viper"
)
const (
FlagEnableMultiCall = "rpc.enable-multi-call"
)
// GetBalanceBatch returns the provided account's balance up to the provided block number.
func (api *PublicEthereumAPI) GetBalanceBatch(addresses []common.Address, blockNrOrHash rpctypes.BlockNumberOrHash) (interface{}, error) {
if !viper.GetBool(FlagEnableMultiCall) {
return nil, errors.New("the method is not allowed")
}
monitor := monitor.GetMonitor("eth_getBalanceBatch", api.logger, api.Metrics).OnBegin()
defer monitor.OnEnd("addresses", addresses, "block number", blockNrOrHash)
blockNum, err := api.backend.ConvertToBlockNumber(blockNrOrHash)
if err != nil {
return nil, err
}
clientCtx := api.clientCtx
useWatchBackend := api.useWatchBackend(blockNum)
if !(blockNum == rpctypes.PendingBlockNumber || blockNum == rpctypes.LatestBlockNumber) && !useWatchBackend {
clientCtx = api.clientCtx.WithHeight(blockNum.Int64())
}
type accBalance struct {
Type token.AccType `json:"type"`
Balance *hexutil.Big `json:"balance"`
}
balances := make(map[string]accBalance)
for _, address := range addresses {
if acc, err := api.wrappedBackend.MustGetAccount(address.Bytes()); err == nil {
balance := acc.GetCoins().AmountOf(sdk.DefaultBondDenom).BigInt()
if balance == nil {
balances[address.String()] = accBalance{accountType(acc), (*hexutil.Big)(sdk.ZeroInt().BigInt())}
} else {
balances[address.String()] = accBalance{accountType(acc), (*hexutil.Big)(balance)}
}
continue
}
bs, err := api.clientCtx.Codec.MarshalJSON(auth.NewQueryAccountParams(address.Bytes()))
if err != nil {
return nil, err
}
res, _, err := clientCtx.QueryWithData(fmt.Sprintf("custom/%s/%s", auth.QuerierRoute, auth.QueryAccount), bs)
if err != nil {
continue
}
var account authexported.Account
if err := api.clientCtx.Codec.UnmarshalJSON(res, &account); err != nil {
return nil, err
}
val := account.GetCoins().AmountOf(sdk.DefaultBondDenom).BigInt()
accType := accountType(account)
if accType == token.UserAccount || accType == token.ContractAccount {
api.watcherBackend.CommitAccountToRpcDb(account)
if blockNum != rpctypes.PendingBlockNumber {
balances[address.String()] = accBalance{accType, (*hexutil.Big)(val)}
continue
}
// update the address balance with the pending transactions value (if applicable)
pendingTxs, err := api.backend.UserPendingTransactions(address.String(), -1)
if err != nil {
return nil, err
}
for _, tx := range pendingTxs {
if tx == nil {
continue
}
if tx.From == address {
val = new(big.Int).Sub(val, tx.Value.ToInt())
}
if *tx.To == address {
val = new(big.Int).Add(val, tx.Value.ToInt())
}
}
}
balances[address.String()] = accBalance{accType, (*hexutil.Big)(val)}
}
return balances, nil
}
// MultiCall performs multiple raw contract call.
func (api *PublicEthereumAPI) MultiCall(args []rpctypes.CallArgs, blockNr rpctypes.BlockNumber, _ *[]evmtypes.StateOverrides) ([]hexutil.Bytes, error) {
if !viper.GetBool(FlagEnableMultiCall) {
return nil, errors.New("the method is not allowed")
}
monitor := monitor.GetMonitor("eth_multiCall", api.logger, api.Metrics).OnBegin()
defer monitor.OnEnd("args", args, "block number", blockNr)
blockNrOrHash := rpctypes.BlockNumberOrHashWithNumber(blockNr)
rets := make([]hexutil.Bytes, 0, len(args))
for _, arg := range args {
ret, err := api.Call(arg, blockNrOrHash, nil)
if err != nil {
return rets, err
}
rets = append(rets, ret)
}
return rets, nil
}
// GetTransactionsByBlock returns some transactions identified by number or hash.
func (api *PublicEthereumAPI) GetTransactionsByBlock(blockNrOrHash rpctypes.BlockNumberOrHash, offset, limit hexutil.Uint) ([]*watcher.Transaction, error) {
if !viper.GetBool(FlagEnableMultiCall) {
return nil, errors.New("the method is not allowed")
}
monitor := monitor.GetMonitor("eth_getTransactionsByBlock", api.logger, api.Metrics).OnBegin()
defer monitor.OnEnd("block number", blockNrOrHash, "offset", offset, "limit", limit)
blockNum, err := api.backend.ConvertToBlockNumber(blockNrOrHash)
if err != nil {
return nil, err
}
txs, e := api.wrappedBackend.GetTransactionsByBlockNumber(uint64(blockNum), uint64(offset), uint64(limit))
if e == nil && txs != nil {
return txs, nil
}
height := blockNum.Int64()
switch blockNum {
case rpctypes.PendingBlockNumber:
// get all the EVM pending txs
pendingTxs, err := api.backend.PendingTransactions()
if err != nil {
return nil, err
}
switch {
case len(pendingTxs) <= int(offset):
return nil, nil
case len(pendingTxs) < int(offset+limit):
return pendingTxs[offset:], nil
default:
return pendingTxs[offset : offset+limit], nil
}
case rpctypes.LatestBlockNumber:
height, err = api.backend.LatestBlockNumber()
if err != nil {
return nil, err
}
}
resBlock, err := api.backend.Block(&height)
if err != nil {
return nil, err
}
for idx := offset; idx < offset+limit && int(idx) < len(resBlock.Block.Txs); idx++ {
tx, _ := api.getTransactionByBlockAndIndex(resBlock.Block, idx)
if tx != nil {
txs = append(txs, tx)
}
}
return txs, nil
}
// GetTransactionReceiptsByBlock returns the transaction receipt identified by block hash or number.
func (api *PublicEthereumAPI) GetTransactionReceiptsByBlock(blockNrOrHash rpctypes.BlockNumberOrHash, offset, limit hexutil.Uint) ([]*watcher.TransactionReceipt, error) {
if !viper.GetBool(FlagEnableMultiCall) {
return nil, errors.New("the method is not allowed")
}
monitor := monitor.GetMonitor("eth_getTransactionReceiptsByBlock", api.logger, api.Metrics).OnBegin()
defer monitor.OnEnd("block number", blockNrOrHash, "offset", offset, "limit", limit)
txs, err := api.GetTransactionsByBlock(blockNrOrHash, offset, limit)
if err != nil || len(txs) == 0 {
return nil, err
}
var receipts []*watcher.TransactionReceipt
var block *ctypes.ResultBlock
var blockHash common.Hash
for _, tx := range txs {
res, _ := api.wrappedBackend.GetTransactionReceipt(tx.Hash)
if res != nil {
receipts = append(receipts, res)
continue
}
tx, err := api.clientCtx.Client.Tx(tx.Hash.Bytes(), false)
if err != nil {
// Return nil for transaction when not found
return nil, nil
}
if block == nil {
// Query block for consensus hash
block, err = api.backend.Block(&tx.Height)
if err != nil {
return nil, err
}
blockHash = common.BytesToHash(block.Block.Hash())
}
// Convert tx bytes to eth transaction
ethTx, err := rpctypes.RawTxToEthTx(api.clientCtx, tx.Tx, tx.Height)
if err != nil {
return nil, err
}
err = ethTx.VerifySig(ethTx.ChainID(), tx.Height)
if err != nil {
return nil, err
}
// Set status codes based on tx result
var status = hexutil.Uint64(0)
if tx.TxResult.IsOK() {
status = hexutil.Uint64(1)
}
txData := tx.TxResult.GetData()
data, err := evmtypes.DecodeResultData(txData)
if err != nil {
status = 0 // transaction failed
}
if len(data.Logs) == 0 {
data.Logs = []*ethtypes.Log{}
}
contractAddr := &data.ContractAddress
if data.ContractAddress == common.HexToAddress("0x00000000000000000000") {
contractAddr = nil
}
// fix gasUsed when deliverTx ante handler check sequence invalid
gasUsed := tx.TxResult.GasUsed
if tx.TxResult.Code == sdkerrors.ErrInvalidSequence.ABCICode() {
gasUsed = 0
}
receipt := &watcher.TransactionReceipt{
Status: status,
//CumulativeGasUsed: hexutil.Uint64(cumulativeGasUsed),
LogsBloom: data.Bloom,
Logs: data.Logs,
TransactionHash: common.BytesToHash(tx.Hash.Bytes()).String(),
ContractAddress: contractAddr,
GasUsed: hexutil.Uint64(gasUsed),
BlockHash: blockHash.String(),
BlockNumber: hexutil.Uint64(tx.Height),
TransactionIndex: hexutil.Uint64(tx.Index),
From: ethTx.GetFrom(),
To: ethTx.To(),
}
receipts = append(receipts, receipt)
}
return receipts, nil
}
// GetTransactionReceiptsByBlock returns the transaction receipt identified by block hash or number.
func (api *PublicEthereumAPI) GetAllTransactionResultsByBlock(blockNrOrHash rpctypes.BlockNumberOrHash, offset, limit hexutil.Uint) ([]*watcher.TransactionResult, error) {
if !viper.GetBool(FlagEnableMultiCall) {
return nil, errors.New("the method is not allowed")
}
monitor := monitor.GetMonitor("eth_getAllTransactionResultsByBlock", api.logger, api.Metrics).OnBegin()
defer monitor.OnEnd("block number", blockNrOrHash, "offset", offset, "limit", limit)
var results []*watcher.TransactionResult
blockNum, err := api.backend.ConvertToBlockNumber(blockNrOrHash)
if err != nil {
return nil, err
}
height := blockNum.Int64()
if blockNum == rpctypes.LatestBlockNumber {
height, err = api.backend.LatestBlockNumber()
if err != nil {
return nil, err
}
}
// try to get from watch db
results, err = api.wrappedBackend.GetTxResultByBlock(api.clientCtx, uint64(height), uint64(offset), uint64(limit))
if err == nil {
return results, nil
}
// try to get from node
resBlock, err := api.backend.Block(&height)
if err != nil {
return nil, err
}
blockHash := common.BytesToHash(resBlock.Block.Hash())
for idx := offset; idx < offset+limit && int(idx) < len(resBlock.Block.Txs); idx++ {
realTx, err := rpctypes.RawTxToRealTx(api.clientCtx, resBlock.Block.Txs[idx],
blockHash, uint64(resBlock.Block.Height), uint64(idx))
if err != nil {
return nil, err
}
if realTx != nil {
txHash := resBlock.Block.Txs[idx].Hash(resBlock.Block.Height)
queryTx, err := api.clientCtx.Client.Tx(txHash, false)
if err != nil {
// Return nil for transaction when not found
return nil, err
}
var res *watcher.TransactionResult
switch realTx.GetType() {
case sdk.EvmTxType:
res, err = rpctypes.RawTxResultToEthReceipt(api.chainIDEpoch, queryTx, realTx, blockHash)
case sdk.StdTxType:
res, err = watcher.RawTxResultToStdResponse(api.clientCtx, queryTx, realTx, resBlock.Block.Time)
}
if err != nil {
// Return nil for transaction when not found
return nil, err
}
results = append(results, res)
}
}
return results, nil
}