github.com/piotrnar/gocoin@v0.0.0-20240512203912-faa0448c5e96/client/network/vars.go (about)

     1  package network
     2  
     3  import (
     4  	"sync"
     5  	"time"
     6  
     7  	"github.com/piotrnar/gocoin/lib/btc"
     8  	"github.com/piotrnar/gocoin/lib/chain"
     9  	"github.com/piotrnar/gocoin/lib/others/sys"
    10  )
    11  
    12  type OneReceivedBlock struct {
    13  	TmStart        time.Time // when we receioved message letting us about this block
    14  	TmPreproc      time.Time // when we added this block to BlocksToGet
    15  	TmDownload     time.Time // when we finished dowloading of this block
    16  	TmQueue        time.Time // when we started comitting this block
    17  	TmAccepted     time.Time // when the block was commited to blockchain
    18  	Cnt            uint
    19  	TxMissing      int
    20  	FromConID      uint32
    21  	NonWitnessSize int
    22  	DoInvs         bool
    23  
    24  	TheWeight    uint
    25  	ThePaidVSize uint
    26  
    27  	TheOrdCnt    uint
    28  	TheOrdSize   uint
    29  	TheOrdWeight uint
    30  }
    31  
    32  type BlockRcvd struct {
    33  	Conn *OneConnection
    34  	*btc.Block
    35  	*chain.BlockTreeNode
    36  	*OneReceivedBlock
    37  	*btc.BlockExtraInfo
    38  	Size int
    39  }
    40  
    41  type TxRcvd struct {
    42  	conn *OneConnection
    43  	*btc.Tx
    44  	trusted, local bool
    45  }
    46  
    47  type OneBlockToGet struct {
    48  	Started time.Time
    49  	*btc.Block
    50  	*chain.BlockTreeNode
    51  	InProgress uint
    52  	TmPreproc  time.Time // how long it took to start downloading this block
    53  	SendInvs   bool
    54  }
    55  
    56  var (
    57  	ReceivedBlocks           map[BIDX]*OneReceivedBlock = make(map[BIDX]*OneReceivedBlock, 400e3)
    58  	BlocksToGet              map[BIDX]*OneBlockToGet    = make(map[BIDX]*OneBlockToGet)
    59  	IndexToBlocksToGet       map[uint32][]BIDX          = make(map[uint32][]BIDX)
    60  	LowestIndexToBlocksToGet uint32
    61  	LastCommitedHeader       *chain.BlockTreeNode
    62  	MutexRcv                 sync.Mutex
    63  
    64  	NetBlocks chan *BlockRcvd = make(chan *BlockRcvd, MAX_BLOCKS_FORWARD_CNT+10)
    65  	NetTxs    chan *TxRcvd    = make(chan *TxRcvd, 2000)
    66  
    67  	CachedBlocksMutex   sync.Mutex
    68  	CachedBlocks        []*BlockRcvd
    69  	CachedBlocksBytes   sys.SyncInt
    70  	MaxCachedBlocksSize sys.SyncInt
    71  	DiscardedBlocks     map[BIDX]bool = make(map[BIDX]bool)
    72  
    73  	HeadersReceived sys.SyncInt
    74  )
    75  
    76  func CachedBlocksLen() (l int) {
    77  	CachedBlocksMutex.Lock()
    78  	l = len(CachedBlocks)
    79  	CachedBlocksMutex.Unlock()
    80  	return
    81  }
    82  
    83  func CachedBlocksAdd(newbl *BlockRcvd) {
    84  	CachedBlocksMutex.Lock()
    85  	CachedBlocks = append(CachedBlocks, newbl)
    86  	CachedBlocksBytes.Add(newbl.Size)
    87  	if CachedBlocksBytes.Get() > MaxCachedBlocksSize.Get() {
    88  		MaxCachedBlocksSize.Store(CachedBlocksBytes.Get())
    89  	}
    90  	CachedBlocksMutex.Unlock()
    91  }
    92  
    93  func CachedBlocksDel(idx int) {
    94  	CachedBlocksMutex.Lock()
    95  	oldbl := CachedBlocks[idx]
    96  	CachedBlocksBytes.Add(-oldbl.Size)
    97  	CachedBlocks = append(CachedBlocks[:idx], CachedBlocks[idx+1:]...)
    98  	CachedBlocksMutex.Unlock()
    99  }
   100  
   101  // make sure to call it with MutexRcv locked
   102  func DiscardBlock(n *chain.BlockTreeNode) {
   103  	if LastCommitedHeader == n {
   104  		LastCommitedHeader = n.Parent
   105  		println("Revert LastCommitedHeader to", LastCommitedHeader.Height)
   106  	}
   107  	for _, c := range n.Childs {
   108  		DiscardBlock(c)
   109  	}
   110  	DiscardedBlocks[n.BlockHash.BIdx()] = true
   111  	return
   112  }
   113  
   114  func AddB2G(b2g *OneBlockToGet) {
   115  	bidx := b2g.Block.Hash.BIdx()
   116  	BlocksToGet[bidx] = b2g
   117  	bh := b2g.BlockTreeNode.Height
   118  	IndexToBlocksToGet[bh] = append(IndexToBlocksToGet[bh], bidx)
   119  	if LowestIndexToBlocksToGet == 0 || bh < LowestIndexToBlocksToGet {
   120  		LowestIndexToBlocksToGet = bh
   121  	}
   122  
   123  	/* TODO: this was causing deadlock. Removing it for now as maybe it is not even needed.
   124  	// Trigger each connection to as the peer for block data
   125  	Mutex_net.Lock()
   126  	for _, v := range OpenCons {
   127  		v.MutexSetBool(&v.X.GetBlocksDataNow, true)
   128  	}
   129  	Mutex_net.Unlock()
   130  	*/
   131  }
   132  
   133  func DelB2G(idx BIDX) {
   134  	b2g := BlocksToGet[idx]
   135  	if b2g == nil {
   136  		println("DelB2G - not found")
   137  		return
   138  	}
   139  
   140  	bh := b2g.BlockTreeNode.Height
   141  	iii := IndexToBlocksToGet[bh]
   142  	if len(iii) > 1 {
   143  		var n []BIDX
   144  		for _, cidx := range iii {
   145  			if cidx != idx {
   146  				n = append(n, cidx)
   147  			}
   148  		}
   149  		if len(n)+1 != len(iii) {
   150  			println("DelB2G - index not found")
   151  		}
   152  		IndexToBlocksToGet[bh] = n
   153  	} else {
   154  		if iii[0] != idx {
   155  			println("DelB2G - index not matching")
   156  		}
   157  		delete(IndexToBlocksToGet, bh)
   158  		if bh == LowestIndexToBlocksToGet {
   159  			if len(IndexToBlocksToGet) > 0 {
   160  				for LowestIndexToBlocksToGet++; ; LowestIndexToBlocksToGet++ {
   161  					if _, ok := IndexToBlocksToGet[LowestIndexToBlocksToGet]; ok {
   162  						break
   163  					}
   164  				}
   165  			} else {
   166  				LowestIndexToBlocksToGet = 0
   167  			}
   168  		}
   169  	}
   170  
   171  	delete(BlocksToGet, idx)
   172  }