本文整理汇总了Golang中github.com/piotrnar/gocoin/lib/btc.NewUint256函数的典型用法代码示例。如果您正苦于以下问题:Golang NewUint256函数的具体用法?Golang NewUint256怎么用?Golang NewUint256使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NewUint256函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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
}
开发者ID:piotrnar,项目名称:gocoin,代码行数:59,代码来源:invs.go
示例2: GetBlockData
func (c *OneConnection) GetBlockData(h []byte) {
var b [1 + 4 + 32]byte
b[0] = 1 // One inv
b[1] = 2 // Block
copy(b[5:37], h[:32])
if common.DebugLevel > 1 {
println("GetBlockData", btc.NewUint256(h).String())
}
bh := btc.NewUint256(h)
c.Mutex.Lock()
c.GetBlockInProgress[bh.BIdx()] = &oneBlockDl{hash: bh, start: time.Now()}
c.Mutex.Unlock()
c.SendRawMsg("getdata", b[:])
}
开发者ID:vancsj,项目名称:gocoin,代码行数:14,代码来源:data.go
示例3: init
func init() {
rd, _ := hex.DecodeString("0100000001b14bdcbc3e01bdaad36cc08e81e69c82e1060bc14e518db2b49aa43ad90ba26000000000490047304402203f16c6f40162ab686621ef3000b04e75418a0c0cb2d8aebeac894ae360ac1e780220ddc15ecdfc3507ac48e1681a33eb60996631bf6bf5bc0a0682c4db743ce7ca2b01ffffffff0140420f00000000001976a914660d4ef3a743e3e696ad990364e555c271ad504b88ac00000000")
dummy_tx, _ := btc.NewTx(rd)
dummy_tx.Size = uint32(len(rd))
ha := btc.Sha2Sum(rd)
dummy_tx.Hash = btc.NewUint256(ha[:])
}
开发者ID:vancsj,项目名称:gocoin,代码行数:7,代码来源:script_test.go
示例4: execute_test_tx
func execute_test_tx(t *testing.T, tv *testvector) bool {
if len(tv.inps) == 0 {
t.Error("Vector has no inputs")
return false
}
rd, er := hex.DecodeString(tv.tx)
if er != nil {
t.Error(er.Error())
return false
}
tx, _ := btc.NewTx(rd)
if tx == nil {
t.Error("Canot decode tx")
return false
}
tx.Size = uint32(len(rd))
ha := btc.Sha2Sum(rd)
tx.Hash = btc.NewUint256(ha[:])
if skip_broken_tests(tx) {
return false
}
if !tx.IsCoinBase() {
for i := range tx.TxIn {
if tx.TxIn[i].Input.IsNull() {
return false
}
}
}
oks := 0
for i := range tx.TxIn {
var j int
for j = range tv.inps {
if bytes.Equal(tx.TxIn[i].Input.Hash[:], tv.inps[j].txid.Hash[:]) &&
tx.TxIn[i].Input.Vout == uint32(tv.inps[j].vout) {
break
}
}
if j >= len(tv.inps) {
t.Error("Matching input not found")
continue
}
pk, er := btc.DecodeScript(tv.inps[j].pkscr)
if er != nil {
t.Error(er.Error())
continue
}
var ss []byte
if tv.inps[j].vout >= 0 {
ss = tx.TxIn[i].ScriptSig
}
if VerifyTxScript(ss, pk, i, tx, tv.ver_flags) {
oks++
}
}
return oks == len(tx.TxIn)
}
开发者ID:liudch,项目名称:gocoin,代码行数:60,代码来源:tx_test.go
示例5: xml_balance
func xml_balance(w http.ResponseWriter, r *http.Request) {
if !ipchecker(r) {
return
}
w.Header()["Content-Type"] = []string{"text/xml"}
w.Write([]byte("<unspent>"))
wallet.BalanceMutex.Lock()
for i := range wallet.MyBalance {
w.Write([]byte("<output>"))
fmt.Fprint(w, "<txid>", btc.NewUint256(wallet.MyBalance[i].TxPrevOut.Hash[:]).String(), "</txid>")
fmt.Fprint(w, "<vout>", wallet.MyBalance[i].TxPrevOut.Vout, "</vout>")
fmt.Fprint(w, "<value>", wallet.MyBalance[i].Value, "</value>")
fmt.Fprint(w, "<inblock>", wallet.MyBalance[i].MinedAt, "</inblock>")
fmt.Fprint(w, "<blocktime>", get_block_time(wallet.MyBalance[i].MinedAt), "</blocktime>")
fmt.Fprint(w, "<addr>", wallet.MyBalance[i].DestAddr(), "</addr>")
fmt.Fprint(w, "<addrorg>", wallet.MyBalance[i].BtcAddr.String(), "</addrorg>")
fmt.Fprint(w, "<wallet>", html.EscapeString(wallet.MyBalance[i].BtcAddr.Extra.Wallet), "</wallet>")
fmt.Fprint(w, "<label>", html.EscapeString(wallet.MyBalance[i].BtcAddr.Extra.Label), "</label>")
fmt.Fprint(w, "<virgin>", fmt.Sprint(wallet.MyBalance[i].BtcAddr.Extra.Virgin), "</virgin>")
w.Write([]byte("</output>"))
}
wallet.BalanceMutex.Unlock()
w.Write([]byte("</unspent>"))
}
开发者ID:liudch,项目名称:gocoin,代码行数:26,代码来源:wallets.go
示例6: output_tx_xml
func output_tx_xml(w http.ResponseWriter, id string) {
txid := btc.NewUint256FromString(id)
w.Write([]byte("<tx>"))
fmt.Fprint(w, "<id>", id, "</id>")
if t2s, ok := network.TransactionsToSend[txid.BIdx()]; ok {
w.Write([]byte("<status>OK</status>"))
w.Write([]byte(fmt.Sprint("<len>", len(t2s.Data), "</len>")))
tx := t2s.Tx
w.Write([]byte("<inputs>"))
for i := range tx.TxIn {
w.Write([]byte("<input>"))
var po *btc.TxOut
inpid := btc.NewUint256(tx.TxIn[i].Input.Hash[:])
if txinmem, ok := network.TransactionsToSend[inpid.BIdx()]; ok {
if int(tx.TxIn[i].Input.Vout) < len(txinmem.TxOut) {
po = txinmem.TxOut[tx.TxIn[i].Input.Vout]
}
} else {
po, _ = common.BlockChain.Unspent.UnspentGet(&tx.TxIn[i].Input)
}
if po != nil {
ok := script.VerifyTxScript(tx.TxIn[i].ScriptSig, po.Pk_script, i, tx, script.VER_P2SH|script.VER_DERSIG)
if !ok {
w.Write([]byte("<status>Script FAILED</status>"))
} else {
w.Write([]byte("<status>OK</status>"))
}
fmt.Fprint(w, "<value>", po.Value, "</value>")
ads := "???"
if ad := btc.NewAddrFromPkScript(po.Pk_script, common.Testnet); ad != nil {
ads = ad.String()
}
fmt.Fprint(w, "<addr>", ads, "</addr>")
fmt.Fprint(w, "<block>", po.BlockHeight, "</block>")
} else {
w.Write([]byte("<status>UNKNOWN INPUT</status>"))
}
w.Write([]byte("</input>"))
}
w.Write([]byte("</inputs>"))
w.Write([]byte("<outputs>"))
for i := range tx.TxOut {
w.Write([]byte("<output>"))
fmt.Fprint(w, "<value>", tx.TxOut[i].Value, "</value>")
adr := btc.NewAddrFromPkScript(tx.TxOut[i].Pk_script, common.Testnet)
if adr != nil {
fmt.Fprint(w, "<addr>", adr.String(), "</addr>")
} else {
fmt.Fprint(w, "<addr>", "scr:"+hex.EncodeToString(tx.TxOut[i].Pk_script), "</addr>")
}
w.Write([]byte("</output>"))
}
w.Write([]byte("</outputs>"))
} else {
w.Write([]byte("<status>Not found</status>"))
}
w.Write([]byte("</tx>"))
}
开发者ID:liudch,项目名称:gocoin,代码行数:59,代码来源:txs.go
示例7: DecodeTx
func DecodeTx(tx *btc.Tx) (s string, missinginp bool, totinp, totout uint64, e error) {
s += fmt.Sprintln("Transaction details (for your information):")
s += fmt.Sprintln(len(tx.TxIn), "Input(s):")
for i := range tx.TxIn {
s += fmt.Sprintf(" %3d %s", i, tx.TxIn[i].Input.String())
var po *btc.TxOut
inpid := btc.NewUint256(tx.TxIn[i].Input.Hash[:])
if txinmem, ok := network.TransactionsToSend[inpid.BIdx()]; ok {
s += fmt.Sprint(" mempool")
if int(tx.TxIn[i].Input.Vout) >= len(txinmem.TxOut) {
s += fmt.Sprintf(" - Vout TOO BIG (%d/%d)!", int(tx.TxIn[i].Input.Vout), len(txinmem.TxOut))
} else {
po = txinmem.TxOut[tx.TxIn[i].Input.Vout]
}
} else {
po, _ = common.BlockChain.Unspent.UnspentGet(&tx.TxIn[i].Input)
if po != nil {
s += fmt.Sprintf("%8d", po.BlockHeight)
}
}
if po != nil {
ok := script.VerifyTxScript(tx.TxIn[i].ScriptSig, po.Pk_script, i, tx, true)
if !ok {
s += fmt.Sprintln("\nERROR: The transacion does not have a valid signature.")
e = errors.New("Invalid signature")
return
}
totinp += po.Value
ads := "???"
if ad := btc.NewAddrFromPkScript(po.Pk_script, common.Testnet); ad != nil {
ads = ad.String()
}
s += fmt.Sprintf(" %15.8f BTC @ %s\n", float64(po.Value)/1e8, ads)
} else {
s += fmt.Sprintln(" - UNKNOWN INPUT")
missinginp = true
}
}
s += fmt.Sprintln(len(tx.TxOut), "Output(s):")
for i := range tx.TxOut {
totout += tx.TxOut[i].Value
adr := btc.NewAddrFromPkScript(tx.TxOut[i].Pk_script, common.Testnet)
if adr != nil {
s += fmt.Sprintf(" %15.8f BTC to adr %s\n", float64(tx.TxOut[i].Value)/1e8, adr.String())
} else {
s += fmt.Sprintf(" %15.8f BTC to scr %s\n", float64(tx.TxOut[i].Value)/1e8, hex.EncodeToString(tx.TxOut[i].Pk_script))
}
}
if missinginp {
s += fmt.Sprintln("WARNING: There are missing inputs and we cannot calc input BTC amount.")
s += fmt.Sprintln("If there is somethign wrong with this transaction, you can loose money...")
} else {
s += fmt.Sprintf("All OK: %.8f BTC in -> %.8f BTC out, with %.8f BTC fee\n", float64(totinp)/1e8,
float64(totout)/1e8, float64(totinp-totout)/1e8)
}
return
}
开发者ID:vancsj,项目名称:gocoin,代码行数:59,代码来源:usif.go
示例8: AcceptBlock
// This function either appends a new block at the end of the existing chain
// in which case it also applies all the transactions to the unspent database.
// If the block does is not the heighest, it is added to the chain, but maked
// as an orphan - its transaction will be verified only if the chain would swap
// to its branch later on.
func (ch *Chain) AcceptBlock(bl *btc.Block) (e error) {
prevblk, ok := ch.BlockIndex[btc.NewUint256(bl.ParentHash()).BIdx()]
if !ok {
panic("This should not happen")
}
// create new BlockTreeNode
cur := new(BlockTreeNode)
cur.BlockHash = bl.Hash
cur.Parent = prevblk
cur.Height = prevblk.Height + 1
cur.TxCount = uint32(bl.TxCount)
copy(cur.BlockHeader[:], bl.Raw[:80])
// Add this block to the block index
ch.BlockIndexAccess.Lock()
prevblk.addChild(cur)
ch.BlockIndex[cur.BlockHash.BIdx()] = cur
ch.BlockIndexAccess.Unlock()
if ch.BlockTreeEnd == prevblk {
// The head of out chain - apply the transactions
var changes *BlockChanges
changes, e = ch.ProcessBlockTransactions(bl, cur.Height)
if e != nil {
// ProcessBlockTransactions failed, so trash the block.
println("ProcessBlockTransactions ", cur.BlockHash.String(), cur.Height, e.Error())
ch.BlockIndexAccess.Lock()
cur.Parent.delChild(cur)
delete(ch.BlockIndex, cur.BlockHash.BIdx())
ch.BlockIndexAccess.Unlock()
} else {
// ProcessBlockTransactions succeeded, so save the block as "trusted".
bl.Trusted = true
ch.Blocks.BlockAdd(cur.Height, bl)
// Apply the block's trabnsactions to the unspent database:
changes.LastKnownHeight = bl.LastKnownHeight
ch.Unspent.CommitBlockTxs(changes, bl.Hash.Hash[:])
if !ch.DoNotSync {
ch.Blocks.Sync()
}
ch.BlockTreeEnd = cur // Advance the head
}
} else {
// The block's parent is not the current head of the chain...
// Save the block, though do not makt it as "trusted" just yet
ch.Blocks.BlockAdd(cur.Height, bl)
// If it has a bigger height than the current head,
// ... move the coin state into a new branch.
if cur.Height > ch.BlockTreeEnd.Height {
ch.MoveToBlock(cur)
}
}
return
}
开发者ID:vancsj,项目名称:gocoin,代码行数:64,代码来源:chain_accept.go
示例9: BlockInvalid
func (db *BlockDB) BlockInvalid(hash []byte) {
idx := btc.NewUint256(hash).BIdx()
db.mutex.Lock()
cur, ok := db.blockIndex[idx]
if !ok {
db.mutex.Unlock()
println("BlockInvalid: no such block")
return
}
println("mark", btc.NewUint256(hash).String(), "as invalid")
if cur.trusted {
panic("if it is trusted - how can be invalid?")
}
db.setBlockFlag(cur, BLOCK_INVALID)
delete(db.blockIndex, idx)
db.mutex.Unlock()
}
开发者ID:vancsj,项目名称:gocoin,代码行数:17,代码来源:blockdb.go
示例10: print_record
func print_record(sl []byte) {
bh := btc.NewSha2Hash(sl[56:136])
fmt.Print("Block ", bh.String(), " Height ", binary.LittleEndian.Uint32(sl[36:40]))
fmt.Println()
fmt.Println(" ...", binary.LittleEndian.Uint32(sl[48:52]), " Bytes @ ",
binary.LittleEndian.Uint64(sl[40:48]), " in dat file")
hdr := sl[56:136]
fmt.Println(" ->", btc.NewUint256(hdr[4:36]).String())
}
开发者ID:piotrnar,项目名称:gocoin,代码行数:9,代码来源:bdb.go
示例11: TxInvNotify
// Handle tx-inv notifications
func (c *OneConnection) TxInvNotify(hash []byte) {
if NeedThisTx(btc.NewUint256(hash), nil) {
var b [1 + 4 + 32]byte
b[0] = 1 // One inv
b[1] = 1 // Tx
copy(b[5:37], hash)
c.SendRawMsg("getdata", b[:])
}
}
开发者ID:vancsj,项目名称:gocoin,代码行数:10,代码来源:txpool.go
示例12: BlockInvalid
func (db *BlockDB) BlockInvalid(hash []byte) {
idx := btc.NewUint256(hash).BIdx()
db.mutex.Lock()
cur, ok := db.blockIndex[idx]
if !ok {
db.mutex.Unlock()
println("BlockInvalid: no such block", btc.NewUint256(hash).String())
return
}
if cur.trusted {
println("Looks like your UTXO database is corrupt")
println("To rebuild it, remove folder: " + db.dirname + "unspent4")
panic("Trusted block cannot be invalid")
}
//println("mark", btc.NewUint256(hash).String(), "as invalid")
db.setBlockFlag(cur, BLOCK_INVALID)
delete(db.blockIndex, idx)
db.mutex.Unlock()
}
开发者ID:piotrnar,项目名称:gocoin,代码行数:19,代码来源:blockdb.go
示例13: DumpBalance
// Call it only from the Chain thread
func DumpBalance(mybal chain.AllUnspentTx, utxt *os.File, details, update_balance bool) (s string) {
var sum uint64
BalanceMutex.Lock()
for i := range mybal {
sum += mybal[i].Value
if details {
if i < 100 {
s += fmt.Sprintf("%7d %s\n", 1+common.Last.Block.Height-mybal[i].MinedAt,
mybal[i].String())
} else if i == 100 {
s += fmt.Sprintln("List of unspent outputs truncated to 100 records")
}
}
// update the balance/ folder
if utxt != nil {
po, e := common.BlockChain.Unspent.UnspentGet(&mybal[i].TxPrevOut)
if e != nil {
println("UnspentGet:", e.Error())
println("This should not happen - please, report a bug.")
println("You can probably fix it by launching the client with -rescan")
os.Exit(1)
}
txid := btc.NewUint256(mybal[i].TxPrevOut.Hash[:])
// Store the unspent line in balance/unspent.txt
fmt.Fprintln(utxt, mybal[i].UnspentTextLine())
// store the entire transactiojn in balance/<txid>.tx
fn := "balance/" + txid.String()[:64] + ".tx"
txf, _ := os.Open(fn)
if txf == nil {
// Do it only once per txid
txf, _ = os.Create(fn)
if txf == nil {
println("Cannot create ", fn)
os.Exit(1)
}
GetRawTransaction(po.BlockHeight, txid, txf)
}
txf.Close()
}
}
if update_balance {
LastBalance = sum
}
BalanceMutex.Unlock()
s += fmt.Sprintf("Total balance: %.8f BTC in %d unspent outputs\n", float64(sum)/1e8, len(mybal))
if utxt != nil {
utxt.Close()
}
return
}
开发者ID:vancsj,项目名称:gocoin,代码行数:56,代码来源:balance.go
示例14: loadBlockIndex
// Loads block index from the disk
func (ch *Chain) loadBlockIndex() {
ch.BlockIndex = make(map[[btc.Uint256IdxLen]byte]*BlockTreeNode, BlockMapInitLen)
ch.BlockTreeRoot = new(BlockTreeNode)
ch.BlockTreeRoot.BlockHash = ch.Genesis
ch.RebuildGenesisHeader()
ch.BlockIndex[ch.Genesis.BIdx()] = ch.BlockTreeRoot
ch.Blocks.LoadBlockIndex(ch, nextBlock)
tlb := ch.Unspent.LastBlockHash
//println("Building tree from", len(ch.BlockIndex), "nodes")
for k, v := range ch.BlockIndex {
if AbortNow {
return
}
if v == ch.BlockTreeRoot {
// skip root block (should be only one)
continue
}
par, ok := ch.BlockIndex[btc.NewUint256(v.BlockHeader[4:36]).BIdx()]
if !ok {
println("ERROR: Block", v.Height, v.BlockHash.String(), "has no Parent")
println("...", btc.NewUint256(v.BlockHeader[4:36]).String(), "- removing it from blocksDB")
delete(ch.BlockIndex, k)
continue
}
v.Parent = par
v.Parent.addChild(v)
}
if tlb == nil {
//println("No last block - full rescan will be needed")
ch.BlockTreeEnd = ch.BlockTreeRoot
return
} else {
//println("Last Block Hash:", btc.NewUint256(tlb).String())
var ok bool
ch.BlockTreeEnd, ok = ch.BlockIndex[btc.NewUint256(tlb).BIdx()]
if !ok {
panic("Last btc.Block Hash not found")
}
}
}
开发者ID:piotrnar,项目名称:gocoin,代码行数:43,代码来源:chain_load.go
示例15: 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())
}
开发者ID:piotrnar,项目名称:gocoin,代码行数:54,代码来源:cblk.go
示例16: send_all_tx
func send_all_tx(par string) {
network.TxMutex.Lock()
for k, v := range network.TransactionsToSend {
if v.Own != 0 {
cnt := network.NetRouteInv(1, btc.NewUint256(k[:]), nil)
v.Invsentcnt += cnt
fmt.Println("INV for TxID", v.Hash.String(), "sent to", cnt, "node(s)")
}
}
network.TxMutex.Unlock()
}
开发者ID:piotrnar,项目名称:gocoin,代码行数:11,代码来源:txcmds.go
示例17: baned_txs
func baned_txs(par string) {
fmt.Println("Rejected transactions:")
cnt := 0
network.TxMutex.Lock()
for k, v := range network.TransactionsRejected {
cnt++
fmt.Println("", cnt, btc.NewUint256(k[:]).String(), "-", v.Size, "bytes",
"-", v.Reason, "-", time.Now().Sub(v.Time).String(), "ago")
}
network.TxMutex.Unlock()
}
开发者ID:piotrnar,项目名称:gocoin,代码行数:11,代码来源:txcmds.go
示例18: chkblock
func chkblock(bl *btc.Block) (er error) {
// Check timestamp (must not be higher than now +2 hours)
if int64(bl.BlockTime()) > time.Now().Unix()+2*60*60 {
er = errors.New("CheckBlock() : block timestamp too far in the future")
return
}
MemBlockChainMutex.Lock()
if prv, pres := MemBlockChain.BlockIndex[bl.Hash.BIdx()]; pres {
MemBlockChainMutex.Unlock()
if prv.Parent == nil {
// This is genesis block
er = errors.New("Genesis")
return
} else {
return
}
}
prevblk, ok := MemBlockChain.BlockIndex[btc.NewUint256(bl.ParentHash()).BIdx()]
if !ok {
er = errors.New("CheckBlock: " + bl.Hash.String() + " parent not found")
return
}
// Check proof of work
gnwr := MemBlockChain.GetNextWorkRequired(prevblk, bl.BlockTime())
if bl.Bits() != gnwr {
if !Testnet || ((prevblk.Height+1)%2016) != 0 {
MemBlockChainMutex.Unlock()
er = errors.New(fmt.Sprint("CheckBlock: Incorrect proof of work at block", prevblk.Height+1))
return
}
}
cur := new(chain.BlockTreeNode)
cur.BlockHash = bl.Hash
cur.Parent = prevblk
cur.Height = prevblk.Height + 1
cur.TxCount = uint32(bl.TxCount)
copy(cur.BlockHeader[:], bl.Raw[:80])
prevblk.Childs = append(prevblk.Childs, cur)
MemBlockChain.BlockIndex[cur.BlockHash.BIdx()] = cur
MemBlockChainMutex.Unlock()
LastBlock.Mutex.Lock()
if cur.Height > LastBlock.node.Height {
LastBlock.node = cur
}
LastBlock.Mutex.Unlock()
return
}
开发者ID:liudch,项目名称:gocoin,代码行数:53,代码来源:hdrs.go
示例19: loadBlockIndex
// Loads block index from the disk
func (ch *Chain) loadBlockIndex() {
ch.BlockIndex = make(map[[btc.Uint256IdxLen]byte]*BlockTreeNode, BlockMapInitLen)
ch.BlockTreeRoot = new(BlockTreeNode)
ch.BlockTreeRoot.BlockHash = ch.Genesis
ch.BlockIndex[ch.Genesis.BIdx()] = ch.BlockTreeRoot
ch.Blocks.LoadBlockIndex(ch, nextBlock)
tlb := ch.Unspent.GetLastBlockHash()
//println("Building tree from", len(ch.BlockIndex), "nodes")
for _, v := range ch.BlockIndex {
if AbortNow {
return
}
if v == ch.BlockTreeRoot {
// skip root block (should be only one)
continue
}
par, ok := ch.BlockIndex[btc.NewUint256(v.BlockHeader[4:36]).BIdx()]
if !ok {
panic(v.BlockHash.String() + " has no Parent " + btc.NewUint256(v.BlockHeader[4:36]).String())
}
/*if par.Height+1 != v.Height {
panic("height mismatch")
}*/
v.Parent = par
v.Parent.addChild(v)
}
if tlb == nil {
//println("No last block - full rescan will be needed")
ch.BlockTreeEnd = ch.BlockTreeRoot
return
} else {
var ok bool
ch.BlockTreeEnd, ok = ch.BlockIndex[btc.NewUint256(tlb).BIdx()]
if !ok {
panic("Last btc.Block Hash not found")
}
}
}
开发者ID:vancsj,项目名称:gocoin,代码行数:41,代码来源:chain_load.go
示例20: TxInvNotify
// Handle tx-inv notifications
func (c *OneConnection) TxInvNotify(hash []byte) {
if NeedThisTx(btc.NewUint256(hash), nil) {
var b [1 + 4 + 32]byte
b[0] = 1 // One inv
if (c.Node.Services & SERVICE_SEGWIT) != 0 {
binary.LittleEndian.PutUint32(b[1:5], MSG_WITNESS_TX) // SegWit Tx
//println(c.ConnID, "getdata", btc.NewUint256(hash).String())
} else {
b[1] = 1 // Tx
}
copy(b[5:37], hash)
c.SendRawMsg("getdata", b[:])
}
}
开发者ID:piotrnar,项目名称:gocoin,代码行数:15,代码来源:txpool.go
注:本文中的github.com/piotrnar/gocoin/lib/btc.NewUint256函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论