Skip to content

Commit

Permalink
monitor: fix comments
Browse files Browse the repository at this point in the history
  • Loading branch information
j75689 committed Feb 3, 2023
1 parent a659064 commit 2464933
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 31 deletions.
8 changes: 3 additions & 5 deletions core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -2583,20 +2583,18 @@ func (bc *BlockChain) trustedDiffLayerLoop() {
func (bc *BlockChain) startDoubleSignMonitor() {
eventChan := make(chan ChainHeadEvent, monitor.MaxCacheHeader)
sub := bc.SubscribeChainHeadEvent(eventChan)
headerChan := make(chan *types.Header, monitor.MaxCacheHeader)
defer func() {
sub.Unsubscribe()
bc.doubleSignMonitor.Close()
close(eventChan)
close(headerChan)
bc.wg.Done()
}()

go bc.doubleSignMonitor.Start(headerChan)
for {
select {
case event := <-eventChan:
headerChan <- event.Block.Header()
if bc.doubleSignMonitor != nil {
bc.doubleSignMonitor.Verify(event.Block.Header())
}
case <-bc.quit:
return
}
Expand Down
40 changes: 14 additions & 26 deletions core/monitor/double_sign_mointor.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,12 @@ func NewDoubleSignMonitor() *DoubleSignMonitor {
return &DoubleSignMonitor{
headerNumbers: prque.New(nil),
headers: make(map[uint64]*types.Header, MaxCacheHeader),
quit: make(chan struct{}),
}
}

type DoubleSignMonitor struct {
headerNumbers *prque.Prque
headers map[uint64]*types.Header
quit chan struct{}
}

func (m *DoubleSignMonitor) isDoubleSignHeaders(h1, h2 *types.Header) (bool, error) {
Expand All @@ -37,11 +35,12 @@ func (m *DoubleSignMonitor) isDoubleSignHeaders(h1, h2 *types.Header) (bool, err
return false, nil
}
// if the Hash is different the signature should not be equal
if bytes.Equal(h1.Hash().Bytes(), h2.Hash().Bytes()) {
hash1, hash2 := h1.Hash(), h2.Hash()
if bytes.Equal(hash1[:], hash2[:]) {
return false, nil
}
// signer is already verified in sync program, we can trust coinbase.
if !bytes.Equal(h1.Coinbase.Bytes(), h2.Coinbase.Bytes()) {
if !bytes.Equal(h1.Coinbase[:], h2.Coinbase[:]) {
return false, nil
}

Expand Down Expand Up @@ -76,27 +75,16 @@ func (m *DoubleSignMonitor) checkHeader(h *types.Header) (bool, *types.Header, e
return false, nil, nil
}

func (m *DoubleSignMonitor) Start(ch <-chan *types.Header) {
for {
select {
case h := <-ch:
isDoubleSign, h2, err := m.checkHeader(h)
if err != nil {
log.Error("check double sign header error", "err", err)
continue
}
if isDoubleSign {
// found a double sign header
log.Error("found a double sign header", "number", h.Number.Uint64(),
"first_hash", h.Hash(), "first_miner", h.Coinbase,
"second_hash", h2.Hash(), "second_miner", h2.Coinbase)
}
case <-m.quit:
return
}
func (m *DoubleSignMonitor) Verify(h *types.Header) {
isDoubleSign, h2, err := m.checkHeader(h)
if err != nil {
log.Error("check double sign header error", "err", err)
return
}
if isDoubleSign {
// found a double sign header
log.Error("found a double sign header", "number", h.Number.Uint64(),
"first_hash", h.Hash(), "first_miner", h.Coinbase,
"second_hash", h2.Hash(), "second_miner", h2.Coinbase)
}
}

func (m *DoubleSignMonitor) Close() {
close(m.quit)
}

0 comments on commit 2464933

Please sign in to comment.