Skip to content

Commit

Permalink
convert ZeroWei to a func to avoid shared pointer (#14043)
Browse files Browse the repository at this point in the history
Co-authored-by: Kasey Kirkham <kasey@users.noreply.github.com>
  • Loading branch information
kasey and kasey authored May 23, 2024
1 parent 2e84208 commit b04baa9
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func TestConstructGenericBeaconBlock(t *testing.T) {

// Test when sBlk or sBlk.Block() is nil
t.Run("NilBlock", func(t *testing.T) {
_, err := vs.constructGenericBeaconBlock(nil, nil, primitives.ZeroWei)
_, err := vs.constructGenericBeaconBlock(nil, nil, primitives.ZeroWei())
require.ErrorContains(t, "block cannot be nil", err)
})

Expand All @@ -39,7 +39,7 @@ func TestConstructGenericBeaconBlock(t *testing.T) {
require.NoError(t, err)
r1, err := eb.Block.HashTreeRoot()
require.NoError(t, err)
result, err := vs.constructGenericBeaconBlock(b, nil, primitives.ZeroWei)
result, err := vs.constructGenericBeaconBlock(b, nil, primitives.ZeroWei())
require.NoError(t, err)
r2, err := result.GetElectra().Block.HashTreeRoot()
require.NoError(t, err)
Expand Down Expand Up @@ -71,7 +71,7 @@ func TestConstructGenericBeaconBlock(t *testing.T) {
contents := &eth.BeaconBlockContentsDeneb{Block: eb.Block, KzgProofs: bundle.Proofs, Blobs: bundle.Blobs}
r1, err := contents.HashTreeRoot()
require.NoError(t, err)
result, err := vs.constructGenericBeaconBlock(b, bundle, primitives.ZeroWei)
result, err := vs.constructGenericBeaconBlock(b, bundle, primitives.ZeroWei())
require.NoError(t, err)
r2, err := result.GetDeneb().HashTreeRoot()
require.NoError(t, err)
Expand All @@ -86,7 +86,7 @@ func TestConstructGenericBeaconBlock(t *testing.T) {
r1, err := b.Block().HashTreeRoot()
require.NoError(t, err)
scs := &enginev1.BlobsBundle{}
result, err := vs.constructGenericBeaconBlock(b, scs, primitives.ZeroWei)
result, err := vs.constructGenericBeaconBlock(b, scs, primitives.ZeroWei())
require.NoError(t, err)
r2, err := result.GetBlindedDeneb().HashTreeRoot()
require.NoError(t, err)
Expand All @@ -99,7 +99,7 @@ func TestConstructGenericBeaconBlock(t *testing.T) {
t.Run("capella block", func(t *testing.T) {
b, err := blocks.NewSignedBeaconBlock(util.NewBeaconBlockCapella())
require.NoError(t, err)
result, err := vs.constructGenericBeaconBlock(b, nil, primitives.ZeroWei)
result, err := vs.constructGenericBeaconBlock(b, nil, primitives.ZeroWei())
require.NoError(t, err)
r1, err := result.GetCapella().HashTreeRoot()
require.NoError(t, err)
Expand All @@ -113,7 +113,7 @@ func TestConstructGenericBeaconBlock(t *testing.T) {
t.Run("blind capella block", func(t *testing.T) {
b, err := blocks.NewSignedBeaconBlock(util.NewBlindedBeaconBlockCapella())
require.NoError(t, err)
result, err := vs.constructGenericBeaconBlock(b, nil, primitives.ZeroWei)
result, err := vs.constructGenericBeaconBlock(b, nil, primitives.ZeroWei())
require.NoError(t, err)
r1, err := result.GetBlindedCapella().HashTreeRoot()
require.NoError(t, err)
Expand All @@ -127,7 +127,7 @@ func TestConstructGenericBeaconBlock(t *testing.T) {
t.Run("bellatrix block", func(t *testing.T) {
b, err := blocks.NewSignedBeaconBlock(util.NewBeaconBlockBellatrix())
require.NoError(t, err)
result, err := vs.constructGenericBeaconBlock(b, nil, primitives.ZeroWei)
result, err := vs.constructGenericBeaconBlock(b, nil, primitives.ZeroWei())
require.NoError(t, err)
r1, err := result.GetBellatrix().HashTreeRoot()
require.NoError(t, err)
Expand All @@ -141,7 +141,7 @@ func TestConstructGenericBeaconBlock(t *testing.T) {
t.Run("altair block", func(t *testing.T) {
b, err := blocks.NewSignedBeaconBlock(util.NewBeaconBlockAltair())
require.NoError(t, err)
result, err := vs.constructGenericBeaconBlock(b, nil, primitives.ZeroWei)
result, err := vs.constructGenericBeaconBlock(b, nil, primitives.ZeroWei())
require.NoError(t, err)
r1, err := result.GetAltair().HashTreeRoot()
require.NoError(t, err)
Expand All @@ -155,7 +155,7 @@ func TestConstructGenericBeaconBlock(t *testing.T) {
t.Run("phase0 block", func(t *testing.T) {
b, err := blocks.NewSignedBeaconBlock(util.NewBeaconBlock())
require.NoError(t, err)
result, err := vs.constructGenericBeaconBlock(b, nil, primitives.ZeroWei)
result, err := vs.constructGenericBeaconBlock(b, nil, primitives.ZeroWei())
require.NoError(t, err)
r1, err := result.GetPhase0().HashTreeRoot()
require.NoError(t, err)
Expand Down
2 changes: 1 addition & 1 deletion beacon-chain/rpc/prysm/v1alpha1/validator/proposer.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ func (vs *Server) BuildBlockParallel(ctx context.Context, sBlk interfaces.Signed
vs.setBlsToExecData(sBlk, head)
}()

winningBid := primitives.ZeroWei
winningBid := primitives.ZeroWei()
var bundle *enginev1.BlobsBundle
if sBlk.Version() >= version.Bellatrix {
local, err := vs.getLocalPayload(ctx, sBlk.Block(), head)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@ func setExecutionData(ctx context.Context, blk interfaces.SignedBeaconBlock, loc

slot := blk.Block().Slot()
if slots.ToEpoch(slot) < params.BeaconConfig().BellatrixForkEpoch {
return primitives.ZeroWei, nil, nil
return primitives.ZeroWei(), nil, nil
}

if local == nil {
return primitives.ZeroWei, nil, errors.New("local payload is nil")
return primitives.ZeroWei(), nil, errors.New("local payload is nil")
}

// Use local payload if builder payload is nil.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,7 @@ func TestServer_setExecutionData(t *testing.T) {
GetPayloadResponse: &blocks.GetPayloadResponse{
ExecutionData: ed,
BlobsBundle: blobsBundle,
Bid: primitives.ZeroWei,
Bid: primitives.ZeroWei(),
},
}
blk.SetSlot(primitives.Slot(params.BeaconConfig().DenebForkEpoch) * params.BeaconConfig().SlotsPerEpoch)
Expand Down
2 changes: 1 addition & 1 deletion consensus-types/blocks/get_payload.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func NewGetPayloadResponse(msg proto.Message) (*GetPayloadResponse, error) {
r.BlobsBundle = bundleGetter.GetBlobsBundle()
}
bidValueGetter, hasBid := msg.(bidValueGetter)
wei := primitives.ZeroWei
wei := primitives.ZeroWei()
if hasBid {
// The protobuf types that engine api responses unmarshal into store their values in little endian form.
// This is done for consistency with other uint256 values stored in protobufs for SSZ values.
Expand Down
6 changes: 4 additions & 2 deletions consensus-types/primitives/wei.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ import (
fssz "github.com/prysmaticlabs/fastssz"
)

// ZeroWei is a non-nil zero value for primitives.Wei
var ZeroWei Wei = big.NewInt(0)
// ZW returns a non-nil zero value for primitives.Wei
func ZeroWei() Wei {
return big.NewInt(0)
}

// Wei is the smallest unit of Ether, represented as a pointer to a bigInt.
type Wei *big.Int
Expand Down
4 changes: 2 additions & 2 deletions consensus-types/primitives/wei_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

func TestWeiStringer(t *testing.T) {
require.Equal(t, "0", primitives.WeiToBigInt(primitives.ZeroWei).String())
require.Equal(t, "0", primitives.WeiToBigInt(primitives.ZeroWei()).String())
require.Equal(t, "1234", primitives.WeiToBigInt(primitives.Uint64ToWei(1234)).String())
require.Equal(t, "18446744073709551615", primitives.WeiToBigInt(primitives.Uint64ToWei(math.MaxUint64)).String())
}
Expand Down Expand Up @@ -41,6 +41,6 @@ func TestWeiToGwei_CopyOk(t *testing.T) {
}

func TestZero(t *testing.T) {
z := primitives.ZeroWei
z := primitives.ZeroWei()
require.Equal(t, 0, big.NewInt(0).Cmp(z))
}

0 comments on commit b04baa9

Please sign in to comment.