Skip to content

Commit

Permalink
Remove auxState field from Transition and use NewTransition function
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan-Ethernal authored and goran-ethernal committed Feb 13, 2024
1 parent 9163007 commit 3f38041
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 45 deletions.
3 changes: 2 additions & 1 deletion helper/predeployment/predeployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"math"
"math/big"

"github.com/hashicorp/go-hclog"
"github.com/umbracle/ethgo/abi"

"github.com/0xPolygon/polygon-edge/chain"
Expand Down Expand Up @@ -67,7 +68,7 @@ func getPredeployAccount(address types.Address, input []byte,
config := chain.AllForksEnabled.At(0)

// Create a transition
transition := state.NewTransition(config, snapshot, radix)
transition := state.NewTransition(hclog.NewNullLogger(), config, snapshot, radix)
transition.ContextPtr().ChainID = chainID

// Run the transition through the EVM
Expand Down
62 changes: 20 additions & 42 deletions state/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,17 +81,8 @@ func (e *Executor) WriteGenesis(
ChainID: e.config.ChainID,
}

transition := &Transition{
logger: e.logger,
ctx: env,
state: txn,
auxState: e.state,
gasPool: uint64(env.GasLimit),
config: config,
precompiles: precompiled.NewPrecompiled(),
journal: &runtime.Journal{},
accessList: runtime.NewAccessList(),
}
transition := NewTransition(e.logger, config, snap, txn)
transition.ctx = env

for addr, account := range alloc {
if account.Balance != nil {
Expand Down Expand Up @@ -182,7 +173,7 @@ func (e *Executor) BeginTxn(
) (*Transition, error) {
forkConfig := e.config.Forks.At(header.Number)

auxSnap2, err := e.state.NewSnapshotAt(parentRoot)
snap, err := e.state.NewSnapshotAt(parentRoot)
if err != nil {
return nil, err
}
Expand All @@ -195,7 +186,7 @@ func (e *Executor) BeginTxn(
}
}

newTxn := NewTxn(auxSnap2)
newTxn := NewTxn(snap)

txCtx := runtime.TxContext{
Coinbase: coinbaseReceiver,
Expand All @@ -208,62 +199,47 @@ func (e *Executor) BeginTxn(
BurnContract: burnContract,
}

txn := &Transition{
logger: e.logger,
ctx: txCtx,
state: newTxn,
snap: auxSnap2,
getHash: e.GetHash(header),
auxState: e.state,
config: forkConfig,
gasPool: uint64(txCtx.GasLimit),

receipts: []*types.Receipt{},
totalGas: 0,

evm: evm.NewEVM(),
precompiles: precompiled.NewPrecompiled(),
PostHook: e.PostHook,
journal: &runtime.Journal{},
accessList: runtime.NewAccessList(),
}
t := NewTransition(e.logger, forkConfig, snap, newTxn)
t.PostHook = e.PostHook
t.getHash = e.GetHash(header)
t.ctx = txCtx
t.gasPool = uint64(txCtx.GasLimit)

// enable contract deployment allow list (if any)
if e.config.ContractDeployerAllowList != nil {
txn.deploymentAllowList = addresslist.NewAddressList(txn, contracts.AllowListContractsAddr)
t.deploymentAllowList = addresslist.NewAddressList(t, contracts.AllowListContractsAddr)
}

if e.config.ContractDeployerBlockList != nil {
txn.deploymentBlockList = addresslist.NewAddressList(txn, contracts.BlockListContractsAddr)
t.deploymentBlockList = addresslist.NewAddressList(t, contracts.BlockListContractsAddr)
}

// enable transactions allow list (if any)
if e.config.TransactionsAllowList != nil {
txn.txnAllowList = addresslist.NewAddressList(txn, contracts.AllowListTransactionsAddr)
t.txnAllowList = addresslist.NewAddressList(t, contracts.AllowListTransactionsAddr)
}

if e.config.TransactionsBlockList != nil {
txn.txnBlockList = addresslist.NewAddressList(txn, contracts.BlockListTransactionsAddr)
t.txnBlockList = addresslist.NewAddressList(t, contracts.BlockListTransactionsAddr)
}

// enable transactions allow list (if any)
if e.config.BridgeAllowList != nil {
txn.bridgeAllowList = addresslist.NewAddressList(txn, contracts.AllowListBridgeAddr)
t.bridgeAllowList = addresslist.NewAddressList(t, contracts.AllowListBridgeAddr)
}

if e.config.BridgeBlockList != nil {
txn.bridgeBlockList = addresslist.NewAddressList(txn, contracts.BlockListBridgeAddr)
t.bridgeBlockList = addresslist.NewAddressList(t, contracts.BlockListBridgeAddr)
}

return txn, nil
return t, nil
}

type Transition struct {
logger hclog.Logger

// dummy
auxState State
snap Snapshot
snap Snapshot

config chain.ForksInTime
state *Txn
Expand Down Expand Up @@ -296,8 +272,9 @@ type Transition struct {
accessList *runtime.AccessList
}

func NewTransition(config chain.ForksInTime, snap Snapshot, radix *Txn) *Transition {
func NewTransition(logger hclog.Logger, config chain.ForksInTime, snap Snapshot, radix *Txn) *Transition {
return &Transition{
logger: logger,
config: config,
state: radix,
snap: snap,
Expand Down Expand Up @@ -467,6 +444,7 @@ func (t *Transition) ContextPtr() *runtime.TxContext {
func (t *Transition) subGasLimitPrice(msg *types.Transaction) error {
upfrontGasCost := new(big.Int).Mul(new(big.Int).SetUint64(msg.Gas()), msg.GetGasPrice(t.ctx.BaseFee.Uint64()))
balanceCheck := new(big.Int).Set(upfrontGasCost)

if msg.Type() == types.DynamicFeeTx {
balanceCheck.Add(balanceCheck, msg.Value())
balanceCheck.SetUint64(msg.Gas())
Expand Down
5 changes: 3 additions & 2 deletions state/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"math/big"
"testing"

"github.com/hashicorp/go-hclog"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

Expand Down Expand Up @@ -36,7 +37,7 @@ func TestOverride(t *testing.T) {
balance := big.NewInt(2)
code := []byte{0x1}

tt := NewTransition(chain.ForksInTime{}, state, newTxn(state))
tt := NewTransition(hclog.NewNullLogger(), chain.ForksInTime{}, state, newTxn(state))

require.Empty(t, tt.state.GetCode(types.ZeroAddress))

Expand Down Expand Up @@ -259,7 +260,7 @@ func Test_Transition_EIP2929(t *testing.T) {
txn.SetCode(addr, tt.code)

enabledForks := chain.AllForksEnabled.At(0)
transition := NewTransition(enabledForks, state, txn)
transition := NewTransition(hclog.NewNullLogger(), enabledForks, state, txn)
initialAccessList := runtime.NewAccessList()
initialAccessList.PrepareAccessList(transition.ctx.Origin, &addr, transition.precompiles.Addrs, nil)
transition.accessList = initialAccessList
Expand Down

0 comments on commit 3f38041

Please sign in to comment.