github.com/ledgerwatch/erigon-lib@v1.0.0/kv/temporal/historyv2/account_changeset_test.go (about) 1 /* 2 Copyright 2022 Erigon contributors 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package historyv2 18 19 import ( 20 "bytes" 21 "encoding/hex" 22 "fmt" 23 "reflect" 24 "testing" 25 26 "github.com/stretchr/testify/assert" 27 28 "github.com/ledgerwatch/erigon-lib/common/hexutility" 29 "github.com/ledgerwatch/erigon-lib/kv" 30 ) 31 32 func TestEncodingAccount(t *testing.T) { 33 bkt := kv.AccountChangeSet 34 m := Mapper[bkt] 35 36 ch := m.New() 37 // empty StorageChangeSset first 38 err := m.Encode(1, ch, func(k, v []byte) error { 39 return fmt.Errorf("must never call") 40 }) 41 assert.NoError(t, err) 42 43 vals := [][]byte{ 44 hexutility.MustDecodeHex("f7f6db1eb17c6d582078e0ffdd0c"), 45 hexutility.MustDecodeHex("b1e9b5c16355eede662031dd621d08faf4ea"), 46 hexutility.MustDecodeHex("862cf52b74f1cea41ddd8ffa4b3e7c7790"), 47 } 48 numOfElements := 3 49 for i := 0; i < numOfElements; i++ { 50 address := hexutility.MustDecodeHex(fmt.Sprintf("0xBe828AD8B538D1D691891F6c725dEdc5989abBc%d", i)) 51 err2 := ch.Add(address, vals[i]) 52 if err2 != nil { 53 t.Fatal(err) 54 } 55 } 56 57 ch2 := m.New() 58 err = m.Encode(1, ch, func(k, v []byte) error { 59 var err error 60 _, k, v, err = m.Decode(k, v) 61 if err != nil { 62 return err 63 } 64 return ch2.Add(k, v) 65 }) 66 if err != nil { 67 t.Fatal(err) 68 } 69 70 if !reflect.DeepEqual(ch, ch2) { 71 fmt.Println("ch", len(ch.Changes), "ch2", len(ch2.Changes)) 72 for i, v := range ch.Changes { 73 fmt.Println("Line ", i) 74 75 if !bytes.Equal(v.Key, ch2.Changes[i].Key) || !bytes.Equal(v.Value, ch2.Changes[i].Value) { 76 fmt.Println("Diff ", i) 77 fmt.Println("k1", hex.EncodeToString(v.Key), len(v.Key)) 78 fmt.Println("k2", hex.EncodeToString(ch2.Changes[i].Key)) 79 fmt.Println("v1", hex.EncodeToString(v.Value)) 80 fmt.Println("v2", hex.EncodeToString(ch2.Changes[i].Value)) 81 } 82 } 83 fmt.Printf("%+v %+v\n", ch, ch2) 84 t.Fatal("not equal") 85 } 86 }