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:
parent
28d076d37e
commit
a007ab786c
|
@ -190,7 +190,7 @@ func (s londonSigner) Sender(tx *Transaction) (common.Address, error) {
|
|||
// id, add 27 to become equivalent to unprotected Homestead signatures.
|
||||
V = new(big.Int).Add(V, big.NewInt(27))
|
||||
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)
|
||||
}
|
||||
|
@ -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,
|
||||
// because it indicates that the chain ID was not specified in the tx.
|
||||
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)
|
||||
V = big.NewInt(int64(sig[64]))
|
||||
|
@ -270,7 +270,7 @@ func (s eip2930Signer) Sender(tx *Transaction) (common.Address, error) {
|
|||
return common.Address{}, ErrTxTypeNotSupported
|
||||
}
|
||||
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)
|
||||
}
|
||||
|
@ -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,
|
||||
// because it indicates that the chain ID was not specified in the tx.
|
||||
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)
|
||||
V = big.NewInt(int64(sig[64]))
|
||||
|
@ -364,7 +364,7 @@ func (s EIP155Signer) Sender(tx *Transaction) (common.Address, error) {
|
|||
return HomesteadSigner{}.Sender(tx)
|
||||
}
|
||||
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 = new(big.Int).Sub(V, s.chainIdMul)
|
||||
|
|
|
@ -17,6 +17,7 @@
|
|||
package types
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"math/big"
|
||||
"testing"
|
||||
|
||||
|
@ -126,8 +127,8 @@ func TestChainId(t *testing.T) {
|
|||
}
|
||||
|
||||
_, err = Sender(NewEIP155Signer(big.NewInt(2)), tx)
|
||||
if err != ErrInvalidChainId {
|
||||
t.Error("expected error:", ErrInvalidChainId)
|
||||
if !errors.Is(err, ErrInvalidChainId) {
|
||||
t.Error("expected error:", ErrInvalidChainId, err)
|
||||
}
|
||||
|
||||
_, err = Sender(NewEIP155Signer(big.NewInt(1)), tx)
|
||||
|
|
|
@ -20,6 +20,7 @@ import (
|
|||
"bytes"
|
||||
"crypto/ecdsa"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"math/big"
|
||||
"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)
|
||||
}
|
||||
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)
|
||||
}
|
||||
if err == nil && sender != keyAddr {
|
||||
t.Errorf("test %d: wrong sender address %x", i, sender)
|
||||
}
|
||||
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)
|
||||
}
|
||||
if signedTx != nil {
|
||||
|
|
Loading…
Reference in New Issue