2020-03-11 15:54:09 +00:00
|
|
|
package state
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
2020-05-25 11:12:25 +00:00
|
|
|
"github.com/holiman/uint256"
|
|
|
|
|
2020-03-11 15:54:09 +00:00
|
|
|
"github.com/ledgerwatch/turbo-geth/common"
|
|
|
|
"github.com/ledgerwatch/turbo-geth/common/changeset"
|
|
|
|
"github.com/ledgerwatch/turbo-geth/common/dbutils"
|
|
|
|
"github.com/ledgerwatch/turbo-geth/common/hexutil"
|
|
|
|
"github.com/ledgerwatch/turbo-geth/core/types/accounts"
|
|
|
|
)
|
|
|
|
|
2020-05-15 07:52:45 +00:00
|
|
|
type changesetFactory func() *changeset.ChangeSet
|
|
|
|
type accountKeyGen func(common.Address) ([]byte, error)
|
|
|
|
type storageKeyGen func(common.Address, uint64, common.Hash) ([]byte, error)
|
|
|
|
|
|
|
|
func plainAccountKeyGen(a common.Address) ([]byte, error) {
|
|
|
|
return a[:], nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func hashedAccountKeyGen(a common.Address) ([]byte, error) {
|
|
|
|
addrHash, err := common.HashData(a[:])
|
|
|
|
return addrHash[:], err
|
|
|
|
}
|
|
|
|
|
|
|
|
func hashedStorageKeyGen(address common.Address, incarnation uint64, key common.Hash) ([]byte, error) {
|
|
|
|
secKey, err := common.HashData(key[:])
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
addrHash, err := common.HashData(address[:])
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return dbutils.GenerateCompositeStorageKey(addrHash, incarnation, secKey), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func plainStorageKeyGen(address common.Address, incarnation uint64, key common.Hash) ([]byte, error) {
|
|
|
|
return dbutils.PlainGenerateCompositeStorageKey(address, incarnation, key), nil
|
|
|
|
}
|
|
|
|
|
2020-03-11 15:54:09 +00:00
|
|
|
// ChangeSetWriter is a mock StateWriter that accumulates changes in-memory into ChangeSets.
|
|
|
|
type ChangeSetWriter struct {
|
|
|
|
accountChanges map[common.Address][]byte
|
|
|
|
storageChanged map[common.Address]bool
|
|
|
|
storageChanges map[string][]byte
|
2020-05-15 07:52:45 +00:00
|
|
|
storageFactory changesetFactory
|
|
|
|
accountFactory changesetFactory
|
|
|
|
accountKeyGen accountKeyGen
|
|
|
|
storageKeyGen storageKeyGen
|
2020-07-01 14:56:56 +00:00
|
|
|
blockNumber uint64
|
2020-03-11 15:54:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewChangeSetWriter() *ChangeSetWriter {
|
|
|
|
return &ChangeSetWriter{
|
|
|
|
accountChanges: make(map[common.Address][]byte),
|
|
|
|
storageChanged: make(map[common.Address]bool),
|
|
|
|
storageChanges: make(map[string][]byte),
|
2020-05-15 07:52:45 +00:00
|
|
|
storageFactory: changeset.NewStorageChangeSet,
|
|
|
|
accountFactory: changeset.NewAccountChangeSet,
|
|
|
|
accountKeyGen: hashedAccountKeyGen,
|
|
|
|
storageKeyGen: hashedStorageKeyGen,
|
|
|
|
}
|
|
|
|
}
|
2020-07-01 14:56:56 +00:00
|
|
|
func NewChangeSetWriterPlain(blockNumber uint64) *ChangeSetWriter {
|
2020-05-15 07:52:45 +00:00
|
|
|
return &ChangeSetWriter{
|
|
|
|
accountChanges: make(map[common.Address][]byte),
|
|
|
|
storageChanged: make(map[common.Address]bool),
|
|
|
|
storageChanges: make(map[string][]byte),
|
|
|
|
storageFactory: changeset.NewStorageChangeSetPlain,
|
|
|
|
accountFactory: changeset.NewAccountChangeSetPlain,
|
|
|
|
accountKeyGen: plainAccountKeyGen,
|
|
|
|
storageKeyGen: plainStorageKeyGen,
|
2020-07-01 14:56:56 +00:00
|
|
|
blockNumber: blockNumber,
|
2020-03-11 15:54:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-09 17:23:29 +00:00
|
|
|
func (w *ChangeSetWriter) GetAccountChanges() (*changeset.ChangeSet, error) {
|
2020-05-15 07:52:45 +00:00
|
|
|
cs := w.accountFactory()
|
|
|
|
for address, val := range w.accountChanges {
|
|
|
|
key, err := w.accountKeyGen(address)
|
2020-04-09 17:23:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-15 07:52:45 +00:00
|
|
|
if err := cs.Add(key, val); err != nil {
|
2020-04-09 17:23:29 +00:00
|
|
|
return nil, err
|
2020-03-11 15:54:09 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-09 17:23:29 +00:00
|
|
|
return cs, nil
|
2020-03-11 15:54:09 +00:00
|
|
|
}
|
2020-04-09 17:23:29 +00:00
|
|
|
func (w *ChangeSetWriter) GetStorageChanges() (*changeset.ChangeSet, error) {
|
2020-05-15 07:52:45 +00:00
|
|
|
cs := w.storageFactory()
|
2020-03-11 15:54:09 +00:00
|
|
|
for key, val := range w.storageChanges {
|
|
|
|
if err := cs.Add([]byte(key), val); err != nil {
|
2020-04-09 17:23:29 +00:00
|
|
|
return nil, err
|
2020-03-11 15:54:09 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-09 17:23:29 +00:00
|
|
|
return cs, nil
|
2020-03-11 15:54:09 +00:00
|
|
|
}
|
|
|
|
|
2020-05-06 08:58:12 +00:00
|
|
|
func accountsEqual(a1, a2 *accounts.Account) bool {
|
|
|
|
if a1.Nonce != a2.Nonce {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if !a1.Initialised {
|
|
|
|
if a2.Initialised {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
} else if !a2.Initialised {
|
|
|
|
return false
|
|
|
|
} else if a1.Balance.Cmp(&a2.Balance) != 0 {
|
|
|
|
return false
|
|
|
|
}
|
2020-07-01 14:56:56 +00:00
|
|
|
if a1.IsEmptyCodeHash() {
|
|
|
|
if !a2.IsEmptyCodeHash() {
|
2020-05-06 08:58:12 +00:00
|
|
|
return false
|
|
|
|
}
|
2020-07-01 14:56:56 +00:00
|
|
|
} else if a2.IsEmptyCodeHash() {
|
2020-05-06 08:58:12 +00:00
|
|
|
return false
|
|
|
|
} else if a1.CodeHash != a2.CodeHash {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-03-11 15:54:09 +00:00
|
|
|
func (w *ChangeSetWriter) UpdateAccountData(ctx context.Context, address common.Address, original, account *accounts.Account) error {
|
|
|
|
if !accountsEqual(original, account) || w.storageChanged[address] {
|
2020-04-15 09:33:22 +00:00
|
|
|
w.accountChanges[address] = originalAccountData(original, true /*omitHashes*/)
|
2020-03-11 15:54:09 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-15 07:52:45 +00:00
|
|
|
func (w *ChangeSetWriter) UpdateAccountCode(address common.Address, incarnation uint64, codeHash common.Hash, code []byte) error {
|
2020-03-11 15:54:09 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *ChangeSetWriter) DeleteAccount(ctx context.Context, address common.Address, original *accounts.Account) error {
|
2020-03-26 21:52:22 +00:00
|
|
|
w.accountChanges[address] = originalAccountData(original, false)
|
2020-03-11 15:54:09 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-25 11:12:25 +00:00
|
|
|
func (w *ChangeSetWriter) WriteAccountStorage(ctx context.Context, address common.Address, incarnation uint64, key *common.Hash, original, value *uint256.Int) error {
|
2020-03-11 15:54:09 +00:00
|
|
|
if *original == *value {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-15 07:52:45 +00:00
|
|
|
compositeKey, err := w.storageKeyGen(address, incarnation, *key)
|
2020-03-11 15:54:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-05-25 11:12:25 +00:00
|
|
|
w.storageChanges[string(compositeKey)] = original.Bytes()
|
2020-03-11 15:54:09 +00:00
|
|
|
w.storageChanged[address] = true
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-02 18:00:42 +00:00
|
|
|
func (w *ChangeSetWriter) CreateContract(address common.Address) error {
|
|
|
|
return nil
|
2020-03-11 15:54:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (w *ChangeSetWriter) PrintChangedAccounts() {
|
|
|
|
fmt.Println("Account Changes")
|
|
|
|
for k := range w.accountChanges {
|
|
|
|
fmt.Println(hexutil.Encode(k.Bytes()))
|
|
|
|
}
|
|
|
|
fmt.Println("------------------------------------------")
|
|
|
|
}
|