Skip to content

Commit d46ae32

Browse files
authored
Merge pull request #844 from onflow/mpeter/modernize-go-codebase
Run Go modernize tool to simplify code-base
2 parents f540b99 + 1405b66 commit d46ae32

File tree

15 files changed

+42
-50
lines changed

15 files changed

+42
-50
lines changed

api/api.go

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ func (b *BlockChainAPI) BlockNumber(ctx context.Context) (hexutil.Uint64, error)
134134
// - startingBlock: block number this node started to synchronize from
135135
// - currentBlock: block number this node is currently importing
136136
// - highestBlock: block number of the highest block header this node has received from peers
137-
func (b *BlockChainAPI) Syncing(ctx context.Context) (interface{}, error) {
137+
func (b *BlockChainAPI) Syncing(ctx context.Context) (any, error) {
138138
if err := b.rateLimiter.Apply(ctx, EthSyncing); err != nil {
139139
return nil, err
140140
}
@@ -322,7 +322,7 @@ func (b *BlockChainAPI) GetTransactionByBlockNumberAndIndex(
322322
func (b *BlockChainAPI) GetTransactionReceipt(
323323
ctx context.Context,
324324
hash common.Hash,
325-
) (map[string]interface{}, error) {
325+
) (map[string]any, error) {
326326
l := b.logger.With().
327327
Str("endpoint", EthGetTransactionReceipt).
328328
Str("hash", hash.String()).
@@ -334,17 +334,17 @@ func (b *BlockChainAPI) GetTransactionReceipt(
334334

335335
tx, err := b.transactions.Get(hash)
336336
if err != nil {
337-
return handleError[map[string]interface{}](err, l, b.collector)
337+
return handleError[map[string]any](err, l, b.collector)
338338
}
339339

340340
receipt, err := b.receipts.GetByTransactionID(hash)
341341
if err != nil {
342-
return handleError[map[string]interface{}](err, l, b.collector)
342+
return handleError[map[string]any](err, l, b.collector)
343343
}
344344

345345
txReceipt, err := ethTypes.MarshalReceipt(receipt, tx)
346346
if err != nil {
347-
return handleError[map[string]interface{}](err, l, b.collector)
347+
return handleError[map[string]any](err, l, b.collector)
348348
}
349349

350350
return txReceipt, nil
@@ -809,10 +809,7 @@ func (b *BlockChainAPI) FeeHistory(
809809
gasUsedRatios []float64
810810
)
811811

812-
maxCount := uint64(blockCount)
813-
if maxCount > lastBlockNumber {
814-
maxCount = lastBlockNumber
815-
}
812+
maxCount := min(uint64(blockCount), lastBlockNumber)
816813

817814
blockRewards := make([]*hexutil.Big, len(rewardPercentiles))
818815
for i := range rewardPercentiles {

api/server.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -436,9 +436,9 @@ type responseHandler struct {
436436
const errCodePanic = -32603
437437

438438
type jsonError struct {
439-
Code int `json:"code"`
440-
Message string `json:"message"`
441-
Data interface{} `json:"data,omitempty"`
439+
Code int `json:"code"`
440+
Message string `json:"message"`
441+
Data any `json:"data,omitempty"`
442442
}
443443

444444
type jsonMessage struct {

bootstrap/bootstrap.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -531,7 +531,7 @@ func retryInterceptor(maxDuration, pauseDuration time.Duration) grpcOpts.UnaryCl
531531
return func(
532532
ctx context.Context,
533533
method string,
534-
req, reply interface{},
534+
req, reply any,
535535
cc *grpcOpts.ClientConn,
536536
invoker grpcOpts.UnaryInvoker,
537537
opts ...grpcOpts.CallOption,

bootstrap/create-multi-key-account.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ func CreateMultiKeyAccount(
8181
}
8282

8383
accountKeys := make([]*flow.AccountKey, keyCount)
84-
for i := 0; i < keyCount; i++ {
84+
for i := range keyCount {
8585
accountKeys[i] = &flow.AccountKey{
8686
Index: uint32(i),
8787
PublicKey: privateKey.PublicKey(),

eth/types/types.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,7 @@ type Block struct {
468468
GasLimit hexutil.Uint64 `json:"gasLimit"`
469469
GasUsed hexutil.Uint64 `json:"gasUsed"`
470470
Timestamp hexutil.Uint64 `json:"timestamp"`
471-
Transactions interface{} `json:"transactions"`
471+
Transactions any `json:"transactions"`
472472
Uncles []common.Hash `json:"uncles"`
473473
MixHash common.Hash `json:"mixHash"`
474474
BaseFeePerGas hexutil.Big `json:"baseFeePerGas"`
@@ -504,15 +504,15 @@ type SyncStatus struct {
504504
func MarshalReceipt(
505505
receipt *models.Receipt,
506506
tx models.Transaction,
507-
) (map[string]interface{}, error) {
507+
) (map[string]any, error) {
508508
from, err := tx.From()
509509
if err != nil {
510-
return map[string]interface{}{}, err
510+
return map[string]any{}, err
511511
}
512512

513513
txHash := tx.Hash()
514514

515-
fields := map[string]interface{}{
515+
fields := map[string]any{
516516
"blockHash": receipt.BlockHash,
517517
"blockNumber": hexutil.Uint64(receipt.BlockNumber.Uint64()),
518518
"transactionHash": txHash,

models/errors/errors.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ func (e *RevertError) ErrorCode() int {
9292
}
9393

9494
// ErrorData returns the hex encoded revert reason.
95-
func (e *RevertError) ErrorData() interface{} {
95+
func (e *RevertError) ErrorData() any {
9696
return e.Reason
9797
}
9898

models/events_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ func TestNewSingleBlockEvents(t *testing.T) {
6969
events := make([]flow.Event, 0)
7070

7171
// generate txs
72-
for i := 0; i < txCount; i++ {
72+
for i := range txCount {
7373
tx, _, txEvent, err := newTransaction(uint64(i), uint16(i))
7474
require.NoError(t, err)
7575
hashes[i] = tx.Hash()
@@ -246,7 +246,7 @@ func TestNewSingleBlockEvents(t *testing.T) {
246246

247247
// assert that EVM transactions & receipts are sorted by their
248248
// TransactionIndex field
249-
for i := 0; i < txCount; i++ {
249+
for i := range txCount {
250250
tx := cdcEvents.transactions[i]
251251
receipt := cdcEvents.receipts[i]
252252
assert.Equal(t, tx.Hash(), receipt.TxHash)
@@ -315,7 +315,7 @@ func TestNewMultiBlockEvents(t *testing.T) {
315315
evmTxEvents := make([]flow.BlockEvents, txCount)
316316

317317
// generate txs
318-
for i := 0; i < txCount; i++ {
318+
for i := range txCount {
319319
tx, _, txEvent, err := newTransaction(uint64(i), uint16(i))
320320
require.NoError(t, err)
321321
hashes[i] = tx.Hash()
@@ -338,7 +338,7 @@ func TestNewMultiBlockEvents(t *testing.T) {
338338

339339
// Below we add all the EVM transaction events, but we have omitted
340340
// the EVM.BlockExecuted event.
341-
for i := 0; i < txCount; i++ {
341+
for i := range txCount {
342342
blockEvents.Events = append(blockEvents.Events, evmTxEvents[i].Events...)
343343
}
344344

@@ -388,7 +388,7 @@ func TestNewMultiBlockEvents(t *testing.T) {
388388
}
389389

390390
// Below we add all the EVM transaction events
391-
for i := 0; i < txCount; i++ {
391+
for i := range txCount {
392392
blockEvents.Events = append(blockEvents.Events, evmTxEvents[i].Events...)
393393
}
394394

@@ -508,7 +508,7 @@ func Test_EventDecoding(t *testing.T) {
508508
}
509509

510510
// generate txs
511-
for i := 0; i < txCount; i++ {
511+
for i := range txCount {
512512
var err error
513513
txs[i], results[i], txEvents[i], err = newTransaction(uint64(i), uint16(i))
514514
require.NoError(t, err)
@@ -535,7 +535,7 @@ func Test_EventDecoding(t *testing.T) {
535535

536536
cumulative := uint64(1)
537537
logIndex := uint(0)
538-
for i := 0; i < txCount; i++ {
538+
for i := range txCount {
539539
tx := cadenceEvents.Transactions()[i]
540540
rcp := cadenceEvents.Receipts()[i]
541541
blockHash, err := block.Hash()

models/stream_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,11 @@ func Test_Stream(t *testing.T) {
7676
// and then unsubscribe all
7777
waitAllSubscribed.Add(10)
7878
waitAllUnsubscribed.Add(10)
79-
for i := 0; i < 10; i++ {
79+
for range 10 {
8080
go func() {
8181
subscriptions := make([]*mockSubscription, 10)
8282

83-
for j := 0; j < 10; j++ {
83+
for j := range 10 {
8484
s := newMockSubscription()
8585
subscriptions[j] = s
8686
p.Subscribe(s)
@@ -90,7 +90,7 @@ func Test_Stream(t *testing.T) {
9090
waitAllSubscribed.Wait()
9191

9292
// wait for all subscribers to receive data
93-
for i := 0; i < 10; i++ {
93+
for range 10 {
9494
<-published
9595
}
9696

@@ -99,7 +99,7 @@ func Test_Stream(t *testing.T) {
9999
}
100100

101101
// there should be at least 1 call
102-
for j := 0; j < 10; j++ {
102+
for j := range 10 {
103103
require.GreaterOrEqual(t, subscriptions[j].CallCount(), uint64(10))
104104
}
105105

services/ingestion/engine_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,7 @@ func TestBlockAndTransactionIngestion(t *testing.T) {
496496
eventCount := 5
497497
txHashes := make([]gethCommon.Hash, eventCount)
498498

499-
for i := 0; i < eventCount; i++ {
499+
for i := range eventCount {
500500
txCdc, txEvent, transaction, res, err := newTransaction(evmHeight)
501501
txHashes[i] = res.TxHash
502502
require.NoError(t, err)

services/ingestion/event_subscriber.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -272,10 +272,7 @@ func (r *RPCEventSubscriber) backfillSporkFromHeight(ctx context.Context, fromCa
272272
r.logger.Debug().Msg(fmt.Sprintf("backfilling [%d / %d] ...", fromCadenceHeight, lastHeight))
273273

274274
startHeight := fromCadenceHeight
275-
endHeight := fromCadenceHeight + maxRangeForGetEvents
276-
if endHeight > lastHeight {
277-
endHeight = lastHeight
278-
}
275+
endHeight := min(fromCadenceHeight+maxRangeForGetEvents, lastHeight)
279276

280277
blocks, err := r.client.GetEventsForHeightRange(ctx, blockExecutedEvent, startHeight, endHeight)
281278
if err != nil {

0 commit comments

Comments
 (0)