/
blocks.go
613 lines (510 loc) · 18.9 KB
/
blocks.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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
package rpc
import (
"context"
"encoding/json"
"errors"
"fmt"
"math"
"math/big"
"sort"
"strconv"
"github.com/artela-network/artela-evm/vm"
tmrpctypes "github.com/cometbft/cometbft/rpc/core/types"
sdktypes "github.com/cosmos/cosmos-sdk/types"
grpctypes "github.com/cosmos/cosmos-sdk/types/grpc"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/consensus/misc"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/state"
"github.com/ethereum/go-ethereum/core/types"
ethtypes "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/event"
"github.com/ethereum/go-ethereum/rpc"
"github.com/ethereum/go-ethereum/trie"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"
"github.com/artela-network/artela/ethereum/rpc/ethapi"
rpctypes "github.com/artela-network/artela/ethereum/rpc/types"
"github.com/artela-network/artela/ethereum/rpc/utils"
"github.com/artela-network/artela/x/evm/txs"
evmtypes "github.com/artela-network/artela/x/evm/types"
)
// Blockchain API
func (b *BackendImpl) SetHead(_ uint64) {
b.logger.Error("SetHead is not implemented")
}
func (b *BackendImpl) HeaderByNumber(_ context.Context, number rpc.BlockNumber) (*ethtypes.Header, error) {
resBlock, err := b.CosmosBlockByNumber(number)
if err != nil {
return nil, err
}
if resBlock == nil {
return nil, fmt.Errorf("block not found for height %d", number)
}
blockRes, err := b.CosmosBlockResultByNumber(&resBlock.Block.Height)
if err != nil {
return nil, fmt.Errorf("block result not found for height %d", resBlock.Block.Height)
}
bloom, err := b.blockBloom(blockRes)
if err != nil {
b.logger.Debug("HeaderByNumber BlockBloom failed", "height", resBlock.Block.Height)
}
baseFee, err := b.BaseFee(blockRes)
if err != nil {
// handle the error for pruned node.
b.logger.Error("failed to fetch Base Fee from prunned block. Check node prunning configuration", "height", resBlock.Block.Height, "error", err)
}
ethHeader := rpctypes.EthHeaderFromTendermint(resBlock.Block.Header, bloom, baseFee)
return ethHeader, nil
}
func (b *BackendImpl) HeaderByHash(_ context.Context, hash common.Hash) (*ethtypes.Header, error) {
return nil, nil
}
func (b *BackendImpl) HeaderByNumberOrHash(ctx context.Context,
blockNrOrHash rpc.BlockNumberOrHash,
) (*ethtypes.Header, error) {
return nil, errors.New("HeaderByNumberOrHash is not implemented")
}
func (b *BackendImpl) CurrentHeader() *ethtypes.Header {
block, err := b.BlockByNumber(context.Background(), rpc.LatestBlockNumber)
if err != nil || block == nil {
b.logger.Error("get CurrentHeader failed, error or block is nil", "error", err)
return nil
}
return block.Header()
}
func (b *BackendImpl) CurrentBlock() *rpctypes.Block {
block, err := b.ArtBlockByNumber(context.Background(), rpc.LatestBlockNumber)
if err != nil {
b.logger.Error("get CurrentBlock failed", "error", err)
return nil
}
return block
}
func (b *BackendImpl) BlockByNumber(_ context.Context, number rpc.BlockNumber) (*ethtypes.Block, error) {
block, err := b.ArtBlockByNumber(context.Background(), number)
if err != nil {
return nil, err
}
return block.EthBlock(), nil
}
func (b *BackendImpl) ArtBlockByNumber(_ context.Context, number rpc.BlockNumber) (*rpctypes.Block, error) {
resBlock, err := b.CosmosBlockByNumber(number)
if err != nil || resBlock == nil {
return nil, fmt.Errorf("query block failed, block number %d, %w", number, err)
}
blockRes, err := b.CosmosBlockResultByNumber(&resBlock.Block.Height)
if err != nil {
return nil, fmt.Errorf("block result not found for height %d", resBlock.Block.Height)
}
return b.BlockFromCosmosBlock(resBlock, blockRes)
}
func (b *BackendImpl) BlockByHash(_ context.Context, hash common.Hash) (*rpctypes.Block, error) {
resBlock, err := b.CosmosBlockByHash(hash)
if err != nil || resBlock == nil {
return nil, fmt.Errorf("failed to get block by hash %s, %w", hash.Hex(), err)
}
blockRes, err := b.CosmosBlockResultByNumber(&resBlock.Block.Height)
if err != nil {
return nil, fmt.Errorf("block result not found for height %d", resBlock.Block.Height)
}
return b.BlockFromCosmosBlock(resBlock, blockRes)
}
func (b *BackendImpl) BlockByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*rpctypes.Block, error) {
return nil, errors.New("BlockByNumberOrHash is not implemented")
}
func (b *BackendImpl) StateAndHeaderByNumber(
ctx context.Context, number rpc.BlockNumber,
) (*state.StateDB, *ethtypes.Header, error) {
return nil, nil, errors.New("StateAndHeaderByNumber is not implemented")
}
func (b *BackendImpl) StateAndHeaderByNumberOrHash(
ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash,
) (*state.StateDB, *ethtypes.Header, error) {
return nil, nil, errors.New("invalid arguments; neither block nor hash specified")
}
func (b *BackendImpl) PendingBlockAndReceipts() (*ethtypes.Block, types.Receipts) {
b.logger.Error("PendingBlockAndReceipts is not implemented")
return nil, nil
}
// GetReceipts get receipts by block hash
func (b *BackendImpl) GetReceipts(_ context.Context, hash common.Hash) (types.Receipts, error) {
return nil, errors.New("GetReceipts is not implemented")
}
func (b *BackendImpl) GetTd(_ context.Context, hash common.Hash) *big.Int {
b.logger.Error("GetTd is not implemented")
return nil
}
func (b *BackendImpl) GetEVM(ctx context.Context, msg *core.Message, state *state.StateDB,
header *ethtypes.Header, vmConfig *vm.Config, _ *vm.BlockContext,
) (*vm.EVM, func() error) {
return nil, func() error {
return errors.New("GetEVM is not impelemted")
}
}
func (b *BackendImpl) SubscribeChainEvent(ch chan<- core.ChainEvent) event.Subscription {
return b.scope.Track(b.chainFeed.Subscribe(ch))
}
func (b *BackendImpl) SubscribeChainHeadEvent(ch chan<- core.ChainHeadEvent) event.Subscription {
b.logger.Debug("called eth.rpc.backend.SubscribeChainHeadEvent", "ch", ch)
return b.scope.Track(b.chainHeadFeed.Subscribe(ch))
}
func (b *BackendImpl) SubscribeChainSideEvent(ch chan<- core.ChainSideEvent) event.Subscription {
b.logger.Debug("called eth.rpc.backend.SubscribeChainSideEvent", "ch", ch)
return b.scope.Track(b.chainSideFeed.Subscribe(ch))
}
func (b *BackendImpl) CosmosBlockByHash(blockHash common.Hash) (*tmrpctypes.ResultBlock, error) {
resBlock, err := b.clientCtx.Client.BlockByHash(b.ctx, blockHash.Bytes())
if err != nil {
return nil, err
}
if resBlock.Block == nil {
return nil, fmt.Errorf("failed to query block for hash: %s", blockHash.Hex())
}
return resBlock, nil
}
func (b *BackendImpl) CosmosBlockByNumber(blockNum rpc.BlockNumber) (*tmrpctypes.ResultBlock, error) {
height := blockNum.Int64()
if height <= 0 {
// fetch the latest block number from the app state, more accurate than the tendermint block store state.
n, err := b.BlockNumber()
if err != nil {
return nil, err
}
height = int64(n) // #nosec G701 -- checked for int overflow already
}
resBlock, err := b.clientCtx.Client.Block(b.ctx, &height)
if err != nil {
return nil, err
}
if resBlock.Block == nil {
return nil, fmt.Errorf("failed to query block for blockNum: %d", blockNum.Int64())
}
return resBlock, nil
}
// BlockNumberFromTendermint returns the BlockNumber from BlockNumberOrHash
func (b *BackendImpl) blockNumberFromCosmos(blockNrOrHash rpc.BlockNumberOrHash) (rpc.BlockNumber, error) {
switch {
case blockNrOrHash.BlockHash == nil && blockNrOrHash.BlockNumber == nil:
return rpc.EarliestBlockNumber, fmt.Errorf("types BlockHash and BlockNumber cannot be both nil")
case blockNrOrHash.BlockHash != nil:
resBlock, err := b.CosmosBlockByHash(*blockNrOrHash.BlockHash)
if err != nil || resBlock.Block == nil {
return rpc.EarliestBlockNumber, err
}
return rpc.BlockNumber(resBlock.Block.Height), nil
case blockNrOrHash.BlockNumber != nil:
if *blockNrOrHash.BlockNumber == rpc.LatestBlockNumber {
currentHeight := b.CurrentHeader().Number
return rpc.BlockNumber(currentHeight.Int64()), nil
}
return *blockNrOrHash.BlockNumber, nil
default:
return rpc.EarliestBlockNumber, nil
}
}
func (b *BackendImpl) BlockNumber() (hexutil.Uint64, error) {
// do any grpc query, ignore the response and use the returned block height
var header metadata.MD
_, err := b.queryClient.Params(b.ctx, &txs.QueryParamsRequest{}, grpc.Header(&header))
if err != nil {
return hexutil.Uint64(0), err
}
blockHeightHeader := header.Get(grpctypes.GRPCBlockHeightHeader)
if headerLen := len(blockHeightHeader); headerLen != 1 {
return 0, fmt.Errorf("unexpected '%s' gRPC header length; got %d, expected: %d", grpctypes.GRPCBlockHeightHeader, headerLen, 1)
}
height, err := strconv.ParseUint(blockHeightHeader[0], 10, 64)
if err != nil {
return 0, fmt.Errorf("failed to parse block height: %w", err)
}
if height > math.MaxInt64 {
return 0, fmt.Errorf("block height %d is greater than max uint64", height)
}
return hexutil.Uint64(height), nil
}
func (b *BackendImpl) BlockTimeByNumber(blockNum int64) (uint64, error) {
resBlock, err := b.clientCtx.Client.Block(b.ctx, &blockNum)
if err != nil {
return 0, err
}
return uint64(resBlock.Block.Time.Unix()), nil
}
func (b *BackendImpl) CosmosBlockResultByNumber(height *int64) (*tmrpctypes.ResultBlockResults, error) {
return b.clientCtx.Client.BlockResults(b.ctx, height)
}
func (b *BackendImpl) GetCode(address common.Address, blockNrOrHash rpc.BlockNumberOrHash) (hexutil.Bytes, error) {
blockNum, err := b.blockNumberFromCosmos(blockNrOrHash)
if err != nil {
return nil, err
}
req := &txs.QueryCodeRequest{
Address: address.String(),
}
res, err := b.queryClient.Code(rpctypes.ContextWithHeight(blockNum.Int64()), req)
if err != nil {
return nil, err
}
return res.Code, nil
}
// GetStorageAt returns the contract storage at the given address, block number, and key.
func (b *BackendImpl) GetStorageAt(address common.Address, key string, blockNrOrHash rpc.BlockNumberOrHash) (hexutil.Bytes, error) {
blockNum, err := b.blockNumberFromCosmos(blockNrOrHash)
if err != nil {
return nil, err
}
req := &txs.QueryStorageRequest{
Address: address.String(),
Key: key,
}
res, err := b.queryClient.Storage(rpctypes.ContextWithHeight(blockNum.Int64()), req)
if err != nil {
return nil, err
}
value := common.HexToHash(res.Value)
return value.Bytes(), nil
}
// BlockBloom query block bloom filter from block results
func (b *BackendImpl) blockBloom(blockRes *tmrpctypes.ResultBlockResults) (ethtypes.Bloom, error) {
for _, event := range blockRes.EndBlockEvents {
if event.Type != evmtypes.EventTypeBlockBloom {
continue
}
for _, attr := range event.Attributes {
if attr.Key == evmtypes.AttributeKeyEthereumBloom {
return ethtypes.BytesToBloom([]byte(attr.Value)), nil
}
}
}
return ethtypes.Bloom{}, errors.New("block bloom event is not found")
}
func (b *BackendImpl) BlockFromCosmosBlock(resBlock *tmrpctypes.ResultBlock, blockRes *tmrpctypes.ResultBlockResults) (*rpctypes.Block, error) {
block := resBlock.Block
height := block.Height
bloom, err := b.blockBloom(blockRes)
if err != nil {
b.logger.Debug("HeaderByNumber BlockBloom failed", "height", height)
}
baseFee, err := b.BaseFee(blockRes)
if err != nil {
b.logger.Error("failed to fetch Base Fee from prunned block. Check node prunning configuration", "height", height, "error", err)
}
ethHeader := rpctypes.EthHeaderFromTendermint(block.Header, bloom, baseFee)
msgs := b.EthMsgsFromCosmosBlock(resBlock, blockRes)
txs := make([]*ethtypes.Transaction, len(msgs))
for i, ethMsg := range msgs {
txs[i] = ethMsg.AsTransaction()
}
gasUsed := uint64(0)
for _, txsResult := range blockRes.TxsResults {
// workaround for cosmos-sdk bug. https://github.com/cosmos/cosmos-sdk/issues/10832
if utils.ShouldIgnoreGasUsed(txsResult) {
// block gas limit has exceeded, other txs must have failed with same reason.
break
}
gasUsed += uint64(txsResult.GetGasUsed())
}
ethHeader.GasUsed = gasUsed
gasLimit, err := rpctypes.BlockMaxGasFromConsensusParams(context.Background(), b.clientCtx, block.Height)
if err != nil {
b.logger.Error("failed to query consensus params", "error", err.Error())
}
ethHeader.GasLimit = uint64(gasLimit)
blockHash := common.BytesToHash(block.Hash().Bytes())
receipts, err := b.GetReceipts(context.Background(), blockHash)
if err != nil {
b.logger.Debug(fmt.Sprintf("failed to fetch receipts, block hash %s, block number %d", blockHash.Hex(), height))
}
ethBlock := ethtypes.NewBlock(ethHeader, txs, nil, receipts, trie.NewStackTrie(nil))
res := rpctypes.EthBlockToBlock(ethBlock)
res.SetHash(blockHash)
return res, nil
}
func (b *BackendImpl) EthMsgsFromCosmosBlock(resBlock *tmrpctypes.ResultBlock, blockRes *tmrpctypes.ResultBlockResults) []*txs.MsgEthereumTx {
var result []*txs.MsgEthereumTx
block := resBlock.Block
txResults := blockRes.TxsResults
for i, tx := range block.Txs {
// Check if tx exists on EVM by cross checking with blockResults:
// - Include unsuccessful tx that exceeds block gas limit
// - Exclude unsuccessful tx with any other error but ExceedBlockGasLimit
if !rpctypes.TxSuccessOrExceedsBlockGasLimit(txResults[i]) {
b.logger.Debug("invalid tx result code", "cosmos-hash", hexutil.Encode(tx.Hash()))
continue
}
tx, err := b.clientCtx.TxConfig.TxDecoder()(tx)
if err != nil {
b.logger.Debug("failed to decode transaction in block", "height", block.Height, "error", err.Error())
continue
}
for _, msg := range tx.GetMsgs() {
ethMsg, ok := msg.(*txs.MsgEthereumTx)
if !ok {
continue
}
ethMsg.Hash = ethMsg.AsTransaction().Hash().Hex()
result = append(result, ethMsg)
}
}
return result
}
func (b *BackendImpl) DoCall(args ethapi.TransactionArgs, blockNrOrHash rpc.BlockNumberOrHash) (*txs.MsgEthereumTxResponse, error) {
blockNum, err := b.blockNumberFromCosmos(blockNrOrHash)
if err != nil {
return nil, err
}
bz, err := json.Marshal(&args)
if err != nil {
return nil, err
}
header, err := b.CosmosBlockByNumber(blockNum)
if err != nil {
// the error message imitates geth behavior
return nil, errors.New("header not found")
}
req := txs.EthCallRequest{
Args: bz,
GasCap: b.RPCGasCap(),
ProposerAddress: sdktypes.ConsAddress(header.Block.ProposerAddress),
ChainId: b.chainID.Int64(),
}
// From ContextWithHeight: if the provided height is 0,
// it will return an empty context and the gRPC query will use
// the latest block height for querying.
ctx := rpctypes.ContextWithHeight(blockNum.Int64())
timeout := b.RPCEVMTimeout()
// Setup context so it may be canceled the call has completed
// or, in case of unmetered gas, setup a context with a timeout.
var cancel context.CancelFunc
if timeout > 0 {
ctx, cancel = context.WithTimeout(ctx, timeout)
} else {
ctx, cancel = context.WithCancel(ctx)
}
// Make sure the context is canceled when the call has completed
// this makes sure resources are cleaned up.
defer cancel()
res, err := b.queryClient.EthCall(ctx, &req)
if err != nil {
return nil, err
}
if res.Failed() {
if res.VmError != vm.ErrExecutionReverted.Error() {
return nil, status.Error(codes.Internal, res.VmError)
}
return nil, evmtypes.NewExecErrorWithReason(res.Ret)
}
return res, nil
}
func (b *BackendImpl) BlockBloom(blockRes *tmrpctypes.ResultBlockResults) (ethtypes.Bloom, error) {
return b.blockBloom(blockRes)
}
func (b *BackendImpl) GetBlockByNumber(blockNum rpc.BlockNumber, fullTx bool) (map[string]interface{}, error) {
block, err := b.BlockByNumber(context.Background(), blockNum)
if err != nil {
return nil, err
}
return ethapi.RPCMarshalHeader(block.Header(), block.Hash()), nil
}
func (b *BackendImpl) processBlock(
tendermintBlock *tmrpctypes.ResultBlock,
ethBlock *map[string]interface{},
rewardPercentiles []float64,
tendermintBlockResult *tmrpctypes.ResultBlockResults,
) (*rpctypes.OneFeeHistory, error) {
blockHeight := tendermintBlock.Block.Height
blockBaseFee, err := b.BaseFee(tendermintBlockResult)
if err != nil {
return nil, err
}
targetOneFeeHistory := &rpctypes.OneFeeHistory{}
targetOneFeeHistory.BaseFee = blockBaseFee
cfg := b.ChainConfig()
if cfg.IsLondon(big.NewInt(blockHeight + 1)) {
header := b.CurrentHeader()
if header == nil {
return nil, errors.New("header does not exist")
}
targetOneFeeHistory.NextBaseFee = misc.CalcBaseFee(cfg, header)
} else {
targetOneFeeHistory.NextBaseFee = new(big.Int)
}
gasLimitUint64, ok := (*ethBlock)["gasLimit"].(hexutil.Uint64)
if !ok {
return nil, fmt.Errorf("invalid gas limit type: %T", (*ethBlock)["gasLimit"])
}
gasUsed, ok := (*ethBlock)["gasUsed"].(hexutil.Uint64)
if !ok {
return nil, fmt.Errorf("invalid gas used type: %T", (*ethBlock)["gasUsed"])
}
gasusedfloat, _ := new(big.Float).SetInt(new(big.Int).SetUint64(uint64(gasUsed))).Float64()
if gasLimitUint64 <= 0 {
return nil, fmt.Errorf("gasLimit of block height %d should be bigger than 0 , current gaslimit %d", blockHeight, gasLimitUint64)
}
gasUsedRatio := gasusedfloat / float64(gasLimitUint64)
blockGasUsed := gasusedfloat
targetOneFeeHistory.GasUsedRatio = gasUsedRatio
rewardCount := len(rewardPercentiles)
targetOneFeeHistory.Reward = make([]*big.Int, rewardCount)
for i := 0; i < rewardCount; i++ {
targetOneFeeHistory.Reward[i] = big.NewInt(0)
}
tendermintTxs := tendermintBlock.Block.Txs
tendermintTxResults := tendermintBlockResult.TxsResults
tendermintTxCount := len(tendermintTxs)
var sorter sortGasAndReward
for i := 0; i < tendermintTxCount; i++ {
eachTendermintTx := tendermintTxs[i]
eachTendermintTxResult := tendermintTxResults[i]
tx, err := b.clientCtx.TxConfig.TxDecoder()(eachTendermintTx)
if err != nil {
b.logger.Debug("failed to decode transaction in block", "height", blockHeight, "error", err.Error())
continue
}
txGasUsed := uint64(eachTendermintTxResult.GasUsed) // #nosec G701
for _, msg := range tx.GetMsgs() {
ethMsg, ok := msg.(*txs.MsgEthereumTx)
if !ok {
continue
}
tx := ethMsg.AsTransaction()
reward := tx.EffectiveGasTipValue(blockBaseFee)
if reward == nil {
reward = big.NewInt(0)
}
sorter = append(sorter, txGasAndReward{gasUsed: txGasUsed, reward: reward})
}
}
// return an all zero row if there are no transactions to gather data from
ethTxCount := len(sorter)
if ethTxCount == 0 {
return targetOneFeeHistory, nil
}
sort.Sort(sorter)
var txIndex int
sumGasUsed := sorter[0].gasUsed
for i, p := range rewardPercentiles {
thresholdGasUsed := uint64(blockGasUsed * p / 100) // #nosec G701
for sumGasUsed < thresholdGasUsed && txIndex < ethTxCount-1 {
txIndex++
sumGasUsed += sorter[txIndex].gasUsed
}
targetOneFeeHistory.Reward[i] = sorter[txIndex].reward
}
return targetOneFeeHistory, nil
}
type txGasAndReward struct {
gasUsed uint64
reward *big.Int
}
type sortGasAndReward []txGasAndReward
func (s sortGasAndReward) Len() int { return len(s) }
func (s sortGasAndReward) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
func (s sortGasAndReward) Less(i, j int) bool {
return s[i].reward.Cmp(s[j].reward) < 0
}