Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

R4R: add balance check for transferring bvm ethTxValue #42

Merged
merged 1 commit into from
Mar 3, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions core/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,7 @@ var (

// ErrSystemTxNotSupported is returned for any deposit tx with IsSystemTx=true after the Regolith fork
ErrSystemTxNotSupported = errors.New("system tx not supported")

// ErrEthTxValueTooLarge is returned when EthTxValue is larger than the BVM balance of msg.from
ErrEthTxValueTooLarge = errors.New("eth tx value is too large")
)
23 changes: 16 additions & 7 deletions core/state_transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -453,9 +453,6 @@ func (st *StateTransition) TransitionDb() (*ExecutionResult, error) {
}
snap := st.state.Snapshot()
// Will be reverted if failed
if ethTxValue := st.msg.ETHTxValue; ethTxValue != nil && ethTxValue.Cmp(big.NewInt(0)) != 0 {
st.transferBVMETH(ethTxValue, rules)
}

result, err := st.innerTransitionDb()
// Failed deposits must still be included. Unless we cannot produce the block at all due to the gas limit.
Expand Down Expand Up @@ -483,6 +480,14 @@ func (st *StateTransition) TransitionDb() (*ExecutionResult, error) {
}

func (st *StateTransition) innerTransitionDb() (*ExecutionResult, error) {
rules := st.evm.ChainConfig().Rules(st.evm.Context.BlockNumber, st.evm.Context.Random != nil, st.evm.Context.Time)
if ethTxValue := st.msg.ETHTxValue; ethTxValue != nil && ethTxValue.Cmp(big.NewInt(0)) != 0 {
err := st.transferBVMETH(ethTxValue, rules)
if err != nil {
return nil, err
}
}

// First check this message satisfies all consensus rules before
// applying the message. The rules include these clauses
//
Expand Down Expand Up @@ -510,7 +515,6 @@ func (st *StateTransition) innerTransitionDb() (*ExecutionResult, error) {
var (
msg = st.msg
sender = vm.AccountRef(msg.From)
rules = st.evm.ChainConfig().Rules(st.evm.Context.BlockNumber, st.evm.Context.Random != nil, st.evm.Context.Time)
contractCreation = msg.To == nil
)

Expand Down Expand Up @@ -713,9 +717,9 @@ func (st *StateTransition) addBVMETHTotalSupply(ethValue *big.Int) {
st.state.SetState(BVM_ETH_ADDR, key, common.BigToHash(bal))
}

func (st *StateTransition) transferBVMETH(ethValue *big.Int, rules params.Rules) {
func (st *StateTransition) transferBVMETH(ethValue *big.Int, rules params.Rules) error {
if !rules.IsMantleBVMETHMintUpgrade {
return
return nil
}
var ethRecipient common.Address
if st.msg.To != nil {
Expand All @@ -724,7 +728,7 @@ func (st *StateTransition) transferBVMETH(ethValue *big.Int, rules params.Rules)
ethRecipient = crypto.CreateAddress(st.msg.From, st.evm.StateDB.GetNonce(st.msg.From))
}
if ethRecipient == st.msg.From {
return
return nil
}

fromKey := getBVMETHBalanceKey(st.msg.From)
Expand All @@ -736,13 +740,18 @@ func (st *StateTransition) transferBVMETH(ethValue *big.Int, rules params.Rules)
fromBalance := fromBalanceValue.Big()
toBalance := toBalanceValue.Big()

if fromBalance.Cmp(ethValue) < 0 {
return ErrEthTxValueTooLarge
}

fromBalance = new(big.Int).Sub(fromBalance, ethValue)
toBalance = new(big.Int).Add(toBalance, ethValue)

st.state.SetState(BVM_ETH_ADDR, fromKey, common.BigToHash(fromBalance))
st.state.SetState(BVM_ETH_ADDR, toKey, common.BigToHash(toBalance))

st.generateBVMETHTransferEvent(st.msg.From, ethRecipient, ethValue)
return nil
}

func getBVMETHBalanceKey(addr common.Address) common.Hash {
Expand Down