github.com/prysmaticlabs/prysm@v1.4.4/beacon-chain/forkchoice/protoarray/no_vote_test.go (about)

     1  package protoarray
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"github.com/prysmaticlabs/prysm/shared/params"
     8  	"github.com/prysmaticlabs/prysm/shared/testutil/assert"
     9  	"github.com/prysmaticlabs/prysm/shared/testutil/require"
    10  )
    11  
    12  func TestNoVote_CanFindHead(t *testing.T) {
    13  	balances := make([]uint64, 16)
    14  	f := setup(1, 1)
    15  
    16  	// The head should always start at the finalized block.
    17  	r, err := f.Head(context.Background(), 1, params.BeaconConfig().ZeroHash, balances, 1)
    18  	require.NoError(t, err)
    19  	if r != params.BeaconConfig().ZeroHash {
    20  		t.Errorf("Incorrect head with genesis")
    21  	}
    22  
    23  	// Insert block 2 into the tree and verify head is at 2:
    24  	//         0
    25  	//        /
    26  	//       2 <- head
    27  	require.NoError(t, f.ProcessBlock(context.Background(), 0, indexToHash(2), params.BeaconConfig().ZeroHash, [32]byte{}, 1, 1))
    28  	r, err = f.Head(context.Background(), 1, params.BeaconConfig().ZeroHash, balances, 1)
    29  	require.NoError(t, err)
    30  	assert.Equal(t, indexToHash(2), r, "Incorrect head for with justified epoch at 1")
    31  
    32  	// Insert block 1 into the tree and verify head is still at 2:
    33  	//            0
    34  	//           / \
    35  	//  head -> 2  1
    36  	require.NoError(t, f.ProcessBlock(context.Background(), 0, indexToHash(1), params.BeaconConfig().ZeroHash, [32]byte{}, 1, 1))
    37  	r, err = f.Head(context.Background(), 1, params.BeaconConfig().ZeroHash, balances, 1)
    38  	require.NoError(t, err)
    39  	assert.Equal(t, indexToHash(2), r, "Incorrect head for with justified epoch at 1")
    40  
    41  	// Insert block 3 into the tree and verify head is still at 2:
    42  	//            0
    43  	//           / \
    44  	//  head -> 2  1
    45  	//             |
    46  	//             3
    47  	require.NoError(t, f.ProcessBlock(context.Background(), 0, indexToHash(3), indexToHash(1), [32]byte{}, 1, 1))
    48  	r, err = f.Head(context.Background(), 1, params.BeaconConfig().ZeroHash, balances, 1)
    49  	require.NoError(t, err)
    50  	assert.Equal(t, indexToHash(2), r, "Incorrect head for with justified epoch at 1")
    51  
    52  	// Insert block 4 into the tree and verify head is at 4:
    53  	//            0
    54  	//           / \
    55  	//          2  1
    56  	//          |  |
    57  	//  head -> 4  3
    58  	require.NoError(t, f.ProcessBlock(context.Background(), 0, indexToHash(4), indexToHash(2), [32]byte{}, 1, 1))
    59  	r, err = f.Head(context.Background(), 1, params.BeaconConfig().ZeroHash, balances, 1)
    60  	require.NoError(t, err)
    61  	assert.Equal(t, indexToHash(4), r, "Incorrect head for with justified epoch at 1")
    62  
    63  	// Insert block 5 with justified epoch of 2, verify head is still at 4.
    64  	//            0
    65  	//           / \
    66  	//          2  1
    67  	//          |  |
    68  	//  head -> 4  3
    69  	//          |
    70  	//          5 <- justified epoch = 2
    71  	require.NoError(t, f.ProcessBlock(context.Background(), 0, indexToHash(5), indexToHash(4), [32]byte{}, 2, 1))
    72  	r, err = f.Head(context.Background(), 1, params.BeaconConfig().ZeroHash, balances, 1)
    73  	require.NoError(t, err)
    74  	assert.Equal(t, indexToHash(4), r, "Incorrect head for with justified epoch at 1")
    75  
    76  	// Verify there's an error when starting from a block with wrong justified epoch.
    77  	//            0
    78  	//           / \
    79  	//          2  1
    80  	//          |  |
    81  	//  head -> 4  3
    82  	//          |
    83  	//          5 <- starting from 5 with justified epoch 0 should error
    84  	_, err = f.Head(context.Background(), 1, indexToHash(5), balances, 1)
    85  	wanted := "head at slot 0 with weight 0 is not eligible, finalizedEpoch 1 != 1, justifiedEpoch 2 != 1"
    86  	require.ErrorContains(t, wanted, err)
    87  
    88  	// Set the justified epoch to 2 and start block to 5 to verify head is 5.
    89  	//            0
    90  	//           / \
    91  	//          2  1
    92  	//          |  |
    93  	//          4  3
    94  	//          |
    95  	//          5 <- head
    96  	r, err = f.Head(context.Background(), 2, indexToHash(5), balances, 1)
    97  	require.NoError(t, err)
    98  	assert.Equal(t, indexToHash(5), r, "Incorrect head for with justified epoch at 2")
    99  
   100  	// Insert block 6 with justified epoch of 2, verify head is at 6.
   101  	//            0
   102  	//           / \
   103  	//          2  1
   104  	//          |  |
   105  	//          4  3
   106  	//          |
   107  	//          5
   108  	//          |
   109  	//          6 <- head
   110  	require.NoError(t, f.ProcessBlock(context.Background(), 0, indexToHash(6), indexToHash(5), [32]byte{}, 2, 1))
   111  	r, err = f.Head(context.Background(), 2, indexToHash(5), balances, 1)
   112  	require.NoError(t, err)
   113  	assert.Equal(t, indexToHash(6), r, "Incorrect head for with justified epoch at 2")
   114  }