Skip to content

Commit 2a56ecc

Browse files
authored
core/types: correct chainId check for pragueSigner ethereum#31032 ethereum#31054 (#1885)
1 parent 292c050 commit 2a56ecc

File tree

3 files changed

+31
-18
lines changed

3 files changed

+31
-18
lines changed

core/state_processor_test.go

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
package core
1818

1919
import (
20+
"crypto/ecdsa"
21+
"math"
2022
"math/big"
2123
"testing"
2224

@@ -60,14 +62,11 @@ func TestStateProcessorErrors(t *testing.T) {
6062
}
6163
signer = types.LatestSigner(config)
6264
key1, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
65+
key2, _ = crypto.HexToECDSA("0202020202020202020202020202020202020202020202020202002020202020")
6366
)
64-
var makeTx = func(nonce uint64, to common.Address, amount *big.Int, gasLimit uint64, gasPrice *big.Int, data []byte) *types.Transaction {
65-
tx := types.NewTransaction(nonce, to, amount, gasLimit, gasPrice, data)
66-
signedTx, err := types.SignTx(tx, signer, key1)
67-
if err != nil {
68-
t.Fatalf("fail to sign tx: %v, err: %v", tx, err)
69-
}
70-
return signedTx
67+
var makeTx = func(key *ecdsa.PrivateKey, nonce uint64, to common.Address, amount *big.Int, gasLimit uint64, gasPrice *big.Int, data []byte) *types.Transaction {
68+
tx, _ := types.SignTx(types.NewTransaction(nonce, to, amount, gasLimit, gasPrice, data), signer, key)
69+
return tx
7170
}
7271
var mkDynamicTx = func(nonce uint64, to common.Address, gasLimit uint64, gasTipCap, gasFeeCap *big.Int) *types.Transaction {
7372
tx, _ := types.SignTx(types.NewTx(&types.DynamicFeeTx{
@@ -117,12 +116,17 @@ func TestStateProcessorErrors(t *testing.T) {
117116
Balance: big.NewInt(1000000000000000000), // 1 ether
118117
Nonce: 0,
119118
},
119+
common.HexToAddress("0xfd0810DD14796680f72adf1a371963d0745BCc64"): types.Account{
120+
Balance: big.NewInt(1000000000000000000), // 1 ether
121+
Nonce: math.MaxUint64,
122+
},
120123
},
121124
}
122125
genesis = gspec.MustCommit(db)
123126
blockchain, _ = NewBlockChain(db, nil, gspec.Config, ethash.NewFaker(), vm.Config{})
124127
tooBigInitCode = [params.MaxInitCodeSize + 1]byte{}
125128
)
129+
126130
defer blockchain.Stop()
127131
bigNumber := new(big.Int).SetBytes(common.MaxHash.Bytes())
128132
tooBigNumber := new(big.Int).Set(bigNumber)
@@ -133,32 +137,38 @@ func TestStateProcessorErrors(t *testing.T) {
133137
}{
134138
{ // ErrNonceTooLow
135139
txs: []*types.Transaction{
136-
makeTx(0, common.Address{}, big.NewInt(0), params.TxGas, big.NewInt(12500000000), nil),
137-
makeTx(0, common.Address{}, big.NewInt(0), params.TxGas, big.NewInt(12500000000), nil),
140+
makeTx(key1, 0, common.Address{}, big.NewInt(0), params.TxGas, big.NewInt(12500000000), nil),
141+
makeTx(key1, 0, common.Address{}, big.NewInt(0), params.TxGas, big.NewInt(12500000000), nil),
138142
},
139143
want: "could not apply tx 1 [0xecd6a889a307155b3562cd64c86957e36fa58267cb4efbbe39aa692fd7aab09a]: nonce too low: address xdc71562b71999873DB5b286dF957af199Ec94617F7, tx: 0 state: 1",
140144
},
141145
{ // ErrNonceTooHigh
142146
txs: []*types.Transaction{
143-
makeTx(100, common.Address{}, big.NewInt(0), params.TxGas, big.NewInt(875000000), nil),
147+
makeTx(key1, 100, common.Address{}, big.NewInt(0), params.TxGas, big.NewInt(875000000), nil),
144148
},
145149
want: "could not apply tx 0 [0xdebad714ca7f363bd0d8121c4518ad48fa469ca81b0a081be3d10c17460f751b]: nonce too high: address xdc71562b71999873DB5b286dF957af199Ec94617F7, tx: 100 state: 0",
146150
},
151+
{ // ErrNonceMax
152+
txs: []*types.Transaction{
153+
makeTx(key2, math.MaxUint64, common.Address{}, big.NewInt(0), params.TxGas, big.NewInt(875000000), nil),
154+
},
155+
want: "could not apply tx 0 [0x84ea18d60eb2bb3b040e3add0eb72f757727122cc257dd858c67cb6591a85986]: nonce has max value: address xdcfd0810DD14796680f72adf1a371963d0745BCc64, nonce: 18446744073709551615",
156+
},
147157
{ // ErrGasLimitReached
148158
txs: []*types.Transaction{
149-
makeTx(0, common.Address{}, big.NewInt(0), 21000000, big.NewInt(12500000000), nil),
159+
makeTx(key1, 0, common.Address{}, big.NewInt(0), 21000000, big.NewInt(12500000000), nil),
150160
},
151161
want: "could not apply tx 0 [0x062b0e84f2d48f09f91e434fca8cb1fb864c4fb82f8bf27d58879ebe60c9f773]: gas limit reached",
152162
},
153163
{ // ErrInsufficientFundsForTransfer
154164
txs: []*types.Transaction{
155-
makeTx(0, common.Address{}, big.NewInt(1000000000000000000), params.TxGas, big.NewInt(12500000000), nil),
165+
makeTx(key1, 0, common.Address{}, big.NewInt(1000000000000000000), params.TxGas, big.NewInt(12500000000), nil),
156166
},
157167
want: "could not apply tx 0 [0x50f89093bf5ad7f4ae6f9e3bad44d4dc130247ea0429df0cf78873584a76dfa1]: insufficient funds for gas * price + value: address xdc71562b71999873DB5b286dF957af199Ec94617F7 have 1000000000000000000 want 1000262500000000000",
158168
},
159169
{ // ErrInsufficientFunds
160170
txs: []*types.Transaction{
161-
makeTx(0, common.Address{}, big.NewInt(0), params.TxGas, big.NewInt(900000000000000000), nil),
171+
makeTx(key1, 0, common.Address{}, big.NewInt(0), params.TxGas, big.NewInt(900000000000000000), nil),
162172
},
163173
want: "could not apply tx 0 [0x4a69690c4b0cd85e64d0d9ea06302455b01e10a83db964d60281739752003440]: insufficient funds for gas * price + value: address xdc71562b71999873DB5b286dF957af199Ec94617F7 have 1000000000000000000 want 18900000000000000000000",
164174
},
@@ -168,13 +178,13 @@ func TestStateProcessorErrors(t *testing.T) {
168178
// multiplication len(data) +gas_per_byte overflows uint64. Not testable at the moment
169179
{ // ErrIntrinsicGas
170180
txs: []*types.Transaction{
171-
makeTx(0, common.Address{}, big.NewInt(0), params.TxGas-1000, big.NewInt(12500000000), nil),
181+
makeTx(key1, 0, common.Address{}, big.NewInt(0), params.TxGas-1000, big.NewInt(12500000000), nil),
172182
},
173183
want: "could not apply tx 0 [0xa3484a466ffa8a88dc95e6ff520c853659dfc5507039c0b1452c2b845438771b]: intrinsic gas too low: have 20000, want 21000",
174184
},
175185
{ // ErrGasLimitReached
176186
txs: []*types.Transaction{
177-
makeTx(0, common.Address{}, big.NewInt(0), params.TxGas*1000, big.NewInt(12500000000), nil),
187+
makeTx(key1, 0, common.Address{}, big.NewInt(0), params.TxGas*1000, big.NewInt(12500000000), nil),
178188
},
179189
want: "could not apply tx 0 [0x062b0e84f2d48f09f91e434fca8cb1fb864c4fb82f8bf27d58879ebe60c9f773]: gas limit reached",
180190
},
@@ -250,7 +260,7 @@ func TestStateProcessorErrors(t *testing.T) {
250260
}
251261
}
252262

253-
// One final error is ErrTxTypeNotSupported. For this, we need an older chain
263+
// ErrTxTypeNotSupported, For this, we need an older chain
254264
{
255265
var (
256266
db = rawdb.NewMemoryDatabase()

core/types/transaction_signing.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ func (s pragueSigner) SignatureValues(tx *Transaction, sig []byte) (R, S, V *big
214214
}
215215
// Check that chain ID of tx matches the signer. We also accept ID zero here,
216216
// because it indicates that the chain ID was not specified in the tx.
217-
if txdata.ChainID != nil && txdata.ChainID.CmpBig(s.chainId) != 0 {
217+
if txdata.ChainID.Sign() != 0 && txdata.ChainID.CmpBig(s.chainId) != 0 {
218218
return nil, nil, nil, fmt.Errorf("%w: have %d want %d", ErrInvalidChainId, txdata.ChainID, s.chainId)
219219
}
220220
R, S, _ = decodeSignature(sig)

core/types/tx_setcode.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ func (tx *SetCodeTx) copy() TxData {
148148
AccessList: make(AccessList, len(tx.AccessList)),
149149
AuthList: make([]SetCodeAuthorization, len(tx.AuthList)),
150150
Value: new(uint256.Int),
151-
ChainID: tx.ChainID,
151+
ChainID: new(uint256.Int),
152152
GasTipCap: new(uint256.Int),
153153
GasFeeCap: new(uint256.Int),
154154
V: new(uint256.Int),
@@ -160,6 +160,9 @@ func (tx *SetCodeTx) copy() TxData {
160160
if tx.Value != nil {
161161
cpy.Value.Set(tx.Value)
162162
}
163+
if tx.ChainID != nil {
164+
cpy.ChainID.Set(tx.ChainID)
165+
}
163166
if tx.GasTipCap != nil {
164167
cpy.GasTipCap.Set(tx.GasTipCap)
165168
}

0 commit comments

Comments
 (0)