Skip to content

Commit

Permalink
Added stateGas value into the contract, but needs to be calculated
Browse files Browse the repository at this point in the history
properly
  • Loading branch information
gameofpointers committed Aug 15, 2024
1 parent e1f01dd commit b6da2f8
Show file tree
Hide file tree
Showing 16 changed files with 236 additions and 254 deletions.
5 changes: 4 additions & 1 deletion core/block_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,13 +261,16 @@ func (v *BlockValidator) SanityCheckWorkObjectShareViewBody(wo *types.WorkObject
// transition, such as amount of used gas, the receipt roots and the state root
// itself. ValidateState returns a database batch if the validation was a success
// otherwise nil and an error is returned.
func (v *BlockValidator) ValidateState(block *types.WorkObject, statedb *state.StateDB, receipts types.Receipts, etxs types.Transactions, usedGas uint64) error {
func (v *BlockValidator) ValidateState(block *types.WorkObject, statedb *state.StateDB, receipts types.Receipts, etxs types.Transactions, usedGas uint64, usedState uint64) error {
start := time.Now()
header := types.CopyHeader(block.Header())
time1 := common.PrettyDuration(time.Since(start))
if block.GasUsed() != usedGas {
return fmt.Errorf("invalid gas used (remote: %d local: %d)", block.GasUsed(), usedGas)
}
if block.StateUsed() != usedState {
return fmt.Errorf("invalid state used (remote: %d local: %d)", block.StateUsed(), usedState)
}
time2 := common.PrettyDuration(time.Since(start))
time3 := common.PrettyDuration(time.Since(start))
// Tre receipt Trie's root (R = (Tr [[H1, R1], ... [Hn, Rn]]))
Expand Down
1 change: 1 addition & 0 deletions core/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ func NewEVMBlockContext(header *types.WorkObject, parent *types.WorkObject, chai
GasLimit: header.GasLimit(),
CheckIfEtxEligible: chain.CheckIfEtxIsEligible,
EtxEligibleSlices: etxEligibleSlices,
QuaiStateSize: parent.QuaiStateSize(), // using the state size at the parent for all the gas calculations
}, nil
}

Expand Down
78 changes: 40 additions & 38 deletions core/state_processor.go

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions core/state_transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ type StateTransition struct {
gp *types.GasPool
msg Message
gas uint64
stateGas uint64
gasPrice *big.Int
gasFeeCap *big.Int
gasTipCap *big.Int
Expand Down Expand Up @@ -90,6 +91,7 @@ type Message interface {
// message no matter the execution itself is successful or not.
type ExecutionResult struct {
UsedGas uint64 // Total used gas but include the refunded gas
UsedState uint64 // Total used state
Err error // Any error encountered during the execution(listed in core/vm/errors.go)
ReturnData []byte // Returned data from evm(function result or data supplied with revert opcode)
Etxs []*types.Transaction // External transactions generated from opETX
Expand Down Expand Up @@ -317,6 +319,7 @@ func (st *StateTransition) TransitionDb() (*ExecutionResult, error) {
if strings.Contains(err.Error(), ErrEtxGasLimitReached.Error()) {
return &ExecutionResult{
UsedGas: params.TxGas,
UsedState: params.EtxStateUsed,
Err: err,
ReturnData: []byte{},
Etxs: nil,
Expand Down Expand Up @@ -433,3 +436,7 @@ func (st *StateTransition) refundGas(refundQuotient uint64) {
func (st *StateTransition) gasUsed() uint64 {
return st.initialGas - st.gas
}

func (st *StateTransition) stateUsed() uint64 {
return 0
}
2 changes: 1 addition & 1 deletion core/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ type Validator interface {

// ValidateState validates the given statedb and optionally the receipts and
// gas used.
ValidateState(block *types.WorkObject, state *state.StateDB, receipts types.Receipts, etxs types.Transactions, usedGas uint64) error
ValidateState(block *types.WorkObject, state *state.StateDB, receipts types.Receipts, etxs types.Transactions, usedGas uint64, usedState uint64) error
}

// Prefetcher is an interface for pre-caching transaction signatures and state.
Expand Down
2 changes: 2 additions & 0 deletions core/types/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -1092,6 +1092,7 @@ type Message struct {
nonce uint64
amount *big.Int
gasLimit uint64
stateLimit uint64
gasPrice *big.Int
gasFeeCap *big.Int
gasTipCap *big.Int
Expand Down Expand Up @@ -1156,6 +1157,7 @@ func (tx *Transaction) AsMessage(s Signer, baseFee *big.Int) (Message, error) {
func (tx *Transaction) AsMessageWithSender(s Signer, baseFee *big.Int, sender *common.InternalAddress) (Message, error) {
msg := Message{
gasLimit: tx.Gas(),
stateLimit: tx.Gas(),
gasPrice: new(big.Int).Set(tx.GasPrice()),
gasFeeCap: new(big.Int).Set(tx.GasFeeCap()),
gasTipCap: new(big.Int).Set(tx.GasTipCap()),
Expand Down
13 changes: 11 additions & 2 deletions core/vm/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,9 @@ type Contract struct {
CodeAddr *common.Address
Input []byte

Gas uint64
value *big.Int
StateGas uint64
Gas uint64
value *big.Int
}

// NewContract returns a new contract environment for the execution of EVM.
Expand All @@ -79,6 +80,9 @@ func NewContract(caller ContractRef, object ContractRef, value *big.Int, gas uin
// ensures a value is set
c.value = value

// Create a state gas
c.StateGas = 0

return c
}

Expand Down Expand Up @@ -172,6 +176,11 @@ func (c *Contract) UseGas(gas uint64) (ok bool) {
return true
}

// UseState keeps a counter on the gas used for state operation
func (c *Contract) UseState(gas uint64) {
c.StateGas += gas
}

// Address returns the contracts address
func (c *Contract) Address() common.Address {
return c.self.Address()
Expand Down
13 changes: 7 additions & 6 deletions core/vm/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,13 @@ type BlockContext struct {
CheckIfEtxEligible CheckIfEtxEligibleFunc

// Block information
Coinbase common.Address // Provides information for COINBASE
GasLimit uint64 // Provides information for GASLIMIT
BlockNumber *big.Int // Provides information for NUMBER
Time *big.Int // Provides information for TIME
Difficulty *big.Int // Provides information for DIFFICULTY
BaseFee *big.Int // Provides information for BASEFEE
Coinbase common.Address // Provides information for COINBASE
GasLimit uint64 // Provides information for GASLIMIT
BlockNumber *big.Int // Provides information for NUMBER
Time *big.Int // Provides information for TIME
Difficulty *big.Int // Provides information for DIFFICULTY
BaseFee *big.Int // Provides information for BASEFEE
QuaiStateSize *big.Int // Provides information for QUAISTATESIZE

// Prime Terminus information for the given block
EtxEligibleSlices common.Hash
Expand Down
Loading

0 comments on commit b6da2f8

Please sign in to comment.