Skip to content

Commit 4656ded

Browse files
authored
experiment: verify in channel (#215)
1 parent 2bbd1e0 commit 4656ded

File tree

2 files changed

+42
-18
lines changed

2 files changed

+42
-18
lines changed

node/consensus/master/broadcast_messaging.go

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -181,25 +181,16 @@ func (e *MasterClockConsensusEngine) handleSelfTestReport(
181181
for i := 0; i < len(proofs); i++ {
182182
proofs[i] = proof[i*516 : (i+1)*516]
183183
}
184-
if !e.frameProver.VerifyChallengeProof(
185-
challenge,
186-
int64(timestamp),
187-
report.DifficultyMetric,
188-
proofs,
189-
) {
190-
e.logger.Warn(
191-
"received invalid proof from peer",
192-
zap.String("peer_id", peer.ID(peerID).String()),
193-
)
194-
e.pubSub.SetPeerScore(peerID, -1000)
195-
196-
return errors.Wrap(
197-
errors.New("invalid report"),
198-
"handle self test report",
199-
)
200-
}
184+
go func() {
185+
e.verifyTestCh <- verifyChallenge{
186+
peerID: peerID,
187+
challenge: challenge,
188+
timestamp: int64(timestamp),
189+
difficultyMetric: report.DifficultyMetric,
190+
proofs: proofs,
191+
}
192+
}()
201193

202-
info.LastSeen = time.Now().UnixMilli()
203194
return nil
204195
}
205196

node/consensus/master/master_clock_consensus_engine.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"sync"
1212
"time"
1313

14+
"github.com/libp2p/go-libp2p/core/peer"
1415
"github.com/mr-tron/base58"
1516
"github.com/pkg/errors"
1617
"go.uber.org/zap"
@@ -62,6 +63,7 @@ type MasterClockConsensusEngine struct {
6263
report *protobufs.SelfTestReport
6364
frameValidationCh chan *protobufs.ClockFrame
6465
bandwidthTestCh chan []byte
66+
verifyTestCh chan verifyChallenge
6567
currentReceivingSyncPeers int
6668
currentReceivingSyncPeersMx sync.Mutex
6769
}
@@ -126,6 +128,7 @@ func NewMasterClockConsensusEngine(
126128
report: report,
127129
frameValidationCh: make(chan *protobufs.ClockFrame),
128130
bandwidthTestCh: make(chan []byte),
131+
verifyTestCh: make(chan verifyChallenge),
129132
}
130133

131134
e.addPeerManifestReport(e.pubSub.GetPeerID(), report)
@@ -186,6 +189,8 @@ func (e *MasterClockConsensusEngine) Start() <-chan error {
186189
e.masterTimeReel.Insert(newFrame, false)
187190
case peerId := <-e.bandwidthTestCh:
188191
e.performBandwidthTest(peerId)
192+
case verifyTest := <-e.verifyTestCh:
193+
e.performVerifyTest(verifyTest)
189194
}
190195
}
191196
}()
@@ -355,6 +360,34 @@ func (e *MasterClockConsensusEngine) Stop(force bool) <-chan error {
355360
return errChan
356361
}
357362

363+
type verifyChallenge struct {
364+
peerID []byte
365+
challenge []byte
366+
timestamp int64
367+
difficultyMetric int64
368+
proofs [][]byte
369+
}
370+
371+
func (e *MasterClockConsensusEngine) performVerifyTest(
372+
challenge verifyChallenge,
373+
) {
374+
if !e.frameProver.VerifyChallengeProof(
375+
challenge.challenge,
376+
challenge.timestamp,
377+
challenge.difficultyMetric,
378+
challenge.proofs,
379+
) {
380+
e.logger.Warn(
381+
"received invalid proof from peer",
382+
zap.String("peer_id", peer.ID(challenge.peerID).String()),
383+
)
384+
e.pubSub.SetPeerScore(challenge.peerID, -1000)
385+
} else {
386+
info := e.peerInfoManager.GetPeerInfo(challenge.peerID)
387+
info.LastSeen = time.Now().UnixMilli()
388+
}
389+
}
390+
358391
func (e *MasterClockConsensusEngine) performBandwidthTest(peerID []byte) {
359392
result := e.pubSub.GetMultiaddrOfPeer(peerID)
360393
if result == "" {

0 commit comments

Comments
 (0)