Skip to content

Commit a465673

Browse files
authored
Merge pull request #98 from renproject/feat/with-height-constructor
Constructing with a given height
2 parents 33851f0 + 3570724 commit a465673

File tree

4 files changed

+50
-7
lines changed

4 files changed

+50
-7
lines changed

process/process.go

+29-1
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,34 @@ func New(
134134
broadcaster Broadcaster,
135135
committer Committer,
136136
catcher Catcher,
137+
) Process {
138+
return NewWithCurrentHeight(
139+
whoami,
140+
DefaultHeight,
141+
f,
142+
timer,
143+
scheduler,
144+
proposer,
145+
validator,
146+
broadcaster,
147+
committer,
148+
catcher,
149+
)
150+
}
151+
152+
// NewWithCurrentHeight returns a new Process that starts at the given height
153+
// with empty message logs.
154+
func NewWithCurrentHeight(
155+
whoami id.Signatory,
156+
height Height,
157+
f int,
158+
timer Timer,
159+
scheduler Scheduler,
160+
proposer Proposer,
161+
validator Validator,
162+
broadcaster Broadcaster,
163+
committer Committer,
164+
catcher Catcher,
137165
) Process {
138166
return Process{
139167
whoami: whoami,
@@ -148,7 +176,7 @@ func New(
148176
committer: committer,
149177
catcher: catcher,
150178

151-
State: DefaultState(),
179+
State: DefaultState().WithCurrentHeight(height),
152180
}
153181
}
154182

process/state.go

+10-5
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@ import (
88
"github.com/renproject/surge"
99
)
1010

11+
var (
12+
// DefaulHeight is set to 1, because the genesis block is assumed to exist
13+
// at Height 0.
14+
DefaultHeight = Height(1)
15+
DefaultRound = Round(0)
16+
)
17+
1118
// The State of a Process. It should be saved after every method call on the
1219
// Process, but should not be saved during method calls (interacting with the
1320
// State concurrently is unsafe). It is worth noting that the State does not
@@ -50,13 +57,11 @@ type State struct {
5057
TraceLogs map[Round]map[id.Signatory]bool
5158
}
5259

53-
// DefaultState returns a State with all fields set to their default values. The
54-
// Height default to 1, because the genesis block is assumed to exist at Height
55-
// 0.
60+
// DefaultState returns a State with all fields set to their default values.
5661
func DefaultState() State {
5762
return State{
58-
CurrentHeight: 1, // Skip genesis.
59-
CurrentRound: 0,
63+
CurrentHeight: DefaultHeight,
64+
CurrentRound: DefaultRound,
6065
CurrentStep: Proposing,
6166
LockedValue: NilValue,
6267
LockedRound: InvalidRound,

replica/opt.go

+9
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@ package replica
22

33
import (
44
"github.com/renproject/hyperdrive/mq"
5+
"github.com/renproject/hyperdrive/process"
56

67
"go.uber.org/zap"
78
)
89

910
// Options represent the options for a Hyperdrive Replica
1011
type Options struct {
1112
Logger *zap.Logger
13+
StartingHeight process.Height
1214
MessageQueueOpts mq.Options
1315
}
1416

@@ -20,6 +22,7 @@ func DefaultOptions() Options {
2022
}
2123
return Options{
2224
Logger: logger,
25+
StartingHeight: process.DefaultHeight,
2326
MessageQueueOpts: mq.DefaultOptions(),
2427
}
2528
}
@@ -30,6 +33,12 @@ func (opts Options) WithLogger(logger *zap.Logger) Options {
3033
return opts
3134
}
3235

36+
// WithStartingHeight updates the height that the Replica will start at
37+
func (opts Options) WithStartingHeight(height process.Height) Options {
38+
opts.StartingHeight = height
39+
return opts
40+
}
41+
3342
// WithMqOptions updates the Replica's message queue options
3443
func (opts Options) WithMqOptions(mqOpts mq.Options) Options {
3544
opts.MessageQueueOpts = mqOpts

replica/replica.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,9 @@ func New(
5353
) *Replica {
5454
f := len(signatories) / 3
5555
scheduler := scheduler.NewRoundRobin(signatories)
56-
proc := process.New(
56+
proc := process.NewWithCurrentHeight(
5757
whoami,
58+
opts.StartingHeight,
5859
f,
5960
linearTimer,
6061
scheduler,

0 commit comments

Comments
 (0)