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.
This commit is contained in:
Marius van der Wijden 2022-10-12 10:27:39 +02:00 committed by GitHub
parent 28d076d37e
commit a007ab786c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 11 additions and 9 deletions

View File

@ -190,7 +190,7 @@ func (s londonSigner) Sender(tx *Transaction) (common.Address, error) {
// id, add 27 to become equivalent to unprotected Homestead signatures. // id, add 27 to become equivalent to unprotected Homestead signatures.
V = new(big.Int).Add(V, big.NewInt(27)) V = new(big.Int).Add(V, big.NewInt(27))
if tx.ChainId().Cmp(s.chainId) != 0 { if tx.ChainId().Cmp(s.chainId) != 0 {
return common.Address{}, ErrInvalidChainId return common.Address{}, fmt.Errorf("%w: have %d want %d", ErrInvalidChainId, tx.ChainId(), s.chainId)
} }
return recoverPlain(s.Hash(tx), R, S, V, true) return recoverPlain(s.Hash(tx), R, S, V, true)
} }
@ -208,7 +208,7 @@ func (s londonSigner) SignatureValues(tx *Transaction, sig []byte) (R, S, V *big
// Check that chain ID of tx matches the signer. We also accept ID zero here, // Check that chain ID of tx matches the signer. We also accept ID zero here,
// because it indicates that the chain ID was not specified in the tx. // because it indicates that the chain ID was not specified in the tx.
if txdata.ChainID.Sign() != 0 && txdata.ChainID.Cmp(s.chainId) != 0 { if txdata.ChainID.Sign() != 0 && txdata.ChainID.Cmp(s.chainId) != 0 {
return nil, nil, nil, ErrInvalidChainId return nil, nil, nil, fmt.Errorf("%w: have %d want %d", ErrInvalidChainId, txdata.ChainID, s.chainId)
} }
R, S, _ = decodeSignature(sig) R, S, _ = decodeSignature(sig)
V = big.NewInt(int64(sig[64])) V = big.NewInt(int64(sig[64]))
@ -270,7 +270,7 @@ func (s eip2930Signer) Sender(tx *Transaction) (common.Address, error) {
return common.Address{}, ErrTxTypeNotSupported return common.Address{}, ErrTxTypeNotSupported
} }
if tx.ChainId().Cmp(s.chainId) != 0 { if tx.ChainId().Cmp(s.chainId) != 0 {
return common.Address{}, ErrInvalidChainId return common.Address{}, fmt.Errorf("%w: have %d want %d", ErrInvalidChainId, tx.ChainId(), s.chainId)
} }
return recoverPlain(s.Hash(tx), R, S, V, true) return recoverPlain(s.Hash(tx), R, S, V, true)
} }
@ -283,7 +283,7 @@ func (s eip2930Signer) SignatureValues(tx *Transaction, sig []byte) (R, S, V *bi
// Check that chain ID of tx matches the signer. We also accept ID zero here, // Check that chain ID of tx matches the signer. We also accept ID zero here,
// because it indicates that the chain ID was not specified in the tx. // because it indicates that the chain ID was not specified in the tx.
if txdata.ChainID.Sign() != 0 && txdata.ChainID.Cmp(s.chainId) != 0 { if txdata.ChainID.Sign() != 0 && txdata.ChainID.Cmp(s.chainId) != 0 {
return nil, nil, nil, ErrInvalidChainId return nil, nil, nil, fmt.Errorf("%w: have %d want %d", ErrInvalidChainId, txdata.ChainID, s.chainId)
} }
R, S, _ = decodeSignature(sig) R, S, _ = decodeSignature(sig)
V = big.NewInt(int64(sig[64])) V = big.NewInt(int64(sig[64]))
@ -364,7 +364,7 @@ func (s EIP155Signer) Sender(tx *Transaction) (common.Address, error) {
return HomesteadSigner{}.Sender(tx) return HomesteadSigner{}.Sender(tx)
} }
if tx.ChainId().Cmp(s.chainId) != 0 { if tx.ChainId().Cmp(s.chainId) != 0 {
return common.Address{}, ErrInvalidChainId return common.Address{}, fmt.Errorf("%w: have %d want %d", ErrInvalidChainId, tx.ChainId(), s.chainId)
} }
V, R, S := tx.RawSignatureValues() V, R, S := tx.RawSignatureValues()
V = new(big.Int).Sub(V, s.chainIdMul) V = new(big.Int).Sub(V, s.chainIdMul)

View File

@ -17,6 +17,7 @@
package types package types
import ( import (
"errors"
"math/big" "math/big"
"testing" "testing"
@ -126,8 +127,8 @@ func TestChainId(t *testing.T) {
} }
_, err = Sender(NewEIP155Signer(big.NewInt(2)), tx) _, err = Sender(NewEIP155Signer(big.NewInt(2)), tx)
if err != ErrInvalidChainId { if !errors.Is(err, ErrInvalidChainId) {
t.Error("expected error:", ErrInvalidChainId) t.Error("expected error:", ErrInvalidChainId, err)
} }
_, err = Sender(NewEIP155Signer(big.NewInt(1)), tx) _, err = Sender(NewEIP155Signer(big.NewInt(1)), tx)

View File

@ -20,6 +20,7 @@ import (
"bytes" "bytes"
"crypto/ecdsa" "crypto/ecdsa"
"encoding/json" "encoding/json"
"errors"
"fmt" "fmt"
"math/big" "math/big"
"math/rand" "math/rand"
@ -170,14 +171,14 @@ func TestEIP2930Signer(t *testing.T) {
t.Errorf("test %d: wrong sig hash: got %x, want %x", i, sigHash, test.wantSignerHash) t.Errorf("test %d: wrong sig hash: got %x, want %x", i, sigHash, test.wantSignerHash)
} }
sender, err := Sender(test.signer, test.tx) sender, err := Sender(test.signer, test.tx)
if err != test.wantSenderErr { if !errors.Is(err, test.wantSenderErr) {
t.Errorf("test %d: wrong Sender error %q", i, err) t.Errorf("test %d: wrong Sender error %q", i, err)
} }
if err == nil && sender != keyAddr { if err == nil && sender != keyAddr {
t.Errorf("test %d: wrong sender address %x", i, sender) t.Errorf("test %d: wrong sender address %x", i, sender)
} }
signedTx, err := SignTx(test.tx, test.signer, key) signedTx, err := SignTx(test.tx, test.signer, key)
if err != test.wantSignErr { if !errors.Is(err, test.wantSignErr) {
t.Fatalf("test %d: wrong SignTx error %q", i, err) t.Fatalf("test %d: wrong SignTx error %q", i, err)
} }
if signedTx != nil { if signedTx != nil {