Skip to content

Commit

Permalink
Merge pull request #98 from renproject/feat/with-height-constructor
Browse files Browse the repository at this point in the history
Constructing with a given height
  • Loading branch information
jazg authored Jun 15, 2021
2 parents 33851f0 + 3570724 commit a465673
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 7 deletions.
30 changes: 29 additions & 1 deletion process/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -148,7 +176,7 @@ func New(
committer: committer,
catcher: catcher,

State: DefaultState(),
State: DefaultState().WithCurrentHeight(height),
}
}

Expand Down
15 changes: 10 additions & 5 deletions process/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
9 changes: 9 additions & 0 deletions replica/opt.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ package replica

import (
"github.com/renproject/hyperdrive/mq"
"github.com/renproject/hyperdrive/process"

"go.uber.org/zap"
)

// Options represent the options for a Hyperdrive Replica
type Options struct {
Logger *zap.Logger
StartingHeight process.Height
MessageQueueOpts mq.Options
}

Expand All @@ -20,6 +22,7 @@ func DefaultOptions() Options {
}
return Options{
Logger: logger,
StartingHeight: process.DefaultHeight,
MessageQueueOpts: mq.DefaultOptions(),
}
}
Expand All @@ -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
Expand Down
3 changes: 2 additions & 1 deletion replica/replica.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit a465673

Please sign in to comment.