github.com/nspcc-dev/neo-go@v0.105.2-0.20240517133400-6be757af3eba/pkg/core/mpt/helpers_test.go (about) 1 package mpt 2 3 import ( 4 "testing" 5 6 "github.com/nspcc-dev/neo-go/pkg/util" 7 "github.com/stretchr/testify/require" 8 ) 9 10 func TestToNibblesFromNibbles(t *testing.T) { 11 check := func(t *testing.T, expected []byte) { 12 actual := fromNibbles(toNibbles(expected)) 13 require.Equal(t, expected, actual) 14 } 15 t.Run("empty path", func(t *testing.T) { 16 check(t, []byte{}) 17 }) 18 t.Run("non-empty path", func(t *testing.T) { 19 check(t, []byte{0x01, 0xAC, 0x8d, 0x04, 0xFF}) 20 }) 21 } 22 23 func TestGetChildrenPaths(t *testing.T) { 24 h1 := NewHashNode(util.Uint256{1, 2, 3}) 25 h2 := NewHashNode(util.Uint256{4, 5, 6}) 26 h3 := NewHashNode(util.Uint256{7, 8, 9}) 27 l := NewLeafNode([]byte{1, 2, 3}) 28 ext1 := NewExtensionNode([]byte{8, 9}, h1) 29 ext2 := NewExtensionNode([]byte{7, 6}, l) 30 branch := NewBranchNode() 31 branch.Children[3] = h1 32 branch.Children[5] = l 33 branch.Children[6] = h1 // 3-th and 6-th children have the same hash 34 branch.Children[7] = h3 35 branch.Children[lastChild] = h2 36 testCases := map[string]struct { 37 node Node 38 expected map[util.Uint256][][]byte 39 }{ 40 "Hash": {h1, nil}, 41 "Leaf": {l, nil}, 42 "Extension with next Hash": {ext1, map[util.Uint256][][]byte{h1.Hash(): {ext1.key}}}, 43 "Extension with next non-Hash": {ext2, map[util.Uint256][][]byte{}}, 44 "Branch": {branch, map[util.Uint256][][]byte{ 45 h1.Hash(): {{0x03}, {0x06}}, 46 h2.Hash(): {{}}, 47 h3.Hash(): {{0x07}}, 48 }}, 49 } 50 parentPath := []byte{4, 5, 6} 51 for name, testCase := range testCases { 52 t.Run(name, func(t *testing.T) { 53 require.Equal(t, testCase.expected, GetChildrenPaths([]byte{}, testCase.node)) 54 if testCase.expected != nil { 55 expectedWithPrefix := make(map[util.Uint256][][]byte, len(testCase.expected)) 56 for h, paths := range testCase.expected { 57 var res [][]byte 58 for _, path := range paths { 59 res = append(res, append(parentPath, path...)) 60 } 61 expectedWithPrefix[h] = res 62 } 63 require.Equal(t, expectedWithPrefix, GetChildrenPaths(parentPath, testCase.node)) 64 } 65 }) 66 } 67 }