1717package vm_test
1818
1919import (
20+ "errors"
2021 "fmt"
2122 "math"
2223 "math/big"
@@ -41,22 +42,25 @@ type preprocessingCharger struct {
4142 charge map [common.Hash ]uint64
4243}
4344
45+ var errUnknownTx = errors .New ("unknown tx" )
46+
4447func (p preprocessingCharger ) PreprocessingGasCharge (tx common.Hash ) (uint64 , error ) {
4548 c , ok := p .charge [tx ]
4649 if ! ok {
47- return 0 , fmt .Errorf ("unknown tx %v" , tx )
50+ return 0 , fmt .Errorf ("%w: %v" , errUnknownTx , tx )
4851 }
4952 return c , nil
5053}
5154
5255func TestChargePreprocessingGas (t * testing.T ) {
5356 tests := []struct {
54- name string
55- to * common.Address
56- charge uint64
57- txGas uint64
58- wantVMErr error
59- wantGasUsed uint64
57+ name string
58+ to * common.Address
59+ charge uint64
60+ skipChargeRegistration bool
61+ txGas uint64
62+ wantVMErr error
63+ wantGasUsed uint64
6064 }{
6165 {
6266 name : "standard create" ,
@@ -92,6 +96,14 @@ func TestChargePreprocessingGas(t *testing.T) {
9296 txGas : params .TxGas + 20000 ,
9397 wantGasUsed : params .TxGas + 13579 ,
9498 },
99+ {
100+ name : "error propagation" ,
101+ to : & common.Address {},
102+ skipChargeRegistration : true ,
103+ txGas : params .TxGas ,
104+ wantGasUsed : params .TxGas ,
105+ wantVMErr : errUnknownTx ,
106+ },
95107 }
96108
97109 config := params .AllDevChainProtocolChanges
@@ -108,14 +120,19 @@ func TestChargePreprocessingGas(t *testing.T) {
108120
109121 var txs types.Transactions
110122 charge := make (map [common.Hash ]uint64 )
111- for _ , tt := range tests {
123+ for i , tt := range tests {
112124 tx := types .MustSignNewTx (key , signer , & types.LegacyTx {
125+ // Although nonces aren't strictly necessary, they guarantee a
126+ // different tx hash for each one.
127+ Nonce : uint64 (i ), //nolint:gosec // Known to not overflow
113128 To : tt .to ,
114129 GasPrice : big .NewInt (1 ),
115130 Gas : tt .txGas ,
116131 })
117132 txs = append (txs , tx )
118- charge [tx .Hash ()] = tt .charge
133+ if ! tt .skipChargeRegistration {
134+ charge [tx .Hash ()] = tt .charge
135+ }
119136 }
120137
121138 vm .RegisterHooks (& preprocessingCharger {
@@ -138,6 +155,7 @@ func TestChargePreprocessingGas(t *testing.T) {
138155 require .NoError (t , err , "state.New(types.EmptyRootHash, [memory db], nil)" )
139156 sdb .SetTxContext (tx .Hash (), i )
140157 sdb .SetBalance (eoa , new (uint256.Int ).SetAllOne ())
158+ sdb .SetNonce (eoa , tx .Nonce ())
141159
142160 var gotGasUsed uint64
143161 gp := core .GasPool (math .MaxUint64 )
@@ -164,8 +182,9 @@ func TestChargePreprocessingGas(t *testing.T) {
164182
165183 t .Run ("VM_error" , func (t * testing.T ) {
166184 sdb , evm := ethtest .NewZeroEVM (t , ethtest .WithChainConfig (config ))
167- sdb .SetBalance (eoa , new (uint256.Int ).SetAllOne ())
168185 sdb .SetTxContext (tx .Hash (), i )
186+ sdb .SetBalance (eoa , new (uint256.Int ).SetAllOne ())
187+ sdb .SetNonce (eoa , tx .Nonce ())
169188
170189 msg , err := core .TransactionToMessage (tx , signer , header .BaseFee )
171190 require .NoError (t , err , "core.TransactionToMessage(...)" )
0 commit comments