Skip to content

Commit

Permalink
only use precommitements for active peer connections
Browse files Browse the repository at this point in the history
  • Loading branch information
cedricfung committed Oct 7, 2024
1 parent d7caee3 commit 50fed80
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 17 deletions.
34 changes: 18 additions & 16 deletions kernel/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ type Chain struct {
CosiCommitments map[crypto.Hash][]*crypto.Key
UsedCommitments map[crypto.Key]bool
ComitmentsSentTime time.Time
CosiCommunicatedAt map[crypto.Hash]time.Time

CosiAggregators map[crypto.Hash]*CosiAggregator
CosiVerifiers map[crypto.Hash]*CosiVerifier
Expand All @@ -78,22 +79,23 @@ func (node *Node) buildChain(chainId crypto.Hash) *Chain {
logger.Printf("node.buildChain(%s)", chainId)

chain := &Chain{
node: node,
ChainId: chainId,
CosiRandoms: make(map[crypto.Key]*crypto.Key),
UsedRandoms: make(map[crypto.Hash]*crypto.Key),
CosiCommitments: make(map[crypto.Hash][]*crypto.Key),
UsedCommitments: make(map[crypto.Key]bool),
CosiAggregators: make(map[crypto.Hash]*CosiAggregator),
CosiVerifiers: make(map[crypto.Hash]*CosiVerifier),
CachePool: make(chan *CosiAction, CachePoolSnapshotsLimit),
persistStore: node.persistStore,
finalActionsRing: make(chan *CosiAction, FinalPoolSlotsLimit),
plc: make(chan struct{}),
clc: make(chan struct{}),
wlc: make(chan struct{}),
slc: make(chan struct{}),
running: false,
node: node,
ChainId: chainId,
CosiRandoms: make(map[crypto.Key]*crypto.Key),
UsedRandoms: make(map[crypto.Hash]*crypto.Key),
CosiCommitments: make(map[crypto.Hash][]*crypto.Key),
UsedCommitments: make(map[crypto.Key]bool),
CosiCommunicatedAt: make(map[crypto.Hash]time.Time),
CosiAggregators: make(map[crypto.Hash]*CosiAggregator),
CosiVerifiers: make(map[crypto.Hash]*CosiVerifier),
CachePool: make(chan *CosiAction, CachePoolSnapshotsLimit),
persistStore: node.persistStore,
finalActionsRing: make(chan *CosiAction, FinalPoolSlotsLimit),
plc: make(chan struct{}),
clc: make(chan struct{}),
wlc: make(chan struct{}),
slc: make(chan struct{}),
running: false,
}

err := chain.loadState()
Expand Down
6 changes: 5 additions & 1 deletion kernel/cosi.go
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ func (chain *Chain) cosiSendAnnouncement(m *CosiAction) error {
continue
}
commitment := chain.cosiPopCommitment(peerId)
if commitment == nil {
if commitment == nil || chain.CosiCommunicatedAt[peerId].Before(clock.Now().Add(-time.Duration(config.SnapshotRoundGap)*10)) {
err := chain.node.Peer.SendSnapshotAnnouncementMessage(peerId, m.Snapshot, R, chain.node.Signer.PrivateSpendKey)
if err != nil {
logger.Verbosef("cosiSendAnnouncement SendSnapshotAnnouncementMessage(%s, %s) ERROR %v\n",
Expand Down Expand Up @@ -425,6 +425,7 @@ func (chain *Chain) cosiHandleAnnouncement(m *CosiAction) error {
if err != nil || !valid {
return err
}
chain.CosiCommunicatedAt[m.PeerId] = clock.Now()

s, cd := m.Snapshot, m.data
r := crypto.CosiCommit(crypto.RandReader())
Expand Down Expand Up @@ -613,6 +614,7 @@ func (chain *Chain) cosiHandleChallenge(m *CosiAction) error {
m, sig, challenge)
return nil
}
chain.CosiCommunicatedAt[m.PeerId] = clock.Now()

priv := chain.node.Signer.PrivateSpendKey
response, err := m.Signature.Response(&priv, v.random, publics, m.SnapshotHash)
Expand All @@ -636,6 +638,7 @@ func (chain *Chain) cosiHandleResponse(m *CosiAction) error {
logger.Verbosef("cosiHandleResponse %v REPEAT\n", m)
return nil
}
chain.CosiCommunicatedAt[m.PeerId] = clock.Now()
if len(agg.Responses) >= len(agg.Commitments) {
logger.Verbosef("cosiHandleResponse %v EXCEED\n", m)
return nil
Expand Down Expand Up @@ -840,6 +843,7 @@ func (chain *Chain) cosiAddCommitments(m *CosiAction) error {
if rn := chain.node.GetRemovingOrSlashingNode(m.PeerId); rn != nil {
return nil
}
chain.CosiCommunicatedAt[m.PeerId] = clock.Now()
var commitments []*crypto.Key
for _, k := range m.Commitments {
if !chain.UsedCommitments[*k] {
Expand Down

0 comments on commit 50fed80

Please sign in to comment.