Skip to content

Commit

Permalink
Fix (#1682)
Browse files Browse the repository at this point in the history
  • Loading branch information
goran-ethernal committed Jul 3, 2023
1 parent b45a404 commit 655d15d
Show file tree
Hide file tree
Showing 5 changed files with 283 additions and 192 deletions.
3 changes: 2 additions & 1 deletion consensus/polybft/consensus_runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ func (c *consensusRuntime) initStateSyncManager(logger hcf.Logger) error {
maxCommitmentSize: maxCommitmentSize,
numBlockConfirmations: c.config.numBlockConfirmations,
},
c,
)

c.stateSyncManager = stateSyncManager
Expand Down Expand Up @@ -604,7 +605,7 @@ func (c *consensusRuntime) setIsActiveValidator(isActiveValidator bool) {
}

// isActiveValidator indicates if node is in validator set or not
func (c *consensusRuntime) isActiveValidator() bool {
func (c *consensusRuntime) IsActiveValidator() bool {
return c.activeValidatorFlag.Load()
}

Expand Down
4 changes: 2 additions & 2 deletions consensus/polybft/consensus_runtime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ func TestConsensusRuntime_FSM_NotEndOfEpoch_NotEndOfSprint(t *testing.T) {
err := runtime.FSM()
require.NoError(t, err)

assert.True(t, runtime.isActiveValidator())
assert.True(t, runtime.IsActiveValidator())
assert.False(t, runtime.fsm.isEndOfEpoch)
assert.False(t, runtime.fsm.isEndOfSprint)
assert.Equal(t, lastBlock.Number, runtime.fsm.parent.Number)
Expand Down Expand Up @@ -482,7 +482,7 @@ func Test_NewConsensusRuntime(t *testing.T) {
runtime, err := newConsensusRuntime(hclog.NewNullLogger(), config)
require.NoError(t, err)

assert.False(t, runtime.isActiveValidator())
assert.False(t, runtime.IsActiveValidator())
assert.Equal(t, runtime.config.DataDir, tmpDir)
assert.Equal(t, uint64(10), runtime.config.PolyBFTConfig.SprintSize)
assert.Equal(t, uint64(10), runtime.config.PolyBFTConfig.EpochSize)
Expand Down
20 changes: 19 additions & 1 deletion consensus/polybft/state_sync_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ import (
"google.golang.org/protobuf/proto"
)

type Runtime interface {
IsActiveValidator() bool
}

type StateSyncProof struct {
Proof []types.Hash
StateSync *contractsapi.StateSyncedEvent
Expand Down Expand Up @@ -81,6 +85,8 @@ type stateSyncManager struct {
validatorSet validator.ValidatorSet
epoch uint64
nextCommittedIndex uint64

runtime Runtime
}

// topic is an interface for p2p message gossiping
Expand All @@ -90,12 +96,14 @@ type topic interface {
}

// newStateSyncManager creates a new instance of state sync manager
func newStateSyncManager(logger hclog.Logger, state *State, config *stateSyncConfig) *stateSyncManager {
func newStateSyncManager(logger hclog.Logger, state *State, config *stateSyncConfig,
runtime Runtime) *stateSyncManager {
return &stateSyncManager{
logger: logger,
state: state,
config: config,
closeCh: make(chan struct{}),
runtime: runtime,
}
}

Expand Down Expand Up @@ -140,6 +148,11 @@ func (s *stateSyncManager) initTracker() error {
// initTransport subscribes to bridge topics (getting votes for commitments)
func (s *stateSyncManager) initTransport() error {
return s.config.topic.Subscribe(func(obj interface{}, _ peer.ID) {
if !s.runtime.IsActiveValidator() {
// don't save votes if not a validator
return
}

msg, ok := obj.(*polybftProto.TransportMessage)
if !ok {
s.logger.Warn("failed to deliver vote, invalid msg", "obj", obj)
Expand Down Expand Up @@ -492,6 +505,11 @@ func (s *stateSyncManager) buildProofs(commitmentMsg *contractsapi.StateSyncComm

// buildCommitment builds a new commitment, signs it and gossips its vote for it
func (s *stateSyncManager) buildCommitment() error {
if !s.runtime.IsActiveValidator() {
// don't build commitment if not a validator
return nil
}

s.lock.Lock()
defer s.lock.Unlock()

Expand Down
Loading

0 comments on commit 655d15d

Please sign in to comment.