github.com/prysmaticlabs/prysm@v1.4.4/shared/testutil/deposits_test.go (about)

     1  package testutil
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/hex"
     6  	"testing"
     7  
     8  	"github.com/prysmaticlabs/prysm/beacon-chain/core/helpers"
     9  	"github.com/prysmaticlabs/prysm/shared/params"
    10  	"github.com/prysmaticlabs/prysm/shared/testutil/require"
    11  	"google.golang.org/protobuf/proto"
    12  )
    13  
    14  func TestSetupInitialDeposits_1024Entries(t *testing.T) {
    15  	entries := 1
    16  	resetCache()
    17  	deposits, privKeys, err := DeterministicDepositsAndKeys(uint64(entries))
    18  	require.NoError(t, err)
    19  	_, depositDataRoots, err := DeterministicDepositTrie(len(deposits))
    20  	require.NoError(t, err)
    21  
    22  	if len(deposits) != entries {
    23  		t.Fatalf("incorrect number of deposits returned, wanted %d but received %d", entries, len(deposits))
    24  	}
    25  	if len(privKeys) != entries {
    26  		t.Fatalf("incorrect number of private keys returned, wanted %d but received %d", entries, len(privKeys))
    27  	}
    28  	expectedPublicKeyAt0 := []byte{0xa9, 0x9a, 0x76, 0xed, 0x77, 0x96, 0xf7, 0xbe, 0x22, 0xd5, 0xb7, 0xe8, 0x5d, 0xee, 0xb7, 0xc5, 0x67, 0x7e, 0x88, 0xe5, 0x11, 0xe0, 0xb3, 0x37, 0x61, 0x8f, 0x8c, 0x4e, 0xb6, 0x13, 0x49, 0xb4, 0xbf, 0x2d, 0x15, 0x3f, 0x64, 0x9f, 0x7b, 0x53, 0x35, 0x9f, 0xe8, 0xb9, 0x4a, 0x38, 0xe4, 0x4c}
    29  	if !bytes.Equal(deposits[0].Data.PublicKey, expectedPublicKeyAt0) {
    30  		t.Fatalf("incorrect public key, wanted %x but received %x", expectedPublicKeyAt0, deposits[0].Data.PublicKey)
    31  	}
    32  	expectedWithdrawalCredentialsAt0 := []byte{0x00, 0xec, 0x7e, 0xf7, 0x78, 0x0c, 0x9d, 0x15, 0x15, 0x97, 0x92, 0x40, 0x36, 0x26, 0x2d, 0xd2, 0x8d, 0xc6, 0x0e, 0x12, 0x28, 0xf4, 0xda, 0x6f, 0xec, 0xf9, 0xd4, 0x02, 0xcb, 0x3f, 0x35, 0x94}
    33  	if !bytes.Equal(deposits[0].Data.WithdrawalCredentials, expectedWithdrawalCredentialsAt0) {
    34  		t.Fatalf("incorrect withdrawal credentials, wanted %x but received %x", expectedWithdrawalCredentialsAt0, deposits[0].Data.WithdrawalCredentials)
    35  	}
    36  
    37  	dRootAt0 := []byte("4bbc31cfec9602242576e8570b3c72cd09f55e0d5ea4d64fd08fb6ca5cb69f17")
    38  	dRootAt0B := make([]byte, hex.DecodedLen(len(dRootAt0)))
    39  	_, err = hex.Decode(dRootAt0B, dRootAt0)
    40  	require.NoError(t, err)
    41  	if !bytes.Equal(depositDataRoots[0][:], dRootAt0B) {
    42  		t.Fatalf("incorrect deposit data root, wanted %x but received %x", dRootAt0B, depositDataRoots[0])
    43  	}
    44  
    45  	sigAt0 := []byte("953b44ee497f9fc9abbc1340212597c264b77f3dea441921d65b2542d64195171ba0598fad34905f03c0c1b6d5540faa10bb2c26084fc5eacbafba119d9a81721f56821cae7044a2ff374e9a128f68dee68d3b48406ea60306148498ffe007c7")
    46  	sigAt0B := make([]byte, hex.DecodedLen(len(sigAt0)))
    47  	_, err = hex.Decode(sigAt0B, sigAt0)
    48  	require.NoError(t, err)
    49  	if !bytes.Equal(deposits[0].Data.Signature, sigAt0B) {
    50  		t.Fatalf("incorrect signature, wanted %x but received %x", sigAt0B, deposits[0].Data.Signature)
    51  	}
    52  
    53  	entries = 1024
    54  	resetCache()
    55  	deposits, privKeys, err = DeterministicDepositsAndKeys(uint64(entries))
    56  	require.NoError(t, err)
    57  	_, depositDataRoots, err = DeterministicDepositTrie(len(deposits))
    58  	require.NoError(t, err)
    59  	if len(deposits) != entries {
    60  		t.Fatalf("incorrect number of deposits returned, wanted %d but received %d", entries, len(deposits))
    61  	}
    62  	if len(privKeys) != entries {
    63  		t.Fatalf("incorrect number of private keys returned, wanted %d but received %d", entries, len(privKeys))
    64  	}
    65  	// Ensure 0  has not changed
    66  	if !bytes.Equal(deposits[0].Data.PublicKey, expectedPublicKeyAt0) {
    67  		t.Fatalf("incorrect public key, wanted %x but received %x", expectedPublicKeyAt0, deposits[0].Data.PublicKey)
    68  	}
    69  	if !bytes.Equal(deposits[0].Data.WithdrawalCredentials, expectedWithdrawalCredentialsAt0) {
    70  		t.Fatalf("incorrect withdrawal credentials, wanted %x but received %x", expectedWithdrawalCredentialsAt0, deposits[0].Data.WithdrawalCredentials)
    71  	}
    72  	if !bytes.Equal(depositDataRoots[0][:], dRootAt0B) {
    73  		t.Fatalf("incorrect deposit data root, wanted %x but received %x", dRootAt0B, depositDataRoots[0])
    74  	}
    75  	if !bytes.Equal(deposits[0].Data.Signature, sigAt0B) {
    76  		t.Fatalf("incorrect signature, wanted %x but received %x", sigAt0B, deposits[0].Data.Signature)
    77  	}
    78  	expectedPublicKeyAt1023 := []byte{0x81, 0x2b, 0x93, 0x5e, 0xc8, 0x4b, 0x0e, 0x9a, 0x83, 0x95, 0x55, 0xaf, 0x33, 0x60, 0xca, 0xfb, 0x83, 0x1b, 0xd6, 0x12, 0xcf, 0xa2, 0x2e, 0x25, 0xea, 0xb0, 0x3c, 0xf5, 0xfd, 0xb0, 0x2a, 0xf5, 0x2b, 0xa4, 0x01, 0x7a, 0xee, 0xa8, 0x8a, 0x2f, 0x62, 0x2c, 0x78, 0x6e, 0x7f, 0x47, 0x6f, 0x4b}
    79  	if !bytes.Equal(deposits[1023].Data.PublicKey, expectedPublicKeyAt1023) {
    80  		t.Fatalf("incorrect public key, wanted %x but received %x", expectedPublicKeyAt1023, deposits[1023].Data.PublicKey)
    81  	}
    82  	expectedWithdrawalCredentialsAt1023 := []byte{0x00, 0x23, 0xd5, 0x76, 0xbc, 0x6c, 0x15, 0xdb, 0xc4, 0x34, 0x70, 0x1f, 0x3f, 0x41, 0xfd, 0x3e, 0x67, 0x59, 0xd2, 0xea, 0x7c, 0xdc, 0x64, 0x71, 0x0e, 0xe2, 0x8d, 0xde, 0xf7, 0xd2, 0xda, 0x28}
    83  	if !bytes.Equal(deposits[1023].Data.WithdrawalCredentials, expectedWithdrawalCredentialsAt1023) {
    84  		t.Fatalf("incorrect withdrawal credentials, wanted %x but received %x", expectedWithdrawalCredentialsAt1023, deposits[1023].Data.WithdrawalCredentials)
    85  	}
    86  	dRootAt1023 := []byte("564c1afed12430965ae8ff6f519a6cb15118c438328024d16c02ffe3d4652893")
    87  	dRootAt1023B := make([]byte, hex.DecodedLen(len(dRootAt1023)))
    88  	_, err = hex.Decode(dRootAt1023B, dRootAt1023)
    89  	require.NoError(t, err)
    90  	if !bytes.Equal(depositDataRoots[1023][:], dRootAt1023B) {
    91  		t.Fatalf("incorrect deposit data root, wanted %x but received %x", dRootAt1023B, depositDataRoots[1023])
    92  	}
    93  	sigAt1023 := []byte("8482cc981976291d19c1d7d298f5e6781ac691151833b89a29ef4d08850f56b972b860ebf7995ada3213b575213c331316c213a8535cf88bff0e98846204b0db186ff84c55903f1c359470be7c1110c94d5aafeef07f4886ed69cb13cb3aadbc")
    94  	sigAt1023B := make([]byte, hex.DecodedLen(len(sigAt1023)))
    95  	_, err = hex.Decode(sigAt1023B, sigAt1023)
    96  	require.NoError(t, err)
    97  	if !bytes.Equal(deposits[1023].Data.Signature, sigAt1023B) {
    98  		t.Fatalf("incorrect signature, wanted %x but received %x", sigAt1023B, deposits[1023].Data.Signature)
    99  	}
   100  }
   101  
   102  func TestDepositsWithBalance_MatchesDeterministic(t *testing.T) {
   103  	entries := 64
   104  	resetCache()
   105  	balances := make([]uint64, entries)
   106  	for i := 0; i < entries; i++ {
   107  		balances[i] = params.BeaconConfig().MaxEffectiveBalance
   108  	}
   109  	deposits, depositTrie, err := DepositsWithBalance(balances)
   110  	require.NoError(t, err)
   111  	_, depositDataRoots, err := DepositTrieSubset(depositTrie, entries)
   112  	require.NoError(t, err)
   113  
   114  	determDeposits, _, err := DeterministicDepositsAndKeys(uint64(entries))
   115  	require.NoError(t, err)
   116  	_, determDepositDataRoots, err := DeterministicDepositTrie(entries)
   117  	require.NoError(t, err)
   118  
   119  	for i := 0; i < entries; i++ {
   120  		if !proto.Equal(deposits[i], determDeposits[i]) {
   121  			t.Errorf("Expected deposit %d to match", i)
   122  		}
   123  		if !bytes.Equal(depositDataRoots[i][:], determDepositDataRoots[i][:]) {
   124  			t.Errorf("Expected deposit root %d to match", i)
   125  		}
   126  	}
   127  }
   128  
   129  func TestDepositsWithBalance_MatchesDeterministic_Cached(t *testing.T) {
   130  	entries := 32
   131  	resetCache()
   132  	// Cache half of the deposit cache.
   133  	_, _, err := DeterministicDepositsAndKeys(uint64(entries))
   134  	require.NoError(t, err)
   135  	_, _, err = DeterministicDepositTrie(entries)
   136  	require.NoError(t, err)
   137  
   138  	// Generate balanced deposits with half cache.
   139  	entries = 64
   140  	balances := make([]uint64, entries)
   141  	for i := 0; i < entries; i++ {
   142  		balances[i] = params.BeaconConfig().MaxEffectiveBalance
   143  	}
   144  	deposits, depositTrie, err := DepositsWithBalance(balances)
   145  	require.NoError(t, err)
   146  	_, depositDataRoots, err := DepositTrieSubset(depositTrie, entries)
   147  	require.NoError(t, err)
   148  
   149  	// Get 64 standard deposits.
   150  	determDeposits, _, err := DeterministicDepositsAndKeys(uint64(entries))
   151  	require.NoError(t, err)
   152  	_, determDepositDataRoots, err := DeterministicDepositTrie(entries)
   153  	require.NoError(t, err)
   154  
   155  	for i := 0; i < entries; i++ {
   156  		if !proto.Equal(deposits[i], determDeposits[i]) {
   157  			t.Errorf("Expected deposit %d to match", i)
   158  		}
   159  		if !bytes.Equal(depositDataRoots[i][:], determDepositDataRoots[i][:]) {
   160  			t.Errorf("Expected deposit root %d to match", i)
   161  		}
   162  	}
   163  }
   164  
   165  func TestSetupInitialDeposits_1024Entries_PartialDeposits(t *testing.T) {
   166  	entries := 1
   167  	resetCache()
   168  	balances := make([]uint64, entries)
   169  	for i := 0; i < entries; i++ {
   170  		balances[i] = params.BeaconConfig().MaxEffectiveBalance / 2
   171  	}
   172  	deposits, depositTrie, err := DepositsWithBalance(balances)
   173  	require.NoError(t, err)
   174  	_, depositDataRoots, err := DepositTrieSubset(depositTrie, entries)
   175  	require.NoError(t, err)
   176  
   177  	if len(deposits) != entries {
   178  		t.Fatalf("incorrect number of deposits returned, wanted %d but received %d", entries, len(deposits))
   179  	}
   180  	expectedPublicKeyAt0 := []byte{0xa9, 0x9a, 0x76, 0xed, 0x77, 0x96, 0xf7, 0xbe, 0x22, 0xd5, 0xb7, 0xe8, 0x5d, 0xee, 0xb7, 0xc5, 0x67, 0x7e, 0x88, 0xe5, 0x11, 0xe0, 0xb3, 0x37, 0x61, 0x8f, 0x8c, 0x4e, 0xb6, 0x13, 0x49, 0xb4, 0xbf, 0x2d, 0x15, 0x3f, 0x64, 0x9f, 0x7b, 0x53, 0x35, 0x9f, 0xe8, 0xb9, 0x4a, 0x38, 0xe4, 0x4c}
   181  	if !bytes.Equal(deposits[0].Data.PublicKey, expectedPublicKeyAt0) {
   182  		t.Fatalf("incorrect public key, wanted %x but received %x", expectedPublicKeyAt0, deposits[0].Data.PublicKey)
   183  	}
   184  	expectedWithdrawalCredentialsAt0 := []byte{0x00, 0xec, 0x7e, 0xf7, 0x78, 0x0c, 0x9d, 0x15, 0x15, 0x97, 0x92, 0x40, 0x36, 0x26, 0x2d, 0xd2, 0x8d, 0xc6, 0x0e, 0x12, 0x28, 0xf4, 0xda, 0x6f, 0xec, 0xf9, 0xd4, 0x02, 0xcb, 0x3f, 0x35, 0x94}
   185  	if !bytes.Equal(deposits[0].Data.WithdrawalCredentials, expectedWithdrawalCredentialsAt0) {
   186  		t.Fatalf("incorrect withdrawal credentials, wanted %x but received %x", expectedWithdrawalCredentialsAt0, deposits[0].Data.WithdrawalCredentials)
   187  	}
   188  	dRootAt0 := []byte("0d3a77b90f83b44d16d0ebbb8b9af4baa048de31b18ef78f9ef9a2250ab91762")
   189  	dRootAt0B := make([]byte, hex.DecodedLen(len(dRootAt0)))
   190  	_, err = hex.Decode(dRootAt0B, dRootAt0)
   191  	require.NoError(t, err)
   192  	if !bytes.Equal(depositDataRoots[0][:], dRootAt0B) {
   193  		t.Fatalf("incorrect deposit data root, wanted %#x but received %#x", dRootAt0B, depositDataRoots[0])
   194  	}
   195  
   196  	sigAt0 := []byte("a32b88e4821fcf0e5ff52a023db91be0e67bb30a5d6e6e7ffd252edc8518290b7d9de71108e8733b91946e489284757b023a3e0125adf34558e63e6e6dc757407b7f2e9fe163a2e65ab1e8ed41309a528aa2d935d405506cb9bc2f6dce62059b")
   197  	sigAt0B := make([]byte, hex.DecodedLen(len(sigAt0)))
   198  	_, err = hex.Decode(sigAt0B, sigAt0)
   199  	require.NoError(t, err)
   200  	if !bytes.Equal(deposits[0].Data.Signature, sigAt0B) {
   201  		t.Fatalf("incorrect signature, wanted %#x but received %#x", sigAt0B, deposits[0].Data.Signature)
   202  	}
   203  
   204  	entries = 1024
   205  	resetCache()
   206  	balances = make([]uint64, entries)
   207  	for i := 0; i < entries; i++ {
   208  		balances[i] = params.BeaconConfig().MaxEffectiveBalance / 2
   209  	}
   210  	deposits, depositTrie, err = DepositsWithBalance(balances)
   211  	require.NoError(t, err)
   212  	_, depositDataRoots, err = DepositTrieSubset(depositTrie, entries)
   213  	require.NoError(t, err)
   214  	if len(deposits) != entries {
   215  		t.Fatalf("incorrect number of deposits returned, wanted %d but received %d", entries, len(deposits))
   216  	}
   217  	// Ensure 0  has not changed
   218  	if !bytes.Equal(deposits[0].Data.PublicKey, expectedPublicKeyAt0) {
   219  		t.Fatalf("incorrect public key, wanted %x but received %x", expectedPublicKeyAt0, deposits[0].Data.PublicKey)
   220  	}
   221  	if !bytes.Equal(deposits[0].Data.WithdrawalCredentials, expectedWithdrawalCredentialsAt0) {
   222  		t.Fatalf("incorrect withdrawal credentials, wanted %x but received %x", expectedWithdrawalCredentialsAt0, deposits[0].Data.WithdrawalCredentials)
   223  	}
   224  	if !bytes.Equal(depositDataRoots[0][:], dRootAt0B) {
   225  		t.Fatalf("incorrect deposit data root, wanted %x but received %x", dRootAt0B, depositDataRoots[0])
   226  	}
   227  	if !bytes.Equal(deposits[0].Data.Signature, sigAt0B) {
   228  		t.Fatalf("incorrect signature, wanted %x but received %x", sigAt0B, deposits[0].Data.Signature)
   229  	}
   230  	expectedPublicKeyAt1023 := []byte{0x81, 0x2b, 0x93, 0x5e, 0xc8, 0x4b, 0x0e, 0x9a, 0x83, 0x95, 0x55, 0xaf, 0x33, 0x60, 0xca, 0xfb, 0x83, 0x1b, 0xd6, 0x12, 0xcf, 0xa2, 0x2e, 0x25, 0xea, 0xb0, 0x3c, 0xf5, 0xfd, 0xb0, 0x2a, 0xf5, 0x2b, 0xa4, 0x01, 0x7a, 0xee, 0xa8, 0x8a, 0x2f, 0x62, 0x2c, 0x78, 0x6e, 0x7f, 0x47, 0x6f, 0x4b}
   231  	if !bytes.Equal(deposits[1023].Data.PublicKey, expectedPublicKeyAt1023) {
   232  		t.Fatalf("incorrect public key, wanted %x but received %x", expectedPublicKeyAt1023, deposits[1023].Data.PublicKey)
   233  	}
   234  	expectedWithdrawalCredentialsAt1023 := []byte{0x00, 0x23, 0xd5, 0x76, 0xbc, 0x6c, 0x15, 0xdb, 0xc4, 0x34, 0x70, 0x1f, 0x3f, 0x41, 0xfd, 0x3e, 0x67, 0x59, 0xd2, 0xea, 0x7c, 0xdc, 0x64, 0x71, 0x0e, 0xe2, 0x8d, 0xde, 0xf7, 0xd2, 0xda, 0x28}
   235  	if !bytes.Equal(deposits[1023].Data.WithdrawalCredentials, expectedWithdrawalCredentialsAt1023) {
   236  		t.Fatalf("incorrect withdrawal credentials, wanted %x but received %x", expectedWithdrawalCredentialsAt1023, deposits[1023].Data.WithdrawalCredentials)
   237  	}
   238  	dRootAt1023 := []byte("5888e3a132ccaed752da8bc5430eddc5ae60b139ccb39f4459d1d5d6ed12c3c1")
   239  	dRootAt1023B := make([]byte, hex.DecodedLen(len(dRootAt1023)))
   240  	_, err = hex.Decode(dRootAt1023B, dRootAt1023)
   241  	require.NoError(t, err)
   242  	if !bytes.Equal(depositDataRoots[1023][:], dRootAt1023B) {
   243  		t.Fatalf("incorrect deposit data root, wanted %#x but received %#x", dRootAt1023B, depositDataRoots[1023])
   244  	}
   245  	sigAt1023 := []byte("a99cb9d7f8cca3a9407e48615ba265cf04c2bdbd630721367a54adb0d4b426cf78b5e13ad8ec21c6b3c59ac0316f2f2d15f308a12cbd71353272f0fe3c3c7d115b26cdb2cf08c22ace29c318419c9b6e62c163e640a4d0f4c4fb0f216b207980")
   246  	sigAt1023B := make([]byte, hex.DecodedLen(len(sigAt1023)))
   247  	_, err = hex.Decode(sigAt1023B, sigAt1023)
   248  	require.NoError(t, err)
   249  	if !bytes.Equal(deposits[1023].Data.Signature, sigAt1023B) {
   250  		t.Fatalf("incorrect signature, wanted %#x but received %#x", sigAt1023B, deposits[1023].Data.Signature)
   251  	}
   252  }
   253  
   254  func TestDeterministicGenesisState_100Validators(t *testing.T) {
   255  	validatorCount := uint64(100)
   256  	beaconState, privKeys := DeterministicGenesisState(t, validatorCount)
   257  	activeValidators, err := helpers.ActiveValidatorCount(beaconState, 0)
   258  	require.NoError(t, err)
   259  
   260  	if len(privKeys) != int(validatorCount) {
   261  		t.Fatalf("expected amount of private keys %d to match requested amount of validators %d", len(privKeys), validatorCount)
   262  	}
   263  	if activeValidators != validatorCount {
   264  		t.Fatalf("expected validators in state %d to match requested amount %d", activeValidators, validatorCount)
   265  	}
   266  }
   267  
   268  func TestDepositTrieFromDeposits(t *testing.T) {
   269  	deposits, _, err := DeterministicDepositsAndKeys(100)
   270  	require.NoError(t, err)
   271  	eth1Data, err := DeterministicEth1Data(len(deposits))
   272  	require.NoError(t, err)
   273  
   274  	depositTrie, _, err := DepositTrieFromDeposits(deposits)
   275  	require.NoError(t, err)
   276  
   277  	root := depositTrie.Root()
   278  	if !bytes.Equal(root[:], eth1Data.DepositRoot) {
   279  		t.Fatal("expected deposit trie root to equal eth1data deposit root")
   280  	}
   281  }