Skip to content

Commit a007ab7

Browse files
core/types: add more context around ErrInvalidChainID (#25367)
This changes the error message for mismatching chain ID to show the given and expected value. Callers expecting this error must be changed to use errors.Is.
1 parent 28d076d commit a007ab7

File tree

3 files changed

+11
-9
lines changed

3 files changed

+11
-9
lines changed

core/types/transaction_signing.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ func (s londonSigner) Sender(tx *Transaction) (common.Address, error) {
190190
// id, add 27 to become equivalent to unprotected Homestead signatures.
191191
V = new(big.Int).Add(V, big.NewInt(27))
192192
if tx.ChainId().Cmp(s.chainId) != 0 {
193-
return common.Address{}, ErrInvalidChainId
193+
return common.Address{}, fmt.Errorf("%w: have %d want %d", ErrInvalidChainId, tx.ChainId(), s.chainId)
194194
}
195195
return recoverPlain(s.Hash(tx), R, S, V, true)
196196
}
@@ -208,7 +208,7 @@ func (s londonSigner) SignatureValues(tx *Transaction, sig []byte) (R, S, V *big
208208
// Check that chain ID of tx matches the signer. We also accept ID zero here,
209209
// because it indicates that the chain ID was not specified in the tx.
210210
if txdata.ChainID.Sign() != 0 && txdata.ChainID.Cmp(s.chainId) != 0 {
211-
return nil, nil, nil, ErrInvalidChainId
211+
return nil, nil, nil, fmt.Errorf("%w: have %d want %d", ErrInvalidChainId, txdata.ChainID, s.chainId)
212212
}
213213
R, S, _ = decodeSignature(sig)
214214
V = big.NewInt(int64(sig[64]))
@@ -270,7 +270,7 @@ func (s eip2930Signer) Sender(tx *Transaction) (common.Address, error) {
270270
return common.Address{}, ErrTxTypeNotSupported
271271
}
272272
if tx.ChainId().Cmp(s.chainId) != 0 {
273-
return common.Address{}, ErrInvalidChainId
273+
return common.Address{}, fmt.Errorf("%w: have %d want %d", ErrInvalidChainId, tx.ChainId(), s.chainId)
274274
}
275275
return recoverPlain(s.Hash(tx), R, S, V, true)
276276
}
@@ -283,7 +283,7 @@ func (s eip2930Signer) SignatureValues(tx *Transaction, sig []byte) (R, S, V *bi
283283
// Check that chain ID of tx matches the signer. We also accept ID zero here,
284284
// because it indicates that the chain ID was not specified in the tx.
285285
if txdata.ChainID.Sign() != 0 && txdata.ChainID.Cmp(s.chainId) != 0 {
286-
return nil, nil, nil, ErrInvalidChainId
286+
return nil, nil, nil, fmt.Errorf("%w: have %d want %d", ErrInvalidChainId, txdata.ChainID, s.chainId)
287287
}
288288
R, S, _ = decodeSignature(sig)
289289
V = big.NewInt(int64(sig[64]))
@@ -364,7 +364,7 @@ func (s EIP155Signer) Sender(tx *Transaction) (common.Address, error) {
364364
return HomesteadSigner{}.Sender(tx)
365365
}
366366
if tx.ChainId().Cmp(s.chainId) != 0 {
367-
return common.Address{}, ErrInvalidChainId
367+
return common.Address{}, fmt.Errorf("%w: have %d want %d", ErrInvalidChainId, tx.ChainId(), s.chainId)
368368
}
369369
V, R, S := tx.RawSignatureValues()
370370
V = new(big.Int).Sub(V, s.chainIdMul)

core/types/transaction_signing_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package types
1818

1919
import (
20+
"errors"
2021
"math/big"
2122
"testing"
2223

@@ -126,8 +127,8 @@ func TestChainId(t *testing.T) {
126127
}
127128

128129
_, err = Sender(NewEIP155Signer(big.NewInt(2)), tx)
129-
if err != ErrInvalidChainId {
130-
t.Error("expected error:", ErrInvalidChainId)
130+
if !errors.Is(err, ErrInvalidChainId) {
131+
t.Error("expected error:", ErrInvalidChainId, err)
131132
}
132133

133134
_, err = Sender(NewEIP155Signer(big.NewInt(1)), tx)

core/types/transaction_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"bytes"
2121
"crypto/ecdsa"
2222
"encoding/json"
23+
"errors"
2324
"fmt"
2425
"math/big"
2526
"math/rand"
@@ -170,14 +171,14 @@ func TestEIP2930Signer(t *testing.T) {
170171
t.Errorf("test %d: wrong sig hash: got %x, want %x", i, sigHash, test.wantSignerHash)
171172
}
172173
sender, err := Sender(test.signer, test.tx)
173-
if err != test.wantSenderErr {
174+
if !errors.Is(err, test.wantSenderErr) {
174175
t.Errorf("test %d: wrong Sender error %q", i, err)
175176
}
176177
if err == nil && sender != keyAddr {
177178
t.Errorf("test %d: wrong sender address %x", i, sender)
178179
}
179180
signedTx, err := SignTx(test.tx, test.signer, key)
180-
if err != test.wantSignErr {
181+
if !errors.Is(err, test.wantSignErr) {
181182
t.Fatalf("test %d: wrong SignTx error %q", i, err)
182183
}
183184
if signedTx != nil {

0 commit comments

Comments
 (0)