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

     1  package wal_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  	"github.com/stretchr/testify/require"
     8  
     9  	"github.com/onflow/flow-go/ledger"
    10  	"github.com/onflow/flow-go/ledger/common/hash"
    11  	"github.com/onflow/flow-go/ledger/common/testutils"
    12  	realWAL "github.com/onflow/flow-go/ledger/complete/wal"
    13  )
    14  
    15  func TestUpdate(t *testing.T) {
    16  
    17  	var rootHash ledger.RootHash
    18  	copy(rootHash[:], []byte{2, 1, 3, 7})
    19  	p1 := testutils.PathByUint16(uint16(1))
    20  	p2 := testutils.PathByUint16(uint16(772))
    21  	paths := []ledger.Path{p1, p2}
    22  	v1 := testutils.LightPayload8(1, 2)
    23  	v2 := testutils.LightPayload(2, 3)
    24  	payloads := []*ledger.Payload{v1, v2}
    25  	update := &ledger.TrieUpdate{RootHash: rootHash, Paths: paths, Payloads: payloads}
    26  
    27  	expected := []byte{
    28  		1, //update flag,
    29  		0, 0, 11, 0, 32, 2, 1, 3, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 32,
    30  		0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    31  		3, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    32  		22, 0, 0, 0, 9, 0, 1, 0, 0, 0, 3, 0, 0, 1, 0, 0,
    33  		0, 0, 0, 0, 0, 1, 2, 0, 0, 0, 24, 0, 0, 0, 10, 0, 1, 0, 0,
    34  		0, 4, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 2, 0, 3, // encoded update
    35  	}
    36  
    37  	t.Run("encode", func(t *testing.T) {
    38  		data := realWAL.EncodeUpdate(update)
    39  		assert.Equal(t, expected, data)
    40  	})
    41  
    42  	t.Run("decode", func(t *testing.T) {
    43  		data := realWAL.EncodeUpdate(update)
    44  		operation, stateCommitment, up, err := realWAL.Decode(data)
    45  		require.NoError(t, err)
    46  		assert.Equal(t, realWAL.WALUpdate, operation)
    47  		assert.Equal(t, stateCommitment, ledger.RootHash(hash.DummyHash))
    48  		assert.Equal(t, update, up)
    49  	})
    50  }
    51  
    52  func TestDelete(t *testing.T) {
    53  
    54  	var rootHash ledger.RootHash
    55  	copy(rootHash[:], []byte{2, 1, 3, 7})
    56  
    57  	expected := []byte{
    58  		2,     // delete flag
    59  		0, 32, // root hash length
    60  		2, 1, 3, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // root hash data
    61  	}
    62  
    63  	t.Run("encode", func(t *testing.T) {
    64  		data := realWAL.EncodeDelete(rootHash)
    65  		assert.Equal(t, expected, data)
    66  	})
    67  
    68  	t.Run("decode", func(t *testing.T) {
    69  		operation, rootH, _, err := realWAL.Decode(expected)
    70  		require.NoError(t, err)
    71  		assert.Equal(t, realWAL.WALDelete, operation)
    72  		assert.Equal(t, rootHash, rootH)
    73  	})
    74  
    75  }