Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix tracer precompile upgrade #979

Merged
merged 4 commits into from
Nov 3, 2023
Merged
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
5 changes: 5 additions & 0 deletions core/chain_makers.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,11 @@ func GenerateChain(config *params.ChainConfig, parent *types.Block, engine conse
b := &BlockGen{i: i, chain: blocks, parent: parent, statedb: statedb, config: config, engine: engine}
b.header = makeHeader(chainreader, config, parent, gap, statedb, b.engine)

err := ApplyUpgrades(config, &parent.Header().Time, b, statedb)
if err != nil {
return nil, nil, fmt.Errorf("failed to configure precompiles %v", err)
}

// Execute any user modifications to the block
if gen != nil {
gen(i, b)
Expand Down
5 changes: 5 additions & 0 deletions eth/tracers/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,11 @@ func (api *baseAPI) traceBlock(ctx context.Context, block *types.Block, config *
}
defer release()

err = core.ApplyUpgrades(api.backend.ChainConfig(), &parent.Header().Time, block, statedb)
if err != nil {
return nil, fmt.Errorf("failed to configure precompiles in block tracing %v", err)
}

// JS tracers have high overhead. In this case run a parallel
// process that generates states in one thread and traces txes
// in separate worker threads.
Expand Down
105 changes: 105 additions & 0 deletions eth/tracers/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ import (
"github.com/ava-labs/subnet-evm/ethdb"
"github.com/ava-labs/subnet-evm/internal/ethapi"
"github.com/ava-labs/subnet-evm/params"
"github.com/ava-labs/subnet-evm/precompile/contracts/txallowlist"
"github.com/ava-labs/subnet-evm/rpc"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
Expand Down Expand Up @@ -914,3 +915,107 @@ func TestTraceChain(t *testing.T) {
}
}
}

func TestTraceBlockPrecompileActivation(t *testing.T) {
t.Parallel()

// Initialize test accounts
accounts := newAccounts(3)
copyConfig := *params.TestChainConfig
genesis := &core.Genesis{
Config: &copyConfig,
Alloc: core.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)},
},
}
// assumes gap is 10 sec, means 3 block away
activateAllowlistBlock := 3
activateAllowListTime := uint64(activateAllowlistBlock * 10)
activateTxAllowListConfig := params.PrecompileUpgrade{
Config: txallowlist.NewConfig(&activateAllowListTime, []common.Address{accounts[0].addr}, nil, nil),
}

deactivateAllowlistBlock := activateAllowlistBlock + 3
deactivateAllowListTime := uint64(deactivateAllowlistBlock) * 10
deactivateTxAllowListConfig := params.PrecompileUpgrade{
Config: txallowlist.NewDisableConfig(&deactivateAllowListTime),
}

genesis.Config.PrecompileUpgrades = []params.PrecompileUpgrade{
activateTxAllowListConfig,
deactivateTxAllowListConfig,
}
genBlocks := 10
signer := types.HomesteadSigner{}
backend := newTestBackend(t, genBlocks, genesis, func(i int, b *core.BlockGen) {
// Transfer from account[0] to account[1]
// value: 1000 wei
// fee: 0 wei
tx, _ := types.SignTx(types.NewTransaction(uint64(i), accounts[1].addr, big.NewInt(1000), params.TxGas, b.BaseFee(), nil), signer, accounts[0].key)
b.AddTx(tx)
})
defer backend.chain.Stop()
api := NewAPI(backend)

testSuite := []struct {
blockNumber rpc.BlockNumber
config *TraceConfig
want string
expectErr error
}{
// Trace head block
{
blockNumber: rpc.BlockNumber(genBlocks),
want: `[{"result":{"gas":21000,"failed":false,"returnValue":"","structLogs":[]}}]`,
},
// Trace block before activation
{
blockNumber: rpc.BlockNumber(activateAllowlistBlock - 1),
want: `[{"result":{"gas":21000,"failed":false,"returnValue":"","structLogs":[]}}]`,
},
// Trace block activation
{
blockNumber: rpc.BlockNumber(activateAllowlistBlock),
want: `[{"result":{"gas":21000,"failed":false,"returnValue":"","structLogs":[]}}]`,
},
// Trace block after activation
{
blockNumber: rpc.BlockNumber(activateAllowlistBlock + 1),
want: `[{"result":{"gas":21000,"failed":false,"returnValue":"","structLogs":[]}}]`,
},
// Trace block deactivation
{
blockNumber: rpc.BlockNumber(deactivateAllowlistBlock),
want: `[{"result":{"gas":21000,"failed":false,"returnValue":"","structLogs":[]}}]`,
},
// Trace block after deactivation
{
blockNumber: rpc.BlockNumber(deactivateAllowlistBlock + 1),
want: `[{"result":{"gas":21000,"failed":false,"returnValue":"","structLogs":[]}}]`,
},
}
for i, tc := range testSuite {
result, err := api.TraceBlockByNumber(context.Background(), tc.blockNumber, tc.config)
if tc.expectErr != nil {
if err == nil {
t.Errorf("test %d, want error %v", i, tc.expectErr)
continue
}
if !reflect.DeepEqual(err, tc.expectErr) {
t.Errorf("test %d: error mismatch, want %v, get %v", i, tc.expectErr, err)
}
continue
}
if err != nil {
t.Errorf("test %d, want no error, have %v", i, err)
continue
}
have, _ := json.Marshal(result)
want := tc.want
if string(have) != want {
t.Errorf("test %d, result mismatch, have\n%v\n, want\n%v\n", i, string(have), want)
}
}
}
Loading