github.com/authcall/reference-optimistic-geth@v0.0.0-20220816224302-06313bfeb8d2/consensus/beacon/consensus_test.go (about)

     1  package beacon
     2  
     3  import (
     4  	"fmt"
     5  	"math/big"
     6  	"testing"
     7  
     8  	"github.com/ethereum/go-ethereum/common"
     9  	"github.com/ethereum/go-ethereum/consensus"
    10  	"github.com/ethereum/go-ethereum/core/types"
    11  	"github.com/ethereum/go-ethereum/params"
    12  )
    13  
    14  type mockChain struct {
    15  	config *params.ChainConfig
    16  	tds    map[uint64]*big.Int
    17  }
    18  
    19  func newMockChain() *mockChain {
    20  	return &mockChain{
    21  		config: new(params.ChainConfig),
    22  		tds:    make(map[uint64]*big.Int),
    23  	}
    24  }
    25  
    26  func (m *mockChain) Config() *params.ChainConfig {
    27  	return m.config
    28  }
    29  
    30  func (m *mockChain) CurrentHeader() *types.Header { panic("not implemented") }
    31  
    32  func (m *mockChain) GetHeader(hash common.Hash, number uint64) *types.Header {
    33  	panic("not implemented")
    34  }
    35  
    36  func (m *mockChain) GetHeaderByNumber(number uint64) *types.Header { panic("not implemented") }
    37  
    38  func (m *mockChain) GetHeaderByHash(hash common.Hash) *types.Header { panic("not implemented") }
    39  
    40  func (m *mockChain) GetTd(hash common.Hash, number uint64) *big.Int {
    41  	num, ok := m.tds[number]
    42  	if ok {
    43  		return new(big.Int).Set(num)
    44  	}
    45  	return nil
    46  }
    47  
    48  func TestVerifyTerminalBlock(t *testing.T) {
    49  	chain := newMockChain()
    50  	chain.tds[0] = big.NewInt(10)
    51  	chain.config.TerminalTotalDifficulty = big.NewInt(50)
    52  
    53  	tests := []struct {
    54  		preHeaders []*types.Header
    55  		ttd        *big.Int
    56  		err        error
    57  		index      int
    58  	}{
    59  		// valid ttd
    60  		{
    61  			preHeaders: []*types.Header{
    62  				{Number: big.NewInt(1), Difficulty: big.NewInt(10)},
    63  				{Number: big.NewInt(2), Difficulty: big.NewInt(10)},
    64  				{Number: big.NewInt(3), Difficulty: big.NewInt(10)},
    65  				{Number: big.NewInt(4), Difficulty: big.NewInt(10)},
    66  			},
    67  			ttd: big.NewInt(50),
    68  		},
    69  		// last block doesn't reach ttd
    70  		{
    71  			preHeaders: []*types.Header{
    72  				{Number: big.NewInt(1), Difficulty: big.NewInt(10)},
    73  				{Number: big.NewInt(2), Difficulty: big.NewInt(10)},
    74  				{Number: big.NewInt(3), Difficulty: big.NewInt(10)},
    75  				{Number: big.NewInt(4), Difficulty: big.NewInt(9)},
    76  			},
    77  			ttd:   big.NewInt(50),
    78  			err:   consensus.ErrInvalidTerminalBlock,
    79  			index: 3,
    80  		},
    81  		// two blocks reach ttd
    82  		{
    83  			preHeaders: []*types.Header{
    84  				{Number: big.NewInt(1), Difficulty: big.NewInt(10)},
    85  				{Number: big.NewInt(2), Difficulty: big.NewInt(10)},
    86  				{Number: big.NewInt(3), Difficulty: big.NewInt(20)},
    87  				{Number: big.NewInt(4), Difficulty: big.NewInt(10)},
    88  			},
    89  			ttd:   big.NewInt(50),
    90  			err:   consensus.ErrInvalidTerminalBlock,
    91  			index: 3,
    92  		},
    93  		// three blocks reach ttd
    94  		{
    95  			preHeaders: []*types.Header{
    96  				{Number: big.NewInt(1), Difficulty: big.NewInt(10)},
    97  				{Number: big.NewInt(2), Difficulty: big.NewInt(10)},
    98  				{Number: big.NewInt(3), Difficulty: big.NewInt(20)},
    99  				{Number: big.NewInt(4), Difficulty: big.NewInt(10)},
   100  				{Number: big.NewInt(4), Difficulty: big.NewInt(10)},
   101  			},
   102  			ttd:   big.NewInt(50),
   103  			err:   consensus.ErrInvalidTerminalBlock,
   104  			index: 3,
   105  		},
   106  		// parent reached ttd
   107  		{
   108  			preHeaders: []*types.Header{
   109  				{Number: big.NewInt(1), Difficulty: big.NewInt(10)},
   110  			},
   111  			ttd:   big.NewInt(9),
   112  			err:   consensus.ErrInvalidTerminalBlock,
   113  			index: 0,
   114  		},
   115  		// unknown parent
   116  		{
   117  			preHeaders: []*types.Header{
   118  				{Number: big.NewInt(4), Difficulty: big.NewInt(10)},
   119  			},
   120  			ttd:   big.NewInt(9),
   121  			err:   consensus.ErrUnknownAncestor,
   122  			index: 0,
   123  		},
   124  	}
   125  
   126  	for i, test := range tests {
   127  		fmt.Printf("Test: %v\n", i)
   128  		chain.config.TerminalTotalDifficulty = test.ttd
   129  		index, err := verifyTerminalPoWBlock(chain, test.preHeaders)
   130  		if err != test.err {
   131  			t.Fatalf("Invalid error encountered, expected %v got %v", test.err, err)
   132  		}
   133  		if index != test.index {
   134  			t.Fatalf("Invalid index, expected %v got %v", test.index, index)
   135  		}
   136  	}
   137  }