Skip to content

Commit

Permalink
Merge pull request #42 from mantlenetworkio/bugfix/add-check-for-tran…
Browse files Browse the repository at this point in the history
…sferBVMETH

R4R: add balance check for transferring bvm ethTxValue
  • Loading branch information
Tri-stone authored Mar 3, 2024
2 parents b2cb932 + 0cf00ba commit 7ea77b8
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
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

0 comments on commit 7ea77b8

Please sign in to comment.