Skip to content

Commit 78187b8

Browse files
committed
core: add test for data blob txs
1 parent e61cf36 commit 78187b8

File tree

2 files changed

+86
-1
lines changed

2 files changed

+86
-1
lines changed

core/bench_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ func genValueTx(nbytes int) func(int, *BlockGen) {
8585
return func(i int, gen *BlockGen) {
8686
toaddr := common.Address{}
8787
data := make([]byte, nbytes)
88-
gas, _ := IntrinsicGas(data, nil, false, false, false)
88+
gas, _ := IntrinsicGas(data, nil, 0, false, false, false)
8989
signer := types.MakeSigner(gen.config, big.NewInt(int64(i)))
9090
gasPrice := big.NewInt(0)
9191
if gen.header.BaseFee != nil {

core/blockchain_test.go

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import (
4141
"github.com/ethereum/go-ethereum/ethdb"
4242
"github.com/ethereum/go-ethereum/params"
4343
"github.com/ethereum/go-ethereum/trie"
44+
"github.com/protolambda/ztyp/view"
4445
)
4546

4647
// So we can deterministically seed different blockchains
@@ -3704,3 +3705,87 @@ func TestEIP1559Transition(t *testing.T) {
37043705
t.Fatalf("sender balance incorrect: expected %d, got %d", expected, actual)
37053706
}
37063707
}
3708+
3709+
// TestDataBlobTxs tests the following:
3710+
//
3711+
// 1. Writes data hash from transaction to storage.
3712+
func TestDataBlobTxs(t *testing.T) {
3713+
var (
3714+
one = common.Hash{1}
3715+
two = common.Hash{2}
3716+
aa = common.HexToAddress("0x000000000000000000000000000000000000aaaa")
3717+
3718+
// Generate a canonical chain to act as the main dataset
3719+
engine = ethash.NewFaker()
3720+
db = rawdb.NewMemoryDatabase()
3721+
3722+
// A sender who makes transactions, has some funds
3723+
key1, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
3724+
addr1 = crypto.PubkeyToAddress(key1.PublicKey)
3725+
funds = new(big.Int).Mul(common.Big1, big.NewInt(params.Ether))
3726+
gspec = &Genesis{
3727+
Config: params.AllEthashProtocolChanges,
3728+
Alloc: GenesisAlloc{
3729+
addr1: {Balance: funds},
3730+
// The address 0xAAAA writes dataHashes[1] to storage slot 0x0.
3731+
aa: {
3732+
Code: []byte{
3733+
byte(vm.PUSH1),
3734+
byte(0x1),
3735+
byte(vm.DATAHASH),
3736+
byte(vm.PUSH1),
3737+
byte(0x0),
3738+
byte(vm.SSTORE),
3739+
},
3740+
Nonce: 0,
3741+
Balance: big.NewInt(0),
3742+
},
3743+
},
3744+
}
3745+
)
3746+
3747+
gspec.Config.BerlinBlock = common.Big0
3748+
gspec.Config.LondonBlock = common.Big0
3749+
gspec.Config.ShardingForkBlock = common.Big0
3750+
genesis := gspec.MustCommit(db)
3751+
signer := types.LatestSigner(gspec.Config)
3752+
3753+
blocks, _ := GenerateChain(gspec.Config, genesis, engine, db, 1, func(i int, b *BlockGen) {
3754+
b.SetCoinbase(common.Address{1})
3755+
msg := types.BlobTxMessage{
3756+
Nonce: 0,
3757+
Gas: 500000,
3758+
}
3759+
msg.To.Address = (*types.AddressSSZ)(&aa)
3760+
msg.ChainID.SetFromBig((*big.Int)(gspec.Config.ChainID))
3761+
msg.Nonce = view.Uint64View(0)
3762+
msg.GasFeeCap.SetFromBig(newGwei(5))
3763+
msg.GasTipCap.SetFromBig(big.NewInt(2))
3764+
msg.BlobVersionedHashes = []common.Hash{one, two}
3765+
txdata := &types.SignedBlobTx{Message: msg}
3766+
3767+
tx := types.NewTx(txdata)
3768+
tx, _ = types.SignTx(tx, signer, key1)
3769+
3770+
b.AddTx(tx)
3771+
})
3772+
3773+
diskdb := rawdb.NewMemoryDatabase()
3774+
gspec.MustCommit(diskdb)
3775+
3776+
chain, err := NewBlockChain(diskdb, nil, gspec.Config, engine, vm.Config{}, nil, nil)
3777+
if err != nil {
3778+
t.Fatalf("failed to create tester chain: %v", err)
3779+
}
3780+
if n, err := chain.InsertChain(blocks); err != nil {
3781+
t.Fatalf("block %d: failed to insert into chain: %v", n, err)
3782+
}
3783+
3784+
state, _ := chain.State()
3785+
3786+
// 1. Check that the storage slot is set to dataHashes[1].
3787+
actual := state.GetState(aa, common.Hash{0})
3788+
if actual != two {
3789+
t.Fatalf("incorrect data hash written to state (want: %s, got: %s)", two, actual)
3790+
}
3791+
}

0 commit comments

Comments
 (0)