mirror of
https://github.com/tendermint/tendermint.git
synced 2026-09-18 22:14:35 +00:00
fix basic tests.
This commit is contained in:
+50
-72
@@ -3,46 +3,16 @@ package blocks
|
||||
import (
|
||||
"bytes"
|
||||
. "github.com/tendermint/tendermint/binary"
|
||||
"math/rand"
|
||||
. "github.com/tendermint/tendermint/common"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Distributed pseudo-exponentially to test for various cases
|
||||
func randUInt64() uint64 {
|
||||
bits := rand.Uint32() % 64
|
||||
if bits == 0 {
|
||||
return 0
|
||||
}
|
||||
n := uint64(1 << (bits - 1))
|
||||
n += uint64(rand.Int63()) & ((1 << (bits - 1)) - 1)
|
||||
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.Time {
|
||||
return time.Unix(int64(randUInt64()), 0)
|
||||
}
|
||||
|
||||
func randBytes(n int) []byte {
|
||||
bs := make([]byte, n)
|
||||
for i := 0; i < n; i++ {
|
||||
bs[i] = byte(rand.Intn(256))
|
||||
}
|
||||
return bs
|
||||
}
|
||||
|
||||
func randSig() Signature {
|
||||
return Signature{randUInt64(), randBytes(32)}
|
||||
return Signature{RandUInt64Exp(), RandBytes(32)}
|
||||
}
|
||||
|
||||
func randBaseTx() BaseTx {
|
||||
return BaseTx{0, randSig()}
|
||||
}
|
||||
|
||||
func TestBlock(t *testing.T) {
|
||||
@@ -50,48 +20,52 @@ func TestBlock(t *testing.T) {
|
||||
// Account Txs
|
||||
|
||||
sendTx := &SendTx{
|
||||
Signature: randSig(),
|
||||
Fee: randUInt64(),
|
||||
To: randUInt64(),
|
||||
Amount: randUInt64(),
|
||||
BaseTx: randBaseTx(),
|
||||
Fee: RandUInt64Exp(),
|
||||
To: RandUInt64Exp(),
|
||||
Amount: RandUInt64Exp(),
|
||||
}
|
||||
|
||||
nameTx := &NameTx{
|
||||
Signature: randSig(),
|
||||
Fee: randUInt64(),
|
||||
Name: string(randBytes(12)),
|
||||
PubKey: randBytes(32),
|
||||
BaseTx: randBaseTx(),
|
||||
Fee: RandUInt64Exp(),
|
||||
Name: string(RandBytes(12)),
|
||||
PubKey: RandBytes(32),
|
||||
}
|
||||
|
||||
// Validation Txs
|
||||
|
||||
bond := &Bond{
|
||||
Signature: randSig(),
|
||||
Fee: randUInt64(),
|
||||
UnbondTo: randUInt64(),
|
||||
Amount: randUInt64(),
|
||||
bondTx := &BondTx{
|
||||
BaseTx: randBaseTx(),
|
||||
Fee: RandUInt64Exp(),
|
||||
UnbondTo: RandUInt64Exp(),
|
||||
Amount: RandUInt64Exp(),
|
||||
}
|
||||
|
||||
unbond := &Unbond{
|
||||
Signature: randSig(),
|
||||
Fee: randUInt64(),
|
||||
Amount: randUInt64(),
|
||||
unbondTx := &UnbondTx{
|
||||
BaseTx: randBaseTx(),
|
||||
Fee: RandUInt64Exp(),
|
||||
Amount: RandUInt64Exp(),
|
||||
}
|
||||
|
||||
timeout := &Timeout{
|
||||
AccountId: randUInt64(),
|
||||
Penalty: randUInt64(),
|
||||
timeoutTx := &TimeoutTx{
|
||||
AccountId: RandUInt64Exp(),
|
||||
Penalty: RandUInt64Exp(),
|
||||
}
|
||||
|
||||
dupeout := &Dupeout{
|
||||
VoteA: BlockVote{
|
||||
Height: randUInt64(),
|
||||
BlockHash: randBytes(32),
|
||||
dupeoutTx := &DupeoutTx{
|
||||
VoteA: Vote{
|
||||
Height: RandUInt32Exp(),
|
||||
Round: RandUInt16Exp(),
|
||||
Type: VoteTypeBare,
|
||||
BlockHash: RandBytes(32),
|
||||
Signature: randSig(),
|
||||
},
|
||||
VoteB: BlockVote{
|
||||
Height: randUInt64(),
|
||||
BlockHash: randBytes(32),
|
||||
VoteB: Vote{
|
||||
Height: RandUInt32Exp(),
|
||||
Round: RandUInt16Exp(),
|
||||
Type: VoteTypeBare,
|
||||
BlockHash: RandBytes(32),
|
||||
Signature: randSig(),
|
||||
},
|
||||
}
|
||||
@@ -100,30 +74,34 @@ func TestBlock(t *testing.T) {
|
||||
|
||||
block := &Block{
|
||||
Header: Header{
|
||||
Network: "Tendermint",
|
||||
Height: randUInt32(),
|
||||
Fees: randUInt64(),
|
||||
Time: randTime(),
|
||||
LastBlockHash: randBytes(32),
|
||||
ValidationHash: randBytes(32),
|
||||
DataHash: randBytes(32),
|
||||
Network: "Tendermint",
|
||||
Height: RandUInt32Exp(),
|
||||
Fees: RandUInt64Exp(),
|
||||
Time: RandTime(),
|
||||
LastBlockHash: RandBytes(32),
|
||||
ValidationStateHash: RandBytes(32),
|
||||
AccountStateHash: RandBytes(32),
|
||||
},
|
||||
Validation: Validation{
|
||||
Signatures: []Signature{randSig(), randSig()},
|
||||
Txs: []Txs{bond, unbond, timeout, dupeout},
|
||||
},
|
||||
Data: Data{
|
||||
Txs: []Tx{sendTx, nameTx},
|
||||
Txs: []Tx{sendTx, nameTx, bondTx, unbondTx, timeoutTx, dupeoutTx},
|
||||
},
|
||||
}
|
||||
|
||||
// Write the block, read it in again, write it again.
|
||||
// Then, compare.
|
||||
// TODO We should compute the hash instead, so Block -> Bytes -> Block and compare hashes.
|
||||
|
||||
blockBytes := BinaryBytes(block)
|
||||
var n int64
|
||||
var err error
|
||||
block2 := ReadBlock(bytes.NewReader(blockBytes), &n, &err)
|
||||
if err != nil {
|
||||
t.Errorf("Reading block failed: %v", err)
|
||||
}
|
||||
|
||||
blockBytes2 := BinaryBytes(block2)
|
||||
|
||||
if !bytes.Equal(blockBytes, blockBytes2) {
|
||||
|
||||
+42
-42
@@ -15,13 +15,13 @@ func BenchmarkTestCustom(b *testing.B) {
|
||||
b.StopTimer()
|
||||
|
||||
h := &Header{
|
||||
Network: "Header",
|
||||
Height: 123,
|
||||
Fees: 123,
|
||||
Time: time.Unix(123, 0),
|
||||
LastBlockHash: []byte("prevhash"),
|
||||
ValidationHash: []byte("validationhash"),
|
||||
DataHash: []byte("datahash"),
|
||||
Network: "Header",
|
||||
Height: 123,
|
||||
Fees: 123,
|
||||
Time: time.Unix(123, 0),
|
||||
LastBlockHash: []byte("prevhash"),
|
||||
ValidationStateHash: []byte("validationhash"),
|
||||
AccountStateHash: []byte("accounthash"),
|
||||
}
|
||||
|
||||
buf := bytes.NewBuffer(nil)
|
||||
@@ -40,26 +40,26 @@ func BenchmarkTestCustom(b *testing.B) {
|
||||
}
|
||||
|
||||
type HHeader struct {
|
||||
Network string `json:"N"`
|
||||
Height uint64 `json:"H"`
|
||||
Fees uint64 `json:"F"`
|
||||
Time uint64 `json:"T"`
|
||||
LastBlockHash []byte `json:"PH"`
|
||||
ValidationHash []byte `json:"VH"`
|
||||
DataHash []byte `json:"DH"`
|
||||
Network string `json:"N"`
|
||||
Height uint64 `json:"H"`
|
||||
Fees uint64 `json:"F"`
|
||||
Time uint64 `json:"T"`
|
||||
LastBlockHash []byte `json:"PH"`
|
||||
ValidationStateHash []byte `json:"VH"`
|
||||
AccountStateHash []byte `json:"DH"`
|
||||
}
|
||||
|
||||
func BenchmarkTestJSON(b *testing.B) {
|
||||
b.StopTimer()
|
||||
|
||||
h := &HHeader{
|
||||
Network: "Header",
|
||||
Height: 123,
|
||||
Fees: 123,
|
||||
Time: 123,
|
||||
LastBlockHash: []byte("prevhash"),
|
||||
ValidationHash: []byte("validationhash"),
|
||||
DataHash: []byte("datahash"),
|
||||
Network: "Header",
|
||||
Height: 123,
|
||||
Fees: 123,
|
||||
Time: 123,
|
||||
LastBlockHash: []byte("prevhash"),
|
||||
ValidationStateHash: []byte("validationhash"),
|
||||
AccountStateHash: []byte("accounthash"),
|
||||
}
|
||||
h2 := &HHeader{}
|
||||
|
||||
@@ -82,13 +82,13 @@ func BenchmarkTestGob(b *testing.B) {
|
||||
b.StopTimer()
|
||||
|
||||
h := &Header{
|
||||
Network: "Header",
|
||||
Height: 123,
|
||||
Fees: 123,
|
||||
Time: time.Unix(123, 0),
|
||||
LastBlockHash: []byte("prevhash"),
|
||||
ValidationHash: []byte("validationhash"),
|
||||
DataHash: []byte("datahash"),
|
||||
Network: "Header",
|
||||
Height: 123,
|
||||
Fees: 123,
|
||||
Time: time.Unix(123, 0),
|
||||
LastBlockHash: []byte("prevhash"),
|
||||
ValidationStateHash: []byte("validationhash"),
|
||||
AccountStateHash: []byte("datahash"),
|
||||
}
|
||||
h2 := &Header{}
|
||||
|
||||
@@ -111,13 +111,13 @@ func BenchmarkTestMsgPack(b *testing.B) {
|
||||
b.StopTimer()
|
||||
|
||||
h := &Header{
|
||||
Network: "Header",
|
||||
Height: 123,
|
||||
Fees: 123,
|
||||
Time: time.Unix(123, 0),
|
||||
LastBlockHash: []byte("prevhash"),
|
||||
ValidationHash: []byte("validationhash"),
|
||||
DataHash: []byte("datahash"),
|
||||
Network: "Header",
|
||||
Height: 123,
|
||||
Fees: 123,
|
||||
Time: time.Unix(123, 0),
|
||||
LastBlockHash: []byte("prevhash"),
|
||||
ValidationStateHash: []byte("validationhash"),
|
||||
AccountStateHash: []byte("datahash"),
|
||||
}
|
||||
h2 := &Header{}
|
||||
|
||||
@@ -140,13 +140,13 @@ func BenchmarkTestMsgPack2(b *testing.B) {
|
||||
b.StopTimer()
|
||||
|
||||
h := &Header{
|
||||
Network: "Header",
|
||||
Height: 123,
|
||||
Fees: 123,
|
||||
Time: time.Unix(123, 0),
|
||||
LastBlockHash: []byte("prevhash"),
|
||||
ValidationHash: []byte("validationhash"),
|
||||
DataHash: []byte("datahash"),
|
||||
Network: "Header",
|
||||
Height: 123,
|
||||
Fees: 123,
|
||||
Time: time.Unix(123, 0),
|
||||
LastBlockHash: []byte("prevhash"),
|
||||
ValidationStateHash: []byte("validationhash"),
|
||||
AccountStateHash: []byte("accounthash"),
|
||||
}
|
||||
h2 := &Header{}
|
||||
var mh codec.MsgpackHandle
|
||||
|
||||
+19
-19
@@ -28,59 +28,59 @@ type Tx interface {
|
||||
|
||||
const (
|
||||
// Account transactions
|
||||
TX_TYPE_SEND = byte(0x01)
|
||||
TX_TYPE_NAME = byte(0x02)
|
||||
txTypeSend = byte(0x01)
|
||||
txTypeName = byte(0x02)
|
||||
|
||||
// Validation transactions
|
||||
TX_TYPE_BOND = byte(0x11)
|
||||
TX_TYPE_UNBOND = byte(0x12)
|
||||
TX_TYPE_TIMEOUT = byte(0x13)
|
||||
TX_TYPE_DUPEOUT = byte(0x14)
|
||||
txTypeBond = byte(0x11)
|
||||
txTypeUnbond = byte(0x12)
|
||||
txTypeTimeout = byte(0x13)
|
||||
txTypeDupeout = byte(0x14)
|
||||
)
|
||||
|
||||
func ReadTx(r io.Reader, n *int64, err *error) Tx {
|
||||
switch t := ReadByte(r, n, err); t {
|
||||
case TX_TYPE_SEND:
|
||||
case txTypeSend:
|
||||
return &SendTx{
|
||||
BaseTx: ReadBaseTx(r, n, err),
|
||||
Fee: ReadUInt64(r, n, err),
|
||||
To: ReadUInt64(r, n, err),
|
||||
Amount: ReadUInt64(r, n, err),
|
||||
}
|
||||
case TX_TYPE_NAME:
|
||||
case txTypeName:
|
||||
return &NameTx{
|
||||
BaseTx: ReadBaseTx(r, n, err),
|
||||
Fee: ReadUInt64(r, n, err),
|
||||
Name: ReadString(r, n, err),
|
||||
PubKey: ReadByteSlice(r, n, err),
|
||||
}
|
||||
case TX_TYPE_BOND:
|
||||
case txTypeBond:
|
||||
return &BondTx{
|
||||
BaseTx: ReadBaseTx(r, n, err),
|
||||
Fee: ReadUInt64(r, n, err),
|
||||
UnbondTo: ReadUInt64(r, n, err),
|
||||
Amount: ReadUInt64(r, n, err),
|
||||
}
|
||||
case TX_TYPE_UNBOND:
|
||||
case txTypeUnbond:
|
||||
return &UnbondTx{
|
||||
BaseTx: ReadBaseTx(r, n, err),
|
||||
Fee: ReadUInt64(r, n, err),
|
||||
Amount: ReadUInt64(r, n, err),
|
||||
}
|
||||
case TX_TYPE_TIMEOUT:
|
||||
case txTypeTimeout:
|
||||
return &TimeoutTx{
|
||||
BaseTx: ReadBaseTx(r, n, err),
|
||||
AccountId: ReadUInt64(r, n, err),
|
||||
Penalty: ReadUInt64(r, n, err),
|
||||
}
|
||||
case TX_TYPE_DUPEOUT:
|
||||
case txTypeDupeout:
|
||||
return &DupeoutTx{
|
||||
BaseTx: ReadBaseTx(r, n, err),
|
||||
VoteA: *ReadVote(r, n, err),
|
||||
VoteB: *ReadVote(r, n, err),
|
||||
}
|
||||
default:
|
||||
Panicf("Unknown Tx type %x", t)
|
||||
*err = Errorf("Unknown Tx type %X", t)
|
||||
return nil
|
||||
}
|
||||
}
|
||||
@@ -123,7 +123,7 @@ type SendTx struct {
|
||||
}
|
||||
|
||||
func (tx *SendTx) Type() byte {
|
||||
return TX_TYPE_SEND
|
||||
return txTypeSend
|
||||
}
|
||||
|
||||
func (tx *SendTx) WriteTo(w io.Writer) (n int64, err error) {
|
||||
@@ -145,7 +145,7 @@ type NameTx struct {
|
||||
}
|
||||
|
||||
func (tx *NameTx) Type() byte {
|
||||
return TX_TYPE_NAME
|
||||
return txTypeName
|
||||
}
|
||||
|
||||
func (tx *NameTx) WriteTo(w io.Writer) (n int64, err error) {
|
||||
@@ -167,7 +167,7 @@ type BondTx struct {
|
||||
}
|
||||
|
||||
func (tx *BondTx) Type() byte {
|
||||
return TX_TYPE_BOND
|
||||
return txTypeBond
|
||||
}
|
||||
|
||||
func (tx *BondTx) WriteTo(w io.Writer) (n int64, err error) {
|
||||
@@ -188,7 +188,7 @@ type UnbondTx struct {
|
||||
}
|
||||
|
||||
func (tx *UnbondTx) Type() byte {
|
||||
return TX_TYPE_UNBOND
|
||||
return txTypeUnbond
|
||||
}
|
||||
|
||||
func (tx *UnbondTx) WriteTo(w io.Writer) (n int64, err error) {
|
||||
@@ -208,7 +208,7 @@ type TimeoutTx struct {
|
||||
}
|
||||
|
||||
func (tx *TimeoutTx) Type() byte {
|
||||
return TX_TYPE_TIMEOUT
|
||||
return txTypeTimeout
|
||||
}
|
||||
|
||||
func (tx *TimeoutTx) WriteTo(w io.Writer) (n int64, err error) {
|
||||
@@ -228,7 +228,7 @@ type DupeoutTx struct {
|
||||
}
|
||||
|
||||
func (tx *DupeoutTx) Type() byte {
|
||||
return TX_TYPE_DUPEOUT
|
||||
return txTypeDupeout
|
||||
}
|
||||
|
||||
func (tx *DupeoutTx) WriteTo(w io.Writer) (n int64, err error) {
|
||||
|
||||
Reference in New Issue
Block a user