Skip to content

Commit ec754c0

Browse files
HagarMeirtock-ibm
authored andcommitted
move state to state
Signed-off-by: Hagar Meir <hagar.meir@ibm.com>
1 parent 41ac539 commit ec754c0

27 files changed

+279
-282
lines changed

core/abc_test.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717

1818
arma_types "github.com/hyperledger/fabric-x-orderer/common/types"
1919
"github.com/hyperledger/fabric-x-orderer/core"
20+
"github.com/hyperledger/fabric-x-orderer/node/consensus/state"
2021
"github.com/hyperledger/fabric-x-orderer/node/router"
2122
"github.com/hyperledger/fabric-x-orderer/testutil"
2223
"github.com/stretchr/testify/assert"
@@ -47,7 +48,7 @@ type bafSender struct {
4748

4849
func (s *bafSender) SendBAF(baf arma_types.BatchAttestationFragment) {
4950
ba := arma_types.NewSimpleBatchAttestationFragment(baf.Shard(), baf.Primary(), baf.Seq(), baf.Digest(), baf.Signer(), nil, 0, nil)
50-
s.totalOrder.SubmitRequest((&core.ControlEvent{BAF: ba}).Bytes())
51+
s.totalOrder.SubmitRequest((&state.ControlEvent{BAF: ba}).Bytes())
5152
}
5253

5354
type naiveblock struct {
@@ -100,11 +101,11 @@ func (s *shardReplicator) Replicate(shard arma_types.ShardID) <-chan arma_types.
100101
}
101102

102103
type stateProvider struct {
103-
s *core.State
104+
s *state.State
104105
}
105106

106-
func (s *stateProvider) GetLatestStateChan() <-chan *core.State {
107-
stateChan := make(chan *core.State, 1)
107+
func (s *stateProvider) GetLatestStateChan() <-chan *state.State {
108+
stateChan := make(chan *state.State, 1)
108109
stateChan <- s.s
109110
return stateChan
110111
}
@@ -169,14 +170,14 @@ func TestAssemblerBatcherConsenter(t *testing.T) {
169170

170171
totalOrder := make(naiveTotalOrder, 1000)
171172

172-
initialState := &core.State{
173+
initialState := &state.State{
173174
Threshold: 1,
174175
N: 1,
175176
ShardCount: uint16(shardCount),
176177
}
177178

178179
for shardID := uint16(0); shardID < initialState.ShardCount; shardID++ {
179-
initialState.Shards = append(initialState.Shards, core.ShardTerm{Shard: arma_types.ShardID(shardID), Term: 1})
180+
initialState.Shards = append(initialState.Shards, state.ShardTerm{Shard: arma_types.ShardID(shardID), Term: 1})
180181
}
181182

182183
consenter := &core.Consenter{
@@ -218,9 +219,9 @@ func TestAssemblerBatcherConsenter(t *testing.T) {
218219

219220
var batchers []*core.Batcher
220221

221-
state := core.State{N: uint16(shardCount)}
222+
s := state.State{N: uint16(shardCount)}
222223
for shard := 0; shard < shardCount; shard++ {
223-
state.Shards = append(state.Shards, core.ShardTerm{Shard: arma_types.ShardID(shard)})
224+
s.Shards = append(s.Shards, state.ShardTerm{Shard: arma_types.ShardID(shard)})
224225
}
225226

226227
var parties []arma_types.PartyID
@@ -236,7 +237,7 @@ func TestAssemblerBatcherConsenter(t *testing.T) {
236237
batchers = append(batchers, batcher)
237238
}
238239

239-
sp := &stateProvider{s: &state}
240+
sp := &stateProvider{s: &s}
240241

241242
for i := 0; i < shardCount; i++ {
242243
sc := &shardCommitter{
@@ -247,7 +248,7 @@ func TestAssemblerBatcherConsenter(t *testing.T) {
247248
batchers[i].BatchAcker = acker
248249
batchers[i].Ledger = sc
249250
batchers[i].BatchPuller = nil
250-
batchers[i].N = state.N
251+
batchers[i].N = s.N
251252
batchers[i].StateProvider = sp
252253
batchers[i].ID = arma_types.PartyID(i)
253254
batchers[i].Start()

core/batcher.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"time"
1717

1818
"github.com/hyperledger/fabric-x-orderer/common/types"
19+
"github.com/hyperledger/fabric-x-orderer/node/consensus/state"
1920
"github.com/pkg/errors"
2021
)
2122

@@ -41,7 +42,7 @@ type BatchPuller interface {
4142

4243
//go:generate counterfeiter -o mocks/state_provider.go . StateProvider
4344
type StateProvider interface {
44-
GetLatestStateChan() <-chan *State
45+
GetLatestStateChan() <-chan *state.State
4546
}
4647

4748
//go:generate counterfeiter -o mocks/complainer.go . Complainer
@@ -178,7 +179,7 @@ func (b *Batcher) Stop() {
178179
b.running.Wait()
179180
}
180181

181-
func (b *Batcher) getTerm(state *State) uint64 {
182+
func (b *Batcher) getTerm(state *state.State) uint64 {
182183
term := uint64(math.MaxUint64)
183184
for _, shard := range state.Shards {
184185
if shard.Shard == b.Shard {
@@ -215,7 +216,7 @@ func (b *Batcher) getTermAndNotifyChange() {
215216
// and that tx is in the pool of other batchers but again not enough (less than f+1 batchers)
216217
// to prevent a case where such a tx falls through the cracks, after a term change
217218
// all batchers resubmit to their pools txs in batches with their BAFs still in pending state
218-
func (b *Batcher) resubmitPendingBAFs(state *State, prevPrimary types.PartyID) {
219+
func (b *Batcher) resubmitPendingBAFs(state *state.State, prevPrimary types.PartyID) {
219220
for _, baf := range state.Pending {
220221
if baf.Signer() == b.ID && baf.Primary() == prevPrimary {
221222
b.Logger.Debugf("found pending BAF signed by me (id: %d) from prev primary: %d ; %s", b.ID, prevPrimary, baf.String())

core/batcher_test.go

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
arma_types "github.com/hyperledger/fabric-x-orderer/common/types"
1616
"github.com/hyperledger/fabric-x-orderer/core"
1717
"github.com/hyperledger/fabric-x-orderer/core/mocks"
18+
"github.com/hyperledger/fabric-x-orderer/node/consensus/state"
1819
"github.com/hyperledger/fabric-x-orderer/testutil"
1920
"github.com/stretchr/testify/require"
2021
)
@@ -141,7 +142,7 @@ func TestPrimaryChangeToSecondary(t *testing.T) {
141142
batcher.Ledger = ledger
142143

143144
stateProvider := &mocks.FakeStateProvider{}
144-
stateChan := make(chan *core.State)
145+
stateChan := make(chan *state.State)
145146
stateProvider.GetLatestStateChanReturns(stateChan)
146147
batcher.StateProvider = stateProvider
147148

@@ -158,8 +159,8 @@ func TestPrimaryChangeToSecondary(t *testing.T) {
158159
return stateProvider.GetLatestStateChanCallCount() == 1
159160
}, 10*time.Second, 10*time.Millisecond)
160161

161-
stateChan <- &core.State{
162-
Shards: []core.ShardTerm{
162+
stateChan <- &state.State{
163+
Shards: []state.ShardTerm{
163164
{
164165
Shard: 0,
165166
Term: 0,
@@ -173,8 +174,8 @@ func TestPrimaryChangeToSecondary(t *testing.T) {
173174

174175
require.NotZero(t, pool.NextRequestsCallCount())
175176

176-
stateChan <- &core.State{
177-
Shards: []core.ShardTerm{
177+
stateChan <- &state.State{
178+
Shards: []state.ShardTerm{
178179
{
179180
Shard: 0,
180181
Term: 1,
@@ -223,7 +224,7 @@ func TestSecondaryChangeToPrimary(t *testing.T) {
223224
batcher.Ledger = ledger
224225

225226
stateProvider := &mocks.FakeStateProvider{}
226-
stateChan := make(chan *core.State)
227+
stateChan := make(chan *state.State)
227228
stateProvider.GetLatestStateChanReturns(stateChan)
228229
batcher.StateProvider = stateProvider
229230

@@ -240,8 +241,8 @@ func TestSecondaryChangeToPrimary(t *testing.T) {
240241
return stateProvider.GetLatestStateChanCallCount() == 1
241242
}, 10*time.Second, 10*time.Millisecond)
242243

243-
stateChan <- &core.State{
244-
Shards: []core.ShardTerm{
244+
stateChan <- &state.State{
245+
Shards: []state.ShardTerm{
245246
{
246247
Shard: 0,
247248
Term: 0,
@@ -256,8 +257,8 @@ func TestSecondaryChangeToPrimary(t *testing.T) {
256257

257258
require.Zero(t, pool.NextRequestsCallCount())
258259

259-
stateChan <- &core.State{
260-
Shards: []core.ShardTerm{
260+
stateChan <- &state.State{
261+
Shards: []state.ShardTerm{
261262
{
262263
Shard: 0,
263264
Term: 1,
@@ -319,7 +320,7 @@ func TestSecondaryChangeToSecondary(t *testing.T) {
319320
batcher.MemPool = pool
320321

321322
stateProvider := &mocks.FakeStateProvider{}
322-
stateChan := make(chan *core.State)
323+
stateChan := make(chan *state.State)
323324
stateProvider.GetLatestStateChanReturns(stateChan)
324325
batcher.StateProvider = stateProvider
325326

@@ -329,8 +330,8 @@ func TestSecondaryChangeToSecondary(t *testing.T) {
329330
return stateProvider.GetLatestStateChanCallCount() == 1
330331
}, 10*time.Second, 10*time.Millisecond)
331332

332-
stateChan <- &core.State{
333-
Shards: []core.ShardTerm{
333+
stateChan <- &state.State{
334+
Shards: []state.ShardTerm{
334335
{
335336
Shard: 0,
336337
Term: 0,
@@ -343,8 +344,8 @@ func TestSecondaryChangeToSecondary(t *testing.T) {
343344
return ledger.AppendCallCount() == 1
344345
}, 10*time.Second, 10*time.Millisecond)
345346

346-
stateChan <- &core.State{
347-
Shards: []core.ShardTerm{
347+
stateChan <- &state.State{
348+
Shards: []state.ShardTerm{
348349
{
349350
Shard: 0,
350351
Term: 1,
@@ -404,7 +405,7 @@ func TestPrimaryChangeToPrimary(t *testing.T) {
404405
batcher.Ledger = ledger
405406

406407
stateProvider := &mocks.FakeStateProvider{}
407-
stateChan := make(chan *core.State)
408+
stateChan := make(chan *state.State)
408409
stateProvider.GetLatestStateChanReturns(stateChan)
409410
batcher.StateProvider = stateProvider
410411

@@ -414,8 +415,8 @@ func TestPrimaryChangeToPrimary(t *testing.T) {
414415
return stateProvider.GetLatestStateChanCallCount() == 1
415416
}, 10*time.Second, 10*time.Millisecond)
416417

417-
stateChan <- &core.State{
418-
Shards: []core.ShardTerm{
418+
stateChan <- &state.State{
419+
Shards: []state.ShardTerm{
419420
{
420421
Shard: 0,
421422
Term: 0,
@@ -429,8 +430,8 @@ func TestPrimaryChangeToPrimary(t *testing.T) {
429430

430431
require.NotZero(t, pool.NextRequestsCallCount())
431432

432-
stateChan <- &core.State{
433-
Shards: []core.ShardTerm{
433+
stateChan <- &state.State{
434+
Shards: []state.ShardTerm{
434435
{
435436
Shard: 0,
436437
Term: 4,
@@ -513,7 +514,7 @@ func TestPrimaryWaitingAndTermChange(t *testing.T) {
513514
batcher.Ledger = ledger
514515

515516
stateProvider := &mocks.FakeStateProvider{}
516-
stateChan := make(chan *core.State)
517+
stateChan := make(chan *state.State)
517518
stateProvider.GetLatestStateChanReturns(stateChan)
518519
batcher.StateProvider = stateProvider
519520

@@ -526,8 +527,8 @@ func TestPrimaryWaitingAndTermChange(t *testing.T) {
526527

527528
batcher.Start()
528529

529-
stateChan <- &core.State{
530-
Shards: []core.ShardTerm{
530+
stateChan <- &state.State{
531+
Shards: []state.ShardTerm{
531532
{
532533
Shard: 0,
533534
Term: 0,
@@ -543,8 +544,8 @@ func TestPrimaryWaitingAndTermChange(t *testing.T) {
543544
return pool.NextRequestsCallCount() == 10
544545
}, 10*time.Second, 10*time.Millisecond)
545546

546-
stateChan <- &core.State{
547-
Shards: []core.ShardTerm{
547+
stateChan <- &state.State{
548+
Shards: []state.ShardTerm{
548549
{
549550
Shard: 0,
550551
Term: 1,
@@ -593,7 +594,7 @@ func TestResubmitPending(t *testing.T) {
593594
batcher.Ledger = ledger
594595

595596
stateProvider := &mocks.FakeStateProvider{}
596-
stateChan := make(chan *core.State)
597+
stateChan := make(chan *state.State)
597598
stateProvider.GetLatestStateChanReturns(stateChan)
598599
batcher.StateProvider = stateProvider
599600

@@ -610,8 +611,8 @@ func TestResubmitPending(t *testing.T) {
610611
return stateProvider.GetLatestStateChanCallCount() == 1
611612
}, 10*time.Second, 10*time.Millisecond)
612613

613-
stateChan <- &core.State{
614-
Shards: []core.ShardTerm{
614+
stateChan <- &state.State{
615+
Shards: []state.ShardTerm{
615616
{
616617
Shard: 0,
617618
Term: 0,
@@ -634,8 +635,8 @@ func TestResubmitPending(t *testing.T) {
634635
notMyBAF := arma_types.NewSimpleBatchAttestationFragment(batch.Shard(), batch.Primary(), batch.Seq(), batch.Digest(), arma_types.PartyID(batcherID+1), nil, 0, nil)
635636
myBAFWithOtherPrimary := arma_types.NewSimpleBatchAttestationFragment(batch.Shard(), batch.Primary()+1, batch.Seq(), batch.Digest(), arma_types.PartyID(batcherID), nil, 0, nil)
636637

637-
stateChan <- &core.State{
638-
Shards: []core.ShardTerm{
638+
stateChan <- &state.State{
639+
Shards: []state.ShardTerm{
639640
{
640641
Shard: 0,
641642
Term: 1,

core/consenter.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"slices"
1111

1212
"github.com/hyperledger/fabric-x-orderer/common/types"
13+
"github.com/hyperledger/fabric-x-orderer/node/consensus/state"
1314
)
1415

1516
type TotalOrder interface {
@@ -26,17 +27,17 @@ type BatchAttestationDB interface {
2627
type Consenter struct {
2728
Logger types.Logger
2829
DB BatchAttestationDB
29-
BAFDeserializer BAFDeserializer
30-
State *State
30+
BAFDeserializer state.BAFDeserializer
31+
State *state.State
3132
}
3233

33-
func (c *Consenter) SimulateStateTransition(prevState *State, requests [][]byte) (*State, [][]types.BatchAttestationFragment) {
34+
func (c *Consenter) SimulateStateTransition(prevState *state.State, requests [][]byte) (*state.State, [][]types.BatchAttestationFragment) {
3435
controlEvents, err := requestsToControlEvents(requests, c.BAFDeserializer.Deserialize)
3536
if err != nil {
3637
panic(err)
3738
}
3839

39-
filteredControlEvents := make([]ControlEvent, 0, len(controlEvents))
40+
filteredControlEvents := make([]state.ControlEvent, 0, len(controlEvents))
4041

4142
// Iterate over all control events and prune those that already exist in our DB
4243
for _, ce := range controlEvents {
@@ -131,10 +132,10 @@ func indexBAFs(batchAttestationFragments []types.BatchAttestationFragment) map[s
131132
return index
132133
}
133134

134-
func requestsToControlEvents(requests [][]byte, fragmentFromBytes func([]byte) (types.BatchAttestationFragment, error)) ([]ControlEvent, error) {
135-
events := make([]ControlEvent, 0, len(requests))
135+
func requestsToControlEvents(requests [][]byte, fragmentFromBytes func([]byte) (types.BatchAttestationFragment, error)) ([]state.ControlEvent, error) {
136+
events := make([]state.ControlEvent, 0, len(requests))
136137
for i := 0; i < len(requests); i++ {
137-
ce := ControlEvent{}
138+
ce := state.ControlEvent{}
138139
if err := ce.FromBytes(requests[i], fragmentFromBytes); err != nil {
139140
return nil, err
140141
}

0 commit comments

Comments
 (0)