|
1 | 1 | package ethapi |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "context" |
4 | 5 | "math/big" |
5 | 6 | "testing" |
6 | 7 |
|
7 | 8 | "github.com/ethereum/go-ethereum/common" |
8 | 9 | "github.com/ethereum/go-ethereum/common/hexutil" |
9 | 10 | "github.com/ethereum/go-ethereum/core/types" |
10 | 11 | "github.com/ethereum/go-ethereum/crypto" |
| 12 | + "github.com/ethereum/go-ethereum/internal/blocktest" |
11 | 13 | "github.com/ethereum/go-ethereum/params" |
12 | 14 | "github.com/stretchr/testify/assert" |
13 | 15 | "github.com/stretchr/testify/require" |
@@ -531,3 +533,83 @@ func checkTxFields( |
531 | 533 | assert.Equal(t, (*hexutil.Big)(tx.GatewayFee()), rpcTx.GatewayFee) |
532 | 534 | assert.Equal(t, tx.GatewayFeeRecipient(), rpcTx.GatewayFeeRecipient) |
533 | 535 | } |
| 536 | + |
| 537 | +// Test_isCelo1Block tests isCelo1Block function to determine whether the given block time |
| 538 | +// corresponds to Celo1 chain based on the provided chain configuration |
| 539 | +func Test_isCelo1Block(t *testing.T) { |
| 540 | + cel2Time := uint64(1000) |
| 541 | + |
| 542 | + t.Run("Non-Celo", func(t *testing.T) { |
| 543 | + res := isCelo1Block(¶ms.ChainConfig{ |
| 544 | + Cel2Time: nil, |
| 545 | + }, 1000) |
| 546 | + |
| 547 | + assert.False(t, res) |
| 548 | + }) |
| 549 | + |
| 550 | + t.Run("Celo1", func(t *testing.T) { |
| 551 | + res := isCelo1Block(¶ms.ChainConfig{ |
| 552 | + Cel2Time: &cel2Time, |
| 553 | + }, 500) |
| 554 | + |
| 555 | + assert.True(t, res) |
| 556 | + }) |
| 557 | + |
| 558 | + t.Run("Celo2", func(t *testing.T) { |
| 559 | + res := isCelo1Block(¶ms.ChainConfig{ |
| 560 | + Cel2Time: &cel2Time, |
| 561 | + }, 1000) |
| 562 | + |
| 563 | + assert.False(t, res) |
| 564 | + }) |
| 565 | +} |
| 566 | + |
| 567 | +// TestRPCMarshalBlock_Celo1TotalDifficulty tests the RPCMarshalBlock function, specifically for totalDifficulty field |
| 568 | +// It validates the result has `totalDifficulty` field only if it's Celo1 block |
| 569 | +func TestRPCMarshalBlock_Celo1TotalDifficulty(t *testing.T) { |
| 570 | + t.Parallel() |
| 571 | + |
| 572 | + blockTime := uint64(1000) |
| 573 | + block := types.NewBlock(&types.Header{Number: big.NewInt(100), Time: blockTime}, &types.Body{Transactions: []*types.Transaction{}}, nil, blocktest.NewHasher(), types.DefaultBlockConfig) |
| 574 | + |
| 575 | + marshalBlock := func(t *testing.T, config *params.ChainConfig) map[string]interface{} { |
| 576 | + t.Helper() |
| 577 | + |
| 578 | + resp, err := RPCMarshalBlock(context.Background(), block, false, false, config, testBackend{}) |
| 579 | + if err != nil { |
| 580 | + require.NoError(t, err) |
| 581 | + } |
| 582 | + |
| 583 | + return resp |
| 584 | + } |
| 585 | + |
| 586 | + t.Run("Non-Celo", func(t *testing.T) { |
| 587 | + config := *params.MainnetChainConfig |
| 588 | + |
| 589 | + res := marshalBlock(t, &config) |
| 590 | + |
| 591 | + assert.Equal(t, nil, res["totalDifficulty"]) |
| 592 | + }) |
| 593 | + |
| 594 | + t.Run("Celo1", func(t *testing.T) { |
| 595 | + expected := (*hexutil.Big)(new(big.Int).Add(block.Number(), common.Big1)) |
| 596 | + |
| 597 | + cel2Time := blockTime + 500 |
| 598 | + config := *params.MainnetChainConfig |
| 599 | + config.Cel2Time = &cel2Time |
| 600 | + |
| 601 | + res := marshalBlock(t, &config) |
| 602 | + |
| 603 | + assert.Equal(t, expected, res["totalDifficulty"]) |
| 604 | + }) |
| 605 | + |
| 606 | + t.Run("Celo2", func(t *testing.T) { |
| 607 | + cel2Time := blockTime - 500 |
| 608 | + config := *params.MainnetChainConfig |
| 609 | + config.Cel2Time = &cel2Time |
| 610 | + |
| 611 | + res := marshalBlock(t, &config) |
| 612 | + |
| 613 | + assert.Equal(t, nil, res["totalDifficulty"]) |
| 614 | + }) |
| 615 | +} |
0 commit comments