tendermint/blocks/block_test.go

111 lines
2.2 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-04 19:16:49 -07:00
func randBaseTx() BaseTx {
return BaseTx{0, randSig()}
2014-06-05 21:20:55 -07:00
}
2014-06-05 02:34:45 -07:00
func TestBlock(t *testing.T) {
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(),
Fee: RandUInt64Exp(),
To: RandUInt64Exp(),
Amount: RandUInt64Exp(),
2014-07-01 14:50:24 -07:00
}
nameTx := &NameTx{
2014-10-04 19:16:49 -07:00
BaseTx: randBaseTx(),
Fee: RandUInt64Exp(),
Name: string(RandBytes(12)),
PubKey: RandBytes(32),
2014-07-01 14:50:24 -07:00
}
// Validation Txs
2014-07-01 14:50:24 -07:00
2014-10-04 19:16:49 -07:00
bondTx := &BondTx{
BaseTx: randBaseTx(),
Fee: RandUInt64Exp(),
UnbondTo: RandUInt64Exp(),
Amount: RandUInt64Exp(),
2014-07-01 14:50:24 -07:00
}
2014-10-04 19:16:49 -07:00
unbondTx := &UnbondTx{
BaseTx: randBaseTx(),
Fee: RandUInt64Exp(),
Amount: RandUInt64Exp(),
2014-07-01 14:50:24 -07:00
}
2014-10-04 19:16:49 -07:00
timeoutTx := &TimeoutTx{
AccountId: RandUInt64Exp(),
Penalty: RandUInt64Exp(),
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: VoteTypeBare,
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: VoteTypeBare,
BlockHash: RandBytes(32),
2014-07-01 14:50:24 -07:00
Signature: randSig(),
},
}
// Block
block := &Block{
2014-08-31 01:48:40 -07:00
Header: Header{
2014-10-04 19:16:49 -07:00
Network: "Tendermint",
Height: RandUInt32Exp(),
Fees: RandUInt64Exp(),
Time: RandTime(),
LastBlockHash: RandBytes(32),
ValidationStateHash: RandBytes(32),
AccountStateHash: RandBytes(32),
2014-07-01 14:50:24 -07:00
},
2014-08-31 01:48:40 -07:00
Validation: Validation{
2014-09-10 02:43:16 -07:00
Signatures: []Signature{randSig(), randSig()},
2014-07-01 14:50:24 -07:00
},
Data: Data{
2014-10-04 19:16:49 -07:00
Txs: []Tx{sendTx, nameTx, bondTx, unbondTx, timeoutTx, dupeoutTx},
2014-07-01 14:50:24 -07:00
},
}
// Write the block, read it in again, write it again.
// Then, compare.
2014-10-04 19:16:49 -07:00
// TODO We should compute the hash instead, so Block -> Bytes -> Block and compare hashes.
2014-07-01 14:50:24 -07:00
blockBytes := BinaryBytes(block)
var n int64
var err error
block2 := ReadBlock(bytes.NewReader(blockBytes), &n, &err)
2014-10-04 19:16:49 -07:00
if err != nil {
t.Errorf("Reading block failed: %v", err)
}
2014-07-01 14:50:24 -07:00
blockBytes2 := BinaryBytes(block2)
if !bytes.Equal(blockBytes, blockBytes2) {
2014-07-01 14:50:24 -07:00
t.Fatal("Write->Read of block failed.")
}
2014-06-05 18:17:09 -07:00
}