2019-12-04 12:48:38 +00:00
|
|
|
package trie
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"testing"
|
|
|
|
|
2021-05-20 18:25:53 +00:00
|
|
|
"github.com/ledgerwatch/erigon/common"
|
|
|
|
"github.com/ledgerwatch/erigon/core/types/accounts"
|
2019-12-04 12:48:38 +00:00
|
|
|
)
|
|
|
|
|
2021-04-21 01:48:37 +00:00
|
|
|
func TestBlockWitness(t *testing.T) {
|
2019-12-04 12:48:38 +00:00
|
|
|
tr := New(common.Hash{})
|
2020-02-06 10:53:09 +00:00
|
|
|
tr.Update([]byte("ABCD0001"), []byte("val1"))
|
|
|
|
tr.Update([]byte("ABCE0002"), []byte("val2"))
|
2019-12-04 12:48:38 +00:00
|
|
|
|
2021-04-21 01:48:37 +00:00
|
|
|
rl := NewRetainList(2)
|
2020-05-12 14:24:43 +00:00
|
|
|
rl.AddKey([]byte("ABCD0001"))
|
2020-01-15 13:56:50 +00:00
|
|
|
|
2021-04-21 01:48:37 +00:00
|
|
|
bwb := NewWitnessBuilder(tr.root, false)
|
2020-01-15 13:56:50 +00:00
|
|
|
|
|
|
|
hr := newHasher(false)
|
|
|
|
defer returnHasherToPool(hr)
|
|
|
|
|
|
|
|
var w *Witness
|
|
|
|
var err error
|
2020-05-12 14:24:43 +00:00
|
|
|
if w, err = bwb.Build(&MerklePathLimiter{rl, hr.hash}); err != nil {
|
2019-12-04 12:48:38 +00:00
|
|
|
t.Errorf("Could not make block witness: %v", err)
|
|
|
|
}
|
|
|
|
|
2021-04-21 01:48:37 +00:00
|
|
|
tr1, err := BuildTrieFromWitness(w, false)
|
2019-12-04 12:48:38 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Could not restore trie from the block witness: %v", err)
|
|
|
|
}
|
2021-04-21 01:48:37 +00:00
|
|
|
if tr.Hash() != tr1.Hash() {
|
2019-12-04 12:48:38 +00:00
|
|
|
t.Errorf("Reconstructed block witness has different root hash than source trie")
|
|
|
|
}
|
|
|
|
|
|
|
|
expected := []byte("val1")
|
2021-04-21 01:48:37 +00:00
|
|
|
got, _ := tr1.Get([]byte("ABCD0001"))
|
2019-12-04 12:48:38 +00:00
|
|
|
if !bytes.Equal(got, expected) {
|
|
|
|
t.Errorf("unexpected value: %x (expected %x)", got, expected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-21 01:48:37 +00:00
|
|
|
func TestBlockWitnessAccount(t *testing.T) {
|
2019-12-04 12:48:38 +00:00
|
|
|
tr := New(common.Hash{})
|
|
|
|
|
|
|
|
account := accounts.NewAccount()
|
2020-05-26 16:53:50 +00:00
|
|
|
account.Balance.SetUint64(1 * 1000 * 1000)
|
2019-12-04 12:48:38 +00:00
|
|
|
|
|
|
|
tr.UpdateAccount([]byte("ABCD0001"), &account)
|
|
|
|
|
2021-04-21 01:48:37 +00:00
|
|
|
rl := NewRetainList(2)
|
2020-05-12 14:24:43 +00:00
|
|
|
rl.AddKey([]byte("ABCD0001"))
|
2020-01-15 13:56:50 +00:00
|
|
|
|
2021-04-21 01:48:37 +00:00
|
|
|
bwb := NewWitnessBuilder(tr.root, false)
|
2020-01-15 13:56:50 +00:00
|
|
|
|
|
|
|
hr := newHasher(false)
|
|
|
|
defer returnHasherToPool(hr)
|
|
|
|
|
|
|
|
var w *Witness
|
|
|
|
var err error
|
2020-05-12 14:24:43 +00:00
|
|
|
if w, err = bwb.Build(&MerklePathLimiter{rl, hr.hash}); err != nil {
|
2019-12-04 12:48:38 +00:00
|
|
|
t.Errorf("Could not make block witness: %v", err)
|
|
|
|
}
|
|
|
|
|
2021-04-21 01:48:37 +00:00
|
|
|
trBin1, err := BuildTrieFromWitness(w, false)
|
2019-12-04 12:48:38 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Could not restore trie from the block witness: %v", err)
|
|
|
|
}
|
2021-04-21 01:48:37 +00:00
|
|
|
if tr.Hash() != trBin1.Hash() {
|
2019-12-04 12:48:38 +00:00
|
|
|
t.Errorf("Reconstructed block witness has different root hash than source trie")
|
|
|
|
}
|
|
|
|
|
|
|
|
got, _ := trBin1.GetAccount([]byte("ABCD0001"))
|
|
|
|
if !account.Equals(got) {
|
|
|
|
t.Errorf("received account is not equal to the initial one")
|
|
|
|
}
|
|
|
|
}
|