From 3570724cd055eec5dc382d50a9046a80fc5f6ac8 Mon Sep 17 00:00:00 2001 From: ross Date: Tue, 15 Jun 2021 13:24:26 +1000 Subject: [PATCH] constructor for given starting height --- process/process.go | 30 +++++++++++++++++++++++++++++- process/state.go | 15 ++++++++++----- replica/opt.go | 9 +++++++++ replica/replica.go | 3 ++- 4 files changed, 50 insertions(+), 7 deletions(-) diff --git a/process/process.go b/process/process.go index d256c745..b448de46 100644 --- a/process/process.go +++ b/process/process.go @@ -134,6 +134,34 @@ func New( broadcaster Broadcaster, committer Committer, catcher Catcher, +) Process { + return NewWithCurrentHeight( + whoami, + DefaultHeight, + f, + timer, + scheduler, + proposer, + validator, + broadcaster, + committer, + catcher, + ) +} + +// NewWithCurrentHeight returns a new Process that starts at the given height +// with empty message logs. +func NewWithCurrentHeight( + whoami id.Signatory, + height Height, + f int, + timer Timer, + scheduler Scheduler, + proposer Proposer, + validator Validator, + broadcaster Broadcaster, + committer Committer, + catcher Catcher, ) Process { return Process{ whoami: whoami, @@ -148,7 +176,7 @@ func New( committer: committer, catcher: catcher, - State: DefaultState(), + State: DefaultState().WithCurrentHeight(height), } } diff --git a/process/state.go b/process/state.go index 9d888e15..6cc186f3 100644 --- a/process/state.go +++ b/process/state.go @@ -8,6 +8,13 @@ import ( "github.com/renproject/surge" ) +var ( + // DefaulHeight is set to 1, because the genesis block is assumed to exist + // at Height 0. + DefaultHeight = Height(1) + DefaultRound = Round(0) +) + // The State of a Process. It should be saved after every method call on the // Process, but should not be saved during method calls (interacting with the // State concurrently is unsafe). It is worth noting that the State does not @@ -50,13 +57,11 @@ type State struct { TraceLogs map[Round]map[id.Signatory]bool } -// DefaultState returns a State with all fields set to their default values. The -// Height default to 1, because the genesis block is assumed to exist at Height -// 0. +// DefaultState returns a State with all fields set to their default values. func DefaultState() State { return State{ - CurrentHeight: 1, // Skip genesis. - CurrentRound: 0, + CurrentHeight: DefaultHeight, + CurrentRound: DefaultRound, CurrentStep: Proposing, LockedValue: NilValue, LockedRound: InvalidRound, diff --git a/replica/opt.go b/replica/opt.go index 66103824..231a8fbf 100644 --- a/replica/opt.go +++ b/replica/opt.go @@ -2,6 +2,7 @@ package replica import ( "github.com/renproject/hyperdrive/mq" + "github.com/renproject/hyperdrive/process" "go.uber.org/zap" ) @@ -9,6 +10,7 @@ import ( // Options represent the options for a Hyperdrive Replica type Options struct { Logger *zap.Logger + StartingHeight process.Height MessageQueueOpts mq.Options } @@ -20,6 +22,7 @@ func DefaultOptions() Options { } return Options{ Logger: logger, + StartingHeight: process.DefaultHeight, MessageQueueOpts: mq.DefaultOptions(), } } @@ -30,6 +33,12 @@ func (opts Options) WithLogger(logger *zap.Logger) Options { return opts } +// WithStartingHeight updates the height that the Replica will start at +func (opts Options) WithStartingHeight(height process.Height) Options { + opts.StartingHeight = height + return opts +} + // WithMqOptions updates the Replica's message queue options func (opts Options) WithMqOptions(mqOpts mq.Options) Options { opts.MessageQueueOpts = mqOpts diff --git a/replica/replica.go b/replica/replica.go index 6d5736d7..ab3a5c19 100644 --- a/replica/replica.go +++ b/replica/replica.go @@ -53,8 +53,9 @@ func New( ) *Replica { f := len(signatories) / 3 scheduler := scheduler.NewRoundRobin(signatories) - proc := process.New( + proc := process.NewWithCurrentHeight( whoami, + opts.StartingHeight, f, linearTimer, scheduler,