Skip to content

Commit

Permalink
fix *uint256 reuse issue
Browse files Browse the repository at this point in the history
  • Loading branch information
Qi Zhou committed Oct 28, 2024
1 parent 5141753 commit d3c847c
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
3 changes: 3 additions & 0 deletions core/state/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,11 @@ func (s *StateDB) DumpToCollector(c DumpCollector, conf *DumpConfig) (nextKey []
)
log.Info("Trie dumping started", "root", s.trie.Hash())
c.OnRoot(s.trie.Hash())
fmt.Println("iterating root")

trieIt, err := s.trie.NodeIterator(conf.Start)
if err != nil {
fmt.Println("trie error", err)
log.Error("Trie dumping error", "err", err)
return nil
}
Expand Down Expand Up @@ -200,6 +202,7 @@ func (s *StateDB) DumpToCollector(c DumpCollector, conf *DumpConfig) (nextKey []
}
}
if missingPreimages > 0 {
fmt.Println("missing image")
log.Warn("Dump incomplete due to missing preimages", "missing", missingPreimages)
}
log.Info("Trie dumping complete", "accounts", accounts,
Expand Down
1 change: 1 addition & 0 deletions core/state_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ func MakeReceipt(evm *vm.EVM, result *ExecutionResult, statedb *state.StateDB, b
// for the transaction, gas used and an error if the transaction failed,
// indicating the block was invalid.
func ApplyTransaction(config *params.ChainConfig, bc ChainContext, author *common.Address, gp *GasPool, statedb *state.StateDB, header *types.Header, tx *types.Transaction, usedGas *uint64, cfg vm.Config) (*types.Receipt, error) {
fmt.Println("ApplyTransaction")
msg, err := TransactionToMessage(tx, types.MakeSigner(config, header.Number, header.Time), header.BaseFee)
if err != nil {
return nil, err
Expand Down
14 changes: 13 additions & 1 deletion core/state_transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/crypto/kzg4844"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/params"
"github.com/holiman/uint256"
)
Expand Down Expand Up @@ -197,6 +198,7 @@ func TransactionToMessage(tx *types.Transaction, s types.Signer, baseFee *big.In
// indicates a core error meaning that the message would always fail for that particular
// state and would never be accepted within a block.
func ApplyMessage(evm *vm.EVM, msg *Message, gp *GasPool) (*ExecutionResult, error) {
fmt.Println("ApplyMessage")
return NewStateTransition(evm, msg, gp).TransitionDb()
}

Expand Down Expand Up @@ -297,7 +299,7 @@ func GetEffectiveGasBalance(state vm.StateDB, chainconfig *params.ChainConfig, a
}

func GetGasBalances(state vm.StateDB, chainconfig *params.ChainConfig, account common.Address) (*uint256.Int, *uint256.Int) {
balance := state.GetBalance(account)
balance := state.GetBalance(account).Clone()
if chainconfig != nil && chainconfig.IsOptimism() && chainconfig.Optimism.UseSoulGasToken {
sgtBalanceSlot := TargetSGTBalanceSlot(account)
sgtBalanceValue := state.GetState(types.SoulGasTokenAddr, sgtBalanceSlot)
Expand Down Expand Up @@ -380,12 +382,15 @@ func (st *StateTransition) buyGas() error {
st.gasFromSoul = false

if st.evm.ChainConfig().IsOptimism() && st.evm.ChainConfig().Optimism.UseSoulGasToken {
fmt.Println("Checking SGT")
have := st.GetSoulBalance(st.msg.From)
if have, want := have.ToBig(), new(big.Int).Sub(balanceCheck, st.msg.Value); have.Cmp(want) >= 0 {
if have, want := st.state.GetBalance(st.msg.From).ToBig(), st.msg.Value; have.Cmp(want) < 0 {
return fmt.Errorf("%w: address %v have %v want %v", ErrInsufficientFunds, st.msg.From.Hex(), have, want)
}
st.gasFromSoul = true
fmt.Println("buySGT")
log.Info(("Run a SGT tx"))
}
}
if !st.gasFromSoul {
Expand Down Expand Up @@ -414,6 +419,8 @@ func (st *StateTransition) buyGas() error {
}

func (st *StateTransition) preCheck() error {
fmt.Println("buyGas")

if st.msg.IsDepositTx {
// No fee fields to check, no nonce to check, and no need to check if EOA (L1 already verified it for us)
// Gas is free, but no refunds!
Expand All @@ -429,6 +436,9 @@ func (st *StateTransition) preCheck() error {
}
return st.gp.SubGas(st.msg.GasLimit) // gas used by deposits may not be used by other txs
}

fmt.Println("buyGas1")

// Only check transactions that are not fake
msg := st.msg
if !msg.SkipNonceChecks {
Expand Down Expand Up @@ -756,6 +766,8 @@ func (st *StateTransition) refundGas(refundQuotient uint64) uint64 {
// Return ETH for remaining gas, exchanged at the original rate.
if st.gasFromSoul {
remaining := new(big.Int).Mul(new(big.Int).SetUint64(st.gasRemaining), st.msg.GasPrice)
log.Info("refund", "remaining", remaining)
fmt.Println("refund")
st.AddSoulBalance(st.msg.From, remaining, tracing.BalanceIncreaseGasReturn)
} else {
remaining := uint256.NewInt(st.gasRemaining)
Expand Down

0 comments on commit d3c847c

Please sign in to comment.