11package driver
22
33import (
4- "sync "
4+ "context "
55 "time"
66
77 "github.com/NethermindEth/juno/consensus/db"
88 "github.com/NethermindEth/juno/consensus/p2p"
9+ "github.com/NethermindEth/juno/consensus/proposer"
910 "github.com/NethermindEth/juno/consensus/tendermint"
1011 "github.com/NethermindEth/juno/consensus/types"
1112 "github.com/NethermindEth/juno/utils"
@@ -23,39 +24,34 @@ type Driver[V types.Hashable[H], H types.Hash, A types.Addr] struct {
2324 db db.TendermintDB [V , H , A ]
2425 stateMachine tendermint.StateMachine [V , H , A ]
2526 blockchain Blockchain [V , H ]
27+ p2p p2p.P2P [V , H , A ]
28+ proposer proposer.Proposer [V , H ]
2629
2730 getTimeout timeoutFn
2831
29- listeners p2p.Listeners [V , H , A ]
30- broadcasters p2p.Broadcasters [V , H , A ]
31-
3232 scheduledTms map [types.Timeout ]* time.Timer
3333 timeoutsCh chan types.Timeout
34-
35- wg sync.WaitGroup
36- quit chan struct {}
3734}
3835
3936func New [V types.Hashable [H ], H types.Hash , A types.Addr ](
4037 log utils.Logger ,
4138 db db.TendermintDB [V , H , A ],
4239 stateMachine tendermint.StateMachine [V , H , A ],
4340 blockchain Blockchain [V , H ],
44- listeners p2p.Listeners [V , H , A ],
45- broadcasters p2p. Broadcasters [V , H , A ],
41+ p2p p2p.P2P [V , H , A ],
42+ proposer proposer. Proposer [V , H ],
4643 getTimeout timeoutFn ,
47- ) * Driver [V , H , A ] {
48- return & Driver [V , H , A ]{
44+ ) Driver [V , H , A ] {
45+ return Driver [V , H , A ]{
4946 log : log ,
5047 db : db ,
5148 stateMachine : stateMachine ,
5249 blockchain : blockchain ,
50+ p2p : p2p ,
51+ proposer : proposer ,
5352 getTimeout : getTimeout ,
54- listeners : listeners ,
55- broadcasters : broadcasters ,
5653 scheduledTms : make (map [types.Timeout ]* time.Timer ),
5754 timeoutsCh : make (chan types.Timeout ),
58- quit : make (chan struct {}),
5955 }
6056}
6157
@@ -64,60 +60,52 @@ func New[V types.Hashable[H], H types.Hash, A types.Addr](
6460// these messages and returns a set of actions to be executed by the Driver.
6561// The Driver executes these actions (namely broadcasting messages
6662// and triggering scheduled timeouts).
67- func (d * Driver [V , H , A ]) Start () {
63+ func (d * Driver [V , H , A ]) Run ( ctx context. Context ) error {
6864 d .stateMachine .ReplayWAL ()
6965
70- d .wg .Add (1 )
71- go func () {
72- defer d .wg .Done ()
73-
74- actions := d .stateMachine .ProcessStart (0 )
75- d .execute (actions )
66+ listeners := d .p2p .Listeners ()
67+ broadcasters := d .p2p .Broadcasters ()
7668
77- // Todo: check message signature everytime a message is received.
78- // For the time being it can be assumed the signature is correct.
69+ actions := d . stateMachine . ProcessStart ( 0 )
70+ d . execute ( ctx , broadcasters , actions )
7971
80- for {
81- select {
82- case <- d .quit :
83- return
84- case tm := <- d .timeoutsCh :
85- // Handling of timeouts is priorities over messages
86- delete (d .scheduledTms , tm )
87- actions = d .stateMachine .ProcessTimeout (tm )
88- case p := <- d .listeners .ProposalListener .Listen ():
89- actions = d .stateMachine .ProcessProposal (p )
90- case p := <- d .listeners .PrevoteListener .Listen ():
91- actions = d .stateMachine .ProcessPrevote (p )
92- case p := <- d .listeners .PrecommitListener .Listen ():
93- actions = d .stateMachine .ProcessPrecommit (p )
72+ // Todo: check message signature everytime a message is received.
73+ // For the time being it can be assumed the signature is correct.
74+ for {
75+ select {
76+ case <- ctx .Done ():
77+ for _ , tm := range d .scheduledTms {
78+ tm .Stop ()
9479 }
95- d .execute (actions )
80+ return nil
81+ case tm := <- d .timeoutsCh :
82+ // Handling of timeouts is priorities over messages
83+ delete (d .scheduledTms , tm )
84+ actions = d .stateMachine .ProcessTimeout (tm )
85+ case p := <- listeners .ProposalListener .Listen ():
86+ actions = d .stateMachine .ProcessProposal (p )
87+ case p := <- listeners .PrevoteListener .Listen ():
88+ actions = d .stateMachine .ProcessPrevote (p )
89+ case p := <- listeners .PrecommitListener .Listen ():
90+ actions = d .stateMachine .ProcessPrecommit (p )
9691 }
97- }()
98- }
99-
100- func (d * Driver [V , H , A ]) Stop () {
101- close (d .quit )
102- d .wg .Wait ()
103- for _ , tm := range d .scheduledTms {
104- tm .Stop ()
92+ d .execute (ctx , broadcasters , actions )
10593 }
10694}
10795
108- func (d * Driver [V , H , A ]) execute (actions []types.Action [V , H , A ]) {
96+ func (d * Driver [V , H , A ]) execute (ctx context. Context , broadcasters p2p. Broadcasters [ V , H , A ], actions []types.Action [V , H , A ]) {
10997 for _ , action := range actions {
11098 switch action := action .(type ) {
11199 case * types.BroadcastProposal [V , H , A ]:
112- d . broadcasters .ProposalBroadcaster .Broadcast (types.Proposal [V , H , A ](* action ))
100+ broadcasters .ProposalBroadcaster .Broadcast (types.Proposal [V , H , A ](* action ))
113101 case * types.BroadcastPrevote [H , A ]:
114- d . broadcasters .PrevoteBroadcaster .Broadcast (types.Prevote [H , A ](* action ))
102+ broadcasters .PrevoteBroadcaster .Broadcast (types.Prevote [H , A ](* action ))
115103 case * types.BroadcastPrecommit [H , A ]:
116- d . broadcasters .PrecommitBroadcaster .Broadcast (types.Precommit [H , A ](* action ))
104+ broadcasters .PrecommitBroadcaster .Broadcast (types.Precommit [H , A ](* action ))
117105 case * types.ScheduleTimeout :
118106 d .scheduledTms [types .Timeout (* action )] = time .AfterFunc (d .getTimeout (action .Step , action .Round ), func () {
119107 select {
120- case <- d . quit :
108+ case <- ctx . Done () :
121109 case d .timeoutsCh <- types .Timeout (* action ):
122110 }
123111 })
@@ -126,7 +114,10 @@ func (d *Driver[V, H, A]) execute(actions []types.Action[V, H, A]) {
126114 d .log .Fatalf ("failed to flush WAL during commit" , "height" , action .Height , "round" , action .Round , "err" , err )
127115 }
128116
117+ d .log .Debugw ("Committing" , "height" , action .Height , "round" , action .Round )
129118 d .blockchain .Commit (action .Height , * action .Value )
119+ d .proposer .OnCommit (ctx , action .Height , * action .Value )
120+ d .p2p .OnCommit (ctx , action .Height , * action .Value )
130121
131122 if err := d .db .DeleteWALEntries (action .Height ); err != nil {
132123 d .log .Errorw ("failed to delete WAL messages during commit" , "height" , action .Height , "round" , action .Round , "err" , err )
0 commit comments