tendermint/blocks/block_test.go

139 lines
2.5 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"
"math/rand"
"testing"
2014-08-31 01:48:40 -07:00
"time"
2014-06-05 02:34:45 -07:00
)
// Distributed pseudo-exponentially to test for various cases
2014-08-31 01:48:40 -07:00
func randuint64() uint64 {
2014-07-01 14:50:24 -07:00
bits := rand.Uint32() % 64
if bits == 0 {
return 0
}
n := uint64(1 << (bits - 1))
n += uint64(rand.Int63()) & ((1 << (bits - 1)) - 1)
2014-08-31 01:48:40 -07:00
return n
}
func randuint32() uint32 {
bits := rand.Uint32() % 32
if bits == 0 {
return 0
}
n := uint32(1 << (bits - 1))
n += uint32(rand.Int31()) & ((1 << (bits - 1)) - 1)
return n
}
func randTime() Time {
return Time{time.Unix(int64(randuint64()), 0)}
}
func randAccount() Account {
return Account{
Id: randuint64(),
PubKey: randBytes(32),
}
2014-06-05 02:34:45 -07:00
}
func randBytes(n int) ByteSlice {
2014-07-01 14:50:24 -07:00
bs := make([]byte, n)
for i := 0; i < n; i++ {
bs[i] = byte(rand.Intn(256))
}
return bs
2014-06-05 02:34:45 -07:00
}
2014-06-05 21:20:55 -07:00
func randSig() Signature {
2014-08-31 01:48:40 -07:00
return Signature{randuint64(), randBytes(32)}
2014-06-05 21:20:55 -07:00
}
2014-06-05 02:34:45 -07:00
func TestBlock(t *testing.T) {
2014-07-01 14:50:24 -07:00
// Txs
sendTx := &SendTx{
Signature: randSig(),
2014-08-31 01:48:40 -07:00
Fee: randuint64(),
To: randuint64(),
Amount: randuint64(),
2014-07-01 14:50:24 -07:00
}
nameTx := &NameTx{
Signature: randSig(),
2014-08-31 01:48:40 -07:00
Fee: randuint64(),
2014-07-01 14:50:24 -07:00
Name: String(randBytes(12)),
PubKey: randBytes(32),
}
// Adjs
bond := &Bond{
Signature: randSig(),
2014-08-31 01:48:40 -07:00
Fee: randuint64(),
UnbondTo: randuint64(),
Amount: randuint64(),
2014-07-01 14:50:24 -07:00
}
unbond := &Unbond{
Signature: randSig(),
2014-08-31 01:48:40 -07:00
Fee: randuint64(),
Amount: randuint64(),
2014-07-01 14:50:24 -07:00
}
timeout := &Timeout{
2014-08-31 01:48:40 -07:00
AccountId: randuint64(),
Penalty: randuint64(),
2014-07-01 14:50:24 -07:00
}
dupeout := &Dupeout{
2014-08-31 01:48:40 -07:00
VoteA: BlockVote{
Height: randuint64(),
2014-07-01 14:50:24 -07:00
BlockHash: randBytes(32),
Signature: randSig(),
},
2014-08-31 01:48:40 -07:00
VoteB: BlockVote{
Height: randuint64(),
2014-07-01 14:50:24 -07:00
BlockHash: randBytes(32),
Signature: randSig(),
},
}
// Block
block := &Block{
2014-08-31 01:48:40 -07:00
Header: Header{
2014-07-01 14:50:24 -07:00
Name: "Tendermint",
2014-08-31 01:48:40 -07:00
Height: randuint32(),
Fees: randuint64(),
Time: randTime(),
2014-07-01 14:50:24 -07:00
PrevHash: randBytes(32),
ValidationHash: randBytes(32),
TxsHash: randBytes(32),
2014-07-01 14:50:24 -07:00
},
2014-08-31 01:48:40 -07:00
Validation: Validation{
2014-07-01 14:50:24 -07:00
Signatures: []Signature{randSig(), randSig()},
Adjustments: []Adjustment{bond, unbond, timeout, dupeout},
},
2014-08-31 01:48:40 -07:00
Txs: Txs{
Txs: []Tx{sendTx, nameTx},
hash: nil,
2014-07-01 14:50:24 -07:00
},
}
// Write the block, read it in again, write it again.
// Then, compare.
blockBytes := BinaryBytes(block)
block2 := ReadBlock(bytes.NewReader(blockBytes))
blockBytes2 := BinaryBytes(block2)
if !BinaryEqual(blockBytes, blockBytes2) {
t.Fatal("Write->Read of block failed.")
}
2014-06-05 18:17:09 -07:00
}