github.com/ledgerwatch/erigon-lib@v1.0.0/commitment/commitment_test.go (about) 1 package commitment 2 3 import ( 4 "encoding/hex" 5 "fmt" 6 "math/rand" 7 "testing" 8 9 "github.com/stretchr/testify/require" 10 ) 11 12 func generateCellRow(t *testing.T, size int) (row []*Cell, bitmap uint16) { 13 t.Helper() 14 15 row = make([]*Cell, size) 16 var bm uint16 17 for i := 0; i < len(row); i++ { 18 row[i] = new(Cell) 19 row[i].hl = 32 20 n, err := rand.Read(row[i].h[:]) 21 require.NoError(t, err) 22 require.EqualValues(t, row[i].hl, n) 23 24 th := rand.Intn(120) 25 switch { 26 case th > 70: 27 n, err = rand.Read(row[i].apk[:]) 28 require.NoError(t, err) 29 row[i].apl = n 30 case th > 20 && th <= 70: 31 n, err = rand.Read(row[i].spk[:]) 32 require.NoError(t, err) 33 row[i].spl = n 34 case th <= 20: 35 n, err = rand.Read(row[i].extension[:th]) 36 row[i].extLen = n 37 require.NoError(t, err) 38 require.EqualValues(t, th, n) 39 } 40 bm |= uint16(1 << i) 41 } 42 return row, bm 43 } 44 45 func TestBranchData_MergeHexBranches2(t *testing.T) { 46 row, bm := generateCellRow(t, 16) 47 48 enc, _, err := EncodeBranch(bm, bm, bm, func(i int, skip bool) (*Cell, error) { 49 return row[i], nil 50 }) 51 52 require.NoError(t, err) 53 require.NotEmpty(t, enc) 54 t.Logf("enc [%d] %x\n", len(enc), enc) 55 56 bmg := NewHexBranchMerger(8192) 57 res, err := bmg.Merge(enc, enc) 58 require.NoError(t, err) 59 require.EqualValues(t, enc, res) 60 61 tm, am, origins, err := res.DecodeCells() 62 require.NoError(t, err) 63 require.EqualValues(t, tm, am) 64 require.EqualValues(t, bm, am) 65 66 i := 0 67 for _, c := range origins { 68 if c == nil { 69 continue 70 } 71 require.EqualValues(t, row[i].extLen, c.extLen) 72 require.EqualValues(t, row[i].extension, c.extension) 73 require.EqualValues(t, row[i].apl, c.apl) 74 require.EqualValues(t, row[i].apk, c.apk) 75 require.EqualValues(t, row[i].spl, c.spl) 76 require.EqualValues(t, row[i].spk, c.spk) 77 i++ 78 } 79 } 80 81 func TestBranchData_MergeHexBranches3(t *testing.T) { 82 encs := "0405040b04080f0b080d030204050b0502090805050d01060e060d070f0903090c04070a0d0a000e090b060b0c040c0700020e0b0c060b0106020c0607050a0b0209070d06040808" 83 enc, err := hex.DecodeString(encs) 84 require.NoError(t, err) 85 86 //tm, am, origins, err := BranchData(enc).DecodeCells() 87 require.NoError(t, err) 88 t.Logf("%s", BranchData(enc).String()) 89 //require.EqualValues(t, tm, am) 90 //_, _ = tm, am 91 } 92 93 // helper to decode row of cells from string 94 func Test_UTIL_UnfoldBranchDataFromString(t *testing.T) { 95 t.Skip() 96 97 //encs := "0405040b04080f0b080d030204050b0502090805050d01060e060d070f0903090c04070a0d0a000e090b060b0c040c0700020e0b0c060b0106020c0607050a0b0209070d06040808" 98 encs := "37ad10eb75ea0fc1c363db0dda0cd2250426ee2c72787155101ca0e50804349a94b649deadcc5cddc0d2fd9fb358c2edc4e7912d165f88877b1e48c69efacf418e923124506fbb2fd64823fd41cbc10427c423" 99 enc, err := hex.DecodeString(encs) 100 require.NoError(t, err) 101 102 bfn := func(pref []byte) ([]byte, error) { 103 return enc, nil 104 } 105 sfn := func(pref []byte, c *Cell) error { 106 return nil 107 } 108 109 hph := NewHexPatriciaHashed(20, bfn, nil, sfn) 110 hph.unfoldBranchNode(1, false, 0) 111 tm, am, origins, err := BranchData(enc).DecodeCells() 112 require.NoError(t, err) 113 t.Logf("%s", BranchData(enc).String()) 114 //require.EqualValues(t, tm, am) 115 _, _ = tm, am 116 117 i := 0 118 for _, c := range origins { 119 if c == nil { 120 continue 121 } 122 fmt.Printf("i %d, c %#+v\n", i, c) 123 i++ 124 } 125 } 126 127 func TestBranchData_ExtractPlainKeys(t *testing.T) { 128 row, bm := generateCellRow(t, 16) 129 130 cg := func(nibble int, skip bool) (*Cell, error) { 131 return row[nibble], nil 132 } 133 134 enc, _, err := EncodeBranch(bm, bm, bm, cg) 135 require.NoError(t, err) 136 137 extAPK, extSPK, err := enc.ExtractPlainKeys() 138 require.NoError(t, err) 139 140 for i, c := range row { 141 if c == nil { 142 continue 143 } 144 switch { 145 case c.apl != 0: 146 require.Containsf(t, extAPK, c.apk[:], "at pos %d expected %x..", i, c.apk[:8]) 147 case c.spl != 0: 148 require.Containsf(t, extSPK, c.spk[:], "at pos %d expected %x..", i, c.spk[:8]) 149 default: 150 continue 151 } 152 } 153 } 154 155 func TestBranchData_ReplacePlainKeys(t *testing.T) { 156 row, bm := generateCellRow(t, 16) 157 158 cg := func(nibble int, skip bool) (*Cell, error) { 159 return row[nibble], nil 160 } 161 162 enc, _, err := EncodeBranch(bm, bm, bm, cg) 163 require.NoError(t, err) 164 165 extAPK, extSPK, err := enc.ExtractPlainKeys() 166 require.NoError(t, err) 167 168 shortApk, shortSpk := make([][]byte, 0), make([][]byte, 0) 169 for i, c := range row { 170 if c == nil { 171 continue 172 } 173 switch { 174 case c.apl != 0: 175 shortApk = append(shortApk, c.apk[:8]) 176 require.Containsf(t, extAPK, c.apk[:], "at pos %d expected %x..", i, c.apk[:8]) 177 case c.spl != 0: 178 shortSpk = append(shortSpk, c.spk[:8]) 179 require.Containsf(t, extSPK, c.spk[:], "at pos %d expected %x..", i, c.spk[:8]) 180 default: 181 continue 182 } 183 } 184 185 target := make([]byte, 0, len(enc)) 186 replaced, err := enc.ReplacePlainKeys(shortApk, shortSpk, target) 187 require.NoError(t, err) 188 require.Truef(t, len(replaced) < len(enc), "replaced expected to be shorter than original enc") 189 190 rextA, rextS, err := replaced.ExtractPlainKeys() 191 require.NoError(t, err) 192 193 for _, apk := range shortApk { 194 require.Containsf(t, rextA, apk, "expected %x to be in replaced account keys", apk) 195 } 196 for _, spk := range shortSpk { 197 require.Containsf(t, rextS, spk, "expected %x to be in replaced storage keys", spk) 198 } 199 require.True(t, len(shortApk) == len(rextA)) 200 require.True(t, len(shortSpk) == len(rextS)) 201 }