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()) }
func makeNewBlock() coin.Block { unsp := coin.NewUnspentPool() body := coin.BlockBody{ Transactions: coin.Transactions{coin.Transaction{}}, } prev := coin.Block{ Body: body, Head: coin.BlockHeader{ Version: 0x02, Time: 100, BkSeq: 0, Fee: 10, PrevHash: cipher.SHA256{}, BodyHash: body.Hash(), }} return coin.NewBlock(prev, 100+20, unsp, coin.Transactions{coin.Transaction{}}, _feeCalc) }
// NewBlockFromTransactions creates a Block given an array of Transactions. It does not verify the // block; ExecuteBlock will handle verification. Transactions must be sorted. func (bc Blockchain) NewBlockFromTransactions(txns coin.Transactions, currentTime uint64) (coin.Block, error) { if currentTime <= bc.Time() { log.Panic("Time can only move forward") } if len(txns) == 0 { return coin.Block{}, errors.New("No transactions") } err := bc.verifyTransactions(txns) if err != nil { return coin.Block{}, err } b := coin.NewBlock(*bc.Head(), currentTime, bc.unspent, txns, bc.TransactionFee) //make sure block is valid if DebugLevel2 == true { if err := bc.verifyBlockHeader(b); err != nil { log.Panic("Impossible Error: not allowed to fail") } if err := bc.verifyTransactions(b.Body.Transactions); err != nil { log.Panic("Impossible Error: not allowed to fail") } } return b, nil }