本文整理汇总了Golang中github.com/skycoin/skycoin/src/coin.Block.HashBody方法的典型用法代码示例。如果您正苦于以下问题:Golang Block.HashBody方法的具体用法?Golang Block.HashBody怎么用?Golang Block.HashBody使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/skycoin/skycoin/src/coin.Block
的用法示例。
在下文中一共展示了Block.HashBody方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: 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")
}
示例2: 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
}