Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 deletions adapters.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,15 +211,24 @@ func (n *NoopAuxiliaryInfoApp) DefaultVersionID() common.VersionID {
return 0
}

type BlockBuilderWaiter struct {
type blockBuilderWaiter struct {

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i dont think this should be exported from this package

lock sync.Mutex
cancel context.CancelFunc
msm *metadata.StateMachine
cs *CachedStorage
e *simplex.Epoch
vm VM
}

func (bw *BlockBuilderWaiter) stop() {
func newBlockBuilderWaiter(msm *metadata.StateMachine, cs *CachedStorage, vm VM) *blockBuilderWaiter {
return &blockBuilderWaiter{
msm: msm,
cs: cs,
vm: vm,
}
}

func (bw *blockBuilderWaiter) stop() {
bw.lock.Lock()
defer bw.lock.Unlock()
if bw.cancel != nil {
Expand All @@ -228,7 +237,7 @@ func (bw *BlockBuilderWaiter) stop() {
}
}

func (bw *BlockBuilderWaiter) WaitForPendingBlock(ctx context.Context) {
func (bw *blockBuilderWaiter) WaitForPendingBlock(ctx context.Context) {
bw.lock.Lock()
if bw.cancel != nil {
bw.cancel()
Expand All @@ -242,18 +251,21 @@ func (bw *BlockBuilderWaiter) WaitForPendingBlock(ctx context.Context) {
bw.msm.WaitForPendingBlock(ctx, md)
}

func (bw *BlockBuilderWaiter) BuildBlock(ctx context.Context, metadata common.ProtocolMetadata, blacklist common.Blacklist) (common.VerifiedBlock, bool) {
func (bw *blockBuilderWaiter) BuildBlock(ctx context.Context, metadata common.ProtocolMetadata, blacklist common.Blacklist) (common.VerifiedBlock, bool) {
block, err := bw.msm.BuildBlock(ctx, metadata, blacklist)
if err != nil {
return nil, false
}

pb := ParsedBlock{
pb := &ParsedBlock{
StateMachineBlock: *block,
msm: bw.msm,
}

return &pb, true
// Ensure the builders block is in the cache after verification
bw.cs.insertBlock(pb)

return pb, true
}

type blockDeserializer struct {
Expand Down
34 changes: 34 additions & 0 deletions adapters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,21 +181,21 @@
Logger: logger,
ID: nodeIDs[0],
VM: vm,
Storage: storage,

Check failure on line 184 in adapters_test.go

View workflow job for this annotation

GitHub Actions / Lint

undefined: newStorageWithGenesis

Check failure on line 184 in adapters_test.go

View workflow job for this annotation

GitHub Actions / build

undefined: newStorageWithGenesis
Sender: comm,
Broadcaster: comm,
PlatformChain: pChain,
CryptoOps: cops,
LastNonSimplexInnerBlock: genesisBlock,
WalCreator: storage.CreateWAL,
ParameterConfig: ParameterConfig{

Check failure on line 191 in adapters_test.go

View workflow job for this annotation

GitHub Actions / Lint

undefined: avalanchego

Check failure on line 191 in adapters_test.go

View workflow job for this annotation

GitHub Actions / build

undefined: avalanchego
MaxNetworkDelay: 500 * time.Millisecond,
MaxRoundWindow: 100,
WALMaxSizeBytes: 1024,
},
WALs: []wal.DeletableWAL{testWAL},
}
instance := NewInstance(config)

Check failure on line 198 in adapters_test.go

View workflow job for this annotation

GitHub Actions / Lint

undefined: newTestVM (typecheck)

Check failure on line 198 in adapters_test.go

View workflow job for this annotation

GitHub Actions / build

undefined: newTestVM
require.NoError(t, instance.Start(t.Context()))
t.Cleanup(instance.Stop)

Expand All @@ -209,3 +209,37 @@
return got.BlockHeader().Digest == block.BlockHeader().Digest
}, 20*time.Second, 100*time.Millisecond)
}

// TestCachedStoragePopulatedBySelfBuiltBlock asserts that a block a node builds for its
// own proposal is inserted into the CachedStorage, retrievable by seq and digest before
// it is finalized and indexed.
func TestCachedStoragePopulatedBySelfBuiltBlock(t *testing.T) {
genesisBlock := &testInnerBlock{Height_: 0, TS: time.Now(), Payload: []byte("genesis")}
cs := NewCachedStorage(newStorageWithGenesis(t, genesisBlock))

msm, err := metadata.NewStateMachine(&metadata.Config{
Logger: testutil.MakeLogger(t, 1),
GetBlock: cs.RetrieveBlock,
LastNonSimplexInnerBlock: genesisBlock,
GenesisValidatorSet: metadata.NodeBLSMappings{
{NodeID: avalanchego.NodeID{1}, BLSKey: []byte{1}, Weight: 1},
},
AuxiliaryInfoApp: &NoopAuxiliaryInfoApp{},
})
require.NoError(t, err)
cs.msm = msm

bw := newBlockBuilderWaiter(msm, cs, newTestVM())

// Build a block on top of genesis
genesis := &ParsedBlock{StateMachineBlock: metadata.StateMachineBlock{InnerBlock: genesisBlock}}
md := common.ProtocolMetadata{Seq: 1, Prev: genesis.BlockHeader().Digest}
vb, built := bw.BuildBlock(t.Context(), md, common.Blacklist{})
require.True(t, built)
require.Equal(t, md.Seq, vb.BlockHeader().Seq)

cached, fin, err := cs.Retrieve(md.Seq, vb.BlockHeader().Digest)
require.NoError(t, err)
require.Nil(t, fin)
require.Same(t, vb, cached)
}
4 changes: 2 additions & 2 deletions instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@ func (i *Instance) createEpochConfig(epoch uint64, validators common.Nodes) (*ep
return nil, err
}

blockBuilder := &BlockBuilderWaiter{vm: i.Config.VM, msm: msm}
blockBuilder := newBlockBuilderWaiter(msm, i.cs, i.Config.VM)

comm := newCommunication(i.Config.Sender, i.Config.Broadcaster, validators)

Expand Down Expand Up @@ -574,5 +574,5 @@ func (i *Instance) startAtEpoch(validators common.Nodes, epoch uint64) error {

type epochConfig struct {
simplex.EpochConfig
bbw *BlockBuilderWaiter
bbw *blockBuilderWaiter
}
16 changes: 14 additions & 2 deletions msm/msm.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,13 @@ func (sm *StateMachine) WaitForPendingBlock(ctx context.Context, currentRoundMet

parentBlock, finalization, err := sm.GetBlock(prevBlockSeq, currentRoundMetadata.Prev)
if err != nil {
sm.Logger.Debug("WaitForPendingBlock failed to get block", zap.Uint64("seq", prevBlockSeq), zap.Error(err))
sm.Logger.Debug(
"WaitForPendingBlock failed to get block",
zap.Uint64("Current Seq", currentRoundMetadata.Seq),
zap.Uint64("Current Round", currentRoundMetadata.Round),
zap.Uint64("seq", prevBlockSeq),
zap.Error(err),
)
sm.BlockBuilder.WaitForPendingBlock(ctx)
return
}
Expand Down Expand Up @@ -367,7 +373,13 @@ func (sm *StateMachine) WaitForPendingBlock(ctx context.Context, currentRoundMet
blockBuildingDecider := sm.createBlockBuildingDecider(pChainReferenceHeight)
_, err = blockBuildingDecider.shouldBuildBlock(ctx)
if err != nil {
sm.Logger.Debug("Error while deciding whether to build a block", zap.Error(err))
sm.Logger.Debug(
"Error while deciding whether to build a block",
zap.Uint64("Current Round", currentRoundMetadata.Round),
zap.Uint64("Current Seq", currentRoundMetadata.Seq),
zap.Stringer("Prev Digest", currentRoundMetadata.Prev),
zap.Error(err),
)
return
}
}
Expand Down
Loading