本文整理匯總了Golang中github.com/niniwzw/gocoin/client/common.CountSafe函數的典型用法代碼示例。如果您正苦於以下問題:Golang CountSafe函數的具體用法?Golang CountSafe怎麽用?Golang CountSafe使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了CountSafe函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: ParseAddr
// Parese network's "addr" message
func ParseAddr(pl []byte) {
b := bytes.NewBuffer(pl)
cnt, _ := btc.ReadVLen(b)
for i := 0; i < int(cnt); i++ {
var buf [30]byte
n, e := b.Read(buf[:])
if n != len(buf) || e != nil {
common.CountSafe("AddrError")
//println("ParseAddr:", n, e)
break
}
a := peersdb.NewPeer(buf[:])
if !sys.ValidIp4(a.Ip4[:]) {
common.CountSafe("AddrInvalid")
} else if time.Unix(int64(a.Time), 0).Before(time.Now().Add(time.Minute)) {
if time.Now().Before(time.Unix(int64(a.Time), 0).Add(peersdb.ExpirePeerAfter)) {
k := qdb.KeyType(a.UniqID())
v := peersdb.PeerDB.Get(k)
if v != nil {
a.Banned = peersdb.NewPeer(v[:]).Banned
}
peersdb.PeerDB.Put(k, a.Bytes())
} else {
common.CountSafe("AddrStale")
}
} else {
common.CountSafe("AddrInFuture")
}
}
}
示例2: ParseTxNet
// Handle incoming "tx" msg
func (c *OneConnection) ParseTxNet(pl []byte) {
tid := btc.NewSha2Hash(pl)
NeedThisTx(tid, func() {
// This body is called with a locked TxMutex
if uint32(len(pl)) > atomic.LoadUint32(&common.CFG.TXPool.MaxTxSize) {
common.CountSafe("TxTooBig")
RejectTx(tid, len(pl), TX_REJECTED_TOO_BIG)
return
}
tx, le := btc.NewTx(pl)
if tx == nil {
RejectTx(tid, len(pl), TX_REJECTED_FORMAT)
c.DoS("TxBroken")
return
}
if le != len(pl) {
RejectTx(tid, len(pl), TX_REJECTED_LEN_MISMATCH)
c.DoS("TxLenMismatch")
return
}
if len(tx.TxIn) < 1 {
RejectTx(tid, len(pl), TX_REJECTED_EMPTY_INPUT)
c.DoS("TxNoInputs")
return
}
tx.Hash = tid
select {
case NetTxs <- &TxRcvd{conn: c, tx: tx, raw: pl}:
TransactionsPending[tid.BIdx()] = true
default:
common.CountSafe("NetTxsFULL")
}
})
}
示例3: retry_cached_blocks
func retry_cached_blocks() bool {
if len(network.CachedBlocks) == 0 {
return false
}
accepted_cnt := 0
for k, v := range network.CachedBlocks {
common.Busy("Cache.CheckBlock " + v.Block.Hash.String())
e, dos, maybelater := common.BlockChain.CheckBlock(v.Block)
if e == nil {
common.Busy("Cache.AcceptBlock " + v.Block.Hash.String())
e := LocalAcceptBlock(v.Block, v.Conn)
if e == nil {
//fmt.Println("*** Old block accepted", common.BlockChain.BlockTreeEnd.Height)
common.CountSafe("BlocksFromCache")
delete(network.CachedBlocks, k)
accepted_cnt++
break // One at a time should be enough
} else {
fmt.Println("retry AcceptBlock:", e.Error())
v.Conn.DoS("BadCachedBlock1")
delete(network.CachedBlocks, k)
}
} else {
if !maybelater {
fmt.Println("retry CheckBlock:", e.Error())
common.CountSafe("BadCachedBlocks")
if dos {
v.Conn.DoS("BadCachedBlock2")
}
delete(network.CachedBlocks, k)
}
}
}
return accepted_cnt > 0 && len(network.CachedBlocks) > 0
}
示例4: netBlockReceived
// This function is called from a net conn thread
func netBlockReceived(conn *OneConnection, b []byte) {
bl, e := btc.NewBlock(b)
if e != nil {
conn.DoS("BrokenBlock")
println("NewBlock:", e.Error())
return
}
idx := bl.Hash.BIdx()
MutexRcv.Lock()
if rb, got := ReceivedBlocks[idx]; got {
rb.Cnt++
MutexRcv.Unlock()
common.CountSafe("BlockSameRcvd")
return
}
orb := &OneReceivedBlock{Time: time.Now()}
if bip, ok := conn.GetBlockInProgress[idx]; ok {
orb.TmDownload = orb.Time.Sub(bip.start)
conn.Mutex.Lock()
delete(conn.GetBlockInProgress, idx)
conn.Mutex.Unlock()
} else {
common.CountSafe("UnxpectedBlockRcvd")
}
ReceivedBlocks[idx] = orb
MutexRcv.Unlock()
NetBlocks <- &BlockRcvd{Conn: conn, Block: bl}
}
示例5: txChecker
func txChecker(h *btc.Uint256) bool {
TxMutex.Lock()
rec, ok := TransactionsToSend[h.BIdx()]
TxMutex.Unlock()
if ok && rec.Own != 0 {
return false // Assume own txs as non-trusted
}
if ok {
common.CountSafe("TxScrBoosted")
} else {
common.CountSafe("TxScrMissed")
}
return ok
}
示例6: HandleNetBlock
// Called from the blockchain thread
func HandleNetBlock(newbl *network.BlockRcvd) {
common.CountSafe("HandleNetBlock")
bl := newbl.Block
common.Busy("CheckBlock " + bl.Hash.String())
e, dos, maybelater := common.BlockChain.CheckBlock(bl)
if e != nil {
if maybelater {
network.AddBlockToCache(bl, newbl.Conn)
} else {
fmt.Println(dos, e.Error())
if dos {
newbl.Conn.DoS("CheckBlock")
}
}
} else {
common.Busy("LocalAcceptBlock " + bl.Hash.String())
e = LocalAcceptBlock(bl, newbl.Conn)
if e == nil {
retryCachedBlocks = retry_cached_blocks()
} else {
fmt.Println("AcceptBlock:", e.Error())
newbl.Conn.DoS("LocalAcceptBl")
}
}
}
示例7: RetryWaitingForInput
func RetryWaitingForInput(wtg *OneWaitingList) {
for k, t := range wtg.Ids {
pendtxrcv := TransactionsRejected[k].Wait4Input.TxRcvd
if HandleNetTx(pendtxrcv, true) {
common.CountSafe("TxRetryAccepted")
if common.DebugLevel > 0 {
fmt.Println(pendtxrcv.tx.Hash.String(), "accepted after", time.Now().Sub(t).String())
}
} else {
common.CountSafe("TxRetryRejected")
if common.DebugLevel > 0 {
fmt.Println(pendtxrcv.tx.Hash.String(), "still rejected", TransactionsRejected[k].Reason)
}
}
}
}
示例8: DoS
func (c *OneConnection) DoS(why string) {
common.CountSafe("Ban" + why)
c.Mutex.Lock()
c.banit = true
c.broken = true
c.Mutex.Unlock()
}
示例9: drop_slowest_peer
// This function should be called only when OutConsActive >= MaxOutCons
func drop_slowest_peer() {
var worst_ping int
var worst_conn *OneConnection
Mutex_net.Lock()
for _, v := range OpenCons {
if v.Incoming && InConsActive < atomic.LoadUint32(&common.CFG.Net.MaxInCons) {
// If this is an incoming connection, but we are not full yet, ignore it
continue
}
v.Mutex.Lock()
ap := v.GetAveragePing()
v.Mutex.Unlock()
if ap > worst_ping {
worst_ping = ap
worst_conn = v
}
}
if worst_conn != nil {
if common.DebugLevel > 0 {
println("Droping slowest peer", worst_conn.PeerAddr.Ip(), "/", worst_ping, "ms")
}
worst_conn.Disconnect()
common.CountSafe("PeersDropped")
}
Mutex_net.Unlock()
}
示例10: ExpireCachedBlocks
// Expire cached blocks
func ExpireCachedBlocks() {
for k, v := range CachedBlocks {
if v.Time.Add(ExpireCachedAfter).Before(time.Now()) {
delete(CachedBlocks, k)
common.CountSafe("BlockExpired")
}
}
}
示例11: blockWanted
// Called from network threads
func blockWanted(h []byte) (yes bool) {
idx := btc.NewUint256(h).BIdx()
MutexRcv.Lock()
_, ok := ReceivedBlocks[idx]
MutexRcv.Unlock()
if !ok {
if atomic.LoadUint32(&common.CFG.Net.MaxBlockAtOnce) == 0 || !blocksLimitReached(idx) {
yes = true
common.CountSafe("BlockWanted")
} else {
common.CountSafe("BlockInProgress")
}
} else {
common.CountSafe("BlockUnwanted")
}
return
}
示例12: SendRawMsg
func (c *OneConnection) SendRawMsg(cmd string, pl []byte) (e error) {
c.Mutex.Lock()
if c.Send.Buf != nil {
// Before adding more data to the buffer, check the limit
if len(c.Send.Buf) > MaxSendBufferSize {
c.Mutex.Unlock()
if common.DebugLevel > 0 {
println(c.PeerAddr.Ip(), "Peer Send Buffer Overflow")
}
c.Disconnect()
common.CountSafe("PeerSendOverflow")
return errors.New("Send buffer overflow")
}
} else {
c.Send.LastSent = time.Now()
}
common.CountSafe("sent_" + cmd)
common.CountSafeAdd("sbts_"+cmd, uint64(len(pl)))
sbuf := make([]byte, 24+len(pl))
c.LastCmdSent = cmd
c.LastBtsSent = uint32(len(pl))
binary.LittleEndian.PutUint32(sbuf[0:4], common.Version)
copy(sbuf[0:4], common.Magic[:])
copy(sbuf[4:16], cmd)
binary.LittleEndian.PutUint32(sbuf[16:20], uint32(len(pl)))
sh := btc.Sha2Sum(pl[:])
copy(sbuf[20:24], sh[:4])
copy(sbuf[24:], pl)
c.Send.Buf = append(c.Send.Buf, sbuf...)
if common.DebugLevel < 0 {
fmt.Println(cmd, len(c.Send.Buf), "->", c.PeerAddr.Ip())
}
c.Mutex.Unlock()
//println(len(c.Send.Buf), "queued for seding to", c.PeerAddr.Ip())
return
}
示例13: isRoutable
func isRoutable(rec *OneTxToSend) bool {
if !common.CFG.TXRoute.Enabled {
common.CountSafe("TxRouteDisabled")
rec.Blocked = TX_REJECTED_DISABLED
return false
}
if uint32(len(rec.Data)) > atomic.LoadUint32(&common.CFG.TXRoute.MaxTxSize) {
common.CountSafe("TxRouteTooBig")
rec.Blocked = TX_REJECTED_TOO_BIG
return false
}
if rec.Fee < (uint64(len(rec.Data)) * atomic.LoadUint64(&common.CFG.TXRoute.FeePerByte)) {
common.CountSafe("TxRouteLowFee")
rec.Blocked = TX_REJECTED_LOW_FEE
return false
}
if rec.Minout < atomic.LoadUint64(&common.CFG.TXRoute.MinVoutValue) {
common.CountSafe("TxRouteDust")
rec.Blocked = TX_REJECTED_DUST
return false
}
return true
}
示例14: NetRouteInv
// 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
}
示例15: GetBlocks
func (c *OneConnection) GetBlocks(pl []byte) {
h2get, hashstop, e := parseLocatorsPayload(pl)
if e != nil || len(h2get) < 1 || hashstop == nil {
println("GetBlocks: error parsing payload from", c.PeerAddr.Ip())
c.DoS("BadGetBlks")
return
}
invs := make(map[[32]byte]bool, 500)
for i := range h2get {
common.BlockChain.BlockIndexAccess.Lock()
if bl, ok := common.BlockChain.BlockIndex[h2get[i].BIdx()]; ok {
// make sure that this block is in our main chain
common.Last.Mutex.Lock()
end := common.Last.Block
common.Last.Mutex.Unlock()
for ; end != nil && end.Height >= bl.Height; end = end.Parent {
if end == bl {
addInvBlockBranch(invs, bl, hashstop) // Yes - this is the main chain
if common.DebugLevel > 0 {
fmt.Println(c.PeerAddr.Ip(), "getblocks from", bl.Height,
"stop at", hashstop.String(), "->", len(invs), "invs")
}
if len(invs) > 0 {
common.BlockChain.BlockIndexAccess.Unlock()
inv := new(bytes.Buffer)
btc.WriteVlen(inv, uint64(len(invs)))
for k, _ := range invs {
binary.Write(inv, binary.LittleEndian, uint32(2))
inv.Write(k[:])
}
c.SendRawMsg("inv", inv.Bytes())
return
}
}
}
}
common.BlockChain.BlockIndexAccess.Unlock()
}
common.CountSafe("GetblksMissed")
return
}