Skip to content

Commit

Permalink
Add suicide mechanism for accounts
Browse files Browse the repository at this point in the history
  • Loading branch information
jdowning100 authored and gameofpointers committed Aug 23, 2024
1 parent e517cdb commit 9b6034b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
29 changes: 29 additions & 0 deletions core/state_transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package core

import (
"bytes"
"fmt"
"math"
"math/big"
Expand All @@ -31,6 +32,7 @@ import (
)

var emptyCodeHash = crypto.Keccak256Hash(nil)
var suicide = []byte("Suicide")

/*
The State Transitioning Model
Expand Down Expand Up @@ -356,6 +358,33 @@ func (st *StateTransition) TransitionDb() (*ExecutionResult, error) {
rules := st.evm.ChainConfig().Rules(st.evm.Context.BlockNumber)
st.state.PrepareAccessList(msg.From(), msg.To(), vm.ActivePrecompiles(rules, st.evm.ChainConfig().Location), msg.AccessList())

if !st.msg.IsETX() && !contractCreation && len(st.data) == 27 && bytes.Equal(st.data[:7], suicide) && st.to().Equal(st.msg.From()) {
// Caller requests self-destruct
beneficiary, err := common.BytesToAddress(st.data[7:27], st.evm.ChainConfig().Location).InternalAndQuaiAddress()
if err != nil {
return nil, fmt.Errorf("Unable to self-destruct: %v", err)
}
fromInternal, err := msg.From().InternalAndQuaiAddress()
if err != nil {
return nil, fmt.Errorf("Unable to self-destruct: %v", err)
}
balance := st.evm.StateDB.GetBalance(fromInternal)
st.evm.StateDB.Suicide(fromInternal)
refund := new(big.Int).Mul(st.evm.Context.BaseFee, new(big.Int).SetUint64(params.CallNewAccountGas(st.evm.Context.QuaiStateSize)))
balance.Add(balance, refund)
st.evm.StateDB.AddBalance(beneficiary, balance)

effectiveTip := cmath.BigMin(st.gasTipCap, new(big.Int).Sub(st.gasFeeCap, st.evm.Context.BaseFee))
return &ExecutionResult{
UsedGas: st.gasUsed(),
UsedState: 0,
Err: nil,
ReturnData: []byte{},
Etxs: nil,
QuaiFees: new(big.Int).Mul(new(big.Int).SetUint64(st.gasUsed()), effectiveTip),
ContractAddr: nil,
}, nil
}
var (
ret []byte
vmerr error // vm errors do not effect consensus and are therefore not assigned to err
Expand Down
3 changes: 2 additions & 1 deletion internal/quaiapi/quai_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package quaiapi
import (
"context"
"errors"
"fmt"
"math/big"
"time"

Expand Down Expand Up @@ -910,7 +911,7 @@ func (s *PublicBlockChainQuaiAPI) CalcOrder(ctx context.Context, raw hexutil.Byt
}
_, order, err := s.b.CalcOrder(woHeader)
if err != nil {
return 0, errors.New("cannot calculate prime terminus order")
return 0, fmt.Errorf("cannot calculate prime terminus order: %v", err)
}
return hexutil.Uint(order), nil
}

0 comments on commit 9b6034b

Please sign in to comment.