// This function is called from the main thread (or from an UI) func NetRouteInv(typ uint32, h *btc.Uint256, fromConn *OneConnection) (cnt uint) { common.CountSafe(fmt.Sprint("NetRouteInv", typ)) // Prepare the inv inv := new([36]byte) binary.LittleEndian.PutUint32(inv[0:4], typ) copy(inv[4:36], h.Bytes()) // Append it to PendingInvs in each open connection Mutex_net.Lock() for _, v := range OpenCons { if v != fromConn { // except the one that this inv came from v.Mutex.Lock() if v.Node.DoNotRelayTxs && typ == 1 { // This node does not want tx inv (it came with its version message) common.CountSafe("SendInvNoTxNode") } else { if fromConn == nil && v.InvsRecieved == 0 { // Do not broadcast own txs to nodes that never sent any invs to us common.CountSafe("SendInvOwnBlocked") } else if len(v.PendingInvs) < 500 { v.PendingInvs = append(v.PendingInvs, inv) cnt++ } else { common.CountSafe("SendInvIgnored") } } v.Mutex.Unlock() } } Mutex_net.Unlock() if cnt == 0 { NetAlerts <- "WARNING: your tx has not been broadcasted to any peer" } return }
func SendInvToRandomPeer(typ uint32, h *btc.Uint256) { common.CountSafe(fmt.Sprint("NetSendOneInv", typ)) // Prepare the inv inv := new([36]byte) binary.LittleEndian.PutUint32(inv[0:4], typ) copy(inv[4:36], h.Bytes()) // Append it to PendingInvs in a random connection network.Mutex_net.Lock() idx := rand.Intn(len(network.OpenCons)) var cnt int for _, v := range network.OpenCons { if idx == cnt { v.Mutex.Lock() v.PendingInvs = append(v.PendingInvs, inv) v.Mutex.Unlock() break } cnt++ } network.Mutex_net.Unlock() return }