本文整理匯總了Golang中github.com/piotrnar/gocoin/client/common.CountSafe函數的典型用法代碼示例。如果您正苦於以下問題:Golang CountSafe函數的具體用法?Golang CountSafe怎麽用?Golang CountSafe使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了CountSafe函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: 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
}
示例2: ParseAddr
// Parese network's "addr" message
func (c *OneConnection) 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")
c.DoS("AddrError")
//println("ParseAddr:", n, e)
break
}
a := peersdb.NewPeer(buf[:])
if !sys.ValidIp4(a.Ip4[:]) {
c.Misbehave("AddrLocal", 2)
} 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 {
c.Misbehave("AddrFuture", 5)
}
}
}
示例3: 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}
}
示例4: ParseTxNet
// Handle incoming "tx" msg
func (c *OneConnection) ParseTxNet(pl []byte) {
if uint32(len(pl)) > atomic.LoadUint32(&common.CFG.TXPool.MaxTxSize) {
common.CountSafe("TxRejectedBig")
return
}
tx, le := btc.NewTx(pl)
if tx == nil {
c.DoS("TxRejectedBroken")
return
}
if le != len(pl) {
c.DoS("TxRejectedLenMismatch")
return
}
if len(tx.TxIn) < 1 {
c.Misbehave("TxRejectedNoInputs", 100)
return
}
tx.SetHash(pl)
NeedThisTx(tx.Hash, func() {
// This body is called with a locked TxMutex
tx.Raw = pl
select {
case NetTxs <- &TxRcvd{conn: c, tx: tx, raw: pl}:
TransactionsPending[tx.Hash.BIdx()] = true
default:
common.CountSafe("TxRejectedFullQ")
//println("NetTxsFULL")
}
})
}
示例5: 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")
}
})
}
示例6: 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()
return
}
示例7: ExpirePeers
func ExpirePeers() {
peerdb_mutex.Lock()
var delcnt uint32
now := time.Now()
todel := make([]qdb.KeyType, PeerDB.Count())
PeerDB.Browse(func(k qdb.KeyType, v []byte) uint32 {
ptim := binary.LittleEndian.Uint32(v[0:4])
if now.After(time.Unix(int64(ptim), 0).Add(ExpirePeerAfter)) {
todel[delcnt] = k // we cannot call Del() from here
delcnt++
}
return 0
})
if delcnt > 0 {
common.CountSafeAdd("PeersExpired", uint64(delcnt))
for delcnt > 0 {
delcnt--
PeerDB.Del(todel[delcnt])
}
common.CountSafe("PeerDefragsDone")
PeerDB.Defrag()
} else {
common.CountSafe("PeerDefragsNone")
}
peerdb_mutex.Unlock()
}
示例8: HandleNetBlock
// Called from the blockchain thread
func HandleNetBlock(newbl *network.BlockRcvd) {
if CheckParentDiscarded(newbl.BlockTreeNode) {
common.CountSafe("DiscardFreshBlockA")
retryCachedBlocks = len(network.CachedBlocks) > 0
return
}
if !common.BlockChain.HasAllParents(newbl.BlockTreeNode) {
// it's not linking - keep it for later
network.CachedBlocks = append(network.CachedBlocks, newbl)
common.CountSafe("BlockPostone")
return
}
common.Busy("LocalAcceptBlock " + newbl.Hash.String())
e := LocalAcceptBlock(newbl)
if e != nil {
common.CountSafe("DiscardFreshBlockB")
fmt.Println("AcceptBlock:", e.Error())
newbl.Conn.DoS("LocalAcceptBl")
} else {
//println("block", newbl.Block.Height, "accepted")
retryCachedBlocks = retry_cached_blocks()
}
}
示例9: 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 := NewPeer(buf[:])
if !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(ExpirePeerAfter)) {
k := qdb.KeyType(a.UniqID())
v := PeerDB.Get(k)
if v != nil {
a.Banned = NewPeer(v[:]).Banned
}
PeerDB.Put(k, a.Bytes())
} else {
common.CountSafe("AddrStale")
}
} else {
common.CountSafe("AddrInFuture")
}
}
}
示例10: retry_cached_blocks
func retry_cached_blocks() bool {
var idx int
common.CountSafe("RedoCachedBlks")
for idx < len(network.CachedBlocks) {
newbl := network.CachedBlocks[idx]
if CheckParentDiscarded(newbl.BlockTreeNode) {
common.CountSafe("DiscardCachedBlock")
network.CachedBlocks = append(network.CachedBlocks[:idx], network.CachedBlocks[idx+1:]...)
return len(network.CachedBlocks) > 0
}
if common.BlockChain.HasAllParents(newbl.BlockTreeNode) {
common.Busy("Cache.LocalAcceptBlock " + newbl.Block.Hash.String())
e := LocalAcceptBlock(newbl)
if e != nil {
fmt.Println("AcceptBlock:", e.Error())
newbl.Conn.DoS("LocalAcceptBl")
}
if usif.Exit_now {
return false
}
// remove it from cache
network.CachedBlocks = append(network.CachedBlocks[:idx], network.CachedBlocks[idx+1:]...)
return len(network.CachedBlocks) > 0
} else {
idx++
}
}
return false
}
示例11: TxMined
func TxMined(h *btc.Uint256) {
TxMutex.Lock()
if rec, ok := TransactionsToSend[h.Hash]; ok {
common.CountSafe("TxMinedToSend")
for i := range rec.Spent {
delete(SpentOutputs, rec.Spent[i])
}
delete(TransactionsToSend, h.Hash)
}
if _, ok := TransactionsRejected[h.BIdx()]; ok {
common.CountSafe("TxMinedRejected")
deleteRejected(h.BIdx())
}
if _, ok := TransactionsPending[h.Hash]; ok {
common.CountSafe("TxMinedPending")
delete(TransactionsPending, h.Hash)
}
wtg := WaitingForInputs[h.BIdx()]
TxMutex.Unlock()
// Try to redo waiting txs
if wtg != nil {
common.CountSafe("TxMinedGotInput")
RetryWaitingForInput(wtg)
}
}
示例12: Misbehave
func (c *OneConnection) Misbehave(why string, how_much int) {
common.CountSafe("Bad" + why)
c.Mutex.Lock()
c.misbehave += how_much
if c.misbehave >= 100 {
common.CountSafe("BanMisbehave")
c.banit = true
c.broken = true
}
c.Mutex.Unlock()
}
示例13: 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
}
示例14: NetRouteInvExt
// This function is called from the main thread (or from an UI)
func NetRouteInvExt(typ uint32, h *btc.Uint256, fromConn *OneConnection, fee_spkb uint64) (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
send_inv := true
v.Mutex.Lock()
if typ == MSG_TX {
if v.Node.DoNotRelayTxs {
send_inv = false
common.CountSafe("SendInvNoTxNode")
} else if v.X.MinFeeSPKB > 0 && uint64(v.X.MinFeeSPKB) > fee_spkb {
send_inv = false
common.CountSafe("SendInvFeeTooLow")
}
/* This is to prevent sending own txs to "spying" peers:
else if fromConn==nil && v.X.InvsRecieved==0 {
send_inv = false
common.CountSafe("SendInvOwnBlocked")
}
*/
}
if send_inv {
if len(v.PendingInvs) < 500 {
if typ, ok := v.InvDone.Map[hash2invid(inv[4:36])]; ok {
common.CountSafe(fmt.Sprint("SendInvSame-", typ))
} else {
v.PendingInvs = append(v.PendingInvs, inv)
cnt++
}
} else {
common.CountSafe("SendInvFull")
}
}
v.Mutex.Unlock()
}
}
Mutex_net.Unlock()
return
}
示例15: HandleVersion
func (c *OneConnection) HandleVersion(pl []byte) error {
if len(pl) >= 80 /*Up to, includiong, the nonce */ {
c.Mutex.Lock()
c.Node.Version = binary.LittleEndian.Uint32(pl[0:4])
if bytes.Equal(pl[72:80], nonce[:]) {
c.Mutex.Unlock()
return errors.New("Connecting to ourselves")
}
if c.Node.Version < MIN_PROTO_VERSION {
c.Mutex.Unlock()
return errors.New("Client version too low")
}
c.Node.Services = binary.LittleEndian.Uint64(pl[4:12])
c.Node.Timestamp = binary.LittleEndian.Uint64(pl[12:20])
c.Node.ReportedIp4 = binary.BigEndian.Uint32(pl[40:44])
if len(pl) >= 86 {
le, of := btc.VLen(pl[80:])
of += 80
c.Node.Agent = string(pl[of : of+le])
of += le
if len(pl) >= of+4 {
c.Node.Height = binary.LittleEndian.Uint32(pl[of : of+4])
c.X.GetBlocksDataNow = true
of += 4
if len(pl) > of && pl[of] == 0 {
c.Node.DoNotRelayTxs = true
}
}
}
c.Mutex.Unlock()
if sys.ValidIp4(pl[40:44]) {
ExternalIpMutex.Lock()
_, use_this_ip := ExternalIp4[c.Node.ReportedIp4]
if !use_this_ip { // New IP
use_this_ip = true
for x, v := range IgnoreExternalIpFrom {
if c.Node.Agent == v {
use_this_ip = false
common.CountSafe(fmt.Sprint("IgnoreExtIP", x))
break
}
}
if use_this_ip {
fmt.Printf("New external IP %d.%d.%d.%d from %s\n> ",
pl[40], pl[41], pl[42], pl[43], c.Node.Agent)
}
}
if use_this_ip {
ExternalIp4[c.Node.ReportedIp4] = [2]uint{ExternalIp4[c.Node.ReportedIp4][0] + 1,
uint(time.Now().Unix())}
}
ExternalIpMutex.Unlock()
}
} else {
return errors.New("version message too short")
}
c.SendRawMsg("verack", []byte{})
return nil
}