Skip to content

Commit

Permalink
wip: fixing golangci
Browse files Browse the repository at this point in the history
  • Loading branch information
abi87 committed Sep 12, 2024
1 parent 017b147 commit e30d141
Show file tree
Hide file tree
Showing 15 changed files with 259 additions and 130 deletions.
9 changes: 7 additions & 2 deletions mod/consensus/pkg/miniavalanche/block/beacon_components.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ var (
errNilBlock = errors.New("nil abci request")
)

func (b *StatelessBlock) GetBeaconBlock(forkVersion uint32) (miniavalanche.BeaconBlockT, error) {
func (b *StatelessBlock) GetBeaconBlock(
forkVersion uint32,
) (miniavalanche.BeaconBlockT, error) {
var blk miniavalanche.BeaconBlockT
if b == nil {
return blk, errNilBlock
Expand All @@ -47,7 +49,10 @@ func (b *StatelessBlock) GetBeaconBlock(forkVersion uint32) (miniavalanche.Beaco
return blk.NewFromSSZ(b.BlkContent.BeaconBlockByte, forkVersion)
}

func (b *StatelessBlock) GetBlobSidecars() (miniavalanche.BlobSidecarsT, error) {
func (b *StatelessBlock) GetBlobSidecars() (
miniavalanche.BlobSidecarsT,
error,
) {
var sidecars miniavalanche.BlobSidecarsT
if b == nil {
return sidecars, errNilBlock
Expand Down
7 changes: 3 additions & 4 deletions mod/consensus/pkg/miniavalanche/block/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,21 @@ import (

"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/utils/hashing"

"github.com/berachain/beacon-kit/mod/consensus/pkg/miniavalanche/encoding"
)

type StatelessBlock struct {
ParentID ids.ID
BlkHeight uint64
ChainTime time.Time
BlkContent BlockContent
BlkContent Content

// in memory attributes, unexported since they won't be serialized
bytes []byte
blkID ids.ID
}

type BlockContent struct {
type Content struct {
GenesisContent []byte
BeaconBlockByte []byte
BlobsBytes []byte
Expand All @@ -51,7 +50,7 @@ func NewStatelessBlock(
parentID ids.ID,
blkHeight uint64,
chainTime time.Time,
blkContent BlockContent,
blkContent Content,
) (*StatelessBlock, error) {
b := &StatelessBlock{
ParentID: parentID,
Expand Down
62 changes: 44 additions & 18 deletions mod/consensus/pkg/miniavalanche/middleware/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,12 @@ import (
/* InitGenesis */
/* -------------------------------------------------------------------------- */

// vm.ValidatorManager should be filled with the content in returned ValidatorUpdates
func (vm *VMMiddleware) InitGenesis(ctx context.Context, bz []byte) (transition.ValidatorUpdates, error) {
// vm.ValidatorManager should be filled with
// the content in returned ValidatorUpdates.
func (vm *VMMiddleware) InitGenesis(
ctx context.Context,
bz []byte,
) (transition.ValidatorUpdates, error) {
waitCtx, cancel := context.WithTimeout(ctx, AwaitTimeout)
defer cancel()

Expand All @@ -57,7 +61,9 @@ func (vm *VMMiddleware) InitGenesis(ctx context.Context, bz []byte) (transition.

// waitForGenesisProcessed waits until the genesis data has been processed and
// returns the validator updates, or err if the context is cancelled.
func (vm *VMMiddleware) waitForGenesisProcessed(ctx context.Context) (transition.ValidatorUpdates, error) {
func (vm *VMMiddleware) waitForGenesisProcessed(
ctx context.Context,
) (transition.ValidatorUpdates, error) {
select {
case <-ctx.Done():
return nil, ErrInitGenesisTimeout(ctx.Err())
Expand All @@ -71,7 +77,10 @@ func (vm *VMMiddleware) waitForGenesisProcessed(ctx context.Context) (transition
/* -------------------------------------------------------------------------- */

// BuildBlock is the internal handler for preparing proposals.
func (vm *VMMiddleware) BuildBlock(ctx context.Context, slotData *miniavalanche.SlotDataT) ([]byte, []byte, error) {
func (vm *VMMiddleware) BuildBlock(
ctx context.Context,
slotData *miniavalanche.SlotDataT,
) ([]byte, []byte, error) {
awaitCtx, cancel := context.WithTimeout(ctx, AwaitTimeout)
defer cancel()

Expand Down Expand Up @@ -111,28 +120,35 @@ func (vm *VMMiddleware) BuildBlock(ctx context.Context, slotData *miniavalanche.
}

// waitForBuiltBeaconBlock waits for the built beacon block to be received.
func (vm *VMMiddleware) waitForBuiltBeaconBlock(ctx context.Context) (miniavalanche.BeaconBlockT, error) {
func (vm *VMMiddleware) waitForBuiltBeaconBlock(
ctx context.Context,
) (miniavalanche.BeaconBlockT, error) {
select {
case <-ctx.Done():
return *new(miniavalanche.BeaconBlockT), ErrBuildBeaconBlockTimeout(ctx.Err())
return miniavalanche.BeaconBlockT(nil), ErrBuildBeaconBlockTimeout(ctx.Err())
case bbEvent := <-vm.subBuiltBeaconBlock:
return bbEvent.Data(), bbEvent.Error()
}
}

// waitForBuiltSidecars waits for the built sidecars to be received.
func (vm *VMMiddleware) waitForBuiltSidecars(ctx context.Context) (miniavalanche.BlobSidecarsT, error) {
func (vm *VMMiddleware) waitForBuiltSidecars(
ctx context.Context,
) (miniavalanche.BlobSidecarsT, error) {
select {
case <-ctx.Done():
return *new(miniavalanche.BlobSidecarsT), ErrBuildSidecarsTimeout(ctx.Err())
return miniavalanche.BlobSidecarsT(nil), ErrBuildSidecarsTimeout(ctx.Err())
case scEvent := <-vm.subBuiltSidecars:
return scEvent.Data(), scEvent.Error()
}
}

// handleBuiltBeaconBlockAndSidecars gossips the built beacon block and blob
// sidecars to the network.
func (vm *VMMiddleware) handleBuiltBeaconBlockAndSidecars(bb miniavalanche.BeaconBlockT, sc miniavalanche.BlobSidecarsT) ([]byte, []byte, error) {
func (vm *VMMiddleware) handleBuiltBeaconBlockAndSidecars(
bb miniavalanche.BeaconBlockT,
sc miniavalanche.BlobSidecarsT,
) ([]byte, []byte, error) {
bbBz, bbErr := bb.MarshalSSZ()
if bbErr != nil {
return nil, nil, bbErr
Expand All @@ -150,8 +166,11 @@ func (vm *VMMiddleware) handleBuiltBeaconBlockAndSidecars(bb miniavalanche.Beaco

// VerifyBlock processes the proposal for the ABCI middleware.
// It handles both the beacon block and blob sidecars concurrently.
// Returns error if block does not verify, nil otherwise
func (vm *VMMiddleware) VerifyBlock(ctx context.Context, outerBlk *block.StatelessBlock) error {
// Returns error if block does not verify, nil otherwise.
func (vm *VMMiddleware) VerifyBlock(
ctx context.Context,
outerBlk *block.StatelessBlock,
) error {
awaitCtx, cancel := context.WithTimeout(ctx, AwaitTimeout)
defer cancel()

Expand Down Expand Up @@ -205,20 +224,24 @@ func (vm *VMMiddleware) VerifyBlock(ctx context.Context, outerBlk *block.Statele

// waitForBeaconBlockVerification waits for the built beacon block to be
// verified.
func (vm *VMMiddleware) waitForBeaconBlockVerification(ctx context.Context) (miniavalanche.BeaconBlockT, error) {
func (vm *VMMiddleware) waitForBeaconBlockVerification(
ctx context.Context,
) (miniavalanche.BeaconBlockT, error) {
select {
case <-ctx.Done():
return *new(miniavalanche.BeaconBlockT), ErrVerifyBeaconBlockTimeout(ctx.Err())
return miniavalanche.BeaconBlockT(nil), ErrVerifyBeaconBlockTimeout(ctx.Err())
case vEvent := <-vm.subBBVerified:
return vEvent.Data(), vEvent.Error()
}
}

// waitForSidecarVerification waits for the built sidecars to be verified.
func (vm *VMMiddleware) waitForSidecarVerification(ctx context.Context) (miniavalanche.BlobSidecarsT, error) {
func (vm *VMMiddleware) waitForSidecarVerification(
ctx context.Context,
) (miniavalanche.BlobSidecarsT, error) {
select {
case <-ctx.Done():
return *new(miniavalanche.BlobSidecarsT), ErrVerifySidecarsTimeout(ctx.Err())
return miniavalanche.BlobSidecarsT(nil), ErrVerifySidecarsTimeout(ctx.Err())
case vEvent := <-vm.subSCVerified:
return vEvent.Data(), vEvent.Error()
}
Expand All @@ -229,12 +252,15 @@ func (vm *VMMiddleware) waitForSidecarVerification(ctx context.Context) (miniava
/* -------------------------------------------------------------------------- */

// AcceptBlock returns the validator set updates from the beacon state.
func (vm *VMMiddleware) AcceptBlock(ctx context.Context, outerBlk *block.StatelessBlock) (transition.ValidatorUpdates, error) {
func (vm *VMMiddleware) AcceptBlock(
ctx context.Context,
outerBlk *block.StatelessBlock,
) (transition.ValidatorUpdates, error) {
awaitCtx, cancel := context.WithTimeout(ctx, AwaitTimeout)
defer cancel()

// flush the channel to ensure that we are not handling old data.
if numMsgs := async.ClearChan(vm.subFinalValidatorUpdates); numMsgs > 0 {
if numMsgs := async.ClearChan(vm.subValidatorUpdates); numMsgs > 0 {
vm.logger.Error(
"WARNING: messages remaining in final validator updates channel",
"num_msgs", numMsgs)
Expand Down Expand Up @@ -278,7 +304,7 @@ func (vm *VMMiddleware) waitForFinalValidatorUpdates(
select {
case <-ctx.Done():
return nil, ErrFinalValidatorUpdatesTimeout(ctx.Err())
case event := <-vm.subFinalValidatorUpdates:
case event := <-vm.subValidatorUpdates:
return event.Data(), event.Error()
}
}
34 changes: 17 additions & 17 deletions mod/consensus/pkg/miniavalanche/middleware/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
"time"

"github.com/berachain/beacon-kit/mod/async/pkg/types"
"github.com/berachain/beacon-kit/mod/consensus/pkg/miniavalanche"
mava "github.com/berachain/beacon-kit/mod/consensus/pkg/miniavalanche"
"github.com/berachain/beacon-kit/mod/log"
"github.com/berachain/beacon-kit/mod/primitives/pkg/async"
)
Expand All @@ -39,33 +39,33 @@ type VMMiddleware struct {
// logger is the logger for the middleware.
logger log.Logger
// subGenDataProcessed is the channel to hold GenesisDataProcessed events.
subGenDataProcessed chan async.Event[miniavalanche.ValidatorUpdates]
subGenDataProcessed chan async.Event[mava.ValidatorUpdates]
// subBuiltBeaconBlock is the channel to hold BuiltBeaconBlock events.
subBuiltBeaconBlock chan async.Event[miniavalanche.BeaconBlockT]
subBuiltBeaconBlock chan async.Event[mava.BeaconBlockT]
// subBuiltSidecars is the channel to hold BuiltSidecars events.
subBuiltSidecars chan async.Event[miniavalanche.BlobSidecarsT]
subBuiltSidecars chan async.Event[mava.BlobSidecarsT]
// subBBVerified is the channel to hold BeaconBlockVerified events.
subBBVerified chan async.Event[miniavalanche.BeaconBlockT]
subBBVerified chan async.Event[mava.BeaconBlockT]
// subSCVerified is the channel to hold SidecarsVerified events.
subSCVerified chan async.Event[miniavalanche.BlobSidecarsT]
// subFinalValidatorUpdates is the channel to hold
subSCVerified chan async.Event[mava.BlobSidecarsT]
// subValidatorUpdates is the channel to hold
// FinalValidatorUpdatesProcessed events.
subFinalValidatorUpdates chan async.Event[miniavalanche.ValidatorUpdates]
subValidatorUpdates chan async.Event[mava.ValidatorUpdates]
}

func NewABCIMiddleware(
dispatcher types.EventDispatcher,
logger log.Logger,
) *VMMiddleware {
return &VMMiddleware{
dispatcher: dispatcher,
logger: logger,
subGenDataProcessed: make(chan async.Event[miniavalanche.ValidatorUpdates]),
subBuiltBeaconBlock: make(chan async.Event[miniavalanche.BeaconBlockT]),
subBuiltSidecars: make(chan async.Event[miniavalanche.BlobSidecarsT]),
subBBVerified: make(chan async.Event[miniavalanche.BeaconBlockT]),
subSCVerified: make(chan async.Event[miniavalanche.BlobSidecarsT]),
subFinalValidatorUpdates: make(chan async.Event[miniavalanche.ValidatorUpdates]),
dispatcher: dispatcher,
logger: logger,
subGenDataProcessed: make(chan async.Event[mava.ValidatorUpdates]),
subBuiltBeaconBlock: make(chan async.Event[mava.BeaconBlockT]),
subBuiltSidecars: make(chan async.Event[mava.BlobSidecarsT]),
subBBVerified: make(chan async.Event[mava.BeaconBlockT]),
subSCVerified: make(chan async.Event[mava.BlobSidecarsT]),
subValidatorUpdates: make(chan async.Event[mava.ValidatorUpdates]),
}
}

Expand Down Expand Up @@ -98,7 +98,7 @@ func (vm *VMMiddleware) Start(_ context.Context) error {
return err
}
if err = vm.dispatcher.Subscribe(
async.FinalValidatorUpdatesProcessed, vm.subFinalValidatorUpdates,
async.FinalValidatorUpdatesProcessed, vm.subValidatorUpdates,
); err != nil {
return err
}
Expand Down
3 changes: 1 addition & 2 deletions mod/consensus/pkg/miniavalanche/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,9 @@ package miniavalanche

import (
"github.com/berachain/beacon-kit/mod/consensus-types/pkg/types"
"github.com/berachain/beacon-kit/mod/primitives/pkg/transition"

consruntimetypes "github.com/berachain/beacon-kit/mod/consensus/pkg/types"
datypes "github.com/berachain/beacon-kit/mod/da/pkg/types"
"github.com/berachain/beacon-kit/mod/primitives/pkg/transition"
)

type (
Expand Down
3 changes: 1 addition & 2 deletions mod/consensus/pkg/miniavalanche/vm/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,8 @@ import (
"context"

"github.com/ava-labs/avalanchego/snow/consensus/snowman"
"go.uber.org/zap"

"github.com/berachain/beacon-kit/mod/consensus/pkg/miniavalanche/block"
"go.uber.org/zap"
)

var _ snowman.Block = (*StatefulBlock)(nil)
Expand Down
18 changes: 12 additions & 6 deletions mod/consensus/pkg/miniavalanche/vm/block_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,14 @@ import (
"time"

"github.com/ava-labs/avalanchego/snow/engine/common"
"go.uber.org/zap"

"github.com/berachain/beacon-kit/mod/consensus/pkg/miniavalanche"
"github.com/berachain/beacon-kit/mod/consensus/pkg/miniavalanche/block"
"github.com/berachain/beacon-kit/mod/primitives/pkg/math"
"go.uber.org/zap"
)

// how often VM should ping consensus to try and build blocks.
// ProposerVM is active, hence it will eventually decide when BuildBlock is really called
// (note: ProposerVM is active).
const pingInterval = time.Second

type blockBuilder struct {
Expand All @@ -60,12 +59,19 @@ func (bb *blockBuilder) Shutdown() {
close(bb.shutdown)
}

func (bb *blockBuilder) BuildBlock(ctx context.Context) (*StatefulBlock, error) {
func (bb *blockBuilder) BuildBlock(ctx context.Context) (
*StatefulBlock,
error,
) {
// STEP 1: retrieve parent block data
parentBlkID := bb.vm.preferredBlkID
parentBlk, err := bb.vm.getBlock(parentBlkID)
if err != nil {
return nil, fmt.Errorf("failed retrieving preferred block, ID: %v: %w", bb.vm.preferredBlkID, err)
return nil, fmt.Errorf(
"failed retrieving preferred block, ID: %v: %w",
bb.vm.preferredBlkID,
err,
)
}

// STEP 2: generate block content
Expand All @@ -84,7 +90,7 @@ func (bb *blockBuilder) BuildBlock(ctx context.Context) (*StatefulBlock, error)
parentBlkID,
childBlkHeight,
childChainTime,
block.BlockContent{
block.Content{
BeaconBlockByte: blkBytes,
BlobsBytes: blobBytes,
},
Expand Down
3 changes: 1 addition & 2 deletions mod/consensus/pkg/miniavalanche/vm/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"github.com/ava-labs/avalanchego/utils/logging"
"github.com/ava-labs/avalanchego/version"
"github.com/ava-labs/avalanchego/vms"

"github.com/berachain/beacon-kit/mod/consensus/pkg/miniavalanche/middleware"
)

Expand All @@ -38,7 +37,7 @@ var (
}
)

// Entry point for node to build the VM
// Entry point for node to build the VM.
type Factory struct {
Config Config
Middleware *middleware.VMMiddleware
Expand Down
Loading

0 comments on commit e30d141

Please sign in to comment.