2020-01-31 04:11:20 +00:00
|
|
|
package trie
|
|
|
|
|
|
|
|
import (
|
2020-05-15 08:58:36 +00:00
|
|
|
"encoding/binary"
|
2020-01-31 04:11:20 +00:00
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
2021-05-20 18:25:53 +00:00
|
|
|
"github.com/ledgerwatch/erigon/common"
|
|
|
|
"github.com/ledgerwatch/erigon/common/dbutils"
|
|
|
|
"github.com/ledgerwatch/erigon/core/types/accounts"
|
2020-01-31 04:11:20 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2020-05-12 07:22:45 +00:00
|
|
|
func TestCreateLoadingPrefixes(t *testing.T) {
|
2020-05-02 18:00:57 +00:00
|
|
|
assert := assert.New(t)
|
|
|
|
|
2020-05-12 07:22:45 +00:00
|
|
|
tr := New(common.Hash{})
|
2020-05-02 18:00:57 +00:00
|
|
|
kAcc1 := common.FromHex("0001cf1ce0664746d39af9f6db99dc3370282f1d9d48df7f804b7e6499558c83")
|
2020-06-02 07:46:21 +00:00
|
|
|
kInc := make([]byte, 8)
|
2020-07-17 14:18:31 +00:00
|
|
|
binary.BigEndian.PutUint64(kInc, uint64(1))
|
2020-05-02 18:00:57 +00:00
|
|
|
ks1 := common.FromHex("0000000000000000000000000000000000000000000000000000000000000001")
|
2020-05-12 07:22:45 +00:00
|
|
|
acc1 := accounts.NewAccount()
|
2020-05-26 16:53:50 +00:00
|
|
|
acc1.Balance.SetUint64(12345)
|
2020-05-12 07:22:45 +00:00
|
|
|
acc1.Incarnation = 1
|
|
|
|
acc1.Initialised = true
|
|
|
|
tr.UpdateAccount(kAcc1, &acc1)
|
|
|
|
tr.Update(concat(kAcc1, ks1...), []byte{1, 2, 3})
|
2020-05-02 18:00:57 +00:00
|
|
|
|
|
|
|
kAcc2 := common.FromHex("0002cf1ce0664746d39af9f6db99dc3370282f1d9d48df7f804b7e6499558c83")
|
|
|
|
ks2 := common.FromHex("0000000000000000000000000000000000000000000000000000000000000001")
|
|
|
|
ks22 := common.FromHex("0000000000000000000000000000000000000000000000000000000000000002")
|
2020-05-12 07:22:45 +00:00
|
|
|
acc2 := accounts.NewAccount()
|
2020-05-26 16:53:50 +00:00
|
|
|
acc2.Balance.SetUint64(6789)
|
2020-05-12 07:22:45 +00:00
|
|
|
acc2.Incarnation = 1
|
|
|
|
acc2.Initialised = true
|
|
|
|
tr.UpdateAccount(kAcc2, &acc2)
|
|
|
|
tr.Update(concat(kAcc2, ks2...), []byte{4, 5, 6})
|
|
|
|
tr.Update(concat(kAcc2, ks22...), []byte{7, 8, 9})
|
|
|
|
tr.Hash()
|
|
|
|
|
|
|
|
// Evict accounts only
|
|
|
|
tr.EvictNode(keybytesToHex(kAcc1))
|
|
|
|
tr.EvictNode(keybytesToHex(kAcc2))
|
2020-05-12 14:24:43 +00:00
|
|
|
rs := NewRetainList(0)
|
2020-06-02 07:46:21 +00:00
|
|
|
rs.AddKey(concat(concat(kAcc1, kInc...), ks1...))
|
|
|
|
rs.AddKey(concat(concat(kAcc2, kInc...), ks2...))
|
|
|
|
rs.AddKey(concat(concat(kAcc2, kInc...), ks22...))
|
2020-05-12 14:24:43 +00:00
|
|
|
dbPrefixes, fixedbits, hooks := tr.FindSubTriesToLoad(rs)
|
2020-07-17 14:18:31 +00:00
|
|
|
assert.Equal("[0001cf1ce0664746d39af9f6db99dc3370282f1d9d48df7f804b7e6499558c830000000000000001 0002cf1ce0664746d39af9f6db99dc3370282f1d9d48df7f804b7e6499558c830000000000000001]", fmt.Sprintf("%x", dbPrefixes))
|
2020-05-12 07:22:45 +00:00
|
|
|
assert.Equal("[320 320]", fmt.Sprintf("%d", fixedbits))
|
|
|
|
assert.Equal("[000000010c0f010c0e000606040704060d03090a0f090f060d0b09090d0c030307000208020f010d090d04080d0f070f0800040b070e060409090505080c0803 000000020c0f010c0e000606040704060d03090a0f090f060d0b09090d0c030307000208020f010d090d04080d0f070f0800040b070e060409090505080c0803]", fmt.Sprintf("%x", hooks))
|
|
|
|
|
|
|
|
// Evict everytning
|
|
|
|
tr.EvictNode([]byte{})
|
2020-05-02 18:00:57 +00:00
|
|
|
// if resolve only accounts
|
2020-05-12 14:24:43 +00:00
|
|
|
rs = NewRetainList(0)
|
2020-05-12 07:22:45 +00:00
|
|
|
rs.AddKey(kAcc1)
|
|
|
|
rs.AddKey(kAcc2)
|
2020-05-12 14:24:43 +00:00
|
|
|
dbPrefixes, fixedbits, hooks = tr.FindSubTriesToLoad(rs)
|
2020-05-12 07:22:45 +00:00
|
|
|
assert.Equal("[]", fmt.Sprintf("%x", dbPrefixes))
|
2020-05-02 18:00:57 +00:00
|
|
|
assert.Equal("[0]", fmt.Sprintf("%d", fixedbits))
|
2020-05-11 04:46:07 +00:00
|
|
|
assert.Equal("[]", fmt.Sprintf("%x", hooks))
|
2020-05-02 18:00:57 +00:00
|
|
|
}
|
|
|
|
|
2020-03-20 11:30:14 +00:00
|
|
|
func TestIsBefore(t *testing.T) {
|
2020-01-31 07:31:50 +00:00
|
|
|
assert := assert.New(t)
|
|
|
|
|
2020-08-27 18:21:50 +00:00
|
|
|
is := keyIsBefore([]byte("a"), []byte("b"))
|
2020-01-31 07:31:50 +00:00
|
|
|
assert.Equal(true, is)
|
|
|
|
|
2020-08-27 18:21:50 +00:00
|
|
|
is = keyIsBefore([]byte("b"), []byte("a"))
|
2020-01-31 07:31:50 +00:00
|
|
|
assert.Equal(false, is)
|
|
|
|
|
2020-08-27 18:21:50 +00:00
|
|
|
is = keyIsBefore([]byte("b"), []byte(""))
|
2020-01-31 07:31:50 +00:00
|
|
|
assert.Equal(false, is)
|
|
|
|
|
2020-08-27 18:21:50 +00:00
|
|
|
is = keyIsBefore(nil, []byte("b"))
|
2020-01-31 07:31:50 +00:00
|
|
|
assert.Equal(false, is)
|
|
|
|
|
2020-08-27 18:21:50 +00:00
|
|
|
is = keyIsBefore([]byte("b"), nil)
|
2020-01-31 07:31:50 +00:00
|
|
|
assert.Equal(true, is)
|
2020-03-11 10:31:49 +00:00
|
|
|
|
|
|
|
contract := fmt.Sprintf("2%063x", 0)
|
|
|
|
storageKey := common.Hex2Bytes(contract + "ffffffff" + fmt.Sprintf("10%062x", 0))
|
2020-04-16 07:42:25 +00:00
|
|
|
cacheKey := common.Hex2Bytes(contract + "ffffffff" + "20")
|
2020-08-27 18:21:50 +00:00
|
|
|
is = keyIsBefore(cacheKey, storageKey)
|
2020-03-11 10:31:49 +00:00
|
|
|
assert.False(is)
|
|
|
|
|
|
|
|
storageKey = common.Hex2Bytes(contract + "ffffffffffffffff" + fmt.Sprintf("20%062x", 0))
|
2020-04-16 07:42:25 +00:00
|
|
|
cacheKey = common.Hex2Bytes(contract + "ffffffffffffffff" + "10")
|
2020-08-27 18:21:50 +00:00
|
|
|
is = keyIsBefore(cacheKey, storageKey)
|
2020-03-11 10:31:49 +00:00
|
|
|
assert.True(is)
|
2020-01-31 07:31:50 +00:00
|
|
|
}
|
2020-04-18 20:09:44 +00:00
|
|
|
|
2020-08-15 00:52:36 +00:00
|
|
|
func TestIsSequence(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
type tc struct {
|
|
|
|
prev, next string
|
|
|
|
expect bool
|
|
|
|
}
|
|
|
|
|
|
|
|
cases := []tc{
|
|
|
|
{prev: "1234", next: "1235", expect: true},
|
|
|
|
{prev: "12ff", next: "13", expect: true},
|
|
|
|
{prev: "12ff", next: "13000000", expect: true},
|
|
|
|
{prev: "1234", next: "5678", expect: false},
|
|
|
|
}
|
|
|
|
for _, tc := range cases {
|
|
|
|
next, _ := dbutils.NextSubtree(common.FromHex(tc.prev))
|
2021-02-21 18:41:59 +00:00
|
|
|
res := isSequenceOld(next, common.FromHex(tc.next))
|
2020-08-15 00:52:36 +00:00
|
|
|
assert.Equal(tc.expect, res, "%s, %s", tc.prev, tc.next)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|