Skip to content

Commit 1cf9c27

Browse files
authored
refactor: Rename pointer receivers for stateMachine (#3015)
1 parent 3300775 commit 1cf9c27

12 files changed

Lines changed: 170 additions & 170 deletions

consensus/tendermint/broadcast.go

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,54 +5,54 @@ import (
55
"github.com/NethermindEth/juno/utils"
66
)
77

8-
func (t *stateMachine[V, H, A]) sendProposal(value *V) types.Action[V, H, A] {
8+
func (s *stateMachine[V, H, A]) sendProposal(value *V) types.Action[V, H, A] {
99
proposalMessage := types.Proposal[V, H, A]{
1010
MessageHeader: types.MessageHeader[A]{
11-
Height: t.state.height,
12-
Round: t.state.round,
13-
Sender: t.nodeAddr,
11+
Height: s.state.height,
12+
Round: s.state.round,
13+
Sender: s.nodeAddr,
1414
},
15-
ValidRound: t.state.validRound,
15+
ValidRound: s.state.validRound,
1616
Value: value,
1717
}
1818

19-
if err := t.db.SetWALEntry(proposalMessage); err != nil && !t.replayMode {
20-
t.log.Fatalf("Failed to store propsal in WAL")
19+
if err := s.db.SetWALEntry(proposalMessage); err != nil && !s.replayMode {
20+
s.log.Fatalf("Failed to store propsal in WAL")
2121
}
2222

23-
t.voteCounter.AddProposal(&proposalMessage)
23+
s.voteCounter.AddProposal(&proposalMessage)
2424

2525
return utils.HeapPtr(types.BroadcastProposal[V, H, A](proposalMessage))
2626
}
2727

28-
func (t *stateMachine[V, H, A]) setStepAndSendPrevote(id *H) types.Action[V, H, A] {
28+
func (s *stateMachine[V, H, A]) setStepAndSendPrevote(id *H) types.Action[V, H, A] {
2929
vote := types.Prevote[H, A]{
3030
MessageHeader: types.MessageHeader[A]{
31-
Height: t.state.height,
32-
Round: t.state.round,
33-
Sender: t.nodeAddr,
31+
Height: s.state.height,
32+
Round: s.state.round,
33+
Sender: s.nodeAddr,
3434
},
3535
ID: id,
3636
}
3737

38-
t.voteCounter.AddPrevote(&vote)
39-
t.state.step = types.StepPrevote
38+
s.voteCounter.AddPrevote(&vote)
39+
s.state.step = types.StepPrevote
4040

4141
return utils.HeapPtr(types.BroadcastPrevote[H, A](vote))
4242
}
4343

44-
func (t *stateMachine[V, H, A]) setStepAndSendPrecommit(id *H) types.Action[V, H, A] {
44+
func (s *stateMachine[V, H, A]) setStepAndSendPrecommit(id *H) types.Action[V, H, A] {
4545
vote := types.Precommit[H, A]{
4646
MessageHeader: types.MessageHeader[A]{
47-
Height: t.state.height,
48-
Round: t.state.round,
49-
Sender: t.nodeAddr,
47+
Height: s.state.height,
48+
Round: s.state.round,
49+
Sender: s.nodeAddr,
5050
},
5151
ID: id,
5252
}
5353

54-
t.voteCounter.AddPrecommit(&vote)
55-
t.state.step = types.StepPrecommit
54+
s.voteCounter.AddPrecommit(&vote)
55+
s.state.step = types.StepPrecommit
5656

5757
return utils.HeapPtr(types.BroadcastPrecommit[H, A](vote))
5858
}

consensus/tendermint/process.go

Lines changed: 48 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -2,77 +2,77 @@ package tendermint
22

33
import "github.com/NethermindEth/juno/consensus/types"
44

5-
func (t *stateMachine[V, H, A]) ProcessStart(round types.Round) []types.Action[V, H, A] {
6-
return t.processLoop(t.startRound(round), nil)
5+
func (s *stateMachine[V, H, A]) ProcessStart(round types.Round) []types.Action[V, H, A] {
6+
return s.processLoop(s.startRound(round), nil)
77
}
88

9-
func (t *stateMachine[V, H, A]) ProcessProposal(p *types.Proposal[V, H, A]) []types.Action[V, H, A] {
10-
return t.processMessage(p.MessageHeader, func() {
11-
if t.voteCounter.AddProposal(p) && !t.replayMode && p.Height == t.state.height {
9+
func (s *stateMachine[V, H, A]) ProcessProposal(p *types.Proposal[V, H, A]) []types.Action[V, H, A] {
10+
return s.processMessage(p.MessageHeader, func() {
11+
if s.voteCounter.AddProposal(p) && !s.replayMode && p.Height == s.state.height {
1212
// Store proposal if its the first time we see it
13-
if err := t.db.SetWALEntry(p); err != nil {
14-
t.log.Fatalf("Failed to store prevote in WAL")
13+
if err := s.db.SetWALEntry(p); err != nil {
14+
s.log.Fatalf("Failed to store prevote in WAL")
1515
}
1616
}
1717
})
1818
}
1919

20-
func (t *stateMachine[V, H, A]) ProcessPrevote(p *types.Prevote[H, A]) []types.Action[V, H, A] {
21-
return t.processMessage(p.MessageHeader, func() {
22-
if t.voteCounter.AddPrevote(p) && !t.replayMode && p.Height == t.state.height {
20+
func (s *stateMachine[V, H, A]) ProcessPrevote(p *types.Prevote[H, A]) []types.Action[V, H, A] {
21+
return s.processMessage(p.MessageHeader, func() {
22+
if s.voteCounter.AddPrevote(p) && !s.replayMode && p.Height == s.state.height {
2323
// Store prevote if its the first time we see it
24-
if err := t.db.SetWALEntry(p); err != nil {
25-
t.log.Fatalf("Failed to store prevote in WAL")
24+
if err := s.db.SetWALEntry(p); err != nil {
25+
s.log.Fatalf("Failed to store prevote in WAL")
2626
}
2727
}
2828
})
2929
}
3030

31-
func (t *stateMachine[V, H, A]) ProcessPrecommit(p *types.Precommit[H, A]) []types.Action[V, H, A] {
32-
return t.processMessage(p.MessageHeader, func() {
33-
if t.voteCounter.AddPrecommit(p) && !t.replayMode && p.Height == t.state.height {
31+
func (s *stateMachine[V, H, A]) ProcessPrecommit(p *types.Precommit[H, A]) []types.Action[V, H, A] {
32+
return s.processMessage(p.MessageHeader, func() {
33+
if s.voteCounter.AddPrecommit(p) && !s.replayMode && p.Height == s.state.height {
3434
// Store precommit if its the first time we see it
35-
if err := t.db.SetWALEntry(p); err != nil {
36-
t.log.Fatalf("Failed to store prevote in WAL")
35+
if err := s.db.SetWALEntry(p); err != nil {
36+
s.log.Fatalf("Failed to store prevote in WAL")
3737
}
3838
}
3939
})
4040
}
4141

42-
func (t *stateMachine[V, H, A]) processMessage(header types.MessageHeader[A], addMessage func()) []types.Action[V, H, A] {
43-
if !t.preprocessMessage(header, addMessage) {
42+
func (s *stateMachine[V, H, A]) processMessage(header types.MessageHeader[A], addMessage func()) []types.Action[V, H, A] {
43+
if !s.preprocessMessage(header, addMessage) {
4444
return nil
4545
}
4646

47-
return t.processLoop(nil, &header.Round)
47+
return s.processLoop(nil, &header.Round)
4848
}
4949

50-
func (t *stateMachine[V, H, A]) ProcessTimeout(tm types.Timeout) []types.Action[V, H, A] {
51-
if !t.replayMode && tm.Height == t.state.height {
52-
if err := t.db.SetWALEntry(tm); err != nil {
53-
t.log.Fatalf("Failed to store timeout trigger in WAL")
50+
func (s *stateMachine[V, H, A]) ProcessTimeout(tm types.Timeout) []types.Action[V, H, A] {
51+
if !s.replayMode && tm.Height == s.state.height {
52+
if err := s.db.SetWALEntry(tm); err != nil {
53+
s.log.Fatalf("Failed to store timeout trigger in WAL")
5454
}
5555
}
5656
switch tm.Step {
5757
case types.StepPropose:
58-
return t.processLoop(t.onTimeoutPropose(tm.Height, tm.Round), nil)
58+
return s.processLoop(s.onTimeoutPropose(tm.Height, tm.Round), nil)
5959
case types.StepPrevote:
60-
return t.processLoop(t.onTimeoutPrevote(tm.Height, tm.Round), nil)
60+
return s.processLoop(s.onTimeoutPrevote(tm.Height, tm.Round), nil)
6161
case types.StepPrecommit:
62-
return t.processLoop(t.onTimeoutPrecommit(tm.Height, tm.Round), nil)
62+
return s.processLoop(s.onTimeoutPrecommit(tm.Height, tm.Round), nil)
6363
}
6464

6565
return nil
6666
}
6767

68-
func (t *stateMachine[V, H, A]) processLoop(action types.Action[V, H, A], recentlyReceivedRound *types.Round) []types.Action[V, H, A] {
68+
func (s *stateMachine[V, H, A]) processLoop(action types.Action[V, H, A], recentlyReceivedRound *types.Round) []types.Action[V, H, A] {
6969
actions, shouldContinue := []types.Action[V, H, A]{}, true
7070
if action != nil {
7171
actions = append(actions, action)
7272
}
7373

7474
for shouldContinue {
75-
action, shouldContinue = t.process(recentlyReceivedRound)
75+
action, shouldContinue = s.process(recentlyReceivedRound)
7676
if action != nil {
7777
actions = append(actions, action)
7878
}
@@ -81,46 +81,46 @@ func (t *stateMachine[V, H, A]) processLoop(action types.Action[V, H, A], recent
8181
return actions
8282
}
8383

84-
func (t *stateMachine[V, H, A]) process(recentlyReceivedRound *types.Round) (action types.Action[V, H, A], shouldContinue bool) {
85-
cachedProposal := t.findProposal(t.state.round)
84+
func (s *stateMachine[V, H, A]) process(recentlyReceivedRound *types.Round) (action types.Action[V, H, A], shouldContinue bool) {
85+
cachedProposal := s.findProposal(s.state.round)
8686

8787
var roundCachedProposal *CachedProposal[V, H, A]
8888
if recentlyReceivedRound != nil {
89-
roundCachedProposal = t.findProposal(*recentlyReceivedRound)
89+
roundCachedProposal = s.findProposal(*recentlyReceivedRound)
9090
}
9191

9292
switch {
9393
// Line 22
94-
case cachedProposal != nil && t.uponFirstProposal(cachedProposal):
95-
return t.doFirstProposal(cachedProposal), true
94+
case cachedProposal != nil && s.uponFirstProposal(cachedProposal):
95+
return s.doFirstProposal(cachedProposal), true
9696

9797
// Line 28
98-
case cachedProposal != nil && t.uponProposalAndPolkaPrevious(cachedProposal):
99-
return t.doProposalAndPolkaPrevious(cachedProposal), true
98+
case cachedProposal != nil && s.uponProposalAndPolkaPrevious(cachedProposal):
99+
return s.doProposalAndPolkaPrevious(cachedProposal), true
100100

101101
// Line 34
102-
case t.uponPolkaAny():
103-
return t.doPolkaAny(), true
102+
case s.uponPolkaAny():
103+
return s.doPolkaAny(), true
104104

105105
// Line 36
106-
case cachedProposal != nil && t.uponProposalAndPolkaCurrent(cachedProposal):
107-
return t.doProposalAndPolkaCurrent(cachedProposal), true
106+
case cachedProposal != nil && s.uponProposalAndPolkaCurrent(cachedProposal):
107+
return s.doProposalAndPolkaCurrent(cachedProposal), true
108108

109109
// Line 44
110-
case t.uponPolkaNil():
111-
return t.doPolkaNil(), true
110+
case s.uponPolkaNil():
111+
return s.doPolkaNil(), true
112112

113113
// Line 47
114-
case t.uponPrecommitAny():
115-
return t.doPrecommitAny(), true
114+
case s.uponPrecommitAny():
115+
return s.doPrecommitAny(), true
116116

117117
// Line 49
118-
case roundCachedProposal != nil && t.uponCommitValue(roundCachedProposal):
119-
return t.doCommitValue(roundCachedProposal), false // We should stop immediately after committing
118+
case roundCachedProposal != nil && s.uponCommitValue(roundCachedProposal):
119+
return s.doCommitValue(roundCachedProposal), false // We should stop immediately after committing
120120

121121
// Line 55
122-
case recentlyReceivedRound != nil && t.uponSkipRound(*recentlyReceivedRound):
123-
return t.doSkipRound(*recentlyReceivedRound), true
122+
case recentlyReceivedRound != nil && s.uponSkipRound(*recentlyReceivedRound):
123+
return s.doSkipRound(*recentlyReceivedRound), true
124124

125125
default:
126126
return nil, false // We should stop if none of the above conditions are met

consensus/tendermint/rule_commit_value.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ height and round.
2121
There is no need to check decision_p[h_p] = nil since it is implied that decision are made
2222
sequentially, i.e. x, x+1, x+2... .
2323
*/
24-
func (t *stateMachine[V, H, A]) uponCommitValue(cachedProposal *CachedProposal[V, H, A]) bool {
25-
hasQuorum := cachedProposal.ID != nil && t.voteCounter.HasQuorumForVote(cachedProposal.Round, votecounter.Precommit, cachedProposal.ID)
24+
func (s *stateMachine[V, H, A]) uponCommitValue(cachedProposal *CachedProposal[V, H, A]) bool {
25+
hasQuorum := cachedProposal.ID != nil && s.voteCounter.HasQuorumForVote(cachedProposal.Round, votecounter.Precommit, cachedProposal.ID)
2626

2727
// This is checked here instead of inside execution, because it's the only case in execution in this rule
2828
isValid := cachedProposal.Valid
@@ -31,13 +31,13 @@ func (t *stateMachine[V, H, A]) uponCommitValue(cachedProposal *CachedProposal[V
3131
return hasQuorum && isValid
3232
}
3333

34-
func (t *stateMachine[V, H, A]) doCommitValue(cachedProposal *CachedProposal[V, H, A]) types.Action[V, H, A] {
35-
t.voteCounter.StartNewHeight()
36-
t.state.height++
37-
t.state.lockedRound = -1
38-
t.state.lockedValue = nil
39-
t.state.validRound = -1
40-
t.state.validValue = nil
41-
t.resetState(0)
34+
func (s *stateMachine[V, H, A]) doCommitValue(cachedProposal *CachedProposal[V, H, A]) types.Action[V, H, A] {
35+
s.voteCounter.StartNewHeight()
36+
s.state.height++
37+
s.state.lockedRound = -1
38+
s.state.lockedValue = nil
39+
s.state.validRound = -1
40+
s.state.validValue = nil
41+
s.resetState(0)
4242
return (*types.Commit[V, H, A])(&cachedProposal.Proposal)
4343
}

consensus/tendermint/rule_first_proposal.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,18 @@ Check the upon condition on line 22:
1414
1515
Since the value's id is expected to be unique the id can be used to compare the values.
1616
*/
17-
func (t *stateMachine[V, H, A]) uponFirstProposal(cachedProposal *CachedProposal[V, H, A]) bool {
18-
return cachedProposal.ValidRound == -1 && t.state.step == types.StepPropose
17+
func (s *stateMachine[V, H, A]) uponFirstProposal(cachedProposal *CachedProposal[V, H, A]) bool {
18+
return cachedProposal.ValidRound == -1 && s.state.step == types.StepPropose
1919
}
2020

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

2626
var votedID *H
2727
if shouldVoteForValue {
2828
votedID = cachedProposal.ID
2929
}
30-
return t.setStepAndSendPrevote(votedID)
30+
return s.setStepAndSendPrevote(votedID)
3131
}

consensus/tendermint/rule_polka_any.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,17 @@ Check the upon condition on line 34:
1111
34: upon 2f + 1 {PREVOTE, h_p, round_p, ∗} while step_p = prevote for the first time do
1212
35: schedule OnTimeoutPrevote(h_p, round_p) to be executed after timeoutPrevote(round_p)
1313
*/
14-
func (t *stateMachine[V, H, A]) uponPolkaAny() bool {
15-
isFirstTime := !t.state.timeoutPrevoteScheduled
14+
func (s *stateMachine[V, H, A]) uponPolkaAny() bool {
15+
isFirstTime := !s.state.timeoutPrevoteScheduled
1616

17-
hasQuorum := t.voteCounter.HasQuorumForAny(t.state.round, votecounter.Prevote)
17+
hasQuorum := s.voteCounter.HasQuorumForAny(s.state.round, votecounter.Prevote)
1818

19-
return t.state.step == types.StepPrevote &&
19+
return s.state.step == types.StepPrevote &&
2020
hasQuorum &&
2121
isFirstTime
2222
}
2323

24-
func (t *stateMachine[V, H, A]) doPolkaAny() types.Action[V, H, A] {
25-
t.state.timeoutPrevoteScheduled = true
26-
return t.scheduleTimeout(types.StepPrevote)
24+
func (s *stateMachine[V, H, A]) doPolkaAny() types.Action[V, H, A] {
25+
s.state.timeoutPrevoteScheduled = true
26+
return s.scheduleTimeout(types.StepPrevote)
2727
}

consensus/tendermint/rule_polka_nil.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ Check the upon condition on line 44:
1414
1515
Line 36 and 44 for a round are mutually exclusive.
1616
*/
17-
func (t *stateMachine[V, H, A]) uponPolkaNil() bool {
18-
hasQuorum := t.voteCounter.HasQuorumForVote(t.state.round, votecounter.Prevote, nil)
17+
func (s *stateMachine[V, H, A]) uponPolkaNil() bool {
18+
hasQuorum := s.voteCounter.HasQuorumForVote(s.state.round, votecounter.Prevote, nil)
1919

20-
return hasQuorum && t.state.step == types.StepPrevote
20+
return hasQuorum && s.state.step == types.StepPrevote
2121
}
2222

23-
func (t *stateMachine[V, H, A]) doPolkaNil() types.Action[V, H, A] {
24-
return t.setStepAndSendPrecommit(nil)
23+
func (s *stateMachine[V, H, A]) doPolkaNil() types.Action[V, H, A] {
24+
return s.setStepAndSendPrecommit(nil)
2525
}

consensus/tendermint/rule_precommit_any.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ Check the upon condition on line 47:
1111
47: upon 2f + 1 {PRECOMMIT, h_p, round_p, ∗} for the first time do
1212
48: schedule OnTimeoutPrecommit(h_p , round_p) to be executed after timeoutPrecommit(round_p)
1313
*/
14-
func (t *stateMachine[V, H, A]) uponPrecommitAny() bool {
15-
isFirstTime := !t.state.timeoutPrecommitScheduled
14+
func (s *stateMachine[V, H, A]) uponPrecommitAny() bool {
15+
isFirstTime := !s.state.timeoutPrecommitScheduled
1616

17-
hasQuorum := t.voteCounter.HasQuorumForAny(t.state.round, votecounter.Precommit)
17+
hasQuorum := s.voteCounter.HasQuorumForAny(s.state.round, votecounter.Precommit)
1818

1919
return hasQuorum && isFirstTime
2020
}
2121

22-
func (t *stateMachine[V, H, A]) doPrecommitAny() types.Action[V, H, A] {
23-
t.state.timeoutPrecommitScheduled = true
24-
return t.scheduleTimeout(types.StepPrecommit)
22+
func (s *stateMachine[V, H, A]) doPrecommitAny() types.Action[V, H, A] {
23+
s.state.timeoutPrecommitScheduled = true
24+
return s.scheduleTimeout(types.StepPrecommit)
2525
}

0 commit comments

Comments
 (0)