From 1301bf6f44af2aa9961fcf1512353efa9c31a12a Mon Sep 17 00:00:00 2001 From: Cal Bera Date: Fri, 15 Aug 2025 09:53:29 -0700 Subject: [PATCH] BRG4-15: error out early if nil pubkey provided when creating PoL tx --- core/types/tx_pol.go | 8 +++----- core/types/tx_pol_test.go | 21 +++++++++++++++++++++ 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/core/types/tx_pol.go b/core/types/tx_pol.go index 1faf17acad..839f5d40a5 100644 --- a/core/types/tx_pol.go +++ b/core/types/tx_pol.go @@ -18,6 +18,7 @@ package types import ( "bytes" + "errors" "math/big" "github.com/ethereum/go-ethereum/accounts/abi" @@ -145,14 +146,11 @@ var ( // getDistributeForData returns the tx data for the `distributeFor(bytes pubkey)` method. func getDistributeForData(pubkey *common.Pubkey) ([]byte, error) { - var pubkeyBytes []byte if pubkey == nil { - pubkeyBytes = common.Pubkey{}.Bytes() - } else { - pubkeyBytes = pubkey.Bytes() + return nil, errors.New("pubkey cannot be nil for PoL transaction") } - arguments, err := distributeForMethod.Inputs.Pack(pubkeyBytes) + arguments, err := distributeForMethod.Inputs.Pack(pubkey.Bytes()) if err != nil { return nil, err } diff --git a/core/types/tx_pol_test.go b/core/types/tx_pol_test.go index eb2ea4188d..fe70ad3966 100644 --- a/core/types/tx_pol_test.go +++ b/core/types/tx_pol_test.go @@ -65,6 +65,27 @@ func TestNewPoLTx_DataPacking(t *testing.T) { } } +// TestNewPoLTx_NilPubkey verifies that NewPoLTx returns an error when pubkey is nil. +func TestNewPoLTx_NilPubkey(t *testing.T) { + chainID := big.NewInt(1) + distributor := common.HexToAddress("0x000000000000000000000000000000000000dEaD") + blockNum := big.NewInt(123) + baseFee := big.NewInt(1000000000) + + // Call NewPoLTx with nil pubkey + tx, err := NewPoLTx(chainID, distributor, blockNum, params.PoLTxGasLimit, baseFee, nil) + if err == nil { + t.Fatalf("expected error for nil pubkey, but got nil") + } + if tx != nil { + t.Fatalf("expected nil transaction when error occurs, but got %v", tx) + } + expectedErr := "pubkey cannot be nil for PoL transaction" + if err.Error() != expectedErr { + t.Fatalf("error message mismatch: have %q, want %q", err.Error(), expectedErr) + } +} + // TestNewPoLTx_NegativeBlockNumber ensures negative block numbers are handled // without panicking (uint64 wrap-around is expected). func TestNewPoLTx_NegativeBlockNumber(t *testing.T) {