forked from ethereum/go-ethereum
-
Notifications
You must be signed in to change notification settings - Fork 149
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(taiko): implement ApplyTransactionWithTimeout for transaction ex…
…ecution with context timeout
- Loading branch information
Showing
3 changed files
with
186 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package core | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
|
||
"github.com/ethereum/go-ethereum/common" | ||
"github.com/ethereum/go-ethereum/core/state" | ||
"github.com/ethereum/go-ethereum/core/types" | ||
"github.com/ethereum/go-ethereum/core/vm" | ||
"github.com/ethereum/go-ethereum/params" | ||
) | ||
|
||
// ApplyTransactionWithTimeout applies a transaction to the state with a timeout context. | ||
// If the context is cancelled or times out, the EVM execution will be stopped. | ||
// | ||
// Parameters: | ||
// - ctx: The context to control the timeout and cancellation. | ||
// - hashFunc: Function to retrieve block hashes. | ||
// - config: The chain configuration parameters. | ||
// - bc: The blockchain context. | ||
// - author: The address of the block author. | ||
// - gp: The gas pool for the transaction. | ||
// - statedb: The state database. | ||
// - header: The block header. | ||
// - tx: The transaction to be applied. | ||
// - usedGas: Pointer to the used gas value. | ||
// - cfg: The EVM configuration. | ||
// | ||
// Returns: | ||
// - *types.Receipt: The receipt of the transaction. | ||
// - error: An error if the transaction application fails. | ||
func ApplyTransactionWithTimeout(ctx context.Context, hashFuncWrapper func(vm.GetHashFunc) vm.GetHashFunc, config *params.ChainConfig, bc ChainContext, author *common.Address, gp *GasPool, statedb *state.StateDB, header *types.Header, tx *types.Transaction, usedGas *uint64, cfg vm.Config) (*types.Receipt, error) { | ||
msg, err := TransactionToMessage(tx, types.MakeSigner(config, header.Number, header.Time), header.BaseFee) | ||
if err != nil { | ||
return nil, err | ||
} | ||
// CHANGE(taiko): decode the basefeeSharingPctg config from the extradata, and | ||
// add it to the Message, if its an ontake block. | ||
if config.IsOntake(header.Number) { | ||
msg.BasefeeSharingPctg = DecodeOntakeExtraData(header.Extra) | ||
} | ||
// Create a new context to be used in the EVM environment | ||
blockContext := NewEVMBlockContext(header, bc, author) | ||
txContext := NewEVMTxContext(msg) | ||
vmenv := vm.NewEVM(blockContext, txContext, statedb, config, cfg) | ||
go func() { | ||
<-ctx.Done() | ||
if errors.Is(ctx.Err(), context.DeadlineExceeded) { | ||
// Stop evm execution. Note cancellation is not necessarily immediate. | ||
vmenv.Cancel() | ||
} | ||
}() | ||
return ApplyTransactionWithEVM(msg, config, gp, statedb, header.Number, header.Hash(), tx, usedGas, vmenv) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package tracers | ||
|
||
import ( | ||
"context" | ||
"math/big" | ||
"sync/atomic" | ||
"testing" | ||
|
||
"github.com/ethereum/go-ethereum/core" | ||
"github.com/ethereum/go-ethereum/core/types" | ||
"github.com/ethereum/go-ethereum/params" | ||
"github.com/ethereum/go-ethereum/rpc" | ||
) | ||
|
||
func (b *testBackend) BlockChain() *core.BlockChain { | ||
return b.chain | ||
} | ||
|
||
func TestProvingPreflights(t *testing.T) { | ||
// Initialize test accounts | ||
accounts := newAccounts(3) | ||
genesis := &core.Genesis{ | ||
Config: params.TestChainConfig, | ||
Alloc: types.GenesisAlloc{ | ||
accounts[0].addr: {Balance: big.NewInt(params.Ether)}, | ||
accounts[1].addr: {Balance: big.NewInt(params.Ether)}, | ||
accounts[2].addr: {Balance: big.NewInt(params.Ether)}, | ||
}, | ||
} | ||
genBlocks := 50 | ||
signer := types.HomesteadSigner{} | ||
|
||
var ( | ||
ref atomic.Uint32 // total refs has made | ||
rel atomic.Uint32 // total rels has made | ||
nonce uint64 | ||
) | ||
backend := newTestBackend(t, genBlocks, genesis, func(i int, b *core.BlockGen) { | ||
// Transfer from account[0] to account[1] | ||
// value: 1000 wei | ||
// fee: 0 wei | ||
for j := 0; j < i+1; j++ { | ||
tx, _ := types.SignTx(types.NewTransaction(nonce, accounts[1].addr, big.NewInt(1000), params.TxGas, b.BaseFee(), nil), signer, accounts[0].key) | ||
b.AddTx(tx) | ||
nonce += 1 | ||
} | ||
}) | ||
backend.refHook = func() { ref.Add(1) } | ||
backend.relHook = func() { rel.Add(1) } | ||
api := NewAPI(backend) | ||
|
||
// single := `{"txHash":"0x0000000000000000000000000000000000000000000000000000000000000000","result":{"gas":21000,"failed":false,"returnValue":"","structLogs":[]}}` | ||
var cases = []struct { | ||
start uint64 | ||
end uint64 | ||
config *TraceConfig | ||
}{ | ||
{0, 50, nil}, // the entire chain range, blocks [1, 50] | ||
{10, 20, nil}, // the middle chain range, blocks [11, 20] | ||
} | ||
for _, c := range cases { | ||
ref.Store(0) | ||
rel.Store(0) | ||
|
||
from, _ := api.blockByNumber(context.Background(), rpc.BlockNumber(c.start)) | ||
to, _ := api.blockByNumber(context.Background(), rpc.BlockNumber(c.end)) | ||
resCh := api.provingPreflights(from, to, c.config, nil) | ||
|
||
next := c.start + 1 | ||
for result := range resCh { | ||
if have, want := result.Block.NumberU64(), next; have != want { | ||
t.Fatalf("unexpected tracing block, have %d want %d", have, want) | ||
} | ||
if next == 1 { | ||
if have, want := len(result.InitAccountProofs), 2; have != want { | ||
t.Fatalf("unexpected result length, have %d want %d", have, want) | ||
} | ||
} else { | ||
if have, want := len(result.InitAccountProofs), 3; have != want { | ||
t.Fatalf("unexpected result length, have %d want %d", have, want) | ||
} | ||
} | ||
next += 1 | ||
} | ||
if next != c.end+1 { | ||
t.Error("Missing tracing block") | ||
} | ||
|
||
if nref, nrel := ref.Load(), rel.Load(); nref != nrel { | ||
t.Errorf("Ref and deref actions are not equal, ref %d rel %d", nref, nrel) | ||
} | ||
} | ||
} |