2020-01-22 10:25:07 +00:00
|
|
|
package changeset
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
2020-05-15 07:52:45 +00:00
|
|
|
|
2021-07-29 11:53:13 +00:00
|
|
|
"github.com/ledgerwatch/erigon-lib/kv"
|
2021-05-20 18:25:53 +00:00
|
|
|
"github.com/ledgerwatch/erigon/common"
|
2020-05-15 07:52:45 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2020-01-22 10:25:07 +00:00
|
|
|
)
|
|
|
|
|
2021-05-04 12:34:08 +00:00
|
|
|
func TestEncodingAccount(t *testing.T) {
|
2021-07-28 02:47:38 +00:00
|
|
|
bkt := kv.AccountChangeSet
|
2020-11-16 12:08:28 +00:00
|
|
|
m := Mapper[bkt]
|
2020-05-15 07:52:45 +00:00
|
|
|
|
2021-05-04 12:34:08 +00:00
|
|
|
ch := m.New()
|
2020-05-15 07:52:45 +00:00
|
|
|
// empty StorageChangeSset first
|
2021-05-04 12:34:08 +00:00
|
|
|
err := m.Encode(1, ch, func(k, v []byte) error {
|
2020-11-16 12:08:28 +00:00
|
|
|
return fmt.Errorf("must never call")
|
|
|
|
})
|
2020-01-22 10:25:07 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
vals := [][]byte{
|
|
|
|
common.FromHex("f7f6db1eb17c6d582078e0ffdd0c"),
|
|
|
|
common.FromHex("b1e9b5c16355eede662031dd621d08faf4ea"),
|
|
|
|
common.FromHex("862cf52b74f1cea41ddd8ffa4b3e7c7790"),
|
|
|
|
}
|
|
|
|
numOfElements := 3
|
|
|
|
for i := 0; i < numOfElements; i++ {
|
2020-05-15 07:52:45 +00:00
|
|
|
address := common.HexToAddress(fmt.Sprintf("0xBe828AD8B538D1D691891F6c725dEdc5989abBc%d", i))
|
2021-02-04 09:59:04 +00:00
|
|
|
err2 := ch.Add(address[:], vals[i])
|
|
|
|
if err2 != nil {
|
2020-01-22 10:25:07 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-04 12:34:08 +00:00
|
|
|
ch2 := m.New()
|
|
|
|
err = m.Encode(1, ch, func(k, v []byte) error {
|
|
|
|
_, k, v = m.Decode(k, v)
|
2020-11-16 12:08:28 +00:00
|
|
|
return ch2.Add(k, v)
|
|
|
|
})
|
2020-01-22 10:25:07 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(ch, ch2) {
|
|
|
|
fmt.Println("ch", len(ch.Changes), "ch2", len(ch2.Changes))
|
|
|
|
for i, v := range ch.Changes {
|
|
|
|
fmt.Println("Line ", i)
|
|
|
|
|
|
|
|
if !bytes.Equal(v.Key, ch2.Changes[i].Key) || !bytes.Equal(v.Value, ch2.Changes[i].Value) {
|
|
|
|
fmt.Println("Diff ", i)
|
|
|
|
fmt.Println("k1", common.Bytes2Hex(v.Key), len(v.Key))
|
|
|
|
fmt.Println("k2", common.Bytes2Hex(ch2.Changes[i].Key))
|
|
|
|
fmt.Println("v1", common.Bytes2Hex(v.Value))
|
|
|
|
fmt.Println("v2", common.Bytes2Hex(ch2.Changes[i].Value))
|
|
|
|
}
|
|
|
|
}
|
2020-05-15 07:52:45 +00:00
|
|
|
fmt.Printf("%+v %+v\n", ch, ch2)
|
2020-01-22 10:25:07 +00:00
|
|
|
t.Fatal("not equal")
|
|
|
|
}
|
|
|
|
}
|