github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/ledger/complete/wal/checkpointer_versioning_test.go (about)

     1  package wal
     2  
     3  import (
     4  	"encoding/hex"
     5  	"testing"
     6  
     7  	"github.com/rs/zerolog"
     8  	"github.com/stretchr/testify/require"
     9  
    10  	"github.com/onflow/flow-go/ledger"
    11  )
    12  
    13  func TestLoadCheckpointV1(t *testing.T) {
    14  
    15  	expectedRootHash := [4]ledger.RootHash{
    16  		mustToHash("568f4ec740fe3b5de88034cb7b1fbddb41548b068f31aebc8ae9189e429c5749"), // empty trie root hash
    17  		mustToHash("f53f9696b85b7428227f1b39f40b2ce07c175f58dea2b86cb6f84dc7c9fbeabd"),
    18  		mustToHash("7ac8daf34733cce3d5d03b5a1afde33a572249f81c45da91106412e94661e109"),
    19  		mustToHash("63df641430e5e0745c3d99ece6ac209467ccfdb77e362e7490a830db8e8803ae"),
    20  	}
    21  
    22  	logger := zerolog.Nop()
    23  	tries, err := LoadCheckpoint("test_data/checkpoint.v1", logger)
    24  	require.NoError(t, err)
    25  	require.Equal(t, len(expectedRootHash), len(tries))
    26  
    27  	for i, trie := range tries {
    28  		require.Equal(t, expectedRootHash[i], trie.RootHash())
    29  		require.True(t, trie.RootNode().VerifyCachedHash())
    30  	}
    31  }
    32  
    33  func TestLoadCheckpointV3(t *testing.T) {
    34  
    35  	expectedRootHash := [4]ledger.RootHash{
    36  		mustToHash("568f4ec740fe3b5de88034cb7b1fbddb41548b068f31aebc8ae9189e429c5749"), // empty trie root hash
    37  		mustToHash("f53f9696b85b7428227f1b39f40b2ce07c175f58dea2b86cb6f84dc7c9fbeabd"),
    38  		mustToHash("7ac8daf34733cce3d5d03b5a1afde33a572249f81c45da91106412e94661e109"),
    39  		mustToHash("63df641430e5e0745c3d99ece6ac209467ccfdb77e362e7490a830db8e8803ae"),
    40  	}
    41  
    42  	logger := zerolog.Nop()
    43  	tries, err := LoadCheckpoint("test_data/checkpoint.v3", logger)
    44  	require.NoError(t, err)
    45  	require.Equal(t, len(expectedRootHash), len(tries))
    46  
    47  	for i, trie := range tries {
    48  		require.Equal(t, expectedRootHash[i], trie.RootHash())
    49  		require.True(t, trie.RootNode().VerifyCachedHash())
    50  	}
    51  }
    52  
    53  func TestLoadCheckpointV4(t *testing.T) {
    54  
    55  	expectedRootHash := [4]ledger.RootHash{
    56  		mustToHash("568f4ec740fe3b5de88034cb7b1fbddb41548b068f31aebc8ae9189e429c5749"), // empty trie root hash
    57  		mustToHash("f53f9696b85b7428227f1b39f40b2ce07c175f58dea2b86cb6f84dc7c9fbeabd"),
    58  		mustToHash("7ac8daf34733cce3d5d03b5a1afde33a572249f81c45da91106412e94661e109"),
    59  		mustToHash("63df641430e5e0745c3d99ece6ac209467ccfdb77e362e7490a830db8e8803ae"),
    60  	}
    61  
    62  	logger := zerolog.Nop()
    63  	tries, err := LoadCheckpoint("test_data/checkpoint.v4", logger)
    64  	require.NoError(t, err)
    65  	require.Equal(t, len(expectedRootHash), len(tries))
    66  
    67  	for i, trie := range tries {
    68  		require.Equal(t, expectedRootHash[i], trie.RootHash())
    69  		require.True(t, trie.RootNode().VerifyCachedHash())
    70  	}
    71  }
    72  
    73  func mustToHash(s string) ledger.RootHash {
    74  	b, err := hex.DecodeString(s)
    75  	if err != nil {
    76  		panic(err)
    77  	}
    78  	h, err := ledger.ToRootHash(b)
    79  	if err != nil {
    80  		panic(err)
    81  	}
    82  	return h
    83  }
    84  
    85  /*
    86  // CreateCheckpointV3 is used to create checkpoint.v3 test file used by Test_LoadingV3Checkpoint.
    87  func CreateCheckpointV3() {
    88  
    89  	f, err := mtrie.NewForest(size*10, metricsCollector, func(tree *trie.MTrie) error { return nil })
    90  	require.NoError(t, err)
    91  
    92  	emptyTrie := trie.NewEmptyMTrie()
    93  
    94  	// key: 0000...
    95  	p1 := utils.PathByUint8(1)
    96  	v1 := utils.LightPayload8('A', 'a')
    97  
    98  	// key: 0100....
    99  	p2 := utils.PathByUint8(64)
   100  	v2 := utils.LightPayload8('B', 'b')
   101  
   102  	paths := []ledger.Path{p1, p2}
   103  	payloads := []ledger.Payload{*v1, *v2}
   104  
   105  	trie1, err := trie.NewTrieWithUpdatedRegisters(emptyTrie, paths, payloads, true)
   106  	require.NoError(t, err)
   107  
   108  	// trie1
   109  	//              n4
   110  	//             /
   111  	//            /
   112  	//          n3
   113  	//        /     \
   114  	//      /         \
   115  	//   n1 (p1/v1)     n2 (p2/v2)
   116  	//
   117  
   118  	f.AddTrie(trie1)
   119  
   120  	// New trie reuses its parent's left sub-trie.
   121  
   122  	// key: 1000...
   123  	p3 := utils.PathByUint8(128)
   124  	v3 := utils.LightPayload8('C', 'c')
   125  
   126  	// key: 1100....
   127  	p4 := utils.PathByUint8(192)
   128  	v4 := utils.LightPayload8('D', 'd')
   129  
   130  	paths = []ledger.Path{p3, p4}
   131  	payloads = []ledger.Payload{*v3, *v4}
   132  
   133  	trie2, err := trie.NewTrieWithUpdatedRegisters(trie1, paths, payloads, true)
   134  	require.NoError(t, err)
   135  
   136  	// trie2
   137  	//              n8
   138  	//             /   \
   139  	//            /      \
   140  	//          n3       n7
   141  	//       (shared)   /   \
   142  	//                /       \
   143  	//              n5         n6
   144  	//            (p3/v3)    (p4/v4)
   145  
   146  	f.AddTrie(trie2)
   147  
   148  	// New trie reuses its parent's right sub-trie, and left sub-trie's leaf node.
   149  
   150  	// key: 0000...
   151  	v5 := utils.LightPayload8('E', 'e')
   152  
   153  	paths = []ledger.Path{p1}
   154  	payloads = []ledger.Payload{*v5}
   155  
   156  	trie3, err := trie.NewTrieWithUpdatedRegisters(trie2, paths, payloads, true)
   157  	require.NoError(t, err)
   158  
   159  	// trie3
   160  	//              n11
   161  	//             /   \
   162  	//            /      \
   163  	//          n10       n7
   164  	//         /   \    (shared)
   165  	//       /       \
   166  	//     n9         n2
   167  	//  (p1/v5)    (shared)
   168  
   169  	f.AddTrie(trie3)
   170  
   171  	flattenedForest, err := flattener.FlattenForest(f)
   172  	require.NoError(t, err)
   173  
   174  	file, err := os.OpenFile("checkpoint.v3", os.O_RDWR|os.O_CREATE|os.O_EXCL, 0600)
   175  	require.NoError(t, err)
   176  
   177  	err = realWAL.StoreCheckpointV5(flattenedForest, file)
   178  	require.NoError(t, err)
   179  
   180  	file.Close()
   181  }
   182  */