Skip to content

Commit

Permalink
Updating proposer calculator on restart (#1915)
Browse files Browse the repository at this point in the history
* Updating proposer calculator on restart

* test fix
  • Loading branch information
stana-miric committed Sep 19, 2023
1 parent 61baa7e commit 38efd92
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 6 deletions.
3 changes: 2 additions & 1 deletion consensus/polybft/consensus_runtime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -465,9 +465,10 @@ func Test_NewConsensusRuntime(t *testing.T) {
blockchainMock.On("GetStateProviderForBlock", mock.Anything).Return(new(stateProviderMock)).Once()
blockchainMock.On("GetSystemState", mock.Anything, mock.Anything).Return(systemStateMock).Once()
blockchainMock.On("GetHeaderByNumber", uint64(0)).Return(&types.Header{Number: 0, ExtraData: createTestExtraForAccounts(t, 0, validators, nil)})
blockchainMock.On("GetHeaderByNumber", uint64(1)).Return(&types.Header{Number: 1, ExtraData: createTestExtraForAccounts(t, 1, validators, nil)})

polybftBackendMock := new(polybftBackendMock)
polybftBackendMock.On("GetValidators", mock.Anything, mock.Anything).Return(validators).Twice()
polybftBackendMock.On("GetValidators", mock.Anything, mock.Anything).Return(validators).Times(3)

tmpDir := t.TempDir()
config := &runtimeConfig{
Expand Down
20 changes: 18 additions & 2 deletions consensus/polybft/proposer_calculator.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,12 +167,23 @@ func NewProposerCalculator(config *runtimeConfig, logger hclog.Logger) (*Propose
return nil, err
}

return &ProposerCalculator{
pc := &ProposerCalculator{
snapshot: snap,
config: config,
state: config.State,
logger: logger,
}, nil
}

// If the node was previously stopped, leaving the proposer calculator in an inconsistent state,
// proposer calculator needs to be updated.
blockNumber := config.blockchain.CurrentHeader().Number
if pc.snapshot.Height <= blockNumber {
if err = pc.update(blockNumber); err != nil {
return nil, err
}
}

return pc, nil
}

// NewProposerCalculator creates a new proposer calculator object
Expand All @@ -199,6 +210,11 @@ func (pc *ProposerCalculator) GetSnapshot() (*ProposerSnapshot, bool) {
// It will update priorities and save the updated snapshot to db
func (pc *ProposerCalculator) PostBlock(req *PostBlockRequest) error {
blockNumber := req.FullBlock.Block.Number()

return pc.update(blockNumber)
}

func (pc *ProposerCalculator) update(blockNumber uint64) error {
pc.logger.Debug("Update proposers snapshot started", "target block", blockNumber)

from := pc.snapshot.Height
Expand Down
17 changes: 14 additions & 3 deletions e2e-polybft/e2e/consensus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"math/big"
"path"
"strconv"
"sync"
"testing"
"time"

Expand Down Expand Up @@ -93,23 +94,33 @@ func TestE2E_Consensus_Basic_WithNonValidators(t *testing.T) {
func TestE2E_Consensus_BulkDrop(t *testing.T) {
const (
clusterSize = 5
bulkToDrop = 3
bulkToDrop = 4
epochSize = 5
)

cluster := framework.NewTestCluster(t, clusterSize,
framework.WithEpochSize(epochSize))
framework.WithEpochSize(epochSize),
framework.WithBlockTime(time.Second))
defer cluster.Stop()

// wait for cluster to start
cluster.WaitForReady(t)

var wg sync.WaitGroup
// drop bulk of nodes from cluster
for i := 0; i < bulkToDrop; i++ {
node := cluster.Servers[i]
node.Stop()

wg.Add(1)

go func(node *framework.TestServer) {
defer wg.Done()
node.Stop()
}(node)
}

wg.Wait()

// start dropped nodes again
for i := 0; i < bulkToDrop; i++ {
node := cluster.Servers[i]
Expand Down
12 changes: 12 additions & 0 deletions e2e-polybft/framework/test-cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ type TestClusterConfig struct {
LogsDir string
TmpDir string
BlockGasLimit uint64
BlockTime time.Duration
BurnContract *polybft.BurnContractInfo
ValidatorPrefix string
Binary string
Expand Down Expand Up @@ -251,6 +252,12 @@ func WithEpochReward(epochReward int) ClusterOption {
}
}

func WithBlockTime(blockTime time.Duration) ClusterOption {
return func(h *TestClusterConfig) {
h.BlockTime = blockTime
}
}

func WithBlockGasLimit(blockGasLimit uint64) ClusterOption {
return func(h *TestClusterConfig) {
h.BlockGasLimit = blockGasLimit
Expand Down Expand Up @@ -467,6 +474,11 @@ func NewTestCluster(t *testing.T, validatorsCount int, opts ...ClusterOption) *T
"--trieroot", cluster.Config.InitialStateRoot.String(),
}

if cluster.Config.BlockTime != 0 {
args = append(args, "--block-time",
cluster.Config.BlockTime.String())
}

if cluster.Config.RelayerTrackerPollInterval != 0 {
args = append(args, "--block-tracker-poll-interval",
cluster.Config.RelayerTrackerPollInterval.String())
Expand Down

0 comments on commit 38efd92

Please sign in to comment.