tendermint/blocks/block_test.go

127 lines
3.6 KiB
Go
Raw Normal View History

2014-06-05 02:34:45 -07:00
package blocks
import (
2014-07-01 14:50:24 -07:00
"bytes"
. "github.com/tendermint/tendermint/binary"
2014-10-04 19:16:49 -07:00
. "github.com/tendermint/tendermint/common"
2014-07-01 14:50:24 -07:00
"testing"
2014-06-05 02:34:45 -07:00
)
2014-10-04 19:16:49 -07:00
func randSig() Signature {
return Signature{RandUInt64Exp(), RandBytes(32)}
2014-06-05 02:34:45 -07:00
}
2014-10-30 03:32:09 -07:00
func randRoundSig() RoundSignature {
return RoundSignature{RandUInt16(), randSig()}
}
2014-10-04 19:16:49 -07:00
func randBaseTx() BaseTx {
2014-10-12 21:14:10 -07:00
return BaseTx{0, RandUInt64Exp(), randSig()}
2014-06-05 21:20:55 -07:00
}
func randBlock() *Block {
2014-09-10 02:43:16 -07:00
// Account Txs
2014-07-01 14:50:24 -07:00
sendTx := &SendTx{
2014-10-04 19:16:49 -07:00
BaseTx: randBaseTx(),
To: RandUInt64Exp(),
Amount: RandUInt64Exp(),
2014-07-01 14:50:24 -07:00
}
nameTx := &NameTx{
2014-10-04 19:16:49 -07:00
BaseTx: randBaseTx(),
Name: string(RandBytes(12)),
PubKey: RandBytes(32),
2014-07-01 14:50:24 -07:00
}
// Validation Txs
2014-10-04 19:16:49 -07:00
bondTx := &BondTx{
2014-10-12 21:14:10 -07:00
BaseTx: randBaseTx(),
//UnbondTo: RandUInt64Exp(),
2014-07-01 14:50:24 -07:00
}
2014-10-04 19:16:49 -07:00
unbondTx := &UnbondTx{
BaseTx: randBaseTx(),
2014-07-01 14:50:24 -07:00
}
2014-10-04 19:16:49 -07:00
dupeoutTx := &DupeoutTx{
VoteA: Vote{
Height: RandUInt32Exp(),
Round: RandUInt16Exp(),
Type: VoteTypePrevote,
2014-10-04 19:16:49 -07:00
BlockHash: RandBytes(32),
2014-07-01 14:50:24 -07:00
Signature: randSig(),
},
2014-10-04 19:16:49 -07:00
VoteB: Vote{
Height: RandUInt32Exp(),
Round: RandUInt16Exp(),
Type: VoteTypePrevote,
2014-10-04 19:16:49 -07:00
BlockHash: RandBytes(32),
2014-07-01 14:50:24 -07:00
Signature: randSig(),
},
}
// Block
block := &Block{
Header: &Header{
Network: "Tendermint",
Height: RandUInt32Exp(),
Fees: RandUInt64Exp(),
Time: RandTime(),
LastBlockHash: RandBytes(32),
StateHash: RandBytes(32),
2014-07-01 14:50:24 -07:00
},
Validation: &Validation{
2014-10-30 03:32:09 -07:00
Commits: []RoundSignature{randRoundSig(), randRoundSig()},
2014-07-01 14:50:24 -07:00
},
Data: &Data{
2014-10-12 21:14:10 -07:00
Txs: []Tx{sendTx, nameTx, bondTx, unbondTx, dupeoutTx},
2014-07-01 14:50:24 -07:00
},
}
return block
}
2014-07-01 14:50:24 -07:00
func TestBlock(t *testing.T) {
2014-07-01 14:50:24 -07:00
block := randBlock()
// Mutate the block and ensure that the hash changed.
lastHash := block.Hash()
expectChange := func(mutateFn func(b *Block), message string) {
// mutate block
mutateFn(block)
// nuke hashes
block.hash = nil
block.Header.hash = nil
block.Validation.hash = nil
block.Data.hash = nil
// compare
if bytes.Equal(lastHash, block.Hash()) {
t.Error(message)
} else {
lastHash = block.Hash()
}
}
expectChange(func(b *Block) { b.Header.Network = "blah" }, "Expected hash to depend on Network")
expectChange(func(b *Block) { b.Header.Height += 1 }, "Expected hash to depend on Height")
expectChange(func(b *Block) { b.Header.Fees += 1 }, "Expected hash to depend on Fees")
expectChange(func(b *Block) { b.Header.Time = RandTime() }, "Expected hash to depend on Time")
expectChange(func(b *Block) { b.Header.LastBlockHash = RandBytes(32) }, "Expected hash to depend on LastBlockHash")
expectChange(func(b *Block) { b.Header.StateHash = RandBytes(32) }, "Expected hash to depend on StateHash")
2014-10-30 03:32:09 -07:00
expectChange(func(b *Block) { b.Validation.Commits[0].Round += 1 }, "Expected hash to depend on Validation Commit")
expectChange(func(b *Block) { b.Validation.Commits[0].SignerId += 1 }, "Expected hash to depend on Validation Commit")
expectChange(func(b *Block) { b.Validation.Commits[0].Bytes = RandBytes(32) }, "Expected hash to depend on Validation Commit")
expectChange(func(b *Block) { b.Data.Txs[0].(*SendTx).Signature.SignerId += 1 }, "Expected hash to depend on tx Signature")
expectChange(func(b *Block) { b.Data.Txs[0].(*SendTx).Amount += 1 }, "Expected hash to depend on send tx Amount")
// Write the block, read it in again, check hash.
block1 := randBlock()
block1Bytes := BinaryBytes(block1)
var n int64
var err error
block2 := ReadBlock(bytes.NewReader(block1Bytes), &n, &err)
2014-10-04 19:16:49 -07:00
if err != nil {
t.Errorf("Reading block failed: %v", err)
}
if !bytes.Equal(block1.Hash(), block2.Hash()) {
t.Errorf("Expected write/read to preserve original hash")
t.Logf("\nBlock1:\n%v", block1)
t.Logf("\nBlock2:\n%v", block2)
2014-07-01 14:50:24 -07:00
}
2014-06-05 18:17:09 -07:00
}