func TestBlockBodyHash(t *testing.T) { b := makeNewBlock() hashes := b.Body.Transactions.Hashes() assert.Equal(t, b.Body.Hash(), cipher.Merkle(hashes)) tx1 := addTransactionToBlock(t, &b) hashes = append(hashes, tx1.Hash()) assert.Equal(t, b.Body.Hash(), cipher.Merkle(hashes)) tx2 := addTransactionToBlock(t, &b) hashes = append(hashes, tx2.Hash()) assert.Equal(t, b.Body.Hash(), cipher.Merkle(hashes)) assert.Equal(t, b.HashBody(), b.Body.Hash()) }
// Returns the merkle hash of contained transactions func (self *BlockBody) Hash() cipher.SHA256 { hashes := make([]cipher.SHA256, len(self.Transactions)) for i, _ := range self.Transactions { hashes[i] = self.Transactions[i].Hash() } // Merkle hash of transactions return cipher.Merkle(hashes) }
// Hash returns the merkle hash of contained transactions func (bb BlockBody) Hash() cipher.SHA256 { hashes := make([]cipher.SHA256, len(bb.Transactions)) for i := range bb.Transactions { hashes[i] = bb.Transactions[i].Hash() } // Merkle hash of transactions return cipher.Merkle(hashes) }
func TestCreateGenesisBlock(t *testing.T) { ft := FakeTree{} now := tNow() b := NewBlockchain(&ft, nil) b.CreateGenesisBlock(genAddress, _genCoins, now) // gb := createGenesisBlock(genAddress, now, _genCoins) assert.Equal(t, b.Time(), now) assert.Equal(t, b.Head().Head.BkSeq, uint64(0)) assert.Equal(t, len(b.Head().Body.Transactions), 1) assert.Equal(t, len(b.Head().Body.Transactions[0].Out), 1) assert.Equal(t, len(b.Head().Body.Transactions[0].In), 0) txn := b.Head().Body.Transactions[0] txo := txn.Out[0] assert.Equal(t, txo.Address, genAddress) assert.Equal(t, txo.Coins, _genCoins) assert.Equal(t, txo.Hours, _genCoins) assert.Equal(t, len(b.GetUnspent().Pool), 1) ux := b.GetUnspent().Array()[0] assert.Equal(t, ux.Head.BkSeq, uint64(0)) assert.Equal(t, ux.Head.Time, now) assert.Equal(t, ux.Head.Time, b.Head().Head.Time) assert.Equal(t, ux.Body.SrcTransaction, txn.InnerHash) assert.Equal(t, ux.Body.Address, genAddress) assert.Equal(t, ux.Body.Coins, _genCoins) assert.Equal(t, txo.Coins, ux.Body.Coins) assert.Equal(t, txo.Hours, ux.Body.Hours) // 1 hour per coin, at init assert.Equal(t, ux.Body.Hours, _genCoins) h := cipher.Merkle([]cipher.SHA256{b.Head().Body.Transactions[0].Hash()}) assert.Equal(t, b.Head().Head.BodyHash, h) assert.Equal(t, b.Head().Head.PrevHash, cipher.SHA256{}) // TODO -- check valid snapshot assert.NotEqual(t, b.Head().Head.UxHash, [4]byte{}) expect := coin.CreateUnspents(b.Head().Head, txn) expect.Sort() have := b.GetUnspent().Array() have.Sort() assert.Equal(t, expect, have) // Panicing assert.Panics(t, func() { b.CreateGenesisBlock(genAddress, _genCoins, _genTime) }) }