github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/libs/tendermint/consensus/block_transport.go (about)

     1  package consensus
     2  
     3  import (
     4  	"fmt"
     5  	"sync"
     6  	"time"
     7  
     8  	"github.com/fibonacci-chain/fbc/libs/system/trace"
     9  	"github.com/fibonacci-chain/fbc/libs/tendermint/libs/log"
    10  )
    11  
    12  type BlockTransport struct {
    13  	height                 int64
    14  	recvProposal           time.Time
    15  	firstPart              time.Time
    16  	droppedDue2NotExpected int
    17  	droppedDue2NotAdded    int
    18  	droppedDue2Error       int
    19  	droppedDue2WrongHeight int
    20  	totalParts             int
    21  	Logger                 log.Logger
    22  
    23  	bpStatMtx       sync.RWMutex
    24  	bpSend          int
    25  	bpNOTransByData int
    26  	bpNOTransByACK  int
    27  }
    28  
    29  func (bt *BlockTransport) onProposal(height int64) {
    30  	if bt.height == height || bt.height == 0 {
    31  		bt.recvProposal = time.Now()
    32  		bt.height = height
    33  	}
    34  }
    35  
    36  func (bt *BlockTransport) reset(height int64) {
    37  	bt.height = height
    38  	bt.droppedDue2NotExpected = 0
    39  	bt.droppedDue2NotAdded = 0
    40  	bt.droppedDue2Error = 0
    41  	bt.droppedDue2WrongHeight = 0
    42  	bt.totalParts = 0
    43  	bt.bpNOTransByData = 0
    44  	bt.bpNOTransByACK = 0
    45  	bt.bpSend = 0
    46  }
    47  
    48  func (bt *BlockTransport) on1stPart(height int64) {
    49  	if bt.height == height || bt.height == 0 {
    50  		bt.firstPart = time.Now()
    51  		bt.height = height
    52  	}
    53  }
    54  
    55  func (bt *BlockTransport) onRecvBlock(height int64) {
    56  	if bt.height == height {
    57  		//totalElapsed := time.Now().Sub(bt.recvProposal)
    58  		//trace.GetElapsedInfo().AddInfo(trace.RecvBlock, fmt.Sprintf("<%dms>", totalElapsed.Milliseconds()))
    59  		first2LastPartElapsed := time.Now().Sub(bt.firstPart)
    60  		trace.GetElapsedInfo().AddInfo(trace.First2LastPart, fmt.Sprintf("%dms", first2LastPartElapsed.Milliseconds()))
    61  	}
    62  }
    63  
    64  // blockpart send times
    65  func (bt *BlockTransport) onBPSend() {
    66  	bt.bpStatMtx.Lock()
    67  	bt.bpSend++
    68  	bt.bpStatMtx.Unlock()
    69  }
    70  
    71  // blockpart-ack receive times, specific blockpart won't send  to the peer from where the ack fired
    72  func (bt *BlockTransport) onBPACKHit() {
    73  	bt.bpStatMtx.Lock()
    74  	bt.bpNOTransByACK++
    75  	bt.bpStatMtx.Unlock()
    76  }
    77  
    78  // blockpart data receive times, specific blockpart won't send to the peer from where the data fired
    79  func (bt *BlockTransport) onBPDataHit() {
    80  	bt.bpStatMtx.Lock()
    81  	bt.bpNOTransByData++
    82  	bt.bpStatMtx.Unlock()
    83  }