Skip to content

Commit

Permalink
Implement EIP-2718 base structure
Browse files Browse the repository at this point in the history
  • Loading branch information
trinhdn2 committed Jun 21, 2023
1 parent 71c3cfe commit d32c96d
Show file tree
Hide file tree
Showing 14 changed files with 1,406 additions and 388 deletions.
11 changes: 11 additions & 0 deletions core/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,20 @@ var (
// next one expected based on the local chain.
ErrNonceTooHigh = errors.New("nonce too high")

// ErrNonceMax is returned if the nonce of a transaction sender account has
// maximum allowed value and would become invalid if incremented.
ErrNonceMax = errors.New("nonce has max value")

// ErrInsufficientFundsForTransfer is returned if the transaction sender doesn't
// have enough funds for transfer(topmost call only).
ErrInsufficientFundsForTransfer = errors.New("insufficient funds for transfer")

ErrNotPoSV = errors.New("Posv not found in config")

ErrNotFoundM1 = errors.New("list M1 not found ")

ErrStopPreparingBlock = errors.New("stop calculating a block not verified by M2")

// ErrSenderNoEOA is returned if the sender of a transaction is a contract.
ErrSenderNoEOA = errors.New("sender not an eoa")
)
38 changes: 25 additions & 13 deletions core/token_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,18 @@ package core

import (
"fmt"
ethereum "github.com/tomochain/tomochain"
"math/big"
"math/rand"
"strings"

tomochain "github.com/tomochain/tomochain"
"github.com/tomochain/tomochain/accounts/abi"
"github.com/tomochain/tomochain/common"
"github.com/tomochain/tomochain/consensus"
"github.com/tomochain/tomochain/contracts/tomox/contract"
"github.com/tomochain/tomochain/core/state"
"github.com/tomochain/tomochain/core/vm"
"github.com/tomochain/tomochain/log"
"math/big"
"math/rand"
"strings"
)

const (
Expand All @@ -38,7 +39,7 @@ const (

// callmsg implements core.Message to allow passing it as a transaction simulator.
type callmsg struct {
ethereum.CallMsg
tomochain.CallMsg
}

func (m callmsg) From() common.Address { return m.CallMsg.From }
Expand All @@ -52,7 +53,7 @@ func (m callmsg) Data() []byte { return m.CallMsg.Data }
func (m callmsg) BalanceTokenFee() *big.Int { return m.CallMsg.BalanceTokenFee }

type SimulatedBackend interface {
CallContractWithState(call ethereum.CallMsg, chain consensus.ChainContext, statedb *state.StateDB) ([]byte, error)
CallContractWithState(call tomochain.CallMsg, chain consensus.ChainContext, statedb *state.StateDB) ([]byte, error)
}

// GetTokenAbi return token abi
Expand All @@ -72,7 +73,7 @@ func RunContract(chain consensus.ChainContext, statedb *state.StateDB, contractA
}
fakeCaller := common.HexToAddress("0x0000000000000000000000000000000000000001")
statedb.SetBalance(fakeCaller, common.BasePrice)
msg := ethereum.CallMsg{To: &contractAddr, Data: input, From: fakeCaller}
msg := tomochain.CallMsg{To: &contractAddr, Data: input, From: fakeCaller}
result, err := CallContractWithState(msg, chain, statedb)
if err != nil {
return nil, err
Expand All @@ -85,9 +86,9 @@ func RunContract(chain consensus.ChainContext, statedb *state.StateDB, contractA
return unpackResult, nil
}

//FIXME: please use copyState for this function
// FIXME: please use copyState for this function
// CallContractWithState executes a contract call at the given state.
func CallContractWithState(call ethereum.CallMsg, chain consensus.ChainContext, statedb *state.StateDB) ([]byte, error) {
func CallContractWithState(call tomochain.CallMsg, chain consensus.ChainContext, statedb *state.StateDB) ([]byte, error) {
// Ensure message is initialized properly.
call.GasPrice = big.NewInt(0)

Expand All @@ -98,11 +99,22 @@ func CallContractWithState(call ethereum.CallMsg, chain consensus.ChainContext,
call.Value = new(big.Int)
}
// Execute the call.
msg := callmsg{call}
msg := &Message{
To: call.To,
From: call.From,
Value: call.Value,
GasLimit: call.Gas,
GasPrice: call.GasPrice,
GasFeeCap: call.GasFeeCap,
GasTipCap: call.GasTipCap,
Data: call.Data,
AccessList: call.AccessList,
SkipAccountChecks: false,
}
feeCapacity := state.GetTRC21FeeCapacityFromState(statedb)
if msg.To() != nil {
if value, ok := feeCapacity[*msg.To()]; ok {
msg.CallMsg.BalanceTokenFee = value
if msg.To != nil {
if value, ok := feeCapacity[*msg.To]; ok {
msg.BalanceTokenFee = value
}
}
evmContext := NewEVMContext(msg, chain.CurrentHeader(), chain, nil)
Expand Down
99 changes: 0 additions & 99 deletions core/types/gen_tx_json.go

This file was deleted.

10 changes: 10 additions & 0 deletions core/types/hashes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package types

import (
"github.com/tomochain/tomochain/crypto"
)

var (
// EmptyCodeHash is the known hash of the empty EVM bytecode.
EmptyCodeHash = crypto.Keccak256Hash(nil) // c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470
)
11 changes: 11 additions & 0 deletions core/types/hashing.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package types

import (
"bytes"
"sync"
)

// encodeBufferPool holds temporary encoder buffers for DeriveSha and TX encoding.
var encodeBufferPool = sync.Pool{
New: func() interface{} { return new(bytes.Buffer) },
}

0 comments on commit d32c96d

Please sign in to comment.