2022-07-06 06:49:00 +00:00
|
|
|
package state
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"container/heap"
|
2022-08-14 11:06:32 +00:00
|
|
|
"context"
|
2022-07-06 06:49:00 +00:00
|
|
|
"encoding/binary"
|
|
|
|
"fmt"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/holiman/uint256"
|
2022-08-31 06:34:16 +00:00
|
|
|
common2 "github.com/ledgerwatch/erigon-lib/common"
|
2022-08-14 11:06:32 +00:00
|
|
|
"github.com/ledgerwatch/erigon-lib/common/length"
|
|
|
|
"github.com/ledgerwatch/erigon-lib/etl"
|
2022-07-06 06:49:00 +00:00
|
|
|
"github.com/ledgerwatch/erigon-lib/kv"
|
2022-07-28 11:16:37 +00:00
|
|
|
libstate "github.com/ledgerwatch/erigon-lib/state"
|
2022-11-30 01:31:13 +00:00
|
|
|
"github.com/ledgerwatch/erigon/cmd/state/exec22"
|
2022-07-06 06:49:00 +00:00
|
|
|
"github.com/ledgerwatch/erigon/common"
|
|
|
|
"github.com/ledgerwatch/erigon/common/dbutils"
|
|
|
|
"github.com/ledgerwatch/erigon/core/types/accounts"
|
2022-08-14 11:06:32 +00:00
|
|
|
"github.com/ledgerwatch/erigon/turbo/shards"
|
2022-12-04 11:15:28 +00:00
|
|
|
btree2 "github.com/tidwall/btree"
|
2022-11-20 05:34:45 +00:00
|
|
|
atomic2 "go.uber.org/atomic"
|
2022-07-06 06:49:00 +00:00
|
|
|
)
|
|
|
|
|
2022-07-23 17:39:08 +00:00
|
|
|
const CodeSizeTable = "CodeSize"
|
|
|
|
|
2022-07-28 11:16:37 +00:00
|
|
|
type State22 struct {
|
2022-07-23 17:39:08 +00:00
|
|
|
lock sync.RWMutex
|
|
|
|
receiveWork *sync.Cond
|
2022-11-30 01:31:13 +00:00
|
|
|
triggers map[uint64]*exec22.TxTask
|
2022-07-23 17:39:08 +00:00
|
|
|
senderTxNums map[common.Address]uint64
|
|
|
|
triggerLock sync.RWMutex
|
2022-11-30 01:31:13 +00:00
|
|
|
queue exec22.TxTaskQueue
|
2022-07-23 17:39:08 +00:00
|
|
|
queueLock sync.Mutex
|
2022-12-04 11:15:28 +00:00
|
|
|
changes map[string]*btree2.BTreeG[statePair]
|
2022-07-23 17:39:08 +00:00
|
|
|
sizeEstimate uint64
|
2022-11-20 05:34:45 +00:00
|
|
|
txsDone *atomic2.Uint64
|
2022-07-23 17:39:08 +00:00
|
|
|
finished bool
|
2022-12-04 11:15:28 +00:00
|
|
|
|
|
|
|
applyStateHint *btree2.PathHint //only for apply func (it always work in 1-thread), only for kv.PlainState table
|
2022-07-23 17:39:08 +00:00
|
|
|
}
|
|
|
|
|
2022-10-13 05:09:14 +00:00
|
|
|
type statePair struct {
|
2022-10-12 10:23:10 +00:00
|
|
|
key, val []byte
|
2022-07-23 17:39:08 +00:00
|
|
|
}
|
|
|
|
|
2022-10-13 05:09:14 +00:00
|
|
|
func stateItemLess(i, j statePair) bool {
|
2022-07-23 17:39:08 +00:00
|
|
|
return bytes.Compare(i.key, j.key) < 0
|
|
|
|
}
|
|
|
|
|
2022-07-28 11:16:37 +00:00
|
|
|
func NewState22() *State22 {
|
|
|
|
rs := &State22{
|
2022-12-04 11:15:28 +00:00
|
|
|
triggers: map[uint64]*exec22.TxTask{},
|
|
|
|
senderTxNums: map[common.Address]uint64{},
|
|
|
|
changes: map[string]*btree2.BTreeG[statePair]{},
|
|
|
|
txsDone: atomic2.NewUint64(0),
|
|
|
|
applyStateHint: &btree2.PathHint{},
|
2022-07-06 06:49:00 +00:00
|
|
|
}
|
2022-07-23 17:39:08 +00:00
|
|
|
rs.receiveWork = sync.NewCond(&rs.queueLock)
|
2022-07-06 06:49:00 +00:00
|
|
|
return rs
|
|
|
|
}
|
|
|
|
|
2022-12-04 11:15:28 +00:00
|
|
|
func (rs *State22) putHint(table string, key, val []byte, hint *btree2.PathHint) {
|
|
|
|
t, ok := rs.changes[table]
|
|
|
|
if !ok {
|
|
|
|
t = btree2.NewBTreeGOptions[statePair](stateItemLess, btree2.Options{Degree: 128, NoLocks: true})
|
|
|
|
rs.changes[table] = t
|
|
|
|
}
|
|
|
|
old, ok := t.SetHint(statePair{key: key, val: val}, hint)
|
|
|
|
rs.sizeEstimate += btreeOverhead + uint64(len(key)) + uint64(len(val))
|
|
|
|
if ok {
|
|
|
|
rs.sizeEstimate -= btreeOverhead + uint64(len(old.key)) + uint64(len(old.val))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-28 11:16:37 +00:00
|
|
|
func (rs *State22) put(table string, key, val []byte) {
|
2022-07-06 06:49:00 +00:00
|
|
|
t, ok := rs.changes[table]
|
|
|
|
if !ok {
|
2022-12-04 11:15:28 +00:00
|
|
|
t = btree2.NewBTreeGOptions[statePair](stateItemLess, btree2.Options{Degree: 128, NoLocks: true})
|
2022-07-06 06:49:00 +00:00
|
|
|
rs.changes[table] = t
|
|
|
|
}
|
2022-12-04 11:15:28 +00:00
|
|
|
old, ok := t.Set(statePair{key: key, val: val})
|
2022-11-04 08:09:29 +00:00
|
|
|
rs.sizeEstimate += btreeOverhead + uint64(len(key)) + uint64(len(val))
|
2022-10-28 14:13:53 +00:00
|
|
|
if ok {
|
2022-11-04 08:09:29 +00:00
|
|
|
rs.sizeEstimate -= btreeOverhead + uint64(len(old.key)) + uint64(len(old.val))
|
2022-10-28 14:13:53 +00:00
|
|
|
}
|
2022-07-06 06:49:00 +00:00
|
|
|
}
|
|
|
|
|
2022-11-04 08:09:29 +00:00
|
|
|
const btreeOverhead = 16
|
|
|
|
|
2022-12-04 11:15:28 +00:00
|
|
|
func (rs *State22) GetHint(table string, key []byte, hint *btree2.PathHint) []byte {
|
|
|
|
rs.lock.RLock()
|
|
|
|
v := rs.getHint(table, key, hint)
|
|
|
|
rs.lock.RUnlock()
|
|
|
|
return v
|
|
|
|
}
|
2022-07-28 11:16:37 +00:00
|
|
|
func (rs *State22) Get(table string, key []byte) []byte {
|
2022-07-06 06:49:00 +00:00
|
|
|
rs.lock.RLock()
|
2022-11-20 05:34:45 +00:00
|
|
|
v := rs.get(table, key)
|
|
|
|
rs.lock.RUnlock()
|
|
|
|
return v
|
2022-07-06 06:49:00 +00:00
|
|
|
}
|
|
|
|
|
2022-12-04 11:15:28 +00:00
|
|
|
func (rs *State22) getHint(table string, key []byte, hint *btree2.PathHint) []byte {
|
|
|
|
t, ok := rs.changes[table]
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if i, ok := t.GetHint(statePair{key: key}, hint); ok {
|
|
|
|
return i.val
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2022-07-28 11:16:37 +00:00
|
|
|
func (rs *State22) get(table string, key []byte) []byte {
|
2022-07-06 06:49:00 +00:00
|
|
|
t, ok := rs.changes[table]
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
2022-10-21 14:02:18 +00:00
|
|
|
if i, ok := t.Get(statePair{key: key}); ok {
|
2022-07-23 17:39:08 +00:00
|
|
|
return i.val
|
|
|
|
}
|
|
|
|
return nil
|
2022-07-06 06:49:00 +00:00
|
|
|
}
|
|
|
|
|
2022-07-28 11:16:37 +00:00
|
|
|
func (rs *State22) Flush(rwTx kv.RwTx) error {
|
2022-07-06 06:49:00 +00:00
|
|
|
rs.lock.Lock()
|
|
|
|
defer rs.lock.Unlock()
|
|
|
|
for table, t := range rs.changes {
|
2022-11-02 03:04:12 +00:00
|
|
|
c, err := rwTx.RwCursor(table)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-12-04 11:15:28 +00:00
|
|
|
t.Walk(func(items []statePair) bool {
|
|
|
|
for _, item := range items {
|
|
|
|
if len(item.val) == 0 {
|
|
|
|
if err = c.Delete(item.key); err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
//fmt.Printf("Flush [%x]=>\n", item.key)
|
|
|
|
} else {
|
|
|
|
if err = c.Put(item.key, item.val); err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
//fmt.Printf("Flush [%x]=>[%x]\n", item.key, item.val)
|
2022-07-06 06:49:00 +00:00
|
|
|
}
|
|
|
|
}
|
2022-07-23 17:39:08 +00:00
|
|
|
return true
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2022-07-06 06:49:00 +00:00
|
|
|
}
|
2022-12-04 11:15:28 +00:00
|
|
|
t.Clear()
|
2022-07-06 06:49:00 +00:00
|
|
|
}
|
|
|
|
rs.sizeEstimate = 0
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-11-30 01:31:13 +00:00
|
|
|
func (rs *State22) Schedule() (*exec22.TxTask, bool) {
|
2022-07-23 17:39:08 +00:00
|
|
|
rs.queueLock.Lock()
|
|
|
|
defer rs.queueLock.Unlock()
|
|
|
|
for !rs.finished && rs.queue.Len() == 0 {
|
|
|
|
rs.receiveWork.Wait()
|
2022-07-06 06:49:00 +00:00
|
|
|
}
|
|
|
|
if rs.queue.Len() > 0 {
|
2022-11-30 01:31:13 +00:00
|
|
|
return heap.Pop(&rs.queue).(*exec22.TxTask), true
|
2022-07-06 06:49:00 +00:00
|
|
|
}
|
2022-07-31 20:54:23 +00:00
|
|
|
return nil, false
|
2022-07-06 06:49:00 +00:00
|
|
|
}
|
|
|
|
|
2022-11-30 01:31:13 +00:00
|
|
|
func (rs *State22) RegisterSender(txTask *exec22.TxTask) bool {
|
2022-07-23 17:39:08 +00:00
|
|
|
rs.triggerLock.Lock()
|
|
|
|
defer rs.triggerLock.Unlock()
|
2022-07-06 06:49:00 +00:00
|
|
|
lastTxNum, deferral := rs.senderTxNums[*txTask.Sender]
|
|
|
|
if deferral {
|
|
|
|
// Transactions with the same sender have obvious data dependency, no point running it before lastTxNum
|
|
|
|
// So we add this data dependency as a trigger
|
|
|
|
//fmt.Printf("trigger[%d] sender [%x]<=%x\n", lastTxNum, *txTask.Sender, txTask.Tx.Hash())
|
|
|
|
rs.triggers[lastTxNum] = txTask
|
|
|
|
}
|
|
|
|
//fmt.Printf("senderTxNums[%x]=%d\n", *txTask.Sender, txTask.TxNum)
|
|
|
|
rs.senderTxNums[*txTask.Sender] = txTask.TxNum
|
|
|
|
return !deferral
|
|
|
|
}
|
|
|
|
|
2022-07-28 11:16:37 +00:00
|
|
|
func (rs *State22) CommitTxNum(sender *common.Address, txNum uint64) uint64 {
|
2022-12-05 03:23:49 +00:00
|
|
|
rs.txsDone.Add(1)
|
|
|
|
|
2022-07-23 17:39:08 +00:00
|
|
|
rs.triggerLock.Lock()
|
|
|
|
defer rs.triggerLock.Unlock()
|
|
|
|
count := uint64(0)
|
2022-07-06 06:49:00 +00:00
|
|
|
if triggered, ok := rs.triggers[txNum]; ok {
|
2022-10-28 01:47:45 +00:00
|
|
|
rs.queuePush(triggered)
|
2022-07-23 17:39:08 +00:00
|
|
|
rs.receiveWork.Signal()
|
|
|
|
count++
|
2022-07-06 06:49:00 +00:00
|
|
|
delete(rs.triggers, txNum)
|
|
|
|
}
|
|
|
|
if sender != nil {
|
|
|
|
if lastTxNum, ok := rs.senderTxNums[*sender]; ok && lastTxNum == txNum {
|
|
|
|
// This is the last transaction so far with this sender, remove
|
|
|
|
delete(rs.senderTxNums, *sender)
|
|
|
|
}
|
|
|
|
}
|
2022-07-23 17:39:08 +00:00
|
|
|
return count
|
2022-07-06 06:49:00 +00:00
|
|
|
}
|
|
|
|
|
2022-11-30 01:31:13 +00:00
|
|
|
func (rs *State22) queuePush(t *exec22.TxTask) {
|
2022-10-28 01:47:45 +00:00
|
|
|
rs.queueLock.Lock()
|
|
|
|
heap.Push(&rs.queue, t)
|
|
|
|
rs.queueLock.Unlock()
|
|
|
|
}
|
|
|
|
|
2022-11-30 01:31:13 +00:00
|
|
|
func (rs *State22) AddWork(txTask *exec22.TxTask) {
|
2022-07-23 17:39:08 +00:00
|
|
|
txTask.BalanceIncreaseSet = nil
|
2022-11-30 01:31:13 +00:00
|
|
|
if txTask.ReadLists != nil {
|
|
|
|
returnReadList(txTask.ReadLists)
|
|
|
|
txTask.ReadLists = nil
|
|
|
|
}
|
|
|
|
if txTask.WriteLists != nil {
|
|
|
|
returnWriteList(txTask.WriteLists)
|
|
|
|
txTask.WriteLists = nil
|
|
|
|
}
|
2022-07-23 17:39:08 +00:00
|
|
|
txTask.ResultsSize = 0
|
2022-07-31 20:54:23 +00:00
|
|
|
txTask.Logs = nil
|
|
|
|
txTask.TraceFroms = nil
|
|
|
|
txTask.TraceTos = nil
|
2022-10-25 09:08:01 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
txTask.ReadLists = nil
|
|
|
|
txTask.WriteLists = nil
|
|
|
|
txTask.AccountPrevs = nil
|
|
|
|
txTask.AccountDels = nil
|
|
|
|
txTask.StoragePrevs = nil
|
|
|
|
txTask.CodePrevs = nil
|
|
|
|
*/
|
2022-10-28 01:47:45 +00:00
|
|
|
rs.queuePush(txTask)
|
2022-07-23 17:39:08 +00:00
|
|
|
rs.receiveWork.Signal()
|
2022-07-06 06:49:00 +00:00
|
|
|
}
|
|
|
|
|
2022-07-28 11:16:37 +00:00
|
|
|
func (rs *State22) Finish() {
|
2022-07-23 17:39:08 +00:00
|
|
|
rs.queueLock.Lock()
|
|
|
|
defer rs.queueLock.Unlock()
|
|
|
|
rs.finished = true
|
|
|
|
rs.receiveWork.Broadcast()
|
|
|
|
}
|
|
|
|
|
2022-11-30 01:31:13 +00:00
|
|
|
func (rs *State22) appplyState1(roTx kv.Tx, txTask *exec22.TxTask, agg *libstate.Aggregator22) error {
|
2022-11-21 03:38:22 +00:00
|
|
|
rs.lock.RLock()
|
|
|
|
defer rs.lock.RUnlock()
|
2022-10-25 09:08:01 +00:00
|
|
|
|
|
|
|
if len(txTask.AccountDels) > 0 {
|
2022-07-28 11:16:37 +00:00
|
|
|
cursor, err := roTx.Cursor(kv.PlainState)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer cursor.Close()
|
2022-10-25 09:08:01 +00:00
|
|
|
addr1 := make([]byte, 20+8)
|
|
|
|
search := statePair{}
|
2022-08-31 06:34:16 +00:00
|
|
|
psChanges := rs.changes[kv.PlainState]
|
2022-10-25 09:08:01 +00:00
|
|
|
for addrS, original := range txTask.AccountDels {
|
|
|
|
addr := []byte(addrS)
|
|
|
|
copy(addr1, addr)
|
|
|
|
binary.BigEndian.PutUint64(addr1[len(addr):], original.Incarnation)
|
|
|
|
|
|
|
|
prev := accounts.Serialise2(original)
|
|
|
|
if err := agg.AddAccountPrev(addr, prev); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
codeHashBytes := original.CodeHash.Bytes()
|
|
|
|
codePrev := rs.get(kv.Code, codeHashBytes)
|
|
|
|
if codePrev == nil {
|
|
|
|
var err error
|
|
|
|
codePrev, err = roTx.GetOne(kv.Code, codeHashBytes)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2022-08-31 06:34:16 +00:00
|
|
|
}
|
2022-10-25 09:08:01 +00:00
|
|
|
}
|
|
|
|
if err := agg.AddCodePrev(addr, codePrev); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Iterate over storage
|
|
|
|
var k, v []byte
|
|
|
|
_, _ = k, v
|
|
|
|
var e error
|
|
|
|
if k, v, e = cursor.Seek(addr1); err != nil {
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
if !bytes.HasPrefix(k, addr1) {
|
|
|
|
k = nil
|
|
|
|
}
|
|
|
|
if psChanges != nil {
|
|
|
|
search.key = addr1
|
2022-12-04 11:15:28 +00:00
|
|
|
psChanges.Ascend(search, func(item statePair) bool {
|
2022-10-25 09:08:01 +00:00
|
|
|
if !bytes.HasPrefix(item.key, addr1) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for ; e == nil && k != nil && bytes.HasPrefix(k, addr1) && bytes.Compare(k, item.key) <= 0; k, v, e = cursor.Next() {
|
|
|
|
if !bytes.Equal(k, item.key) {
|
|
|
|
// Skip the cursor item when the key is equal, i.e. prefer the item from the changes tree
|
|
|
|
if e = agg.AddStoragePrev(addr, k[28:], v); e != nil {
|
|
|
|
return false
|
|
|
|
}
|
2022-08-31 06:34:16 +00:00
|
|
|
}
|
2022-07-28 11:16:37 +00:00
|
|
|
}
|
2022-10-25 09:08:01 +00:00
|
|
|
if e != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if e = agg.AddStoragePrev(addr, item.key[28:], item.val); e != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
}
|
|
|
|
for ; e == nil && k != nil && bytes.HasPrefix(k, addr1); k, v, e = cursor.Next() {
|
|
|
|
if e = agg.AddStoragePrev(addr, k[28:], v); e != nil {
|
|
|
|
return e
|
2022-07-28 11:16:37 +00:00
|
|
|
}
|
2022-10-25 09:08:01 +00:00
|
|
|
}
|
|
|
|
if e != nil {
|
2022-07-28 11:16:37 +00:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-10-25 09:08:01 +00:00
|
|
|
|
|
|
|
k := make([]byte, 20+8)
|
2022-07-31 20:54:23 +00:00
|
|
|
for addrS, incarnation := range txTask.CodePrevs {
|
|
|
|
addr := []byte(addrS)
|
2022-10-25 09:08:01 +00:00
|
|
|
copy(k, addr)
|
|
|
|
binary.BigEndian.PutUint64(k[20:], incarnation)
|
|
|
|
|
2022-07-31 20:54:23 +00:00
|
|
|
codeHash := rs.get(kv.PlainContractCode, k)
|
|
|
|
if codeHash == nil {
|
|
|
|
var err error
|
|
|
|
codeHash, err = roTx.GetOne(kv.PlainContractCode, k)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var codePrev []byte
|
|
|
|
if codeHash != nil {
|
|
|
|
codePrev = rs.get(kv.Code, codeHash)
|
|
|
|
if codePrev == nil {
|
|
|
|
var err error
|
|
|
|
codePrev, err = roTx.GetOne(kv.Code, codeHash)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err := agg.AddCodePrev(addr, codePrev); err != nil {
|
2022-07-28 11:16:37 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2022-11-21 03:38:22 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-11-30 01:31:13 +00:00
|
|
|
func (rs *State22) appplyState(roTx kv.Tx, txTask *exec22.TxTask, agg *libstate.Aggregator22) error {
|
2022-11-21 03:38:22 +00:00
|
|
|
emptyRemoval := txTask.Rules.IsSpuriousDragon
|
|
|
|
rs.lock.Lock()
|
|
|
|
defer rs.lock.Unlock()
|
|
|
|
|
|
|
|
for addr := range txTask.BalanceIncreaseSet {
|
|
|
|
addrBytes := addr.Bytes()
|
|
|
|
increase := txTask.BalanceIncreaseSet[addr]
|
2022-12-04 11:15:28 +00:00
|
|
|
enc0 := rs.getHint(kv.PlainState, addrBytes, rs.applyStateHint)
|
2022-11-21 03:38:22 +00:00
|
|
|
if enc0 == nil {
|
|
|
|
var err error
|
|
|
|
enc0, err = roTx.GetOne(kv.PlainState, addrBytes)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var a accounts.Account
|
|
|
|
if err := a.DecodeForStorage(enc0); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(enc0) > 0 {
|
|
|
|
// Need to convert before balance increase
|
|
|
|
enc0 = accounts.Serialise2(&a)
|
|
|
|
}
|
|
|
|
a.Balance.Add(&a.Balance, &increase)
|
|
|
|
var enc1 []byte
|
|
|
|
if emptyRemoval && a.Nonce == 0 && a.Balance.IsZero() && a.IsEmptyCodeHash() {
|
|
|
|
enc1 = []byte{}
|
|
|
|
} else {
|
|
|
|
enc1 = make([]byte, a.EncodingLengthForStorage())
|
|
|
|
a.EncodeForStorage(enc1)
|
|
|
|
}
|
2022-12-04 11:15:28 +00:00
|
|
|
rs.putHint(kv.PlainState, addrBytes, enc1, rs.applyStateHint)
|
2022-11-21 03:38:22 +00:00
|
|
|
if err := agg.AddAccountPrev(addrBytes, enc0); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-20 05:34:45 +00:00
|
|
|
if txTask.WriteLists != nil {
|
|
|
|
for table, list := range txTask.WriteLists {
|
2022-12-04 11:15:28 +00:00
|
|
|
if table == kv.PlainState {
|
|
|
|
for i, key := range list.Keys {
|
|
|
|
rs.putHint(table, key, list.Vals[i], rs.applyStateHint)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for i, key := range list.Keys {
|
|
|
|
rs.put(table, key, list.Vals[i])
|
|
|
|
}
|
|
|
|
|
2022-11-20 05:34:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-11-30 01:31:13 +00:00
|
|
|
func (rs *State22) ApplyState(roTx kv.Tx, txTask *exec22.TxTask, agg *libstate.Aggregator22) error {
|
2022-11-20 05:34:45 +00:00
|
|
|
agg.SetTxNum(txTask.TxNum)
|
2022-11-21 03:38:22 +00:00
|
|
|
if err := rs.appplyState1(roTx, txTask, agg); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-11-20 05:34:45 +00:00
|
|
|
if err := rs.appplyState(roTx, txTask, agg); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-11-30 01:31:13 +00:00
|
|
|
|
|
|
|
returnReadList(txTask.ReadLists)
|
|
|
|
returnWriteList(txTask.WriteLists)
|
|
|
|
|
|
|
|
txTask.ReadLists, txTask.WriteLists = nil, nil
|
2022-11-21 03:38:22 +00:00
|
|
|
return nil
|
|
|
|
}
|
2022-11-20 05:34:45 +00:00
|
|
|
|
2022-11-30 01:31:13 +00:00
|
|
|
func (rs *State22) ApplyHistory(roTx kv.Tx, txTask *exec22.TxTask, agg *libstate.Aggregator22) error {
|
2022-11-20 05:34:45 +00:00
|
|
|
for addrS, enc0 := range txTask.AccountPrevs {
|
|
|
|
if err := agg.AddAccountPrev([]byte(addrS), enc0); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for compositeS, val := range txTask.StoragePrevs {
|
|
|
|
composite := []byte(compositeS)
|
|
|
|
if err := agg.AddStoragePrev(composite[:20], composite[28:], val); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2022-07-31 20:54:23 +00:00
|
|
|
if txTask.TraceFroms != nil {
|
|
|
|
for addr := range txTask.TraceFroms {
|
2022-10-21 06:45:20 +00:00
|
|
|
if err := agg.AddTraceFrom(addr[:]); err != nil {
|
2022-07-31 20:54:23 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if txTask.TraceTos != nil {
|
|
|
|
for addr := range txTask.TraceTos {
|
2022-10-21 06:45:20 +00:00
|
|
|
if err := agg.AddTraceTo(addr[:]); err != nil {
|
2022-07-31 20:54:23 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, log := range txTask.Logs {
|
|
|
|
if err := agg.AddLogAddr(log.Address[:]); err != nil {
|
|
|
|
return fmt.Errorf("adding event log for addr %x: %w", log.Address, err)
|
|
|
|
}
|
|
|
|
for _, topic := range log.Topics {
|
|
|
|
if err := agg.AddLogTopic(topic[:]); err != nil {
|
|
|
|
return fmt.Errorf("adding event log for topic %x: %w", topic, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-07-23 17:39:08 +00:00
|
|
|
return nil
|
2022-07-06 06:49:00 +00:00
|
|
|
}
|
|
|
|
|
2022-08-14 11:06:32 +00:00
|
|
|
func recoverCodeHashPlain(acc *accounts.Account, db kv.Tx, key []byte) {
|
|
|
|
var address common.Address
|
|
|
|
copy(address[:], key)
|
|
|
|
if acc.Incarnation > 0 && acc.IsEmptyCodeHash() {
|
|
|
|
if codeHash, err2 := db.GetOne(kv.PlainContractCode, dbutils.PlainGenerateStoragePrefix(address[:], acc.Incarnation)); err2 == nil {
|
|
|
|
copy(acc.CodeHash[:], codeHash)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rs *State22) Unwind(ctx context.Context, tx kv.RwTx, txUnwindTo uint64, agg *libstate.Aggregator22, accumulator *shards.Accumulator) error {
|
2022-08-17 09:24:11 +00:00
|
|
|
agg.SetTx(tx)
|
2022-08-14 11:06:32 +00:00
|
|
|
var currentInc uint64
|
|
|
|
if err := agg.Unwind(ctx, txUnwindTo, func(k, v []byte, table etl.CurrentTableReader, next etl.LoadNextFunc) error {
|
2022-08-31 06:34:16 +00:00
|
|
|
if len(k) == length.Addr {
|
2022-08-14 11:06:32 +00:00
|
|
|
if len(v) > 0 {
|
|
|
|
var acc accounts.Account
|
2022-08-24 04:37:58 +00:00
|
|
|
if err := accounts.Deserialise2(&acc, v); err != nil {
|
2022-08-14 11:06:32 +00:00
|
|
|
return fmt.Errorf("%w, %x", err, v)
|
|
|
|
}
|
|
|
|
currentInc = acc.Incarnation
|
|
|
|
// Fetch the code hash
|
|
|
|
recoverCodeHashPlain(&acc, tx, k)
|
|
|
|
var address common.Address
|
|
|
|
copy(address[:], k)
|
|
|
|
|
|
|
|
// cleanup contract code bucket
|
2022-08-17 09:42:07 +00:00
|
|
|
original, err := NewPlainStateReader(tx).ReadAccountData(address)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("read account for %x: %w", address, err)
|
|
|
|
}
|
|
|
|
if original != nil {
|
|
|
|
// clean up all the code incarnations original incarnation and the new one
|
|
|
|
for incarnation := original.Incarnation; incarnation > acc.Incarnation && incarnation > 0; incarnation-- {
|
|
|
|
err = tx.Delete(kv.PlainContractCode, dbutils.PlainGenerateStoragePrefix(address[:], incarnation))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("writeAccountPlain for %x: %w", address, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-08-14 11:06:32 +00:00
|
|
|
|
|
|
|
newV := make([]byte, acc.EncodingLengthForStorage())
|
|
|
|
acc.EncodeForStorage(newV)
|
|
|
|
if accumulator != nil {
|
|
|
|
accumulator.ChangeAccount(address, acc.Incarnation, newV)
|
|
|
|
}
|
|
|
|
if err := next(k, k, newV); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
2022-08-31 06:34:16 +00:00
|
|
|
var address common.Address
|
|
|
|
copy(address[:], k)
|
|
|
|
original, err := NewPlainStateReader(tx).ReadAccountData(address)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if original != nil {
|
|
|
|
currentInc = original.Incarnation
|
|
|
|
} else {
|
|
|
|
currentInc = 1
|
|
|
|
}
|
|
|
|
|
2022-08-14 11:06:32 +00:00
|
|
|
if accumulator != nil {
|
|
|
|
accumulator.DeleteAccount(address)
|
|
|
|
}
|
|
|
|
if err := next(k, k, nil); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if accumulator != nil {
|
|
|
|
var address common.Address
|
|
|
|
var location common.Hash
|
|
|
|
copy(address[:], k[:length.Addr])
|
2022-08-31 06:34:16 +00:00
|
|
|
copy(location[:], k[length.Addr:])
|
|
|
|
accumulator.ChangeStorage(address, currentInc, location, common2.Copy(v))
|
2022-08-14 11:06:32 +00:00
|
|
|
}
|
2022-08-15 07:36:57 +00:00
|
|
|
newKeys := dbutils.PlainGenerateCompositeStorageKey(k[:20], currentInc, k[20:])
|
2022-08-14 11:06:32 +00:00
|
|
|
if len(v) > 0 {
|
|
|
|
if err := next(k, newKeys, v); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if err := next(k, newKeys, nil); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-11-20 05:34:45 +00:00
|
|
|
func (rs *State22) DoneCount() uint64 { return rs.txsDone.Load() }
|
2022-07-06 06:49:00 +00:00
|
|
|
|
2022-07-28 11:16:37 +00:00
|
|
|
func (rs *State22) SizeEstimate() uint64 {
|
2022-07-06 06:49:00 +00:00
|
|
|
rs.lock.RLock()
|
2022-10-28 01:47:45 +00:00
|
|
|
r := rs.sizeEstimate
|
|
|
|
rs.lock.RUnlock()
|
|
|
|
return r
|
2022-07-06 06:49:00 +00:00
|
|
|
}
|
|
|
|
|
2022-11-30 01:31:13 +00:00
|
|
|
func (rs *State22) ReadsValid(readLists map[string]*exec22.KvList) bool {
|
2022-10-21 06:45:20 +00:00
|
|
|
search := statePair{}
|
2022-12-04 11:15:28 +00:00
|
|
|
var t *btree2.BTreeG[statePair]
|
2022-10-21 06:45:20 +00:00
|
|
|
|
2022-07-06 06:49:00 +00:00
|
|
|
rs.lock.RLock()
|
|
|
|
defer rs.lock.RUnlock()
|
2022-07-23 17:39:08 +00:00
|
|
|
//fmt.Printf("ValidReads\n")
|
|
|
|
for table, list := range readLists {
|
|
|
|
//fmt.Printf("Table %s\n", table)
|
|
|
|
var ok bool
|
|
|
|
if table == CodeSizeTable {
|
|
|
|
t, ok = rs.changes[kv.Code]
|
|
|
|
} else {
|
|
|
|
t, ok = rs.changes[table]
|
|
|
|
}
|
2022-07-06 06:49:00 +00:00
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
2022-07-23 17:39:08 +00:00
|
|
|
for i, key := range list.Keys {
|
2022-10-21 06:45:20 +00:00
|
|
|
search.key = key
|
|
|
|
if item, ok := t.Get(search); ok {
|
2022-07-23 17:39:08 +00:00
|
|
|
//fmt.Printf("key [%x] => [%x] vs [%x]\n", key, val, rereadVal)
|
|
|
|
if table == CodeSizeTable {
|
2022-10-21 06:45:20 +00:00
|
|
|
if binary.BigEndian.Uint64(list.Vals[i]) != uint64(len(item.val)) {
|
2022-07-23 17:39:08 +00:00
|
|
|
return false
|
|
|
|
}
|
2022-10-21 06:45:20 +00:00
|
|
|
} else if !bytes.Equal(list.Vals[i], item.val) {
|
2022-07-06 06:49:00 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2022-07-28 11:16:37 +00:00
|
|
|
type StateWriter22 struct {
|
|
|
|
rs *State22
|
|
|
|
txNum uint64
|
2022-11-30 01:31:13 +00:00
|
|
|
writeLists map[string]*exec22.KvList
|
2022-07-28 11:16:37 +00:00
|
|
|
accountPrevs map[string][]byte
|
|
|
|
accountDels map[string]*accounts.Account
|
|
|
|
storagePrevs map[string][]byte
|
2022-07-31 20:54:23 +00:00
|
|
|
codePrevs map[string]uint64
|
2022-07-06 06:49:00 +00:00
|
|
|
}
|
|
|
|
|
2022-07-28 11:16:37 +00:00
|
|
|
func NewStateWriter22(rs *State22) *StateWriter22 {
|
|
|
|
return &StateWriter22{
|
2022-11-30 01:31:13 +00:00
|
|
|
rs: rs,
|
|
|
|
writeLists: newWriteList(),
|
2022-07-28 11:16:37 +00:00
|
|
|
accountPrevs: map[string][]byte{},
|
|
|
|
accountDels: map[string]*accounts.Account{},
|
|
|
|
storagePrevs: map[string][]byte{},
|
2022-07-31 20:54:23 +00:00
|
|
|
codePrevs: map[string]uint64{},
|
2022-07-06 06:49:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-28 11:16:37 +00:00
|
|
|
func (w *StateWriter22) SetTxNum(txNum uint64) {
|
2022-07-06 06:49:00 +00:00
|
|
|
w.txNum = txNum
|
|
|
|
}
|
|
|
|
|
2022-07-28 11:16:37 +00:00
|
|
|
func (w *StateWriter22) ResetWriteSet() {
|
2022-12-01 07:42:39 +00:00
|
|
|
w.writeLists = map[string]*exec22.KvList{
|
|
|
|
kv.PlainState: {},
|
|
|
|
kv.Code: {},
|
|
|
|
kv.PlainContractCode: {},
|
|
|
|
kv.IncarnationMap: {},
|
|
|
|
}
|
2022-07-28 11:16:37 +00:00
|
|
|
w.accountPrevs = map[string][]byte{}
|
|
|
|
w.accountDels = map[string]*accounts.Account{}
|
|
|
|
w.storagePrevs = map[string][]byte{}
|
2022-07-31 20:54:23 +00:00
|
|
|
w.codePrevs = map[string]uint64{}
|
2022-07-06 06:49:00 +00:00
|
|
|
}
|
|
|
|
|
2022-11-30 01:31:13 +00:00
|
|
|
func (w *StateWriter22) WriteSet() map[string]*exec22.KvList {
|
2022-10-23 12:22:33 +00:00
|
|
|
return w.writeLists
|
2022-07-06 06:49:00 +00:00
|
|
|
}
|
|
|
|
|
2022-07-31 20:54:23 +00:00
|
|
|
func (w *StateWriter22) PrevAndDels() (map[string][]byte, map[string]*accounts.Account, map[string][]byte, map[string]uint64) {
|
2022-07-28 11:16:37 +00:00
|
|
|
return w.accountPrevs, w.accountDels, w.storagePrevs, w.codePrevs
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *StateWriter22) UpdateAccountData(address common.Address, original, account *accounts.Account) error {
|
2022-07-06 06:49:00 +00:00
|
|
|
value := make([]byte, account.EncodingLengthForStorage())
|
|
|
|
account.EncodeForStorage(value)
|
|
|
|
//fmt.Printf("account [%x]=>{Balance: %d, Nonce: %d, Root: %x, CodeHash: %x} txNum: %d\n", address, &account.Balance, account.Nonce, account.Root, account.CodeHash, w.txNum)
|
2022-07-23 17:39:08 +00:00
|
|
|
w.writeLists[kv.PlainState].Keys = append(w.writeLists[kv.PlainState].Keys, address.Bytes())
|
|
|
|
w.writeLists[kv.PlainState].Vals = append(w.writeLists[kv.PlainState].Vals, value)
|
2022-07-28 11:16:37 +00:00
|
|
|
var prev []byte
|
|
|
|
if original.Initialised {
|
2022-08-24 04:37:58 +00:00
|
|
|
prev = accounts.Serialise2(original)
|
2022-07-28 11:16:37 +00:00
|
|
|
}
|
|
|
|
w.accountPrevs[string(address.Bytes())] = prev
|
2022-07-06 06:49:00 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-07-28 11:16:37 +00:00
|
|
|
func (w *StateWriter22) UpdateAccountCode(address common.Address, incarnation uint64, codeHash common.Hash, code []byte) error {
|
2022-07-23 17:39:08 +00:00
|
|
|
w.writeLists[kv.Code].Keys = append(w.writeLists[kv.Code].Keys, codeHash.Bytes())
|
|
|
|
w.writeLists[kv.Code].Vals = append(w.writeLists[kv.Code].Vals, code)
|
2022-07-06 06:49:00 +00:00
|
|
|
if len(code) > 0 {
|
|
|
|
//fmt.Printf("code [%x] => [%x] CodeHash: %x, txNum: %d\n", address, code, codeHash, w.txNum)
|
2022-07-23 17:39:08 +00:00
|
|
|
w.writeLists[kv.PlainContractCode].Keys = append(w.writeLists[kv.PlainContractCode].Keys, dbutils.PlainGenerateStoragePrefix(address[:], incarnation))
|
|
|
|
w.writeLists[kv.PlainContractCode].Vals = append(w.writeLists[kv.PlainContractCode].Vals, codeHash.Bytes())
|
2022-07-06 06:49:00 +00:00
|
|
|
}
|
2022-07-31 20:54:23 +00:00
|
|
|
w.codePrevs[string(address.Bytes())] = incarnation
|
2022-07-06 06:49:00 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-07-28 11:16:37 +00:00
|
|
|
func (w *StateWriter22) DeleteAccount(address common.Address, original *accounts.Account) error {
|
2022-07-23 17:39:08 +00:00
|
|
|
w.writeLists[kv.PlainState].Keys = append(w.writeLists[kv.PlainState].Keys, address.Bytes())
|
|
|
|
w.writeLists[kv.PlainState].Vals = append(w.writeLists[kv.PlainState].Vals, []byte{})
|
2022-07-06 06:49:00 +00:00
|
|
|
if original.Incarnation > 0 {
|
|
|
|
var b [8]byte
|
|
|
|
binary.BigEndian.PutUint64(b[:], original.Incarnation)
|
2022-07-23 17:39:08 +00:00
|
|
|
w.writeLists[kv.IncarnationMap].Keys = append(w.writeLists[kv.IncarnationMap].Keys, address.Bytes())
|
|
|
|
w.writeLists[kv.IncarnationMap].Vals = append(w.writeLists[kv.IncarnationMap].Vals, b[:])
|
2022-07-06 06:49:00 +00:00
|
|
|
}
|
2022-07-28 11:16:37 +00:00
|
|
|
if original.Initialised {
|
|
|
|
w.accountDels[string(address.Bytes())] = original
|
|
|
|
}
|
2022-07-06 06:49:00 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-07-28 11:16:37 +00:00
|
|
|
func (w *StateWriter22) WriteAccountStorage(address common.Address, incarnation uint64, key *common.Hash, original, value *uint256.Int) error {
|
2022-07-06 06:49:00 +00:00
|
|
|
if *original == *value {
|
|
|
|
return nil
|
|
|
|
}
|
2022-07-28 11:16:37 +00:00
|
|
|
composite := dbutils.PlainGenerateCompositeStorageKey(address.Bytes(), incarnation, key.Bytes())
|
|
|
|
w.writeLists[kv.PlainState].Keys = append(w.writeLists[kv.PlainState].Keys, composite)
|
2022-07-23 17:39:08 +00:00
|
|
|
w.writeLists[kv.PlainState].Vals = append(w.writeLists[kv.PlainState].Vals, value.Bytes())
|
2022-07-06 06:49:00 +00:00
|
|
|
//fmt.Printf("storage [%x] [%x] => [%x], txNum: %d\n", address, *key, v, w.txNum)
|
2022-07-28 11:16:37 +00:00
|
|
|
w.storagePrevs[string(composite)] = original.Bytes()
|
2022-07-06 06:49:00 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-07-28 11:16:37 +00:00
|
|
|
func (w *StateWriter22) CreateContract(address common.Address) error {
|
2022-07-06 06:49:00 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-07-28 11:16:37 +00:00
|
|
|
type StateReader22 struct {
|
2022-08-02 15:01:53 +00:00
|
|
|
tx kv.Tx
|
|
|
|
txNum uint64
|
|
|
|
trace bool
|
|
|
|
rs *State22
|
|
|
|
composite []byte
|
2022-11-30 01:31:13 +00:00
|
|
|
readLists map[string]*exec22.KvList
|
2022-12-04 11:15:28 +00:00
|
|
|
|
|
|
|
stateHint *btree2.PathHint
|
2022-07-06 06:49:00 +00:00
|
|
|
}
|
|
|
|
|
2022-07-28 11:16:37 +00:00
|
|
|
func NewStateReader22(rs *State22) *StateReader22 {
|
|
|
|
return &StateReader22{
|
2022-12-01 07:42:39 +00:00
|
|
|
rs: rs,
|
|
|
|
readLists: map[string]*exec22.KvList{
|
|
|
|
kv.PlainState: {},
|
|
|
|
kv.Code: {},
|
|
|
|
CodeSizeTable: {},
|
|
|
|
kv.IncarnationMap: {},
|
|
|
|
},
|
2022-12-04 11:15:28 +00:00
|
|
|
stateHint: &btree2.PathHint{},
|
2022-07-23 17:39:08 +00:00
|
|
|
}
|
2022-07-06 06:49:00 +00:00
|
|
|
}
|
|
|
|
|
2022-07-28 11:16:37 +00:00
|
|
|
func (r *StateReader22) SetTxNum(txNum uint64) {
|
2022-07-06 06:49:00 +00:00
|
|
|
r.txNum = txNum
|
|
|
|
}
|
|
|
|
|
2022-07-28 11:16:37 +00:00
|
|
|
func (r *StateReader22) SetTx(tx kv.Tx) {
|
2022-07-06 06:49:00 +00:00
|
|
|
r.tx = tx
|
|
|
|
}
|
|
|
|
|
2022-07-28 11:16:37 +00:00
|
|
|
func (r *StateReader22) ResetReadSet() {
|
2022-12-01 07:42:39 +00:00
|
|
|
r.readLists = map[string]*exec22.KvList{
|
|
|
|
kv.PlainState: {},
|
|
|
|
kv.Code: {},
|
|
|
|
CodeSizeTable: {},
|
|
|
|
kv.IncarnationMap: {},
|
|
|
|
}
|
2022-07-06 06:49:00 +00:00
|
|
|
}
|
|
|
|
|
2022-11-30 01:31:13 +00:00
|
|
|
func (r *StateReader22) ReadSet() map[string]*exec22.KvList {
|
2022-10-23 12:22:33 +00:00
|
|
|
return r.readLists
|
2022-07-06 06:49:00 +00:00
|
|
|
}
|
|
|
|
|
2022-07-28 11:16:37 +00:00
|
|
|
func (r *StateReader22) SetTrace(trace bool) {
|
2022-07-06 06:49:00 +00:00
|
|
|
r.trace = trace
|
|
|
|
}
|
|
|
|
|
2022-07-28 11:16:37 +00:00
|
|
|
func (r *StateReader22) ReadAccountData(address common.Address) (*accounts.Account, error) {
|
2022-10-13 05:09:14 +00:00
|
|
|
addr := address.Bytes()
|
2022-12-04 11:15:28 +00:00
|
|
|
enc := r.rs.GetHint(kv.PlainState, addr, r.stateHint)
|
2022-07-06 06:49:00 +00:00
|
|
|
if enc == nil {
|
|
|
|
var err error
|
2022-10-13 05:09:14 +00:00
|
|
|
enc, err = r.tx.GetOne(kv.PlainState, addr)
|
2022-07-06 06:49:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2022-10-13 05:09:14 +00:00
|
|
|
r.readLists[kv.PlainState].Keys = append(r.readLists[kv.PlainState].Keys, addr)
|
2022-08-31 06:34:16 +00:00
|
|
|
r.readLists[kv.PlainState].Vals = append(r.readLists[kv.PlainState].Vals, common2.Copy(enc))
|
2022-07-06 06:49:00 +00:00
|
|
|
if len(enc) == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
var a accounts.Account
|
|
|
|
if err := a.DecodeForStorage(enc); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if r.trace {
|
|
|
|
fmt.Printf("ReadAccountData [%x] => [nonce: %d, balance: %d, codeHash: %x], txNum: %d\n", address, a.Nonce, &a.Balance, a.CodeHash, r.txNum)
|
|
|
|
}
|
|
|
|
return &a, nil
|
|
|
|
}
|
|
|
|
|
2022-07-28 11:16:37 +00:00
|
|
|
func (r *StateReader22) ReadAccountStorage(address common.Address, incarnation uint64, key *common.Hash) ([]byte, error) {
|
2022-07-06 06:49:00 +00:00
|
|
|
if cap(r.composite) < 20+8+32 {
|
|
|
|
r.composite = make([]byte, 20+8+32)
|
|
|
|
} else if len(r.composite) != 20+8+32 {
|
|
|
|
r.composite = r.composite[:20+8+32]
|
|
|
|
}
|
|
|
|
copy(r.composite, address.Bytes())
|
|
|
|
binary.BigEndian.PutUint64(r.composite[20:], incarnation)
|
|
|
|
copy(r.composite[20+8:], key.Bytes())
|
|
|
|
|
2022-12-04 11:15:28 +00:00
|
|
|
enc := r.rs.GetHint(kv.PlainState, r.composite, r.stateHint)
|
2022-07-06 06:49:00 +00:00
|
|
|
if enc == nil {
|
|
|
|
var err error
|
|
|
|
enc, err = r.tx.GetOne(kv.PlainState, r.composite)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2022-08-31 06:34:16 +00:00
|
|
|
r.readLists[kv.PlainState].Keys = append(r.readLists[kv.PlainState].Keys, common2.Copy(r.composite))
|
|
|
|
r.readLists[kv.PlainState].Vals = append(r.readLists[kv.PlainState].Vals, common2.Copy(enc))
|
2022-07-06 06:49:00 +00:00
|
|
|
if r.trace {
|
|
|
|
if enc == nil {
|
|
|
|
fmt.Printf("ReadAccountStorage [%x] [%x] => [], txNum: %d\n", address, key.Bytes(), r.txNum)
|
|
|
|
} else {
|
|
|
|
fmt.Printf("ReadAccountStorage [%x] [%x] => [%x], txNum: %d\n", address, key.Bytes(), enc, r.txNum)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if enc == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
return enc, nil
|
|
|
|
}
|
|
|
|
|
2022-07-28 11:16:37 +00:00
|
|
|
func (r *StateReader22) ReadAccountCode(address common.Address, incarnation uint64, codeHash common.Hash) ([]byte, error) {
|
2022-10-25 09:08:01 +00:00
|
|
|
addr, codeHashBytes := address.Bytes(), codeHash.Bytes()
|
|
|
|
enc := r.rs.Get(kv.Code, codeHashBytes)
|
2022-07-06 06:49:00 +00:00
|
|
|
if enc == nil {
|
|
|
|
var err error
|
2022-10-25 09:08:01 +00:00
|
|
|
enc, err = r.tx.GetOne(kv.Code, codeHashBytes)
|
2022-07-06 06:49:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2022-10-25 09:08:01 +00:00
|
|
|
r.readLists[kv.Code].Keys = append(r.readLists[kv.Code].Keys, addr)
|
2022-08-31 06:34:16 +00:00
|
|
|
r.readLists[kv.Code].Vals = append(r.readLists[kv.Code].Vals, common2.Copy(enc))
|
2022-07-06 06:49:00 +00:00
|
|
|
if r.trace {
|
|
|
|
fmt.Printf("ReadAccountCode [%x] => [%x], txNum: %d\n", address, enc, r.txNum)
|
|
|
|
}
|
|
|
|
return enc, nil
|
|
|
|
}
|
|
|
|
|
2022-07-28 11:16:37 +00:00
|
|
|
func (r *StateReader22) ReadAccountCodeSize(address common.Address, incarnation uint64, codeHash common.Hash) (int, error) {
|
2022-10-21 06:45:20 +00:00
|
|
|
codeHashBytes := codeHash.Bytes()
|
|
|
|
enc := r.rs.Get(kv.Code, codeHashBytes)
|
2022-07-06 06:49:00 +00:00
|
|
|
if enc == nil {
|
|
|
|
var err error
|
2022-10-21 06:45:20 +00:00
|
|
|
enc, err = r.tx.GetOne(kv.Code, codeHashBytes)
|
2022-07-06 06:49:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
}
|
2022-07-23 17:39:08 +00:00
|
|
|
var sizebuf [8]byte
|
|
|
|
binary.BigEndian.PutUint64(sizebuf[:], uint64(len(enc)))
|
|
|
|
r.readLists[CodeSizeTable].Keys = append(r.readLists[CodeSizeTable].Keys, address.Bytes())
|
|
|
|
r.readLists[CodeSizeTable].Vals = append(r.readLists[CodeSizeTable].Vals, sizebuf[:])
|
2022-07-06 06:49:00 +00:00
|
|
|
size := len(enc)
|
|
|
|
if r.trace {
|
|
|
|
fmt.Printf("ReadAccountCodeSize [%x] => [%d], txNum: %d\n", address, size, r.txNum)
|
|
|
|
}
|
|
|
|
return size, nil
|
|
|
|
}
|
|
|
|
|
2022-07-28 11:16:37 +00:00
|
|
|
func (r *StateReader22) ReadAccountIncarnation(address common.Address) (uint64, error) {
|
2022-07-06 06:49:00 +00:00
|
|
|
enc := r.rs.Get(kv.IncarnationMap, address.Bytes())
|
|
|
|
if enc == nil {
|
|
|
|
var err error
|
|
|
|
enc, err = r.tx.GetOne(kv.IncarnationMap, address.Bytes())
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
}
|
2022-07-23 17:39:08 +00:00
|
|
|
r.readLists[kv.IncarnationMap].Keys = append(r.readLists[kv.IncarnationMap].Keys, address.Bytes())
|
2022-08-31 06:34:16 +00:00
|
|
|
r.readLists[kv.IncarnationMap].Vals = append(r.readLists[kv.IncarnationMap].Vals, common2.Copy(enc))
|
2022-07-06 06:49:00 +00:00
|
|
|
if len(enc) == 0 {
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
return binary.BigEndian.Uint64(enc), nil
|
|
|
|
}
|
2022-11-30 01:31:13 +00:00
|
|
|
|
|
|
|
var writeListPool = sync.Pool{
|
|
|
|
New: func() any {
|
|
|
|
return map[string]*exec22.KvList{
|
|
|
|
kv.PlainState: {},
|
|
|
|
kv.Code: {},
|
|
|
|
kv.PlainContractCode: {},
|
|
|
|
kv.IncarnationMap: {},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func newWriteList() map[string]*exec22.KvList {
|
2022-11-30 11:24:42 +00:00
|
|
|
return map[string]*exec22.KvList{
|
|
|
|
kv.PlainState: {},
|
|
|
|
kv.Code: {},
|
|
|
|
kv.PlainContractCode: {},
|
|
|
|
kv.IncarnationMap: {},
|
2022-11-30 01:31:13 +00:00
|
|
|
}
|
2022-11-30 11:24:42 +00:00
|
|
|
|
|
|
|
//w := writeListPool.Get().(map[string]*exec22.KvList)
|
|
|
|
//for _, tbl := range w {
|
|
|
|
// tbl.Keys, tbl.Vals = tbl.Keys[:0], tbl.Vals[:0]
|
|
|
|
//}
|
|
|
|
//return w
|
|
|
|
}
|
|
|
|
func returnWriteList(w map[string]*exec22.KvList) {
|
|
|
|
//writeListPool.Put(w)
|
2022-11-30 01:31:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var readListPool = sync.Pool{
|
|
|
|
New: func() any {
|
|
|
|
return map[string]*exec22.KvList{
|
|
|
|
kv.PlainState: {},
|
|
|
|
kv.Code: {},
|
|
|
|
CodeSizeTable: {},
|
|
|
|
kv.IncarnationMap: {},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func newReadList() map[string]*exec22.KvList {
|
2022-11-30 11:24:42 +00:00
|
|
|
return map[string]*exec22.KvList{
|
|
|
|
kv.PlainState: {},
|
|
|
|
kv.Code: {},
|
|
|
|
CodeSizeTable: {},
|
|
|
|
kv.IncarnationMap: {},
|
2022-11-30 01:31:13 +00:00
|
|
|
}
|
2022-11-30 11:24:42 +00:00
|
|
|
|
|
|
|
//w := readListPool.Get().(map[string]*exec22.KvList)
|
|
|
|
//for _, tbl := range w {
|
|
|
|
// tbl.Keys, tbl.Vals = tbl.Keys[:0], tbl.Vals[:0]
|
|
|
|
//}
|
|
|
|
//return w
|
|
|
|
}
|
|
|
|
func returnReadList(w map[string]*exec22.KvList) {
|
|
|
|
//readListPool.Put(w)
|
2022-11-30 01:31:13 +00:00
|
|
|
}
|