Skip to content
New issue

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

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

Already on GitHub? # to your account

eupgrade/cancun: verify no blobs in header #611

Merged
merged 10 commits into from
Aug 2, 2024
63 changes: 38 additions & 25 deletions plugin/evm/vm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ import (
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/rlp"
"github.com/holiman/uint256"

"github.com/ava-labs/coreth/constants"
"github.com/ava-labs/coreth/eth/filters"
"github.com/ava-labs/coreth/internal/ethapi"
"github.com/ava-labs/coreth/metrics"
Expand All @@ -41,7 +43,6 @@ import (
"github.com/ava-labs/avalanchego/snow"
"github.com/ava-labs/avalanchego/snow/validators"
"github.com/ava-labs/avalanchego/utils/cb58"
"github.com/ava-labs/avalanchego/utils/constants"
"github.com/ava-labs/avalanchego/utils/crypto/bls"
"github.com/ava-labs/avalanchego/utils/crypto/secp256k1"
"github.com/ava-labs/avalanchego/utils/formatting"
Expand All @@ -55,6 +56,7 @@ import (
"github.com/ava-labs/avalanchego/vms/secp256k1fx"

commonEng "github.com/ava-labs/avalanchego/snow/engine/common"
constantsEng "github.com/ava-labs/avalanchego/utils/constants"

"github.com/ava-labs/coreth/consensus/dummy"
"github.com/ava-labs/coreth/core"
Expand Down Expand Up @@ -217,9 +219,9 @@ func NewContext() *snow.Context {
ctx.ValidatorState = &validators.TestState{
GetSubnetIDF: func(_ context.Context, chainID ids.ID) (ids.ID, error) {
subnetID, ok := map[ids.ID]ids.ID{
constants.PlatformChainID: constants.PrimaryNetworkID,
testXChainID: constants.PrimaryNetworkID,
testCChainID: constants.PrimaryNetworkID,
constantsEng.PlatformChainID: constantsEng.PrimaryNetworkID,
testXChainID: constantsEng.PrimaryNetworkID,
testCChainID: constantsEng.PrimaryNetworkID,
}[chainID]
if !ok {
return ids.Empty, errors.New("unknown chain")
Expand Down Expand Up @@ -4091,35 +4093,46 @@ func TestParentBeaconRootBlock(t *testing.T) {
}

func TestNoBlobsAllowed(t *testing.T) {
darioush marked this conversation as resolved.
Show resolved Hide resolved
ctx := context.Background()
require := require.New(t)
importAmount := uint64(1000000000)
issuer, vm, _, _, _ := GenesisVMWithUTXOs(t, true, genesisJSONCancun, "", "", map[ids.ShortID]uint64{
testShortIDAddrs[0]: importAmount,
})
defer func() { require.NoError(vm.Shutdown(context.Background())) }()

// Build a valid block in Cancun
importTx, err := vm.newImportTx(vm.ctx.XChainID, testEthAddrs[0], initialBaseFee, []*secp256k1.PrivateKey{testKeys[0]})
require.NoError(err)
require.NoError(vm.mempool.AddLocalTx(importTx))
<-issuer
blk, err := vm.BuildBlock(context.Background())
gspec := new(core.Genesis)
err := json.Unmarshal([]byte(genesisJSONCancun), gspec)
require.NoError(err)

// Modify the block to have a non-zero blob gas
ethBlock := blk.(*chain.BlockWrapper).Block.(*Block).ethBlock
header := types.CopyHeader(ethBlock.Header())
blobGasUsed := uint64(params.BlobTxBlobGasPerBlob)
header.BlobGasUsed = &blobGasUsed
modifiedEthBlock := types.NewBlockWithExtData(
header, nil, nil, nil, new(trie.Trie), ethBlock.ExtData(), false)
modifiedBlock, err := vm.newBlock(modifiedEthBlock)
// Make one block with a single blob tx
signer := types.NewCancunSigner(gspec.Config.ChainID)
blockGen := func(_ int, b *core.BlockGen) {
b.SetCoinbase(constants.BlackholeAddr)
fee := big.NewInt(500)
fee.Add(fee, b.BaseFee())
tx, err := types.SignTx(types.NewTx(&types.BlobTx{
Nonce: 0,
GasTipCap: uint256.NewInt(1),
GasFeeCap: uint256.MustFromBig(fee),
Gas: params.TxGas,
To: testEthAddrs[0],
BlobFeeCap: uint256.NewInt(1),
BlobHashes: []common.Hash{{1}},
darioush marked this conversation as resolved.
Show resolved Hide resolved
Value: new(uint256.Int),
}), signer, testKeys[0].ToECDSA())
require.NoError(err)
b.AddTx(tx)
}
// FullFaker used to skip header verification so we can generate a block with blobs
_, blocks, _, err := core.GenerateChainWithGenesis(gspec, dummy.NewFullFaker(), 1, 10, blockGen)
require.NoError(err)

// Create a VM with the genesis (will use header verification)
_, vm, _, _, _ := GenesisVM(t, true, genesisJSONCancun, "", "")
defer func() { require.NoError(vm.Shutdown(ctx)) }()

// Verification should fail
_, err = vm.ParseBlock(context.Background(), modifiedBlock.Bytes())
vmBlock, err := vm.newBlock(blocks[0])
require.NoError(err)
_, err = vm.ParseBlock(ctx, vmBlock.Bytes())
require.ErrorContains(err, "blobs not enabled on avalanche networks")
err = modifiedBlock.Verify(context.Background())
err = vmBlock.Verify(ctx)
require.ErrorContains(err, "blobs not enabled on avalanche networks")
}

Expand Down
Loading