• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Golang coin.Block类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Golang中github.com/skycoin/skycoin/src/coin.Block的典型用法代码示例。如果您正苦于以下问题:Golang Block类的具体用法?Golang Block怎么用?Golang Block使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



在下文中一共展示了Block类的18个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。

示例1: TestNewBlock

func TestNewBlock(t *testing.T) {
	// TODO -- update this test for newBlock changes
	prev := coin.Block{Head: coin.BlockHeader{Version: 0x02, Time: 100, BkSeq: 98}}
	unsp := coin.NewUnspentPool()
	unsp.XorHash = randSHA256()
	txns := coin.Transactions{coin.Transaction{}}
	// invalid txn fees panics
	assert.Panics(t, func() { coin.NewBlock(prev, 133, unsp, txns, _badFeeCalc) })
	// no txns panics
	assert.Panics(t, func() {
		coin.NewBlock(prev, 133, unsp, nil, _feeCalc)
	})
	assert.Panics(t, func() {
		coin.NewBlock(prev, 133, unsp, coin.Transactions{}, _feeCalc)
	})
	// valid block is fine
	fee := uint64(121)
	currentTime := uint64(133)
	b := coin.NewBlock(prev, currentTime, unsp, txns, _makeFeeCalc(fee))
	assert.Equal(t, b.Body.Transactions, txns)
	assert.Equal(t, b.Head.Fee, fee*uint64(len(txns)))
	assert.Equal(t, b.Body, coin.BlockBody{txns})
	assert.Equal(t, b.Head.PrevHash, prev.HashHeader())
	assert.Equal(t, b.Head.Time, currentTime)
	assert.Equal(t, b.Head.BkSeq, prev.Head.BkSeq+1)
	assert.Equal(t, b.Head.UxHash,
		unsp.GetUxHash())
}
开发者ID:skycoin,项目名称:skycoin,代码行数:28,代码来源:blockchain_test.go


示例2: ExecuteBlock

// ExecuteBlock Attempts to append block to blockchain.  Returns the UxOuts created,
// and an error if the block is invalid.
func (bc *Blockchain) ExecuteBlock(b *coin.Block) (coin.UxArray, error) {
	var uxs coin.UxArray
	err := bc.verifyBlock(*b)
	if err != nil {
		return nil, err
	}
	txns := b.Body.Transactions
	for _, tx := range txns {
		// Remove spent outputs
		bc.unspent.DelMultiple(tx.In)
		// Create new outputs
		txUxs := coin.CreateUnspents(b.Head, tx)
		for i := range txUxs {
			bc.unspent.Add(txUxs[i])
		}
		uxs = append(uxs, txUxs...)
	}

	b.Head.PrevHash = bc.head
	bc.addBlock(b)

	// update the head
	bc.head = b.HashHeader()
	bc.notify(*b)
	return uxs, nil
}
开发者ID:skycoin,项目名称:skycoin,代码行数:28,代码来源:blockchain.go


示例3: signBlock

// Signs a block for master.  Will panic if anything is invalid
func (self *Blockchain) signBlock(b coin.Block) SignedBlock {
	if !self.IsMaster {
		log.Panic("Only master chain can sign blocks")
	}
	sig := coin.SignHash(b.HashHeader(), self.SecKey)
	sb := SignedBlock{
		Block: b,
		Sig:   sig,
	}
	return sb
}
开发者ID:up4k,项目名称:skycoin,代码行数:12,代码来源:blockchain.go


示例4: SignBlock

// Signs a block for master.  Will panic if anything is invalid
func (self *Visor) SignBlock(b coin.Block) SignedBlock {
	if !self.Config.IsMaster {
		log.Panic("Only master chain can sign blocks")
	}
	sig := cipher.SignHash(b.HashHeader(), self.Config.BlockchainSeckey)
	sb := SignedBlock{
		Block: b,
		Sig:   sig,
	}
	return sb
}
开发者ID:keepwalking1234,项目名称:skycoin,代码行数:12,代码来源:visor.go


示例5: hasChild

// check if this block has children
func hasChild(bkt *bolt.Bucket, b coin.Block) (bool, error) {
	// get the child block hash pair, whose pre hash point to current block.
	childHashPair, err := getHashPairInDepth(bkt, b.Head.BkSeq+1, func(hp coin.HashPair) bool {
		return hp.PreHash == b.HashHeader()
	})

	if err != nil {
		return false, nil
	}

	return len(childHashPair) > 0, nil
}
开发者ID:skycoin,项目名称:skycoin,代码行数:13,代码来源:block_tree.go


示例6: testCase

func testCase(t *testing.T, cases []blockCase) {
	_, teardown, err := setup(t)
	if err != nil {
		t.Fatal(err)
	}

	defer teardown()

	btree := NewBlockTree()
	blocks := make([]coin.Block, len(cases))
	for i, d := range cases {
		var preHash cipher.SHA256
		if d.BInfo.Pre != -1 {
			preHash = blocks[d.BInfo.Pre].HashHeader()
		}

		b := coin.Block{
			Head: coin.BlockHeader{
				BkSeq:    d.BInfo.Seq,
				Time:     d.BInfo.Time,
				Fee:      d.BInfo.Fee,
				PrevHash: preHash,
			},
		}
		blocks[i] = b

		switch d.Action {
		case "add":
			err := btree.AddBlock(&b)
			if err != d.Err {
				t.Fatal(fmt.Sprintf("expect err:%v, but get err:%v", d.Err, err))
			}

			if err == nil {
				b1 := btree.GetBlock(b.HashHeader())
				assert.Equal(t, *b1, b)
			}
		case "remove":
			err := btree.RemoveBlock(&b)
			if err != d.Err {
				t.Fatal(fmt.Sprintf("expect err:%v, but get err:%v", d.Err, err))
			}
			if err == nil {
				b1 := btree.GetBlock(b.HashHeader())
				assert.Nil(t, b1)
			}
		}
	}

}
开发者ID:skycoin,项目名称:skycoin,代码行数:50,代码来源:block_tree_test.go


示例7: NewReadableBlockBody

func NewReadableBlockBody(b *coin.Block) ReadableBlockBody {
	txns := make([]ReadableTransaction, len(b.Body.Transactions))
	for i := range b.Body.Transactions {
		if b.Seq() == uint64(0) {
			// genesis block
			txns[i] = NewGenesisReadableTransaction(&Transaction{Txn: b.Body.Transactions[i]})
		} else {
			txns[i] = NewReadableTransaction(&Transaction{Txn: b.Body.Transactions[i]})
		}
	}
	return ReadableBlockBody{
		Transactions: txns,
	}
}
开发者ID:skycoin,项目名称:skycoin,代码行数:14,代码来源:readable.go


示例8: verifyBlock

// VerifyBlock verifies the BlockHeader and BlockBody
func (bc Blockchain) verifyBlock(b coin.Block) error {
	gb := bc.GetGenesisBlock()
	if gb.HashHeader() != b.HashHeader() {
		if err := bc.verifyBlockHeader(b); err != nil {
			return err
		}

		if err := bc.verifyTransactions(b.Body.Transactions); err != nil {
			return err
		}
	}

	if err := bc.verifyUxHash(b); err != nil {
		return err
	}
	return nil
}
开发者ID:skycoin,项目名称:skycoin,代码行数:18,代码来源:blockchain.go


示例9: verifyBlockHeader

// VerifyBlockHeader Returns error if the BlockHeader is not valid
func (bc Blockchain) verifyBlockHeader(b coin.Block) error {
	//check BkSeq
	head := bc.Head()
	if b.Head.BkSeq != head.Head.BkSeq+1 {
		return errors.New("BkSeq invalid")
	}
	//check Time, only requirement is that its monotonely increasing
	if b.Head.Time <= head.Head.Time {
		return errors.New("Block time must be > head time")
	}
	// Check block hash against previous head
	if b.Head.PrevHash != head.HashHeader() {
		return errors.New("PrevHash does not match current head")
	}
	if b.HashBody() != b.Head.BodyHash {
		return errors.New("Computed body hash does not match")
	}
	return nil
}
开发者ID:skycoin,项目名称:skycoin,代码行数:20,代码来源:blockchain.go


示例10: RemoveBlock

// RemoveBlock remove block from blocks bucket and tree bucket.
// can't remove block if it has children.
func (bt *BlockTree) RemoveBlock(b *coin.Block) error {
	return UpdateTx(func(tx *bolt.Tx) error {
		// delete block in blocks bucket.
		blocks := tx.Bucket(bt.blocks.Name)
		hash := b.HashHeader()
		if err := blocks.Delete(hash[:]); err != nil {
			return err
		}

		// get tree bucket.
		tree := tx.Bucket(bt.tree.Name)

		// check if this block has children
		has, err := hasChild(tree, *b)
		if err != nil {
			return err
		}
		if has {
			return errHasChild
		}

		// get block hash pairs in depth
		hashPairs, err := getHashPairInDepth(tree, b.Seq(), func(hp coin.HashPair) bool {
			return true
		})
		if err != nil {
			return err
		}

		// remove block hash pair in tree.
		ps := removePairs(hashPairs, coin.HashPair{hash, b.PreHashHeader()})
		if len(ps) == 0 {
			tree.Delete(itob(b.Seq()))
			return nil
		}

		// update the hash pairs in tree.
		return setHashPairInDepth(tree, b.Seq(), ps)
	})
}
开发者ID:skycoin,项目名称:skycoin,代码行数:42,代码来源:block_tree.go


示例11: GetAddressTransactions

// GetAddressTransactions returns the Transactions whose unspents give coins to a cipher.Address.
// This includes unconfirmed txns' predicted unspents.
func (vs *Visor) GetAddressTransactions(a cipher.Address) []Transaction {
	var txns []Transaction
	// Look in the blockchain
	uxs := vs.Blockchain.GetUnspent().AllForAddress(a)
	mxSeq := vs.HeadBkSeq()
	var bk *coin.Block
	for _, ux := range uxs {
		if bk = vs.GetBlockBySeq(ux.Head.BkSeq); bk == nil {
			return txns
		}

		tx, ok := bk.GetTransaction(ux.Body.SrcTransaction)
		if ok {
			h := mxSeq - bk.Head.BkSeq + 1
			txns = append(txns, Transaction{
				Txn:    tx,
				Status: NewConfirmedTransactionStatus(h),
				Time:   bk.Time(),
			})
		}
	}

	// Look in the unconfirmed pool
	uxs = vs.Unconfirmed.Unspent.AllForAddress(a)
	for _, ux := range uxs {
		tx, ok := vs.Unconfirmed.Txns[ux.Body.SrcTransaction]
		if !ok {
			logger.Critical("Unconfirmed unspent missing unconfirmed txn")
			continue
		}
		txns = append(txns, Transaction{
			Txn:    tx.Txn,
			Status: NewUnconfirmedTransactionStatus(),
			Time:   uint64(tx.Received.Unix()),
		})
	}

	return txns
}
开发者ID:skycoin,项目名称:skycoin,代码行数:41,代码来源:visor.go


示例12: walkTree

func (bc *Blockchain) walkTree() {
	var dep uint64
	var preBlock *coin.Block

	for {
		b := bc.tree.GetBlockInDepth(dep, bc.walker)
		if b == nil {
			break
		}
		if dep > 0 {
			if b.PreHashHeader() != preBlock.HashHeader() {
				logger.Panicf("walk tree failed, pre hash header not match")
			}
		}
		preBlock = b
		if err := bc.updateUnspent(*b); err != nil {
			logger.Panicf("update unspent failed, err: %v", err.Error())
		}

		bc.head = b.HashHeader()
		dep++
	}
}
开发者ID:skycoin,项目名称:skycoin,代码行数:23,代码来源:blockchain.go


示例13: CreateGenesisBlock

// CreateGenesisBlock creates genesis block in blockchain.
func (bc *Blockchain) CreateGenesisBlock(genesisAddr cipher.Address, genesisCoins, timestamp uint64) coin.Block {
	txn := coin.Transaction{}
	txn.PushOutput(genesisAddr, genesisCoins, genesisCoins)
	body := coin.BlockBody{coin.Transactions{txn}}
	prevHash := cipher.SHA256{}
	head := coin.BlockHeader{
		Time:     timestamp,
		BodyHash: body.Hash(),
		PrevHash: prevHash,
		BkSeq:    0,
		Version:  0,
		Fee:      0,
		UxHash:   coin.NewUnspentPool().GetUxHash(),
	}
	b := coin.Block{
		Head: head,
		Body: body,
	}
	// b.Body.Transactions[0].UpdateHeader()
	bc.addBlock(&b)
	bc.head = b.HashHeader()
	ux := coin.UxOut{
		Head: coin.UxHead{
			Time:  timestamp,
			BkSeq: 0,
		},
		Body: coin.UxBody{
			SrcTransaction: txn.InnerHash, //user inner hash
			Address:        genesisAddr,
			Coins:          genesisCoins,
			Hours:          genesisCoins, // Allocate 1 coin hour per coin
		},
	}
	bc.unspent.Add(ux)
	bc.notify(b)
	return b
}
开发者ID:skycoin,项目名称:skycoin,代码行数:38,代码来源:blockchain.go


示例14: TestVerifyBlockHeader

func TestVerifyBlockHeader(t *testing.T) {
	ft := FakeTree{}
	bc := NewBlockchain(&ft, nil)
	gb := bc.CreateGenesisBlock(genAddress, _genCoins, _genTime)
	b := coin.Block{Body: coin.BlockBody{}}
	b.Body.Transactions = append(b.Body.Transactions, makeTransaction(t))
	h := coin.BlockHeader{}

	h.BkSeq = 1
	h.Time = gb.Head.Time + 1
	h.PrevHash = gb.HashHeader()
	h.BodyHash = b.HashBody()

	// Valid header
	b.Head = h
	assert.Nil(t, bc.verifyBlockHeader(b))

	// Invalid bkSeq
	i := h
	i.BkSeq++
	b.Head = i
	assertError(t, bc.verifyBlockHeader(b), "BkSeq invalid")

	// Invalid time
	i = h
	i.Time = gb.Head.Time
	b.Head = i
	assertError(t, bc.verifyBlockHeader(b),
		"Block time must be > head time")
	b.Head.Time--
	assertError(t, bc.verifyBlockHeader(b),
		"Block time must be > head time")

	// Invalid prevHash
	i = h
	i.PrevHash = cipher.SHA256{}
	b.Head = i
	assertError(t, bc.verifyBlockHeader(b),
		"PrevHash does not match current head")

	// Invalid bodyHash
	i = h
	i.BodyHash = cipher.SHA256{}
	b.Head = i
	assertError(t, bc.verifyBlockHeader(b),
		"Computed body hash does not match")
}
开发者ID:skycoin,项目名称:skycoin,代码行数:47,代码来源:blockchain_test.go


示例15: GetLastTxs

// GetLastTxs returns last confirmed transactions, return nil if empty
func (vs *Visor) GetLastTxs() ([]*Transaction, error) {
	ltxs, err := vs.history.GetLastTxs()
	if err != nil {
		return nil, err
	}

	txs := make([]*Transaction, len(ltxs))
	var confirms uint64
	bh := vs.GetHeadBlock().Seq()
	var b *coin.Block
	for i, tx := range ltxs {
		confirms = bh - tx.BlockSeq + 1
		if b = vs.GetBlockBySeq(tx.BlockSeq); b == nil {
			return nil, fmt.Errorf("found no block in seq %v", tx.BlockSeq)
		}

		txs[i] = &Transaction{
			Txn:    tx.Tx,
			Status: NewConfirmedTransactionStatus(confirms),
			Time:   b.Time(),
		}
	}
	return txs, nil
}
开发者ID:skycoin,项目名称:skycoin,代码行数:25,代码来源:visor.go


示例16: ProcessBlock

// ProcessBlock will index the transaction, outputs,etc.
func (hd *HistoryDB) ProcessBlock(b *coin.Block) error {
	// index the transactions
	for _, t := range b.Body.Transactions {
		tx := Transaction{
			Tx:       t,
			BlockSeq: b.Seq(),
		}
		if err := hd.txns.Add(&tx); err != nil {
			return err
		}

		// handle tx in, genesis transaction's vin is empty, so should be ignored.
		if b.Seq() > 0 {
			for _, in := range t.In {
				o, err := hd.outputs.Get(in)
				if err != nil {
					return err
				}
				// update output's spent block seq and txid.
				o.SpentBlockSeq = b.Seq()
				o.SpentTxID = t.Hash()
				if err := hd.outputs.Set(*o); err != nil {
					return err
				}
			}
		}

		// handle the tx out
		uxArray := coin.CreateUnspents(b.Head, t)
		for _, ux := range uxArray {
			uxOut := UxOut{
				Out: ux,
			}
			if err := hd.outputs.Set(uxOut); err != nil {
				return err
			}

			if err := hd.addrUx.Add(ux.Body.Address, ux.Hash()); err != nil {
				return err
			}
		}
	}
	return nil
}
开发者ID:skycoin,项目名称:skycoin,代码行数:45,代码来源:historydb.go


示例17: setBlock

func setBlock(bkt *bolt.Bucket, b *coin.Block) error {
	bin := encoder.Serialize(b)
	key := b.HashHeader()
	return bkt.Put(key[:], bin)
}
开发者ID:skycoin,项目名称:skycoin,代码行数:5,代码来源:block_tree.go


示例18: AddBlock

// AddBlock write the block into blocks bucket, add the pair of block hash and pre block hash into
// tree in the block depth.
func (bt *BlockTree) AddBlock(b *coin.Block) error {
	return UpdateTx(func(tx *bolt.Tx) error {
		blocks := tx.Bucket(bt.blocks.Name)

		// can't store block if it's not genesis block and has no parent.
		if b.Seq() > 0 && b.PreHashHeader() == emptyHash {
			return errNoParent
		}

		// check if the block already exist.
		hash := b.HashHeader()
		if blk := blocks.Get(hash[:]); blk != nil {
			return errBlockExist
		}

		// write block into blocks bucket.
		if err := setBlock(blocks, b); err != nil {
			return err
		}

		// get tree bucket.
		tree := tx.Bucket(bt.tree.Name)

		// the pre hash must be in depth - 1.
		if b.Seq() > 0 {
			preHash := b.PreHashHeader()
			parentHashPair, err := getHashPairInDepth(tree, b.Seq()-1, func(hp coin.HashPair) bool {
				return hp.Hash == preHash
			})
			if err != nil {
				return err
			}
			if len(parentHashPair) == 0 {
				return errWrongParent
			}
		}

		hp := coin.HashPair{hash, b.Head.PrevHash}

		// get block pairs in the depth
		hashPairs, err := getHashPairInDepth(tree, b.Seq(), allPairs)
		if err != nil {
			return err
		}

		if len(hashPairs) == 0 {
			// no hash pair exist in the depth.
			// write the hash pair into tree.
			return setHashPairInDepth(tree, b.Seq(), []coin.HashPair{hp})
		}

		// check dup block
		if containHash(hashPairs, hp) {
			return errBlockExist
		}

		hashPairs = append(hashPairs, hp)
		return setHashPairInDepth(tree, b.Seq(), hashPairs)
	})
}
开发者ID:skycoin,项目名称:skycoin,代码行数:62,代码来源:block_tree.go



注:本文中的github.com/skycoin/skycoin/src/coin.Block类示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Golang coin.Blockchain类代码示例发布时间:2022-05-28
下一篇:
Golang coin.SortTransactions函数代码示例发布时间:2022-05-28
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap