Skip to content

Commit

Permalink
Serializing and Deserializing to V3 (#13390)
Browse files Browse the repository at this point in the history
closes #12480

---------

Co-authored-by: JkLondon <ilya@mikheev.fun>
Co-authored-by: alex.sharov <AskAlexSharov@gmail.com>
  • Loading branch information
3 people authored Feb 1, 2025
1 parent 490b5fe commit f1f235f
Show file tree
Hide file tree
Showing 16 changed files with 384 additions and 353 deletions.
3 changes: 1 addition & 2 deletions core/state/rw_v3.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,8 +258,7 @@ func (rs *StateV3) Unwind(ctx context.Context, tx kv.RwTx, blockUnwindTo, txUnwi
var address common.Address
copy(address[:], k)

newV := make([]byte, acc.EncodingLengthForStorage())
acc.EncodeForStorage(newV)
newV := accounts.SerialiseV3(&acc)
if accumulator != nil {
accumulator.ChangeAccount(address, acc.Incarnation, newV)
}
Expand Down
9 changes: 7 additions & 2 deletions core/test/domains_restart_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ import (
"github.com/erigontech/erigon-lib/kv/temporal"
"github.com/erigontech/erigon-lib/log/v3"
"github.com/erigontech/erigon-lib/state"
types2 "github.com/erigontech/erigon-lib/types"
"github.com/erigontech/erigon-lib/types/accounts"
"github.com/erigontech/erigon/core"
reset2 "github.com/erigontech/erigon/core/rawdb/rawdbreset"
Expand Down Expand Up @@ -491,7 +490,13 @@ func TestCommit(t *testing.T) {
require.NoError(t, err)
defer domains.Close()

buf := types2.EncodeAccountBytesV3(0, uint256.NewInt(7), nil, 1)
acc := accounts.Account{
Nonce: 0,
Balance: *uint256.NewInt(7),
CodeHash: libcommon.Hash{},
Incarnation: 1,
}
buf := accounts.SerialiseV3(&acc)

addr := libcommon.Hex2Bytes("8e5476fc5990638a4fb0b5fd3f61bb4b5c5f395e")
loc := libcommon.Hex2Bytes("24f3a02dc65eda502dbf75919e795458413d3c45b38bb35b51235432707900ed")
Expand Down
28 changes: 16 additions & 12 deletions erigon-lib/commitment/commitment.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"encoding/binary"
"errors"
"fmt"
"github.com/erigontech/erigon-lib/types/accounts"
"math/bits"
"sort"
"strings"
Expand All @@ -34,12 +35,10 @@ import (

"github.com/erigontech/erigon-lib/common"
"github.com/erigontech/erigon-lib/common/cryptozerocopy"
"github.com/erigontech/erigon-lib/log/v3"
"github.com/erigontech/erigon-lib/metrics"
"github.com/erigontech/erigon-lib/types"

"github.com/erigontech/erigon-lib/common/length"
"github.com/erigontech/erigon-lib/etl"
"github.com/erigontech/erigon-lib/log/v3"
"github.com/erigontech/erigon-lib/metrics"
)

var (
Expand Down Expand Up @@ -1060,21 +1059,26 @@ func (t *Updates) TouchAccount(c *KeyUpdate, val []byte) {
if c.update.Flags&DeleteUpdate != 0 {
c.update.Flags = 0 // also could invert with ^ but 0 is just a reset
}
nonce, balance, chash := types.DecodeAccountBytesV3(val)
if c.update.Nonce != nonce {
c.update.Nonce = nonce

acc := accounts.Account{}
err := accounts.DeserialiseV3(&acc, val)
if err != nil {
panic(err)
}
if c.update.Nonce != acc.Nonce {
c.update.Nonce = acc.Nonce
c.update.Flags |= NonceUpdate
}
if !c.update.Balance.Eq(balance) {
c.update.Balance.Set(balance)
if !c.update.Balance.Eq(&acc.Balance) {
c.update.Balance.Set(&acc.Balance)
c.update.Flags |= BalanceUpdate
}
if !bytes.Equal(chash, c.update.CodeHash[:]) {
if len(chash) == 0 {
if !bytes.Equal(acc.CodeHash.Bytes(), c.update.CodeHash[:]) {
if len(acc.CodeHash.Bytes()) == 0 {
copy(c.update.CodeHash[:], EmptyCodeHash)
} else {
c.update.Flags |= CodeUpdate
copy(c.update.CodeHash[:], chash)
copy(c.update.CodeHash[:], acc.CodeHash.Bytes())
}
}
}
Expand Down
16 changes: 12 additions & 4 deletions erigon-lib/kv/kvcache/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"encoding/binary"
"fmt"
"github.com/erigontech/erigon-lib/types/accounts"
"runtime"
"sync"
"sync/atomic"
Expand All @@ -37,7 +38,6 @@ import (
"github.com/erigontech/erigon-lib/kv/temporal/temporaltest"
"github.com/erigontech/erigon-lib/log/v3"
"github.com/erigontech/erigon-lib/state"
"github.com/erigontech/erigon-lib/types"
)

func TestEvictionInUnexpectedOrder(t *testing.T) {
Expand Down Expand Up @@ -179,9 +179,17 @@ func TestAPI(t *testing.T) {
c := New(DefaultCoherentConfig)
k1, k2 := [20]byte{1}, [20]byte{2}
db, _ := temporaltest.NewTestDB(t, datadir.New(t.TempDir()))
account1Enc := types.EncodeAccountBytesV3(1, uint256.NewInt(11), make([]byte, 32), 2)
account2Enc := types.EncodeAccountBytesV3(1, uint256.NewInt(11), make([]byte, 32), 3)
account4Enc := types.EncodeAccountBytesV3(1, uint256.NewInt(11), make([]byte, 32), 5)
acc := accounts.Account{
Nonce: 1,
Balance: *uint256.NewInt(11),
CodeHash: common.Hash{},
Incarnation: 2,
}
account1Enc := accounts.SerialiseV3(&acc)
acc.Incarnation = 3
account2Enc := accounts.SerialiseV3(&acc)
acc.Incarnation = 5
account4Enc := accounts.SerialiseV3(&acc)

get := func(key [20]byte, expectTxnID uint64) (res [1]chan []byte) {

Expand Down
20 changes: 15 additions & 5 deletions erigon-lib/state/aggregator_fuzz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package state
import (
"context"
"encoding/binary"
"github.com/erigontech/erigon-lib/types/accounts"
"testing"
"time"

Expand All @@ -31,7 +32,6 @@ import (
"github.com/erigontech/erigon-lib/kv"
"github.com/erigontech/erigon-lib/kv/mdbx"
"github.com/erigontech/erigon-lib/log/v3"
"github.com/erigontech/erigon-lib/types"
"github.com/holiman/uint256"

"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -96,8 +96,13 @@ func Fuzz_AggregatorV3_Merge(f *testing.F) {
}
for txNum := uint64(1); txNum <= txs; txNum++ {
domains.SetTxNum(txNum)

buf := types.EncodeAccountBytesV3(1, uint256.NewInt(0), nil, 0)
acc := accounts.Account{
Nonce: 1,
Balance: *uint256.NewInt(0),
CodeHash: common.Hash{},
Incarnation: 0,
}
buf := accounts.SerialiseV3(&acc)
err = domains.DomainPut(kv.AccountsDomain, addrs[txNum].Bytes(), nil, buf, nil, 0)
require.NoError(t, err)

Expand Down Expand Up @@ -215,8 +220,13 @@ func Fuzz_AggregatorV3_MergeValTransform(f *testing.F) {
}
for txNum := uint64(1); txNum <= txs; txNum++ {
domains.SetTxNum(txNum)

buf := types.EncodeAccountBytesV3(1, uint256.NewInt(txNum*1e6), nil, 0)
acc := accounts.Account{
Nonce: 1,
Balance: *uint256.NewInt(txNum * 1e6),
CodeHash: common.Hash{},
Incarnation: 0,
}
buf := accounts.SerialiseV3(&acc)
err = domains.DomainPut(kv.AccountsDomain, addrs[txNum].Bytes(), nil, buf, nil, 0)
require.NoError(t, err)

Expand Down
96 changes: 78 additions & 18 deletions erigon-lib/state/aggregator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"encoding/binary"
"encoding/hex"
"fmt"
"github.com/erigontech/erigon-lib/types/accounts"
"math"
"math/rand"
"os"
Expand All @@ -48,7 +49,6 @@ import (
"github.com/erigontech/erigon-lib/kv/stream"
"github.com/erigontech/erigon-lib/log/v3"
"github.com/erigontech/erigon-lib/seg"
"github.com/erigontech/erigon-lib/types"
"github.com/holiman/uint256"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -93,8 +93,13 @@ func TestAggregatorV3_Merge(t *testing.T) {
n, err = rnd.Read(loc)
require.NoError(t, err)
require.EqualValues(t, length.Hash, n)

buf := types.EncodeAccountBytesV3(1, uint256.NewInt(0), nil, 0)
acc := accounts.Account{
Nonce: 1,
Balance: *uint256.NewInt(0),
CodeHash: common.Hash{},
Incarnation: 0,
}
buf := accounts.SerialiseV3(&acc)
err = domains.DomainPut(kv.AccountsDomain, addr, nil, buf, nil, 0)
require.NoError(t, err)

Expand Down Expand Up @@ -208,8 +213,13 @@ func TestAggregatorV3_MergeValTransform(t *testing.T) {
n, err = rnd.Read(loc)
require.NoError(t, err)
require.EqualValues(t, length.Hash, n)

buf := types.EncodeAccountBytesV3(1, uint256.NewInt(txNum*1e6), nil, 0)
acc := accounts.Account{
Nonce: 1,
Balance: *uint256.NewInt(txNum * 1e6),
CodeHash: common.Hash{},
Incarnation: 0,
}
buf := accounts.SerialiseV3(&acc)
err = domains.DomainPut(kv.AccountsDomain, addr, nil, buf, nil, 0)
require.NoError(t, err)

Expand Down Expand Up @@ -335,8 +345,13 @@ func aggregatorV3_RestartOnDatadir(t *testing.T, rc runCfg) {
require.NoError(t, err)
require.EqualValues(t, length.Hash, n)
//keys[txNum-1] = append(addr, loc...)

buf := types.EncodeAccountBytesV3(1, uint256.NewInt(rnd.Uint64()), nil, 0)
acc := accounts.Account{
Nonce: 1,
Balance: *uint256.NewInt(rnd.Uint64()),
CodeHash: common.Hash{},
Incarnation: 0,
}
buf := accounts.SerialiseV3(&acc)
err = domains.DomainPut(kv.AccountsDomain, addr, nil, buf, nil, 0)
require.NoError(t, err)

Expand Down Expand Up @@ -678,7 +693,13 @@ func generateSharedDomainsUpdatesForTx(t *testing.T, domains *SharedDomains, txN
r := rnd.IntN(101)
switch {
case r <= 33:
buf := types.EncodeAccountBytesV3(txNum, uint256.NewInt(txNum*100_000), nil, 0)
acc := accounts.Account{
Nonce: txNum,
Balance: *uint256.NewInt(txNum * 100_000),
CodeHash: common.Hash{},
Incarnation: 0,
}
buf := accounts.SerialiseV3(&acc)
prev, step, err := domains.GetLatest(kv.AccountsDomain, key)
require.NoError(t, err)

Expand Down Expand Up @@ -723,7 +744,13 @@ func generateSharedDomainsUpdatesForTx(t *testing.T, domains *SharedDomains, txN
require.NoError(t, err)
if prev == nil {
usedKeys[string(key)] = struct{}{}
buf := types.EncodeAccountBytesV3(txNum, uint256.NewInt(txNum*100_000), nil, 0)
acc := accounts.Account{
Nonce: txNum,
Balance: *uint256.NewInt(txNum * 100_000),
CodeHash: common.Hash{},
Incarnation: 0,
}
buf := accounts.SerialiseV3(&acc)
err = domains.DomainPut(kv.AccountsDomain, key, nil, buf, prev, step)
require.NoError(t, err)
}
Expand Down Expand Up @@ -788,7 +815,13 @@ func TestAggregatorV3_RestartOnFiles(t *testing.T) {
require.NoError(t, err)
require.EqualValues(t, length.Hash, n)

buf := types.EncodeAccountBytesV3(txNum, uint256.NewInt(1000000000000), nil, 0)
acc := accounts.Account{
Nonce: txNum,
Balance: *uint256.NewInt(1000000000000),
CodeHash: common.Hash{},
Incarnation: 0,
}
buf := accounts.SerialiseV3(&acc)
err = domains.DomainPut(kv.AccountsDomain, addr, nil, buf[:], nil, 0)
require.NoError(t, err)

Expand Down Expand Up @@ -853,9 +886,11 @@ func TestAggregatorV3_RestartOnFiles(t *testing.T) {
//fmt.Printf("%x [%d/%d]", key, miss, i+1) // txnum starts from 1
continue
}
nonce, _, _ := types.DecodeAccountBytesV3(stored)
acc := accounts.Account{}
err = accounts.DeserialiseV3(&acc, stored)
require.NoError(t, err)

require.EqualValues(t, i+1, int(nonce))
require.EqualValues(t, i+1, int(acc.Nonce))

storedV, _, found, err := ac.GetLatest(kv.StorageDomain, key, newTx)
require.NoError(t, err)
Expand Down Expand Up @@ -929,7 +964,13 @@ func TestAggregatorV3_ReplaceCommittedKeys(t *testing.T) {
require.EqualValues(t, length.Hash, n)
keys[txNum-1] = append(addr, loc...)

buf := types.EncodeAccountBytesV3(1, uint256.NewInt(0), nil, 0)
acc := accounts.Account{
Nonce: 1,
Balance: *uint256.NewInt(0),
CodeHash: common.Hash{},
Incarnation: 0,
}
buf := accounts.SerialiseV3(&acc)

err = domains.DomainPut(kv.AccountsDomain, addr, nil, buf, prev1, 0)
require.NoError(t, err)
Expand Down Expand Up @@ -1161,7 +1202,13 @@ func TestAggregatorV3_SharedDomains(t *testing.T) {
}

for j := 0; j < len(keys); j++ {
buf := types.EncodeAccountBytesV3(uint64(i), uint256.NewInt(uint64(i*100_000)), nil, 0)
acc := accounts.Account{
Nonce: uint64(i),
Balance: *uint256.NewInt(uint64(i * 100_000)),
CodeHash: common.Hash{},
Incarnation: 0,
}
buf := accounts.SerialiseV3(&acc)
prev, step, err := domains.GetLatest(kv.AccountsDomain, keys[j])
require.NoError(t, err)

Expand Down Expand Up @@ -1196,7 +1243,13 @@ func TestAggregatorV3_SharedDomains(t *testing.T) {
domains.SetTxNum(uint64(i))

for j := 0; j < len(keys); j++ {
buf := types.EncodeAccountBytesV3(uint64(i), uint256.NewInt(uint64(i*100_000)), nil, 0)
acc := accounts.Account{
Nonce: uint64(i),
Balance: *uint256.NewInt(uint64(i * 100_000)),
CodeHash: common.Hash{},
Incarnation: 0,
}
buf := accounts.SerialiseV3(&acc)
prev, step, _, err := mc.GetLatest(kv.AccountsDomain, keys[j], rwTx)
require.NoError(t, err)

Expand Down Expand Up @@ -1233,7 +1286,13 @@ func TestAggregatorV3_SharedDomains(t *testing.T) {
domains.SetTxNum(uint64(i))

for j := 0; j < len(keys); j++ {
buf := types.EncodeAccountBytesV3(uint64(i), uint256.NewInt(uint64(i*100_000)), nil, 0)
acc := accounts.Account{
Nonce: uint64(i),
Balance: *uint256.NewInt(uint64(i * 100_000)),
CodeHash: common.Hash{},
Incarnation: 0,
}
buf := accounts.SerialiseV3(&acc)
prev, step, _, err := mc.GetLatest(kv.AccountsDomain, keys[j], rwTx)
require.NoError(t, err)

Expand All @@ -1256,8 +1315,9 @@ func Test_helper_decodeAccountv3Bytes(t *testing.T) {
input, err := hex.DecodeString("000114000101")
require.NoError(t, err)

n, b, ch := types.DecodeAccountBytesV3(input)
fmt.Printf("input %x nonce %d balance %d codeHash %d\n", input, n, b.Uint64(), ch)
acc := accounts.Account{}
_ = accounts.DeserialiseV3(&acc, input)
fmt.Printf("input %x nonce %d balance %d codeHash %d\n", input, acc.Nonce, acc.Balance.Uint64(), acc.CodeHash.Bytes())
}

func TestAggregator_RebuildCommitmentBasedOnFiles(t *testing.T) {
Expand Down
Loading

0 comments on commit f1f235f

Please sign in to comment.