Skip to content

Commit

Permalink
Merge pull request #1648 from orbs-network/fix-sync-config
Browse files Browse the repository at this point in the history
fix sync config used in consensusContext
  • Loading branch information
gadcl authored Nov 19, 2020
2 parents 1eb59f7 + d0da095 commit 1111ecd
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 12 deletions.
1 change: 1 addition & 0 deletions config/config_component_tests_presets.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ func ForConsensusContextTests(triggersEnabled bool) ConsensusContextConfig {
cfg.SetDuration(CONSENSUS_CONTEXT_SYSTEM_TIMESTAMP_ALLOWED_JITTER, 2*time.Second)
cfg.SetBool(CONSENSUS_CONTEXT_TRIGGERS_ENABLED, triggersEnabled)
cfg.SetDuration(MANAGEMENT_CONSENSUS_GRACE_TIMEOUT, 1*time.Minute)
cfg.SetDuration(COMMITTEE_GRACE_PERIOD, 1*time.Hour)

return cfg
}
Expand Down
5 changes: 2 additions & 3 deletions config/config_interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type NodeConfig interface {
ManagementMaxFileSize() uint32
ManagementPollingInterval() time.Duration
ManagementConsensusGraceTimeout() time.Duration
CommitteeGracePeriod() time.Duration // also used in blockSync
CommitteeGracePeriod() time.Duration // also used in blockSync (via consensus context)

// consensus
ActiveConsensusAlgo() consensus.ConsensusAlgoType
Expand Down Expand Up @@ -142,7 +142,6 @@ type BlockStorageConfig interface {
BlockSyncCollectResponseTimeout() time.Duration
BlockSyncCollectChunksTimeout() time.Duration
BlockSyncDescendingEnabled() bool
CommitteeGracePeriod() time.Duration
BlockStorageTransactionReceiptQueryTimestampGrace() time.Duration
TransactionExpirationWindow() time.Duration
BlockTrackerGraceTimeout() time.Duration
Expand All @@ -163,14 +162,14 @@ type GossipTransportConfig interface {
GossipReconnectInterval() time.Duration
}

// Config based on https://github.com/orbs-network/orbs-spec/blob/master/behaviors/config/services.md#consensus-context
type ConsensusContextConfig interface {
VirtualChainId() primitives.VirtualChainId
ConsensusContextMaximumTransactionsInBlock() uint32
LeanHelixConsensusMinimumCommitteeSize() uint32
ConsensusContextSystemTimestampAllowedJitter() time.Duration
ConsensusContextTriggersEnabled() bool
ManagementConsensusGraceTimeout() time.Duration
CommitteeGracePeriod() time.Duration
}

type PublicApiConfig interface {
Expand Down
4 changes: 1 addition & 3 deletions services/blockstorage/internodesync/block_sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ type blockSyncConfig interface {
BlockSyncCollectResponseTimeout() time.Duration
BlockSyncCollectChunksTimeout() time.Duration
BlockSyncDescendingEnabled() bool
CommitteeGracePeriod() time.Duration
}

type SyncState struct {
Expand Down Expand Up @@ -132,8 +131,7 @@ func newBlockSyncWithFactory(ctx context.Context, config blockSyncConfig, factor
log.Stringable("collect-responses-timeout", bs.factory.config.BlockSyncCollectResponseTimeout()),
log.Stringable("collect-chunks-timeout", bs.factory.config.BlockSyncCollectChunksTimeout()),
log.Uint32("batch-size", bs.factory.config.BlockSyncNumBlocksInBatch()),
log.Stringable("blocks-order", bs.factory.getSyncBlocksOrder()),
log.Stringable("committee-grace-period", bs.factory.config.CommitteeGracePeriod()))
log.Stringable("blocks-order", bs.factory.getSyncBlocksOrder()))

bs.Supervise(govnr.Forever(ctx, "Node sync state machine", logfields.GovnrErrorer(logger), func() {
bs.syncLoop(ctx)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func (s *processingBlocksState) processState(ctx context.Context) syncState {
logger.Info("failed to verify the blocks chunk range received via sync", log.Error(err))
return s.factory.CreateCollectingAvailabilityResponseState()
}
if err := s.validateChainFork(ctx, s.blocks.BlockPairs, syncState, s.factory.config.CommitteeGracePeriod(), receivedSyncBlocksOrder); err != nil {
if err := s.validateChainFork(ctx, s.blocks.BlockPairs, syncState, receivedSyncBlocksOrder); err != nil {
s.metrics.failedValidationBlocks.Inc()
logger.Info("failed to verify the blocks chunk PoS received via sync", log.Error(err))
return s.factory.CreateCollectingAvailabilityResponseState()
Expand Down Expand Up @@ -165,7 +165,7 @@ func (s *processingBlocksState) validateBlocksRange(blocks []*protocol.BlockPair
}

// assumes blocks range is correct. Specifically in descending (blockStorage.lastSynced.height - 1 == blocks[0].height ) or ( blocks[0].height > blockStorage.top.height)
func (s *processingBlocksState) validateChainFork(ctx context.Context, blocks []*protocol.BlockPairContainer, syncState SyncState, committeeGraePeriod time.Duration, receivedSyncBlocksOrder gossipmessages.SyncBlocksOrder) error {
func (s *processingBlocksState) validateChainFork(ctx context.Context, blocks []*protocol.BlockPairContainer, syncState SyncState, receivedSyncBlocksOrder gossipmessages.SyncBlocksOrder) error {
syncBlocksOrder := s.factory.getSyncBlocksOrder()
topHeight, _, lastSyncedHeight := syncState.GetSyncStateBlockHeights()

Expand Down
4 changes: 2 additions & 2 deletions services/consensuscontext/committee.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ func (s *service) ValidateBlockReferenceTime(ctx context.Context, input *service
}

now := time.Duration(time.Now().Unix()) * time.Second
if (time.Duration(adjustedPrevBlockReferenceTime)*time.Second)+s.config.ManagementConsensusGraceTimeout() < now { // prevRefTime-committee is too old
return nil, errors.New("ValidateBlockReferenceTime: block reference time is outdated ( used on :=prevBlock.RefTime for current consensus round committee grace time ) ")
if (time.Duration(adjustedPrevBlockReferenceTime)*time.Second)+s.config.CommitteeGracePeriod() < now { // prevRefTime-committee is too old
return nil, errors.New(fmt.Sprintf("ValidateBlockReferenceTime: block reference time is outdated ( verified against :=prevBlock.RefTime for current consensus round committee ) : blockheight=%d, prevblock.refTime(sec)=%d", uint(input.BlockHeight), input.PrevBlockReferenceTime))
}

return &services.ValidateBlockReferenceTimeOutput{}, nil
Expand Down
44 changes: 42 additions & 2 deletions services/consensuscontext/test/validate_block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,11 @@ func TestValidateResultsBlockFailsOnBadGenesis(t *testing.T) {
with.Logging(t, func(harness *with.LoggingHarness) {
s := newHarness(harness.Logger, false)
s.management.Reset()
setManagementValues(s.management, 1, primitives.TimestampSeconds(time.Now().Unix()), primitives.TimestampSeconds(time.Now().Unix() + 5000))
setManagementValues(s.management, 1, primitives.TimestampSeconds(time.Now().Unix()), primitives.TimestampSeconds(time.Now().Unix()+5000))

input := &services.ValidateResultsBlockInput{
CurrentBlockHeight: 1,
PrevBlockReferenceTime: primitives.TimestampSeconds(time.Now().Unix() -1000),
PrevBlockReferenceTime: primitives.TimestampSeconds(time.Now().Unix() - 1000),
}

_, err := s.service.ValidateResultsBlock(ctx, input)
Expand All @@ -117,3 +117,43 @@ func TestValidateResultsBlockFailsOnBadGenesis(t *testing.T) {
})
}

func TestValidateBlockReferenceTime(t *testing.T) {
with.Context(func(ctx context.Context) {
with.Logging(t, func(harness *with.LoggingHarness) {
s := newHarness(harness.Logger, false)
s.management.Reset()
now := time.Now()
currentRefTime := primitives.TimestampSeconds(now.Unix())
genesisRefTime := primitives.TimestampSeconds(now.Add(-s.config.CommitteeGracePeriod() * 2).Unix()) // invalid committee grace wise
setManagementValues(s.management, 1, currentRefTime, genesisRefTime)

// validate genesis block (prev refTime := Management.genesis)
input := &services.ValidateBlockReferenceTimeInput{
BlockHeight: 1,
PrevBlockReferenceTime: 0,
}

// genesis block with an invalid genesis ref time
_, err := s.service.ValidateBlockReferenceTime(ctx, input) // note: ValidateBlockReferenceTime uses time.now within function
require.Error(t, err, "validation should fail as genesis reference time is outdated (not within committee grace - honesty assumption time)")

// genesis block with valid genesis ref time
s.management.Reset()
genesisRefTime = primitives.TimestampSeconds(now.Add(-s.config.CommitteeGracePeriod() / 2).Unix())
setManagementValues(s.management, 1, currentRefTime, genesisRefTime)
_, err = s.service.ValidateBlockReferenceTime(ctx, input)
require.NoError(t, err, "validation should succeed as genesis reference time is within committee grace - honesty assumption time")

// too old ref time (non genesis)
input.BlockHeight = primitives.BlockHeight(10)
input.PrevBlockReferenceTime = primitives.TimestampSeconds(now.Add(-s.config.CommitteeGracePeriod() * 2).Unix()) // invalid committee grace wise
_, err = s.service.ValidateBlockReferenceTime(ctx, input)
require.Error(t, err, "validation should fail as prev block reference time is outdated (not within committee grace - honesty assumption time)")

// valid ref time
input.PrevBlockReferenceTime = primitives.TimestampSeconds(now.Add(-s.config.CommitteeGracePeriod() / 2).Unix()) // valid refTime - committee grace wise
_, err = s.service.ValidateBlockReferenceTime(ctx, input)
require.NoError(t, err, "validation should succeed as prev reference time is within committee grace - honesty assumption time")
})
})
}

0 comments on commit 1111ecd

Please sign in to comment.