Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions consensus/consortium/v1/consortium.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,6 @@ import (
"sync"
"time"

"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/internal/ethapi"
"github.com/ethereum/go-ethereum/rlp"
"github.com/hashicorp/golang-lru/arc/v2"
"golang.org/x/crypto/sha3"

Expand All @@ -39,11 +35,15 @@ import (
"github.com/ethereum/go-ethereum/consensus"
consortiumCommon "github.com/ethereum/go-ethereum/consensus/consortium/common"
"github.com/ethereum/go-ethereum/consensus/misc"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/state"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/internal/ethapi"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/rlp"
"github.com/ethereum/go-ethereum/rpc"
"github.com/ethereum/go-ethereum/trie"
)
Expand Down Expand Up @@ -512,7 +512,8 @@ func (c *Consortium) Prepare(chain consensus.ChainHeaderReader, header *types.He
// Finalize implements consensus.Engine, ensuring no uncles are set, nor block
// rewards given.
func (c *Consortium) Finalize(chain consensus.ChainHeaderReader, header *types.Header, state *state.StateDB, txs *[]*types.Transaction,
uncles []*types.Header, receipts *[]*types.Receipt, systemTxs *[]*types.Transaction, internalTxs *[]*types.InternalTransaction, usedGas *uint64) error {
uncles []*types.Header, receipts *[]*types.Receipt, systemTxs *[]*types.Transaction, internalTxs *[]*types.InternalTransaction, usedGas *uint64,
) error {
lastBlockInV1 := c.chainConfig.IsOnConsortiumV2(new(big.Int).Add(header.Number, common.Big1))
if (len(*systemTxs) > 0 && !lastBlockInV1) || (len(*systemTxs) == 0 && lastBlockInV1) {
return errors.New("the length of systemTxs does not match")
Expand Down Expand Up @@ -555,7 +556,8 @@ func (c *Consortium) Finalize(chain consensus.ChainHeaderReader, header *types.H
// FinalizeAndAssemble implements consensus.Engine, ensuring no uncles are set,
// nor block rewards given, and returns the final block.
func (c *Consortium) FinalizeAndAssemble(chain consensus.ChainHeaderReader, header *types.Header, state *state.StateDB, txs []*types.Transaction,
uncles []*types.Header, receipts []*types.Receipt) (*types.Block, []*types.Receipt, error) {
uncles []*types.Header, receipts []*types.Receipt,
) (*types.Block, []*types.Receipt, error) {
// No block rewards in PoA, so the state remains as is and uncles are dropped
header.Root = state.IntermediateRoot(chain.Config().IsEIP158(header.Number))
header.UncleHash = types.CalcUncleHash(nil)
Expand Down
6 changes: 5 additions & 1 deletion core/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,15 @@ func NewEVMBlockContext(header *types.Header, chain ChainContext, author *common

// NewEVMTxContext creates a new transaction context for a single transaction.
func NewEVMTxContext(msg Message) vm.TxContext {
return vm.TxContext{
ctx := vm.TxContext{
Origin: msg.From(),
GasPrice: new(big.Int).Set(msg.GasPrice()),
BlobHashes: msg.BlobHashes(),
}
if msg.BlobGasFeeCap() != nil {
ctx.BlobFeeCap = new(big.Int).Set(msg.BlobGasFeeCap())
}
return ctx
}

// GetHashFn returns a GetHashFunc which retrieves header hashes by number
Expand Down
17 changes: 11 additions & 6 deletions core/state_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func (p *StateProcessor) Process(block *types.Block, statedb *state.StateDB, cfg
gp = new(GasPool).AddGas(block.GasLimit())
)

var receipts = make([]*types.Receipt, 0)
receipts := make([]*types.Receipt, 0)
// Mutate the block and state according to any hard-fork specs
if p.config.DAOForkSupport && p.config.DAOForkBlock != nil && p.config.DAOForkBlock.Cmp(block.Number()) == 0 {
misc.ApplyDAOHardFork(statedb)
Expand Down Expand Up @@ -187,10 +187,17 @@ func applyTransaction(
root = statedb.IntermediateRoot(config.IsEIP158(blockNumber)).Bytes()
}
*usedGas += result.UsedGas
receipt := MakeReceipt(evm, result, statedb, blockNumber, blockHash, tx, *usedGas, root)
// create the bloom filter
receiptProcessor.Apply(receipt)
return receipt, result, err
}

// MakeReceipt generates the receipt object for a transaction given its execution result.
func MakeReceipt(evm *vm.EVM, result *ExecutionResult, statedb *state.StateDB, blockNumber *big.Int, blockHash common.Hash, tx *types.Transaction, usedGas uint64, root []byte) *types.Receipt {
// Create a new receipt for the transaction, storing the intermediate root and gas used
// by the tx.
receipt := &types.Receipt{Type: tx.Type(), PostState: root, CumulativeGasUsed: *usedGas}
receipt := &types.Receipt{Type: tx.Type(), PostState: root, CumulativeGasUsed: usedGas}
if result.Failed() {
receipt.Status = types.ReceiptStatusFailed
} else {
Expand All @@ -205,7 +212,7 @@ func applyTransaction(
}

// If the transaction created a contract, store the creation address in the receipt.
if msg.To() == nil {
if tx.To() == nil {
receipt.ContractAddress = crypto.CreateAddress(evm.TxContext.Origin, tx.Nonce())
}

Expand All @@ -214,10 +221,8 @@ func applyTransaction(
receipt.BlockHash = blockHash
receipt.BlockNumber = blockNumber
receipt.TransactionIndex = uint(statedb.TxIndex())
// create the bloom filter
receiptProcessor.Apply(receipt)

return receipt, result, err
return receipt
}

// ApplyTransaction attempts to apply a transaction to the given state database
Expand Down
57 changes: 49 additions & 8 deletions core/state_processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,11 @@ func TestStateProcessorErrors(t *testing.T) {
key1, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
key2, _ = crypto.HexToECDSA("0202020202020202020202020202020202020202020202020202002020202020")
)
var makeTx = func(key *ecdsa.PrivateKey, nonce uint64, to common.Address, amount *big.Int, gasLimit uint64, gasPrice *big.Int, data []byte) *types.Transaction {
makeTx := func(key *ecdsa.PrivateKey, nonce uint64, to common.Address, amount *big.Int, gasLimit uint64, gasPrice *big.Int, data []byte) *types.Transaction {
tx, _ := types.SignTx(types.NewTransaction(nonce, to, amount, gasLimit, gasPrice, data), signer, key)
return tx
}
var mkDynamicTx = func(nonce uint64, to common.Address, gasLimit uint64, gasTipCap, gasFeeCap *big.Int) *types.Transaction {
mkDynamicTx := func(nonce uint64, to common.Address, gasLimit uint64, gasTipCap, gasFeeCap *big.Int) *types.Transaction {
tx, _ := types.SignTx(types.NewTx(&types.DynamicFeeTx{
Nonce: nonce,
GasTipCap: gasTipCap,
Expand All @@ -79,7 +79,7 @@ func TestStateProcessorErrors(t *testing.T) {
}), signer, key1)
return tx
}
var mkDynamicCreationTx = func(nonce uint64, gasLimit uint64, gasTipCap, gasFeeCap *big.Int, data []byte) *types.Transaction {
mkDynamicCreationTx := func(nonce uint64, gasLimit uint64, gasTipCap, gasFeeCap *big.Int, data []byte) *types.Transaction {
tx, _ := types.SignTx(types.NewTx(&types.DynamicFeeTx{
Nonce: nonce,
GasTipCap: gasTipCap,
Expand All @@ -90,6 +90,23 @@ func TestStateProcessorErrors(t *testing.T) {
}), signer, key1)
return tx
}
mkBlobTx := func(nonce uint64, to common.Address, gasLimit uint64, gasTipCap, gasFeeCap, blobGasFeeCap *big.Int, hashes []common.Hash) *types.Transaction {
tx, err := types.SignTx(types.NewTx(&types.BlobTx{
Nonce: nonce,
GasTipCap: uint256.MustFromBig(gasTipCap),
GasFeeCap: uint256.MustFromBig(gasFeeCap),
Gas: gasLimit,
To: to,
BlobHashes: hashes,
BlobFeeCap: uint256.MustFromBig(blobGasFeeCap),
Value: new(uint256.Int),
}), signer, key1)
if err != nil {
t.Fatal(err)
}
return tx
}

{ // Tests against a 'recent' chain definition
var (
db = rawdb.NewMemoryDatabase()
Expand All @@ -106,8 +123,8 @@ func TestStateProcessorErrors(t *testing.T) {
},
},
}
genesis = gspec.MustCommit(db, trie.NewDatabase(db, newDbConfig(rawdb.HashScheme)))
blockchain, _ = NewBlockChain(db, nil, gspec, nil, ethash.NewFaker(), vm.Config{}, nil, nil)
blockchain, _ = NewBlockChain(db, nil, gspec, nil, ethash.NewFaker(), vm.Config{}, nil, nil)
tooBigInitCode = [params.MaxInitCodeSize + 1]byte{}
)
defer blockchain.Stop()
bigNumber := new(big.Int).SetBytes(common.FromHex("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"))
Expand Down Expand Up @@ -170,6 +187,12 @@ func TestStateProcessorErrors(t *testing.T) {
},
want: "could not apply tx 0 [0xbd49d8dadfd47fb846986695f7d4da3f7b2c48c8da82dbc211a26eb124883de9]: gas limit reached",
},
{ // ErrFeeCapTooLow
txs: []*types.Transaction{
mkDynamicTx(0, common.Address{}, params.TxGas, big.NewInt(0), big.NewInt(0)),
},
want: "could not apply tx 0 [0xc4ab868fef0c82ae0387b742aee87907f2d0fc528fc6ea0a021459fb0fc4a4a8]: max fee per gas less than block base fee: address 0x71562b71999873DB5b286dF957af199Ec94617F7, maxFeePerGas: 0, baseFee: 875000000",
},
{ // ErrTipVeryHigh
txs: []*types.Transaction{
mkDynamicTx(0, common.Address{}, params.TxGas, tooBigNumber, big.NewInt(1)),
Expand Down Expand Up @@ -205,8 +228,26 @@ func TestStateProcessorErrors(t *testing.T) {
},
want: "could not apply tx 0 [0xd82a0c2519acfeac9a948258c47e784acd20651d9d80f9a1c67b4137651c3a24]: insufficient funds for gas * price + value: address 0x71562b71999873DB5b286dF957af199Ec94617F7 have 1000000000000000000 want 2431633873983640103894990685182446064918669677978451844828609264166175722438635000",
},
{ // ErrMaxInitCodeSizeExceeded
txs: []*types.Transaction{
mkDynamicCreationTx(0, 500000, common.Big0, big.NewInt(params.InitialBaseFee), tooBigInitCode[:]),
},
want: "could not apply tx 0 [0xd491405f06c92d118dd3208376fcee18a57c54bc52063ee4a26b1cf296857c25]: max initcode size exceeded: code size 49153 limit 49152",
},
{ // ErrIntrinsicGas: Not enough gas to cover init code
txs: []*types.Transaction{
mkDynamicCreationTx(0, 54299, common.Big0, big.NewInt(params.InitialBaseFee), make([]byte, 320)),
},
want: "could not apply tx 0 [0xfd49536a9b323769d8472fcb3ebb3689b707a349379baee3e2ee3fe7baae06a1]: intrinsic gas too low: have 54299, want 54300",
},
{ // ErrBlobFeeCapTooLow
txs: []*types.Transaction{
mkBlobTx(0, common.Address{}, params.TxGas, big.NewInt(1), big.NewInt(1), big.NewInt(0), []common.Hash{(common.Hash{1})}),
},
want: "could not apply tx 0 [0x6c11015985ce82db691d7b2d017acda296db88b811c3c60dc71449c76256c716]: max fee per gas less than block base fee: address 0x71562b71999873DB5b286dF957af199Ec94617F7, maxFeePerGas: 1, baseFee: 875000000",
},
} {
block := GenerateBadBlock(genesis, ethash.NewFaker(), tt.txs, gspec.Config)
block := GenerateBadBlock(gspec.ToBlock(), ethash.NewFaker(), tt.txs, gspec.Config)
_, err := blockchain.InsertChain(types.Blocks{block}, nil)
if err == nil {
t.Fatal("block imported without errors")
Expand Down Expand Up @@ -485,8 +526,8 @@ func mkBlobTx(nonce uint64, to common.Address, gasLimit uint64, gasTipCap, gasFe
}

func TestBlobTxStateTransition(t *testing.T) {
var roninTreasuryAddress = &common.Address{0x11}
var testBlobTxGasExecution = func(funds *big.Int, nBlobs int) (*state.StateDB, common.Address, error) {
roninTreasuryAddress := &common.Address{0x11}
testBlobTxGasExecution := func(funds *big.Int, nBlobs int) (*state.StateDB, common.Address, error) {
var (
gendb = rawdb.NewMemoryDatabase()
key, _ = crypto.GenerateKey()
Expand Down
29 changes: 17 additions & 12 deletions core/state_transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,17 +310,23 @@ func (st *StateTransition) preCheck() error {
return fmt.Errorf("%w: address %v, nonce: %d", ErrNonceMax,
msg.From().Hex(), stNonce)
}
// Make sure the sender is an EOA
if codeHash := st.state.GetCodeHash(msg.From()); codeHash != emptyCodeHash && codeHash != (common.Hash{}) {
return fmt.Errorf("%w: address %v, codehash: %s", ErrSenderNoEOA,
msg.From().Hex(), codeHash)
}
}

// TODO: Support SkipFromEOACheck later
// if !msg.SkipFromEOACheck {
// // Make sure the sender is an EOA
// if codeHash := st.state.GetCodeHash(msg.From()); codeHash != emptyCodeHash && codeHash != (common.Hash{}) {
// return fmt.Errorf("%w: address %v, codehash: %s", ErrSenderNoEOA,
// msg.From().Hex(), codeHash)
// }
// }

// Make sure that transaction gasFeeCap is greater than the baseFee (post london)
if !st.evm.Config.IsSystemTransaction && st.evm.ChainConfig().IsLondon(st.evm.Context.BlockNumber) {
// Skip the checks if gas fields are zero and baseFee was explicitly disabled (eth_call)
if !st.evm.Config.NoBaseFee || st.gasFeeCap.BitLen() > 0 || st.gasTipCap.BitLen() > 0 {
if l := st.gasFeeCap.BitLen(); l > 256 {
skipCheck := st.evm.Config.NoBaseFee && msg.GasFeeCap().BitLen() == 0 && msg.GasTipCap().BitLen() == 0
if !skipCheck {
if l := msg.GasFeeCap().BitLen(); l > 256 {
return fmt.Errorf("%w: address %v, maxFeePerGas bit length: %d", ErrFeeCapVeryHigh,
msg.From().Hex(), l)
}
Expand All @@ -334,9 +340,9 @@ func (st *StateTransition) preCheck() error {
}
// This will panic if baseFee is nil, but basefee presence is verified
// as part of header validation.
if st.gasFeeCap.Cmp(st.evm.Context.BaseFee) < 0 {
return fmt.Errorf("%w: address %v, maxFeePerGas: %s baseFee: %s", ErrFeeCapTooLow,
msg.From().Hex(), st.gasFeeCap, st.evm.Context.BaseFee)
if msg.GasFeeCap().Cmp(st.evm.Context.BaseFee) < 0 {
return fmt.Errorf("%w: address %v, maxFeePerGas: %s, baseFee: %s", ErrFeeCapTooLow,
msg.From().Hex(), msg.GasFeeCap(), st.evm.Context.BaseFee)
}
}
}
Expand Down Expand Up @@ -384,13 +390,12 @@ func (st *StateTransition) preCheck() error {
// This will panic if blobBaseFee is nil, but blobBaseFee presence
// is verified as part of header validation.
if msg.BlobGasFeeCap().Cmp(st.evm.Context.BlobBaseFee) < 0 {
return fmt.Errorf("%w: address %v blobGasFeeCap: %v, blobBaseFee: %v", ErrBlobFeeCapTooLow,
return fmt.Errorf("%w: address %v, blobGasFeeCap: %v, blobBaseFee: %v", ErrBlobFeeCapTooLow,
msg.From().Hex(), msg.BlobGasFeeCap(), st.evm.Context.BlobBaseFee)
}
}
}
}

return st.buyGas()
}

Expand Down
Loading
Loading