Skip to content

Commit 114c318

Browse files
committed
feat: Integrate Sync P2P protocol to consensus
1 parent 585741c commit 114c318

File tree

16 files changed

+280
-425
lines changed

16 files changed

+280
-425
lines changed

consensus/consensus.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@ import (
1212
"github.com/NethermindEth/juno/consensus/proposal"
1313
"github.com/NethermindEth/juno/consensus/proposer"
1414
"github.com/NethermindEth/juno/consensus/starknet"
15+
consensusSync "github.com/NethermindEth/juno/consensus/sync"
1516
"github.com/NethermindEth/juno/consensus/tendermint"
1617
"github.com/NethermindEth/juno/consensus/types"
1718
"github.com/NethermindEth/juno/consensus/votecounter"
1819
"github.com/NethermindEth/juno/core/felt"
1920
"github.com/NethermindEth/juno/db"
21+
"github.com/NethermindEth/juno/p2p/sync"
2022
"github.com/NethermindEth/juno/utils"
2123
"github.com/NethermindEth/juno/vm"
2224
"github.com/libp2p/go-libp2p/core/host"
@@ -59,13 +61,22 @@ func Init(
5961
p2p := p2p.New(host, logger, &builder, &proposalStore, currentHeight, &config.DefaultBufferSizes, bootstrapPeersFn)
6062

6163
commitListener := driver.NewCommitListener(logger, &proposalStore, proposer, p2p)
64+
65+
blockFetcher := sync.NewBlockFetcher(blockchain, host, logger)
66+
messageExtractor := consensusSync.New[starknet.Value, starknet.Hash, starknet.Address](
67+
toValue,
68+
&proposalStore,
69+
)
70+
6271
driver := driver.New(
6372
logger,
6473
tendermintDB,
6574
stateMachine,
6675
commitListener,
6776
p2p.Broadcasters(),
6877
p2p.Listeners(),
78+
&blockFetcher,
79+
&messageExtractor,
6980
timeoutFn,
7081
)
7182

consensus/driver/commit_listener.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ type CommitHook[V types.Hashable[H], H types.Hash] interface {
1515
}
1616

1717
// CommitListener is a component that is used to notify different components that a new committed block is available.
18-
//
19-
//go:generate mockgen -destination=../mocks/mock_commit_listener.go -package=mocks github.com/NethermindEth/juno/consensus/driver CommitListener
2018
type CommitListener[V types.Hashable[H], H types.Hash] interface {
2119
CommitHook[V, H]
2220
// Listen returns a channel that will receive committed blocks.

consensus/driver/driver.go

Lines changed: 48 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,31 @@ import (
77

88
"github.com/NethermindEth/juno/consensus/db"
99
"github.com/NethermindEth/juno/consensus/p2p"
10+
consensusSync "github.com/NethermindEth/juno/consensus/sync"
1011
"github.com/NethermindEth/juno/consensus/tendermint"
1112
"github.com/NethermindEth/juno/consensus/types"
1213
"github.com/NethermindEth/juno/consensus/types/actions"
14+
"github.com/NethermindEth/juno/p2p/sync"
1315
"github.com/NethermindEth/juno/utils"
1416
)
1517

1618
type TimeoutFn func(step types.Step, round types.Round) time.Duration
1719

1820
type Driver[V types.Hashable[H], H types.Hash, A types.Addr] struct {
19-
log utils.Logger
20-
db db.TendermintDB[V, H, A]
21-
stateMachine tendermint.StateMachine[V, H, A]
22-
commitListener CommitListener[V, H]
23-
broadcasters p2p.Broadcasters[V, H, A]
24-
listeners p2p.Listeners[V, H, A]
21+
log utils.Logger
22+
db db.TendermintDB[V, H, A]
23+
stateMachine tendermint.StateMachine[V, H, A]
24+
commitListener CommitListener[V, H]
25+
broadcasters p2p.Broadcasters[V, H, A]
26+
listeners p2p.Listeners[V, H, A]
27+
blockFetcher *sync.BlockFetcher
28+
messageExtractor *consensusSync.MessageExtractor[V, H, A]
2529

2630
getTimeout TimeoutFn
2731

2832
scheduledTms map[types.Timeout]*time.Timer
2933
timeoutsCh chan types.Timeout
34+
syncListener chan sync.BlockBody
3035
}
3136

3237
func New[V types.Hashable[H], H types.Hash, A types.Addr](
@@ -36,18 +41,23 @@ func New[V types.Hashable[H], H types.Hash, A types.Addr](
3641
commitListener CommitListener[V, H],
3742
broadcasters p2p.Broadcasters[V, H, A],
3843
listeners p2p.Listeners[V, H, A],
44+
blockFetcher *sync.BlockFetcher,
45+
messageExtractor *consensusSync.MessageExtractor[V, H, A],
3946
getTimeout TimeoutFn,
4047
) Driver[V, H, A] {
4148
return Driver[V, H, A]{
42-
log: log,
43-
db: db,
44-
stateMachine: stateMachine,
45-
commitListener: commitListener,
46-
broadcasters: broadcasters,
47-
listeners: listeners,
48-
getTimeout: getTimeout,
49-
scheduledTms: make(map[types.Timeout]*time.Timer),
50-
timeoutsCh: make(chan types.Timeout),
49+
log: log,
50+
db: db,
51+
stateMachine: stateMachine,
52+
commitListener: commitListener,
53+
broadcasters: broadcasters,
54+
listeners: listeners,
55+
blockFetcher: blockFetcher,
56+
messageExtractor: messageExtractor,
57+
getTimeout: getTimeout,
58+
scheduledTms: make(map[types.Timeout]*time.Timer),
59+
timeoutsCh: make(chan types.Timeout),
60+
syncListener: make(chan sync.BlockBody),
5161
}
5262
}
5363

@@ -123,6 +133,12 @@ func (d *Driver[V, H, A]) listen(ctx context.Context) error {
123133
return nil
124134
}
125135
actions = d.stateMachine.ProcessPrecommit(p)
136+
case p, ok := <-d.syncListener:
137+
if !ok {
138+
return nil
139+
}
140+
proposal, precommits := d.messageExtractor.Extract(&p)
141+
actions = d.stateMachine.ProcessSync(&proposal, precommits)
126142
}
127143

128144
isCommitted, err = d.execute(ctx, false, actions)
@@ -180,14 +196,28 @@ func (d *Driver[V, H, A]) execute(
180196
return true, nil
181197

182198
case *actions.TriggerSync:
183-
d.triggerSync(*action)
199+
d.triggerSync(ctx, *action)
184200
}
185201
}
186202
return false, nil
187203
}
188204

189-
func (d *Driver[V, H, A]) triggerSync(sync actions.TriggerSync) {
190-
// TODO: Implement this
205+
func (d *Driver[V, H, A]) triggerSync(ctx context.Context, sync actions.TriggerSync) {
206+
for i := sync.Start; i <= sync.End; i++ {
207+
for {
208+
select {
209+
case <-ctx.Done():
210+
return
211+
default:
212+
}
213+
214+
if err := d.blockFetcher.ProcessBlock(ctx, uint64(i), d.syncListener); err != nil {
215+
d.log.Errorw("failed to process block", "block", i, "err", err)
216+
} else {
217+
break
218+
}
219+
}
220+
}
191221
}
192222

193223
func (d *Driver[V, H, A]) setTimeout(ctx context.Context, timeout types.Timeout) {

consensus/driver/driver_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,8 @@ func TestDriver(t *testing.T) {
178178
newMockCommitListener(t, &commitAction),
179179
broadcasters,
180180
listeners,
181+
nil, // TODO: Add tests for trigger sync
182+
nil, // TODO: Add tests for trigger sync
181183
mockTimeoutFn,
182184
)
183185

consensus/mock.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88

99
"github.com/NethermindEth/juno/consensus/driver"
1010
"github.com/NethermindEth/juno/consensus/starknet"
11+
consensusSync "github.com/NethermindEth/juno/consensus/sync"
1112
"github.com/NethermindEth/juno/consensus/types"
1213
"github.com/NethermindEth/juno/consensus/votecounter"
1314
"github.com/NethermindEth/juno/core/felt"
@@ -47,7 +48,14 @@ func (n mockValidators) TotalVotingPower(height types.Height) types.VotingPower
4748
return types.VotingPower(len(n))
4849
}
4950

51+
// Currently we mock one voting power for all validators, to be removed once
52+
// we can query the voting power from the staking contracts.
53+
// The special case is for precommits from sync protocol, to be removed once
54+
// we can extract precommits from the sync protocol messages.
5055
func (n mockValidators) ValidatorVotingPower(height types.Height, addr *starknet.Address) types.VotingPower {
56+
if addr != nil && *addr == consensusSync.SyncProtocolPrecommitSender {
57+
return types.VotingPower(len(n))
58+
}
5159
return types.VotingPower(1)
5260
}
5361

consensus/mocks/mock_application.go

Lines changed: 0 additions & 69 deletions
This file was deleted.

consensus/mocks/mock_commit_listener.go

Lines changed: 0 additions & 69 deletions
This file was deleted.

consensus/mocks/mock_state_machine.go

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

consensus/sync/mock_p2p_test.go

Lines changed: 0 additions & 31 deletions
This file was deleted.

0 commit comments

Comments
 (0)