BRG4-15: error out early if nil pubkey provided when creating PoL tx

This commit is contained in:
Cal Bera 2025-08-15 09:53:29 -07:00
parent 9d70186ec4
commit 1301bf6f44
2 changed files with 24 additions and 5 deletions

View file

@ -18,6 +18,7 @@ package types
import ( import (
"bytes" "bytes"
"errors"
"math/big" "math/big"
"github.com/ethereum/go-ethereum/accounts/abi" "github.com/ethereum/go-ethereum/accounts/abi"
@ -145,14 +146,11 @@ var (
// getDistributeForData returns the tx data for the `distributeFor(bytes pubkey)` method. // getDistributeForData returns the tx data for the `distributeFor(bytes pubkey)` method.
func getDistributeForData(pubkey *common.Pubkey) ([]byte, error) { func getDistributeForData(pubkey *common.Pubkey) ([]byte, error) {
var pubkeyBytes []byte
if pubkey == nil { if pubkey == nil {
pubkeyBytes = common.Pubkey{}.Bytes() return nil, errors.New("pubkey cannot be nil for PoL transaction")
} else {
pubkeyBytes = pubkey.Bytes()
} }
arguments, err := distributeForMethod.Inputs.Pack(pubkeyBytes) arguments, err := distributeForMethod.Inputs.Pack(pubkey.Bytes())
if err != nil { if err != nil {
return nil, err return nil, err
} }

View file

@ -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 // TestNewPoLTx_NegativeBlockNumber ensures negative block numbers are handled
// without panicking (uint64 wrap-around is expected). // without panicking (uint64 wrap-around is expected).
func TestNewPoLTx_NegativeBlockNumber(t *testing.T) { func TestNewPoLTx_NegativeBlockNumber(t *testing.T) {