Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion nil/cmd/prover/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func execute() error {
}
}
client := prover.NewRPCClient(commonCfg.NilRpcEndpoint, logging.NewLogger("client"))
return tracer.GenerateTrace(context.Background(), client, &traceConfig)
return tracer.CollectTracesToFile(context.Background(), client, &traceConfig)
},
}
addCommonFlags(generateTraceCmd, commonCfg)
Expand Down
6 changes: 3 additions & 3 deletions nil/internal/execution/account_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ func (asr *AccountStateReader) GetTokenBalance(id types.TokenId) types.Value {
return *res
}

type IExecutionState interface {
type IAccountExecutionState interface {
AppendToJournal(entry JournalEntry)
GetRwTx() db.RwTx
}

type AccountState struct {
db IExecutionState
db IAccountExecutionState
address types.Address // address of the ethereum account

Balance types.Value
Expand Down Expand Up @@ -87,7 +87,7 @@ func NewAccountStateReader(account *AccountState) *AccountStateReader {
}

func NewAccountState(
es IExecutionState,
es IAccountExecutionState,
addr types.Address,
account *types.SmartContract,
logger logging.Logger,
Expand Down
17 changes: 14 additions & 3 deletions nil/internal/execution/block_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@ import (
"github.com/NilFoundation/nil/nil/common/logging"
"github.com/NilFoundation/nil/nil/internal/config"
"github.com/NilFoundation/nil/nil/internal/db"
"github.com/NilFoundation/nil/nil/internal/tracing"
"github.com/NilFoundation/nil/nil/internal/types"
)

type BlockGeneratorParams struct {
ShardId types.ShardId
NShards uint32
TraceEVM bool
EvmTracingHooks *tracing.Hooks
MainKeysPath string
DisableConsensus bool
FeeCalculator FeeCalculator
Expand Down Expand Up @@ -79,8 +80,18 @@ func NewBlockGenerator(
if err != nil {
return nil, err
}
executionState.TraceVm = params.TraceEVM
executionState.EvmTracingHooks = params.EvmTracingHooks

return NewBlockGeneratorWithEs(ctx, params, txFabric, rwTx, executionState)
}

func NewBlockGeneratorWithEs(
ctx context.Context,
params BlockGeneratorParams,
txFabric db.DB,
rwTx db.RwTx,
es *ExecutionState,
) (*BlockGenerator, error) {
const mhName = "github.com/NilFoundation/nil/nil/internal/execution"
mh, err := NewMetricsHandler(mhName, params.ShardId)
if err != nil {
Expand All @@ -92,7 +103,7 @@ func NewBlockGenerator(
params: params,
txFabric: txFabric,
rwTx: rwTx,
executionState: executionState,
executionState: es,
logger: logging.NewLogger("block-gen").With().
Stringer(logging.FieldShardId, params.ShardId).
Logger(),
Expand Down
2 changes: 0 additions & 2 deletions nil/internal/execution/contract_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,8 +302,6 @@ func TestSendTransaction(t *testing.T) {
state := newState(t)
defer state.tx.Rollback()

state.TraceVm = false

contracts, err := solc.CompileSource(common.GetAbsolutePath("../../tests/contracts/async_call.sol"))
require.NoError(t, err)

Expand Down
137 changes: 46 additions & 91 deletions nil/internal/execution/journal.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,12 @@ const (
)

type IRevertableExecutionState interface {
deleteObject(addr types.Address)
deleteContractChange(addr types.Address)
revertSelfDestructChange(addr types.Address, prev bool, prevBalance types.Value)
revertBalanceChange(addr types.Address, prevBalance types.Value)
revertTokenChange(addr types.Address, tokenId types.TokenId, prevValue types.Value)
revertSeqnoChange(addr types.Address, prevSeqno types.Seqno)
revertExtSeqnoChange(addr types.Address, prevExtSeqno types.Seqno)
revertCodeChange(addr types.Address, prevCodeHash common.Hash, prevCode []byte)
revertStorageChange(addr types.Address, key common.Hash, prevValue common.Hash)
revertRefundChange(prevRefund uint64)
deleteLog(txHash common.Hash)
revertTransientStorageChange(addr types.Address, key common.Hash, prevValue common.Hash)
revertOutTransactionsChange(index int, txnHash common.Hash)
revertAsyncContextChange(addr types.Address, requestId types.TransactionIndex)
DeleteAccount(addr types.Address)
GetAccount(addr types.Address) (*AccountState, error)
SetRefund(value uint64)
DeleteLog(txHash common.Hash)
SetTransientNoJournal(addr types.Address, key common.Hash, prevValue common.Hash)
DeleteOutTransaction(index int, txnHash common.Hash)
}

// JournalEntry is a modification entry in the state change journal that can be
Expand Down Expand Up @@ -67,14 +59,14 @@ func (j *journal) length() int {

type (
// Changes to the account trie.
createObjectChange struct {
createAccountChange struct {
account *types.Address
}

// createContractChange represents an account becoming a contract-account.
// accountBecameContractChange represents an account becoming a contract-account.
// This event happens prior to executing initcode. The journal-event simply
// manages the created-flag, in order to allow same-tx destruction.
createContractChange struct {
accountBecameContractChange struct {
account types.Address
}

Expand Down Expand Up @@ -136,85 +128,79 @@ type (
}
)

func (ch createObjectChange) revert(s IRevertableExecutionState) {
s.deleteObject(*ch.account)
func (ch createAccountChange) revert(s IRevertableExecutionState) {
reverter{s}.revertCreateAccount(*ch.account)
}

func (ch createContractChange) revert(s IRevertableExecutionState) {
s.deleteContractChange(ch.account)
func (ch accountBecameContractChange) revert(s IRevertableExecutionState) {
reverter{s}.revertAccountBecameContractChange(ch.account)
}

func (ch selfDestructChange) revert(s IRevertableExecutionState) {
s.revertSelfDestructChange(*ch.account, ch.prev, ch.prevbalance)
reverter{s}.revertSelfDestructChange(*ch.account, ch.prev, ch.prevbalance)
}

func (ch balanceChange) revert(s IRevertableExecutionState) {
s.revertBalanceChange(*ch.account, ch.prev)
reverter{s}.revertBalanceChange(*ch.account, ch.prev)
}

func (ch tokenChange) revert(s IRevertableExecutionState) {
s.revertTokenChange(*ch.account, ch.id, ch.prev)
reverter{s}.revertTokenChange(*ch.account, ch.id, ch.prev)
}

func (ch seqnoChange) revert(s IRevertableExecutionState) {
s.revertSeqnoChange(*ch.account, ch.prev)
reverter{s}.revertSeqnoChange(*ch.account, ch.prev)
}

func (ch extSeqnoChange) revert(s IRevertableExecutionState) {
s.revertExtSeqnoChange(*ch.account, ch.prev)
reverter{s}.revertExtSeqnoChange(*ch.account, ch.prev)
}

func (ch codeChange) revert(s IRevertableExecutionState) {
s.revertCodeChange(*ch.account, common.BytesToHash(ch.prevhash), ch.prevcode)
reverter{s}.revertCodeChange(*ch.account, common.BytesToHash(ch.prevhash), ch.prevcode)
}

func (ch storageChange) revert(s IRevertableExecutionState) {
s.revertStorageChange(*ch.account, ch.key, ch.prevvalue)
reverter{s}.revertStorageChange(*ch.account, ch.key, ch.prevvalue)
}

func (ch refundChange) revert(s IRevertableExecutionState) {
s.revertRefundChange(ch.prev)
reverter{s}.revertRefundChange(ch.prev)
}

func (ch addLogChange) revert(s IRevertableExecutionState) {
s.deleteLog(ch.txhash)
reverter{s}.deleteLog(ch.txhash)
}

func (ch transientStorageChange) revert(s IRevertableExecutionState) {
s.revertTransientStorageChange(*ch.account, ch.key, ch.prevalue)
reverter{s}.revertTransientStorageChange(*ch.account, ch.key, ch.prevalue)
}

func (ch outTransactionsChange) revert(s IRevertableExecutionState) {
s.revertOutTransactionsChange(ch.index, ch.txnHash)
reverter{s}.revertOutTransactionsChange(ch.index, ch.txnHash)
}

func (ch asyncContextChange) revert(s IRevertableExecutionState) {
s.revertAsyncContextChange(*ch.account, ch.requestId)
reverter{s}.revertAsyncContextChange(*ch.account, ch.requestId)
}

type ExecutionStateRevertableWrapper struct {
es *ExecutionState
type reverter struct {
es IRevertableExecutionState
}

var _ IRevertableExecutionState = new(ExecutionStateRevertableWrapper)

func (w *ExecutionStateRevertableWrapper) deleteObject(addr types.Address) {
delete(w.es.Accounts, addr)
func (w reverter) revertCreateAccount(addr types.Address) {
w.es.DeleteAccount(addr)
}

func (w *ExecutionStateRevertableWrapper) deleteContractChange(addr types.Address) {
func (w reverter) revertAccountBecameContractChange(addr types.Address) {
account, err := w.es.GetAccount(addr)
check.PanicIfErr(err)
if account != nil {
account.NewContract = false
}
}

func (w *ExecutionStateRevertableWrapper) revertSelfDestructChange(
addr types.Address,
prev bool,
prevBalance types.Value,
) {
func (w reverter) revertSelfDestructChange(addr types.Address, prev bool, prevBalance types.Value) {
account, err := w.es.GetAccount(addr)
check.PanicIfErr(err)
if account != nil {
Expand All @@ -223,105 +209,74 @@ func (w *ExecutionStateRevertableWrapper) revertSelfDestructChange(
}
}

func (w *ExecutionStateRevertableWrapper) revertBalanceChange(addr types.Address, prevBalance types.Value) {
func (w reverter) revertBalanceChange(addr types.Address, prevBalance types.Value) {
account, err := w.es.GetAccount(addr)
check.PanicIfErr(err)
if account != nil {
account.setBalance(prevBalance)
}
}

func (w *ExecutionStateRevertableWrapper) revertTokenChange(
addr types.Address,
tokenId types.TokenId,
prevValue types.Value,
) {
func (w reverter) revertTokenChange(addr types.Address, tokenId types.TokenId, prevValue types.Value) {
account, err := w.es.GetAccount(addr)
check.PanicIfErr(err)
if account != nil {
account.setTokenBalance(tokenId, prevValue)
}
}

func (w *ExecutionStateRevertableWrapper) revertSeqnoChange(addr types.Address, prevSeqno types.Seqno) {
func (w reverter) revertSeqnoChange(addr types.Address, prevSeqno types.Seqno) {
account, err := w.es.GetAccount(addr)
check.PanicIfErr(err)
if account != nil {
account.Seqno = prevSeqno
}
}

func (w *ExecutionStateRevertableWrapper) revertExtSeqnoChange(addr types.Address, prevExtSeqno types.Seqno) {
func (w reverter) revertExtSeqnoChange(addr types.Address, prevExtSeqno types.Seqno) {
account, err := w.es.GetAccount(addr)
check.PanicIfErr(err)
if account != nil {
account.ExtSeqno = prevExtSeqno
}
}

func (w *ExecutionStateRevertableWrapper) revertCodeChange(
addr types.Address,
prevCodeHash common.Hash,
prevCode []byte,
) {
func (w reverter) revertCodeChange(addr types.Address, prevCodeHash common.Hash, prevCode []byte) {
account, err := w.es.GetAccount(addr)
check.PanicIfErr(err)
if account != nil {
account.setCode(prevCodeHash, prevCode)
}
}

func (w *ExecutionStateRevertableWrapper) revertStorageChange(
addr types.Address,
key common.Hash,
prevValue common.Hash,
) {
func (w reverter) revertStorageChange(addr types.Address, key common.Hash, prevValue common.Hash) {
account, err := w.es.GetAccount(addr)
check.PanicIfErr(err)
if account != nil {
account.setState(key, prevValue)
}
}

func (w *ExecutionStateRevertableWrapper) revertRefundChange(prevRefund uint64) {
w.es.refund = prevRefund
func (w reverter) revertRefundChange(prevRefund uint64) {
w.es.SetRefund(prevRefund)
}

func (w *ExecutionStateRevertableWrapper) deleteLog(txHash common.Hash) {
func (w reverter) deleteLog(txHash common.Hash) {
if doNotRevertLogsFeatureEnabled {
return
}
logs := w.es.Logs[txHash]
if len(logs) == 1 {
delete(w.es.Logs, txHash)
} else {
w.es.Logs[txHash] = logs[:len(logs)-1]
}
w.es.DeleteLog(txHash)
}

func (w *ExecutionStateRevertableWrapper) revertTransientStorageChange(
addr types.Address,
key common.Hash,
prevValue common.Hash,
) {
w.es.setTransientState(addr, key, prevValue)
func (w reverter) revertTransientStorageChange(addr types.Address, key common.Hash, prevValue common.Hash) {
w.es.SetTransientNoJournal(addr, key, prevValue)
}

func (w *ExecutionStateRevertableWrapper) revertOutTransactionsChange(index int, txnHash common.Hash) {
outTransactions, ok := w.es.OutTransactions[txnHash]
check.PanicIfNot(ok)

// Probably it is possible that the transaction is not the last in the list, but let's assume it is for a now.
// And catch opposite case with this assert.
check.PanicIfNot(index == len(outTransactions)-1)

w.es.OutTransactions[txnHash] = outTransactions[:index]
func (w reverter) revertOutTransactionsChange(index int, txnHash common.Hash) {
w.es.DeleteOutTransaction(index, txnHash)
}

func (w *ExecutionStateRevertableWrapper) revertAsyncContextChange(
addr types.Address,
requestId types.TransactionIndex,
) {
func (w reverter) revertAsyncContextChange(addr types.Address, requestId types.TransactionIndex) {
account, err := w.es.GetAccount(addr)
check.PanicIfErr(err)
if account != nil {
Expand Down
Loading