本文整理汇总了Golang中github.com/piotrnar/gocoin/lib/btc.WriteVlen函数的典型用法代码示例。如果您正苦于以下问题:Golang WriteVlen函数的具体用法?Golang WriteVlen怎么用?Golang WriteVlen使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了WriteVlen函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: SendInvs
func (c *OneConnection) SendInvs() (res bool) {
b_txs := new(bytes.Buffer)
b_blk := new(bytes.Buffer)
var c_blk []*btc.Uint256
c.Mutex.Lock()
if len(c.PendingInvs) > 0 {
for i := range c.PendingInvs {
var inv_sent_otherwise bool
typ := binary.LittleEndian.Uint32((*c.PendingInvs[i])[:4])
c.InvStore(typ, (*c.PendingInvs[i])[4:36])
if typ == MSG_BLOCK {
if c.Node.SendCmpctVer >= 1 && c.Node.HighBandwidth {
c_blk = append(c_blk, btc.NewUint256((*c.PendingInvs[i])[4:]))
inv_sent_otherwise = true
} else if c.Node.SendHeaders {
// convert block inv to block header
common.BlockChain.BlockIndexAccess.Lock()
bl := common.BlockChain.BlockIndex[btc.NewUint256((*c.PendingInvs[i])[4:]).BIdx()]
if bl != nil {
b_blk.Write(bl.BlockHeader[:])
b_blk.Write([]byte{0}) // 0 txs
}
common.BlockChain.BlockIndexAccess.Unlock()
inv_sent_otherwise = true
}
}
if !inv_sent_otherwise {
b_txs.Write((*c.PendingInvs[i])[:])
}
}
res = true
}
c.PendingInvs = nil
c.Mutex.Unlock()
if len(c_blk) > 0 {
for _, h := range c_blk {
c.SendCmpctBlk(h)
}
}
if b_blk.Len() > 0 {
common.CountSafe("InvSentAsHeader")
b := new(bytes.Buffer)
btc.WriteVlen(b, uint64(b_blk.Len()/81))
c.SendRawMsg("headers", append(b.Bytes(), b_blk.Bytes()...))
//println("sent block's header(s)", b_blk.Len(), uint64(b_blk.Len()/81))
}
if b_txs.Len() > 0 {
b := new(bytes.Buffer)
btc.WriteVlen(b, uint64(b_txs.Len()/36))
c.SendRawMsg("inv", append(b.Bytes(), b_txs.Bytes()...))
}
return
}
示例2: HashFromMessage
// LTC signing uses different seed string
func HashFromMessage(msg []byte, out []byte) {
const MessageMagic = "Litecoin Signed Message:\n"
b := new(bytes.Buffer)
btc.WriteVlen(b, uint32(len(MessageMagic)))
b.Write([]byte(MessageMagic))
btc.WriteVlen(b, uint32(len(msg)))
b.Write(msg)
btc.ShaHash(b.Bytes(), out)
}
示例3: SendCmpctBlk
func (c *OneConnection) SendCmpctBlk(hash *btc.Uint256) {
crec := GetchBlockForBIP152(hash)
if crec == nil {
fmt.Println(c.ConnID, "cmpctblock not sent for", hash.String())
return
}
k0 := binary.LittleEndian.Uint64(crec.BIP152[8:16])
k1 := binary.LittleEndian.Uint64(crec.BIP152[16:24])
msg := new(bytes.Buffer)
msg.Write(crec.Data[:80])
msg.Write(crec.BIP152[:8])
btc.WriteVlen(msg, uint64(len(crec.Block.Txs)-1)) // all except coinbase
for i := 1; i < len(crec.Block.Txs); i++ {
var lsb [8]byte
var hasz *btc.Uint256
if c.Node.SendCmpctVer == 2 {
hasz = crec.Block.Txs[i].WTxID()
} else {
hasz = crec.Block.Txs[i].Hash
}
binary.LittleEndian.PutUint64(lsb[:], siphash.Hash(k0, k1, hasz.Hash[:]))
msg.Write(lsb[:6])
}
msg.Write([]byte{1}) // one preffiled tx
msg.Write([]byte{0}) // coinbase - index 0
if c.Node.SendCmpctVer == 2 {
msg.Write(crec.Block.Txs[0].Raw) // coinbase - index 0
} else {
crec.Block.Txs[0].WriteSerialized(msg) // coinbase - index 0
}
c.SendRawMsg("cmpctblock", msg.Bytes())
}
示例4: main
func main() {
if len(os.Args) != 5 {
fmt.Println("This tool needs to be executed with 4 arguments:")
fmt.Println(" 1) Name of the unsigned transaction file")
fmt.Println(" 2) Input index to add the key & signature to")
fmt.Println(" 3) Hex dump of the canonical signature")
fmt.Println(" 4) Hex dump of the public key")
return
}
tx := raw_tx_from_file(os.Args[1])
if tx == nil {
return
}
in, er := strconv.ParseUint(os.Args[2], 10, 32)
if er != nil {
println("Input index:", er.Error())
return
}
if int(in) >= len(tx.TxIn) {
println("Input index too big:", int(in), "/", len(tx.TxIn))
return
}
sig, er := hex.DecodeString(os.Args[3])
if er != nil {
println("Signature:", er.Error())
return
}
pk, er := hex.DecodeString(os.Args[4])
if er != nil {
println("Public key:", er.Error())
return
}
buf := new(bytes.Buffer)
btc.WriteVlen(buf, uint64(len(sig)))
buf.Write(sig)
btc.WriteVlen(buf, uint64(len(pk)))
buf.Write(pk)
tx.TxIn[in].ScriptSig = buf.Bytes()
write_tx_file(tx)
}
示例5: SendOwnAddr
func (c *OneConnection) SendOwnAddr() {
if ExternalAddrLen() > 0 {
buf := new(bytes.Buffer)
btc.WriteVlen(buf, uint64(1))
binary.Write(buf, binary.LittleEndian, uint32(time.Now().Unix()))
buf.Write(BestExternalAddr())
c.SendRawMsg("addr", buf.Bytes())
}
}
示例6: Assemble
func (col *CmpctBlockCollector) Assemble() []byte {
bdat := new(bytes.Buffer)
bdat.Write(col.Header)
btc.WriteVlen(bdat, uint64(len(col.Txs)))
for _, txd := range col.Txs {
bdat.Write(txd.([]byte))
}
return bdat.Bytes()
}
示例7: ProcessGetBlockTxn
func (c *OneConnection) ProcessGetBlockTxn(pl []byte) {
if len(pl) < 34 {
println(c.ConnID, "GetBlockTxnShort")
c.DoS("GetBlockTxnShort")
return
}
hash := btc.NewUint256(pl[:32])
crec := GetchBlockForBIP152(hash)
if crec == nil {
fmt.Println(c.ConnID, "GetBlockTxn aborting for", hash.String())
return
}
req := bytes.NewReader(pl[32:])
indexes_length, _ := btc.ReadVLen(req)
if indexes_length == 0 {
println(c.ConnID, "GetBlockTxnEmpty")
c.DoS("GetBlockTxnEmpty")
return
}
var exp_idx uint64
msg := new(bytes.Buffer)
msg.Write(hash.Hash[:])
btc.WriteVlen(msg, indexes_length)
for {
idx, er := btc.ReadVLen(req)
if er != nil {
println(c.ConnID, "GetBlockTxnERR")
c.DoS("GetBlockTxnERR")
return
}
idx += exp_idx
if int(idx) >= len(crec.Block.Txs) {
println(c.ConnID, "GetBlockTxnIdx+")
c.DoS("GetBlockTxnIdx+")
return
}
if c.Node.SendCmpctVer == 2 {
msg.Write(crec.Block.Txs[idx].Raw) // coinbase - index 0
} else {
crec.Block.Txs[idx].WriteSerialized(msg) // coinbase - index 0
}
if indexes_length == 1 {
break
}
indexes_length--
exp_idx = idx + 1
}
c.SendRawMsg("blocktxn", msg.Bytes())
}
示例8: SendAddr
func (c *OneConnection) SendAddr() {
pers := peersdb.GetBestPeers(MaxAddrsPerMessage, nil)
if len(pers) > 0 {
buf := new(bytes.Buffer)
btc.WriteVlen(buf, uint64(len(pers)))
for i := range pers {
binary.Write(buf, binary.LittleEndian, pers[i].Time)
buf.Write(pers[i].NetAddr.Bytes())
}
c.SendRawMsg("addr", buf.Bytes())
}
}
示例9: 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, uint32(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
}
示例10: GetHeaders
// Handle getheaders protocol command
// https://en.bitcoin.it/wiki/Protocol_specification#getheaders
func (c *OneConnection) GetHeaders(pl []byte) {
h2get, hashstop, e := parseLocatorsPayload(pl)
if e != nil || hashstop == nil {
println("GetHeaders: error parsing payload from", c.PeerAddr.Ip())
c.DoS("BadGetHdrs")
return
}
if common.DebugLevel > 1 {
println("GetHeaders", len(h2get), hashstop.String())
}
var best_block, last_block *chain.BlockTreeNode
common.BlockChain.BlockIndexAccess.Lock()
if len(h2get) > 0 {
for i := range h2get {
if bl, ok := common.BlockChain.BlockIndex[h2get[i].BIdx()]; ok {
if best_block == nil || bl.Height > best_block.Height {
best_block = bl
}
}
}
} else {
best_block = common.BlockChain.BlockIndex[hashstop.BIdx()]
}
last_block = common.BlockChain.BlockTreeEnd
common.BlockChain.BlockIndexAccess.Unlock()
var resp []byte
var cnt uint32
for cnt < 2000 {
best_block = best_block.FindPathTo(last_block)
if best_block == nil {
break
}
resp = append(resp, append(best_block.BlockHeader[:], 0)...) // 81st byte is always zero
cnt++
}
out := new(bytes.Buffer)
btc.WriteVlen(out, cnt)
out.Write(resp)
c.SendRawMsg("headers", out.Bytes())
return
}
示例11: SendInvs
func (c *OneConnection) SendInvs() (res bool) {
b := new(bytes.Buffer)
c.Mutex.Lock()
if len(c.PendingInvs) > 0 {
btc.WriteVlen(b, uint32(len(c.PendingInvs)))
for i := range c.PendingInvs {
b.Write((*c.PendingInvs[i])[:])
}
res = true
}
c.PendingInvs = nil
c.Mutex.Unlock()
if res {
c.SendRawMsg("inv", b.Bytes())
}
return
}
示例12: ProcessInv
func (c *OneConnection) ProcessInv(pl []byte) {
if len(pl) < 37 {
//println(c.PeerAddr.Ip(), "inv payload too short", len(pl))
c.DoS("InvEmpty")
return
}
c.InvsRecieved++
cnt, of := btc.VLen(pl)
if len(pl) != of+36*cnt {
println("inv payload length mismatch", len(pl), of, cnt)
}
var blinv2ask []byte
for i := 0; i < cnt; i++ {
typ := binary.LittleEndian.Uint32(pl[of : of+4])
common.CountSafe(fmt.Sprint("InvGot", typ))
if typ == 2 {
if blockWanted(pl[of+4 : of+36]) {
blinv2ask = append(blinv2ask, pl[of+4:of+36]...)
}
} else if typ == 1 {
if common.CFG.TXPool.Enabled {
c.TxInvNotify(pl[of+4 : of+36])
}
}
of += 36
}
if len(blinv2ask) > 0 {
bu := new(bytes.Buffer)
btc.WriteVlen(bu, uint64(len(blinv2ask)/32))
for i := 0; i < len(blinv2ask); i += 32 {
bh := btc.NewUint256(blinv2ask[i : i+32])
c.Mutex.Lock()
c.GetBlockInProgress[bh.BIdx()] = &oneBlockDl{hash: bh, start: time.Now()}
c.Mutex.Unlock()
binary.Write(bu, binary.LittleEndian, uint32(2))
bu.Write(bh.Hash[:])
}
c.SendRawMsg("getdata", bu.Bytes())
}
return
}
示例13: ping_idle
func (c *one_net_conn) ping_idle() {
c.ping.Lock()
if c.ping.inProgress {
if time.Now().After(c.ping.timeSent.Add(PING_TIMEOUT)) {
c.store_ping_result()
c.ping.Unlock()
//fmt.Println(c.peerip, "ping timeout", c.ping.seq)
} else {
c.ping.Unlock()
time.Sleep(time.Millisecond)
}
} else if c.ping.now {
//fmt.Println("ping", c.peerip, c.ping.seq)
c.ping.inProgress = true
c.ping.timeSent = time.Now()
c.ping.now = false
if false {
rand.Read(c.ping.pattern[:])
c.ping.Unlock()
c.sendmsg("ping", c.ping.pattern[:])
} else {
b := new(bytes.Buffer)
btc.WriteVlen(b, PING_FETCH_BLOCKS)
BlocksMutex.Lock()
for i := uint32(1); ; i++ {
binary.Write(b, binary.LittleEndian, uint32(2))
btg := BlocksToGet[i]
b.Write(btg[:])
if i == PING_FETCH_BLOCKS {
c.ping.lastBlock = btc.NewUint256(btg[:])
break
}
}
BlocksMutex.Unlock()
c.ping.bytes = 0
c.ping.Unlock()
c.sendmsg("getdata", b.Bytes())
//fmt.Println("ping sent", c.ping.lastBlock.String())
}
} else {
c.ping.Unlock()
time.Sleep(10 * time.Millisecond)
}
}
示例14: sendGetHeaders
func (c *OneConnection) sendGetHeaders() {
MutexRcv.Lock()
lb := LastCommitedHeader
MutexRcv.Unlock()
min_height := int(lb.Height) - chain.MovingCheckopintDepth
if min_height < 0 {
min_height = 0
}
blks := new(bytes.Buffer)
var cnt uint64
var step int
step = 1
for cnt < 50 /*it shoudl never get that far, but just in case...*/ {
blks.Write(lb.BlockHash.Hash[:])
cnt++
//println(" geth", cnt, "height", lb.Height, lb.BlockHash.String())
if int(lb.Height) <= min_height {
break
}
for tmp := 0; tmp < step && lb != nil && int(lb.Height) > min_height; tmp++ {
lb = lb.Parent
}
if lb == nil {
break
}
if cnt >= 10 {
step = step * 2
}
}
var null_stop [32]byte
blks.Write(null_stop[:])
bhdr := new(bytes.Buffer)
binary.Write(bhdr, binary.LittleEndian, common.Version)
btc.WriteVlen(bhdr, cnt)
c.SendRawMsg("getheaders", append(bhdr.Bytes(), blks.Bytes()...))
c.X.LastHeadersHeightAsk = lb.Height
c.X.GetHeadersInProgress = true
c.X.GetHeadersTimeout = time.Now().Add(NO_DATA_TIMEOUT)
}
示例15: CommitBlockTxs
// Commit the given add/del transactions to UTXO and Wnwind DBs
func (db *UnspentDB) CommitBlockTxs(changes *BlockChanges, blhash []byte) (e error) {
undo_fn := fmt.Sprint(db.dir, changes.Height)
if changes.UndoData != nil || (changes.Height%db.UnwindBufLen) == 0 {
bu := new(bytes.Buffer)
bu.Write(blhash)
if changes.UndoData != nil {
for _, xx := range changes.UndoData {
bin := xx.Serialize(true)
btc.WriteVlen(bu, uint64(len(bin)))
bu.Write(bin)
}
}
ioutil.WriteFile(db.dir+"tmp", bu.Bytes(), 0666)
os.Rename(db.dir+"tmp", undo_fn+".tmp")
}
db.nosync()
db.commit(changes)
if changes.LastKnownHeight <= changes.Height {
db.Sync()
}
os.Rename(undo_fn+".tmp", undo_fn)
if db.LastBlockHash == nil {
db.LastBlockHash = make([]byte, 32)
}
copy(db.LastBlockHash, blhash)
db.LastBlockHeight = changes.Height
if changes.Height > db.UnwindBufLen {
os.Remove(fmt.Sprint(db.dir, changes.Height-db.UnwindBufLen))
}
return
}