Skip to content

Commit 5e231cc

Browse files
authored
Add IsProcessing method to chain.State (ava-labs#2578)
1 parent d7cf792 commit 5e231cc

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

vms/components/chain/state.go

+6
Original file line numberDiff line numberDiff line change
@@ -484,3 +484,9 @@ func (s *State) LastAcceptedBlock() *BlockWrapper {
484484
func (s *State) LastAcceptedBlockInternal() snowman.Block {
485485
return s.LastAcceptedBlock().Block
486486
}
487+
488+
// IsProcessing returns whether [blkID] is processing in consensus
489+
func (s *State) IsProcessing(blkID ids.ID) bool {
490+
_, ok := s.verifiedBlocks[blkID]
491+
return ok
492+
}

vms/components/chain/state_test.go

+41
Original file line numberDiff line numberDiff line change
@@ -894,3 +894,44 @@ func TestStateParseTransitivelyAcceptedBlock(t *testing.T) {
894894
t.Fatalf("Parsed blk1 reported incorrect height. Expected %d got %d", blk1.Height(), parsedBlk1.Height())
895895
}
896896
}
897+
898+
func TestIsProcessing(t *testing.T) {
899+
testBlks := NewTestBlocks(2)
900+
genesisBlock := testBlks[0]
901+
genesisBlock.SetStatus(choices.Accepted)
902+
blk1 := testBlks[1]
903+
904+
getBlock, parseBlock, getCanonicalBlockID := createInternalBlockFuncs(t, testBlks)
905+
chainState := NewState(&Config{
906+
DecidedCacheSize: 2,
907+
MissingCacheSize: 2,
908+
UnverifiedCacheSize: 2,
909+
BytesToIDCacheSize: 2,
910+
LastAcceptedBlock: genesisBlock,
911+
GetBlock: getBlock,
912+
UnmarshalBlock: parseBlock,
913+
BuildBlock: cantBuildBlock,
914+
GetBlockIDAtHeight: getCanonicalBlockID,
915+
})
916+
917+
// Parse blk1
918+
parsedBlk1, err := chainState.ParseBlock(context.Background(), blk1.Bytes())
919+
require.NoError(t, err)
920+
921+
// Check that it is not processing in consensus
922+
require.False(t, chainState.IsProcessing(parsedBlk1.ID()))
923+
924+
// Verify blk1
925+
err = parsedBlk1.Verify(context.Background())
926+
require.NoError(t, err)
927+
928+
// Check that it is processing in consensus
929+
require.True(t, chainState.IsProcessing(parsedBlk1.ID()))
930+
931+
// Accept blk1
932+
err = parsedBlk1.Accept(context.Background())
933+
require.NoError(t, err)
934+
935+
// Check that it is no longer processing in consensus
936+
require.False(t, chainState.IsProcessing(parsedBlk1.ID()))
937+
}

0 commit comments

Comments
 (0)