Skip to content

Commit 10cc6ac

Browse files
authored
refactor: Move actions to separate packages to avoid cyclic deps (#3090)
1 parent 9a2a643 commit 10cc6ac

20 files changed

+112
-90
lines changed

consensus/driver/driver.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/NethermindEth/juno/consensus/p2p"
99
"github.com/NethermindEth/juno/consensus/tendermint"
1010
"github.com/NethermindEth/juno/consensus/types"
11+
"github.com/NethermindEth/juno/consensus/types/actions"
1112
"github.com/NethermindEth/juno/utils"
1213
)
1314

@@ -109,24 +110,24 @@ func (d *Driver[V, H, A]) Run(ctx context.Context) error {
109110
func (d *Driver[V, H, A]) execute(
110111
ctx context.Context,
111112
broadcasters p2p.Broadcasters[V, H, A],
112-
actions []types.Action[V, H, A],
113+
executingActions []actions.Action[V, H, A],
113114
) (isCommitted bool) {
114-
for _, action := range actions {
115+
for _, action := range executingActions {
115116
switch action := action.(type) {
116-
case *types.BroadcastProposal[V, H, A]:
117+
case *actions.BroadcastProposal[V, H, A]:
117118
broadcasters.ProposalBroadcaster.Broadcast(ctx, (*types.Proposal[V, H, A])(action))
118-
case *types.BroadcastPrevote[H, A]:
119+
case *actions.BroadcastPrevote[H, A]:
119120
broadcasters.PrevoteBroadcaster.Broadcast(ctx, (*types.Prevote[H, A])(action))
120-
case *types.BroadcastPrecommit[H, A]:
121+
case *actions.BroadcastPrecommit[H, A]:
121122
broadcasters.PrecommitBroadcaster.Broadcast(ctx, (*types.Precommit[H, A])(action))
122-
case *types.ScheduleTimeout:
123+
case *actions.ScheduleTimeout:
123124
d.scheduledTms[types.Timeout(*action)] = time.AfterFunc(d.getTimeout(action.Step, action.Round), func() {
124125
select {
125126
case <-ctx.Done():
126127
case d.timeoutsCh <- types.Timeout(*action):
127128
}
128129
})
129-
case *types.Commit[V, H, A]:
130+
case *actions.Commit[V, H, A]:
130131
if err := d.db.Flush(); err != nil {
131132
d.log.Fatalf("failed to flush WAL during commit", "height", action.Height, "round", action.Round, "err", err)
132133
}

consensus/driver/driver_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/NethermindEth/juno/consensus/p2p"
1313
"github.com/NethermindEth/juno/consensus/starknet"
1414
"github.com/NethermindEth/juno/consensus/types"
15+
"github.com/NethermindEth/juno/consensus/types/actions"
1516
"github.com/NethermindEth/juno/core/felt"
1617
"github.com/NethermindEth/juno/core/hash"
1718
"github.com/NethermindEth/juno/db/pebble"
@@ -110,7 +111,7 @@ func generateAndRegisterRandomActions(
110111
}
111112

112113
func toAction(timeout types.Timeout) starknet.Action {
113-
return utils.HeapPtr(types.ScheduleTimeout(timeout))
114+
return utils.HeapPtr(actions.ScheduleTimeout(timeout))
114115
}
115116

116117
func increaseBroadcasterWaitGroup[M any](

consensus/mocks/mock_state_machine.go

Lines changed: 11 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

consensus/starknet/starknet.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package starknet
22

33
import (
44
"github.com/NethermindEth/juno/consensus/types"
5+
"github.com/NethermindEth/juno/consensus/types/actions"
56
"github.com/NethermindEth/juno/consensus/types/wal"
67
"github.com/NethermindEth/juno/core/address"
78
"github.com/NethermindEth/juno/core/hash"
@@ -24,11 +25,11 @@ type (
2425
Vote = types.Vote[Hash, Address]
2526
MessageHeader = types.MessageHeader[Address]
2627

27-
Action = types.Action[Value, Hash, Address]
28-
BroadcastProposal = types.BroadcastProposal[Value, Hash, Address]
29-
BroadcastPrevote = types.BroadcastPrevote[Hash, Address]
30-
BroadcastPrecommit = types.BroadcastPrecommit[Hash, Address]
31-
Commit = types.Commit[Value, Hash, Address]
28+
Action = actions.Action[Value, Hash, Address]
29+
BroadcastProposal = actions.BroadcastProposal[Value, Hash, Address]
30+
BroadcastPrevote = actions.BroadcastPrevote[Hash, Address]
31+
BroadcastPrecommit = actions.BroadcastPrecommit[Hash, Address]
32+
Commit = actions.Commit[Value, Hash, Address]
3233

3334
WALEntry = wal.Entry[Value, Hash, Address]
3435
WALProposal = wal.WALProposal[Value, Hash, Address]

consensus/tendermint/action_asserter_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"testing"
55

66
"github.com/NethermindEth/juno/consensus/starknet"
7-
"github.com/NethermindEth/juno/consensus/types"
7+
"github.com/NethermindEth/juno/consensus/types/actions"
88
"github.com/NethermindEth/juno/core/hash"
99
"github.com/NethermindEth/juno/utils"
1010
"github.com/stretchr/testify/assert"
@@ -38,7 +38,7 @@ func (a actionAsserter[T]) expectActions(expected ...starknet.Action) actionAsse
3838
assert.Equal(a.testing, getHash(a.stateMachine.state.validValue), action.ID)
3939
}
4040
assertPrecommit(a.testing, a.stateMachine, starknet.Precommit(*action))
41-
case *types.ScheduleTimeout:
41+
case *actions.ScheduleTimeout:
4242
// ScheduleTimeout doesn't come with any state change, so there's nothing to assert here.
4343
}
4444
}

consensus/tendermint/action_builder_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package tendermint
33
import (
44
"github.com/NethermindEth/juno/consensus/starknet"
55
"github.com/NethermindEth/juno/consensus/types"
6+
"github.com/NethermindEth/juno/consensus/types/actions"
67
)
78

89
// actionBuilder is a helper struct to build expected actions as the result of processing messages and timeouts for the state machine.
@@ -43,7 +44,7 @@ func (t actionBuilder) broadcastPrecommit(val *starknet.Value) starknet.Action {
4344

4445
// scheduleTimeout builds and returns a ScheduleTimeout action.
4546
func (t actionBuilder) scheduleTimeout(s types.Step) starknet.Action {
46-
return &types.ScheduleTimeout{
47+
return &actions.ScheduleTimeout{
4748
Step: s,
4849
Height: t.actionHeight,
4950
Round: t.actionRound,

consensus/tendermint/broadcast.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ package tendermint
22

33
import (
44
"github.com/NethermindEth/juno/consensus/types"
5+
"github.com/NethermindEth/juno/consensus/types/actions"
56
"github.com/NethermindEth/juno/consensus/types/wal"
67
)
78

8-
func (s *stateMachine[V, H, A]) sendProposal(value *V) types.Action[V, H, A] {
9+
func (s *stateMachine[V, H, A]) sendProposal(value *V) actions.Action[V, H, A] {
910
proposalMessage := types.Proposal[V, H, A]{
1011
MessageHeader: types.MessageHeader[A]{
1112
Height: s.state.height,
@@ -22,10 +23,10 @@ func (s *stateMachine[V, H, A]) sendProposal(value *V) types.Action[V, H, A] {
2223

2324
s.voteCounter.AddProposal(&proposalMessage)
2425

25-
return (*types.BroadcastProposal[V, H, A])(&proposalMessage)
26+
return (*actions.BroadcastProposal[V, H, A])(&proposalMessage)
2627
}
2728

28-
func (s *stateMachine[V, H, A]) setStepAndSendPrevote(id *H) types.Action[V, H, A] {
29+
func (s *stateMachine[V, H, A]) setStepAndSendPrevote(id *H) actions.Action[V, H, A] {
2930
vote := types.Prevote[H, A]{
3031
MessageHeader: types.MessageHeader[A]{
3132
Height: s.state.height,
@@ -38,10 +39,10 @@ func (s *stateMachine[V, H, A]) setStepAndSendPrevote(id *H) types.Action[V, H,
3839
s.voteCounter.AddPrevote(&vote)
3940
s.state.step = types.StepPrevote
4041

41-
return (*types.BroadcastPrevote[H, A])(&vote)
42+
return (*actions.BroadcastPrevote[H, A])(&vote)
4243
}
4344

44-
func (s *stateMachine[V, H, A]) setStepAndSendPrecommit(id *H) types.Action[V, H, A] {
45+
func (s *stateMachine[V, H, A]) setStepAndSendPrecommit(id *H) actions.Action[V, H, A] {
4546
vote := types.Precommit[H, A]{
4647
MessageHeader: types.MessageHeader[A]{
4748
Height: s.state.height,
@@ -54,5 +55,5 @@ func (s *stateMachine[V, H, A]) setStepAndSendPrecommit(id *H) types.Action[V, H
5455
s.voteCounter.AddPrecommit(&vote)
5556
s.state.step = types.StepPrecommit
5657

57-
return (*types.BroadcastPrecommit[H, A])(&vote)
58+
return (*actions.BroadcastPrecommit[H, A])(&vote)
5859
}

consensus/tendermint/process.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@ package tendermint
22

33
import (
44
"github.com/NethermindEth/juno/consensus/types"
5+
"github.com/NethermindEth/juno/consensus/types/actions"
56
"github.com/NethermindEth/juno/consensus/types/wal"
67
)
78

8-
func (s *stateMachine[V, H, A]) ProcessStart(round types.Round) []types.Action[V, H, A] {
9+
func (s *stateMachine[V, H, A]) ProcessStart(round types.Round) []actions.Action[V, H, A] {
910
return s.processLoop(s.startRound(round), nil)
1011
}
1112

12-
func (s *stateMachine[V, H, A]) ProcessProposal(p *types.Proposal[V, H, A]) []types.Action[V, H, A] {
13+
func (s *stateMachine[V, H, A]) ProcessProposal(p *types.Proposal[V, H, A]) []actions.Action[V, H, A] {
1314
return s.processMessage(p.MessageHeader, func() {
1415
if s.voteCounter.AddProposal(p) && !s.replayMode && p.Height == s.state.height {
1516
// Store proposal if its the first time we see it
@@ -20,7 +21,7 @@ func (s *stateMachine[V, H, A]) ProcessProposal(p *types.Proposal[V, H, A]) []ty
2021
})
2122
}
2223

23-
func (s *stateMachine[V, H, A]) ProcessPrevote(p *types.Prevote[H, A]) []types.Action[V, H, A] {
24+
func (s *stateMachine[V, H, A]) ProcessPrevote(p *types.Prevote[H, A]) []actions.Action[V, H, A] {
2425
return s.processMessage(p.MessageHeader, func() {
2526
if s.voteCounter.AddPrevote(p) && !s.replayMode && p.Height == s.state.height {
2627
// Store prevote if its the first time we see it
@@ -31,7 +32,7 @@ func (s *stateMachine[V, H, A]) ProcessPrevote(p *types.Prevote[H, A]) []types.A
3132
})
3233
}
3334

34-
func (s *stateMachine[V, H, A]) ProcessPrecommit(p *types.Precommit[H, A]) []types.Action[V, H, A] {
35+
func (s *stateMachine[V, H, A]) ProcessPrecommit(p *types.Precommit[H, A]) []actions.Action[V, H, A] {
3536
return s.processMessage(p.MessageHeader, func() {
3637
if s.voteCounter.AddPrecommit(p) && !s.replayMode && p.Height == s.state.height {
3738
// Store precommit if its the first time we see it
@@ -42,15 +43,15 @@ func (s *stateMachine[V, H, A]) ProcessPrecommit(p *types.Precommit[H, A]) []typ
4243
})
4344
}
4445

45-
func (s *stateMachine[V, H, A]) processMessage(header types.MessageHeader[A], addMessage func()) []types.Action[V, H, A] {
46+
func (s *stateMachine[V, H, A]) processMessage(header types.MessageHeader[A], addMessage func()) []actions.Action[V, H, A] {
4647
if !s.preprocessMessage(header, addMessage) {
4748
return nil
4849
}
4950

5051
return s.processLoop(nil, &header.Round)
5152
}
5253

53-
func (s *stateMachine[V, H, A]) ProcessTimeout(tm types.Timeout) []types.Action[V, H, A] {
54+
func (s *stateMachine[V, H, A]) ProcessTimeout(tm types.Timeout) []actions.Action[V, H, A] {
5455
if !s.replayMode && tm.Height == s.state.height {
5556
if err := s.db.SetWALEntry((*wal.WALTimeout)(&tm)); err != nil {
5657
s.log.Fatalf("Failed to store timeout trigger in WAL")
@@ -68,8 +69,8 @@ func (s *stateMachine[V, H, A]) ProcessTimeout(tm types.Timeout) []types.Action[
6869
return nil
6970
}
7071

71-
func (s *stateMachine[V, H, A]) processLoop(action types.Action[V, H, A], recentlyReceivedRound *types.Round) []types.Action[V, H, A] {
72-
actions, shouldContinue := []types.Action[V, H, A]{}, true
72+
func (s *stateMachine[V, H, A]) processLoop(action actions.Action[V, H, A], recentlyReceivedRound *types.Round) []actions.Action[V, H, A] {
73+
actions, shouldContinue := []actions.Action[V, H, A]{}, true
7374
if action != nil {
7475
actions = append(actions, action)
7576
}
@@ -84,7 +85,7 @@ func (s *stateMachine[V, H, A]) processLoop(action types.Action[V, H, A], recent
8485
return actions
8586
}
8687

87-
func (s *stateMachine[V, H, A]) process(recentlyReceivedRound *types.Round) (action types.Action[V, H, A], shouldContinue bool) {
88+
func (s *stateMachine[V, H, A]) process(recentlyReceivedRound *types.Round) (action actions.Action[V, H, A], shouldContinue bool) {
8889
cachedProposal := s.findProposal(s.state.round)
8990

9091
roundCachedProposal := cachedProposal

consensus/tendermint/rule_commit_value.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package tendermint
22

33
import (
4-
"github.com/NethermindEth/juno/consensus/types"
4+
"github.com/NethermindEth/juno/consensus/types/actions"
55
"github.com/NethermindEth/juno/consensus/votecounter"
66
)
77

@@ -31,13 +31,13 @@ func (s *stateMachine[V, H, A]) uponCommitValue(cachedProposal *CachedProposal[V
3131
return hasQuorum && isValid
3232
}
3333

34-
func (s *stateMachine[V, H, A]) doCommitValue(cachedProposal *CachedProposal[V, H, A]) types.Action[V, H, A] {
34+
func (s *stateMachine[V, H, A]) doCommitValue(cachedProposal *CachedProposal[V, H, A]) actions.Action[V, H, A] {
3535
s.voteCounter.StartNewHeight()
3636
s.state.height++
3737
s.state.lockedRound = -1
3838
s.state.lockedValue = nil
3939
s.state.validRound = -1
4040
s.state.validValue = nil
4141
s.resetState(0)
42-
return (*types.Commit[V, H, A])(&cachedProposal.Proposal)
42+
return (*actions.Commit[V, H, A])(&cachedProposal.Proposal)
4343
}

consensus/tendermint/rule_first_proposal.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package tendermint
22

3-
import "github.com/NethermindEth/juno/consensus/types"
3+
import (
4+
"github.com/NethermindEth/juno/consensus/types"
5+
"github.com/NethermindEth/juno/consensus/types/actions"
6+
)
47

58
/*
69
Check the upon condition on line 22:
@@ -18,7 +21,7 @@ func (s *stateMachine[V, H, A]) uponFirstProposal(cachedProposal *CachedProposal
1821
return cachedProposal.ValidRound == -1 && s.state.step == types.StepPropose
1922
}
2023

21-
func (s *stateMachine[V, H, A]) doFirstProposal(cachedProposal *CachedProposal[V, H, A]) types.Action[V, H, A] {
24+
func (s *stateMachine[V, H, A]) doFirstProposal(cachedProposal *CachedProposal[V, H, A]) actions.Action[V, H, A] {
2225
shouldVoteForValue := cachedProposal.Valid &&
2326
(s.state.lockedRound == -1 ||
2427
s.state.lockedValue != nil && (*s.state.lockedValue).Hash() == *cachedProposal.ID)

0 commit comments

Comments
 (0)