fix test cases

This commit is contained in:
Jae Kwon 2014-08-31 01:48:40 -07:00
parent fa382a3b05
commit a8ece216f0
9 changed files with 104 additions and 69 deletions

View File

@ -9,6 +9,10 @@ type Time struct {
time.Time time.Time
} }
func TimeFromUnix(secSinceEpoch int64) Time {
return Time{time.Unix(secSinceEpoch, 0)}
}
func (self Time) Equals(other interface{}) bool { func (self Time) Equals(other interface{}) bool {
if o, ok := other.(Time); ok { if o, ok := other.(Time); ok {
return self.Equal(o.Time) return self.Equal(o.Time)

View File

@ -31,21 +31,21 @@ func ReadAdjustment(r io.Reader) Adjustment {
switch t := ReadByte(r); t { switch t := ReadByte(r); t {
case ADJ_TYPE_BOND: case ADJ_TYPE_BOND:
return &Bond{ return &Bond{
Fee: ReadUInt64(r), Fee: Readuint64(r),
UnbondTo: ReadUInt64(r), UnbondTo: Readuint64(r),
Amount: ReadUInt64(r), Amount: Readuint64(r),
Signature: ReadSignature(r), Signature: ReadSignature(r),
} }
case ADJ_TYPE_UNBOND: case ADJ_TYPE_UNBOND:
return &Unbond{ return &Unbond{
Fee: ReadUInt64(r), Fee: Readuint64(r),
Amount: ReadUInt64(r), Amount: Readuint64(r),
Signature: ReadSignature(r), Signature: ReadSignature(r),
} }
case ADJ_TYPE_TIMEOUT: case ADJ_TYPE_TIMEOUT:
return &Timeout{ return &Timeout{
Account: ReadUInt64(r), AccountId: Readuint64(r),
Penalty: ReadUInt64(r), Penalty: Readuint64(r),
} }
case ADJ_TYPE_DUPEOUT: case ADJ_TYPE_DUPEOUT:
return &Dupeout{ return &Dupeout{
@ -62,9 +62,9 @@ func ReadAdjustment(r io.Reader) Adjustment {
/* Bond < Adjustment */ /* Bond < Adjustment */
type Bond struct { type Bond struct {
Fee UInt64 Fee uint64
UnbondTo UInt64 UnbondTo uint64
Amount UInt64 Amount uint64
Signature Signature
} }
@ -74,9 +74,9 @@ func (self *Bond) Type() Byte {
func (self *Bond) WriteTo(w io.Writer) (n int64, err error) { func (self *Bond) WriteTo(w io.Writer) (n int64, err error) {
n, err = WriteTo(self.Type(), w, n, err) n, err = WriteTo(self.Type(), w, n, err)
n, err = WriteTo(self.Fee, w, n, err) n, err = WriteTo(UInt64(self.Fee), w, n, err)
n, err = WriteTo(self.UnbondTo, w, n, err) n, err = WriteTo(UInt64(self.UnbondTo), w, n, err)
n, err = WriteTo(self.Amount, w, n, err) n, err = WriteTo(UInt64(self.Amount), w, n, err)
n, err = WriteTo(self.Signature, w, n, err) n, err = WriteTo(self.Signature, w, n, err)
return return
} }
@ -85,8 +85,8 @@ func (self *Bond) WriteTo(w io.Writer) (n int64, err error) {
/* Unbond < Adjustment */ /* Unbond < Adjustment */
type Unbond struct { type Unbond struct {
Fee UInt64 Fee uint64
Amount UInt64 Amount uint64
Signature Signature
} }
@ -96,8 +96,8 @@ func (self *Unbond) Type() Byte {
func (self *Unbond) WriteTo(w io.Writer) (n int64, err error) { func (self *Unbond) WriteTo(w io.Writer) (n int64, err error) {
n, err = WriteTo(self.Type(), w, n, err) n, err = WriteTo(self.Type(), w, n, err)
n, err = WriteTo(self.Fee, w, n, err) n, err = WriteTo(UInt64(self.Fee), w, n, err)
n, err = WriteTo(self.Amount, w, n, err) n, err = WriteTo(UInt64(self.Amount), w, n, err)
n, err = WriteTo(self.Signature, w, n, err) n, err = WriteTo(self.Signature, w, n, err)
return return
} }
@ -106,8 +106,8 @@ func (self *Unbond) WriteTo(w io.Writer) (n int64, err error) {
/* Timeout < Adjustment */ /* Timeout < Adjustment */
type Timeout struct { type Timeout struct {
Account UInt64 AccountId uint64
Penalty UInt64 Penalty uint64
} }
func (self *Timeout) Type() Byte { func (self *Timeout) Type() Byte {
@ -116,8 +116,8 @@ func (self *Timeout) Type() Byte {
func (self *Timeout) WriteTo(w io.Writer) (n int64, err error) { func (self *Timeout) WriteTo(w io.Writer) (n int64, err error) {
n, err = WriteTo(self.Type(), w, n, err) n, err = WriteTo(self.Type(), w, n, err)
n, err = WriteTo(self.Account, w, n, err) n, err = WriteTo(UInt64(self.AccountId), w, n, err)
n, err = WriteTo(self.Penalty, w, n, err) n, err = WriteTo(UInt64(self.Penalty), w, n, err)
return return
} }
@ -128,21 +128,21 @@ The full vote structure is only needed when presented as evidence.
Typically only the signature is passed around, as the hash & height are implied. Typically only the signature is passed around, as the hash & height are implied.
*/ */
type BlockVote struct { type BlockVote struct {
Height UInt64 Height uint64
BlockHash ByteSlice BlockHash ByteSlice
Signature Signature
} }
func ReadBlockVote(r io.Reader) BlockVote { func ReadBlockVote(r io.Reader) BlockVote {
return BlockVote{ return BlockVote{
Height: ReadUInt64(r), Height: Readuint64(r),
BlockHash: ReadByteSlice(r), BlockHash: ReadByteSlice(r),
Signature: ReadSignature(r), Signature: ReadSignature(r),
} }
} }
func (self BlockVote) WriteTo(w io.Writer) (n int64, err error) { func (self BlockVote) WriteTo(w io.Writer) (n int64, err error) {
n, err = WriteTo(self.Height, w, n, err) n, err = WriteTo(UInt64(self.Height), w, n, err)
n, err = WriteTo(self.BlockHash, w, n, err) n, err = WriteTo(self.BlockHash, w, n, err)
n, err = WriteTo(self.Signature, w, n, err) n, err = WriteTo(self.Signature, w, n, err)
return return

View File

@ -5,17 +5,39 @@ import (
. "github.com/tendermint/tendermint/binary" . "github.com/tendermint/tendermint/binary"
"math/rand" "math/rand"
"testing" "testing"
"time"
) )
// Distributed pseudo-exponentially to test for various cases // Distributed pseudo-exponentially to test for various cases
func randVar() UInt64 { func randuint64() uint64 {
bits := rand.Uint32() % 64 bits := rand.Uint32() % 64
if bits == 0 { if bits == 0 {
return 0 return 0
} }
n := uint64(1 << (bits - 1)) n := uint64(1 << (bits - 1))
n += uint64(rand.Int63()) & ((1 << (bits - 1)) - 1) n += uint64(rand.Int63()) & ((1 << (bits - 1)) - 1)
return UInt64(n) 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),
}
} }
func randBytes(n int) ByteSlice { func randBytes(n int) ByteSlice {
@ -27,7 +49,7 @@ func randBytes(n int) ByteSlice {
} }
func randSig() Signature { func randSig() Signature {
return Signature{AccountNumber(randVar()), randBytes(32)} return Signature{randuint64(), randBytes(32)}
} }
func TestBlock(t *testing.T) { func TestBlock(t *testing.T) {
@ -36,14 +58,14 @@ func TestBlock(t *testing.T) {
sendTx := &SendTx{ sendTx := &SendTx{
Signature: randSig(), Signature: randSig(),
Fee: randVar(), Fee: randuint64(),
To: AccountNumber(randVar()), To: randuint64(),
Amount: randVar(), Amount: randuint64(),
} }
nameTx := &NameTx{ nameTx := &NameTx{
Signature: randSig(), Signature: randSig(),
Fee: randVar(), Fee: randuint64(),
Name: String(randBytes(12)), Name: String(randBytes(12)),
PubKey: randBytes(32), PubKey: randBytes(32),
} }
@ -52,30 +74,30 @@ func TestBlock(t *testing.T) {
bond := &Bond{ bond := &Bond{
Signature: randSig(), Signature: randSig(),
Fee: randVar(), Fee: randuint64(),
UnbondTo: AccountNumber(randVar()), UnbondTo: randuint64(),
Amount: randVar(), Amount: randuint64(),
} }
unbond := &Unbond{ unbond := &Unbond{
Signature: randSig(), Signature: randSig(),
Fee: randVar(), Fee: randuint64(),
Amount: randVar(), Amount: randuint64(),
} }
timeout := &Timeout{ timeout := &Timeout{
Account: AccountNumber(randVar()), AccountId: randuint64(),
Penalty: randVar(), Penalty: randuint64(),
} }
dupeout := &Dupeout{ dupeout := &Dupeout{
VoteA: Vote{ VoteA: BlockVote{
Height: randVar(), Height: randuint64(),
BlockHash: randBytes(32), BlockHash: randBytes(32),
Signature: randSig(), Signature: randSig(),
}, },
VoteB: Vote{ VoteB: BlockVote{
Height: randVar(), Height: randuint64(),
BlockHash: randBytes(32), BlockHash: randBytes(32),
Signature: randSig(), Signature: randSig(),
}, },
@ -84,21 +106,22 @@ func TestBlock(t *testing.T) {
// Block // Block
block := &Block{ block := &Block{
Header{ Header: Header{
Name: "Tendermint", Name: "Tendermint",
Height: randVar(), Height: randuint32(),
Fees: randVar(), Fees: randuint64(),
Time: randVar(), Time: randTime(),
PrevHash: randBytes(32), PrevHash: randBytes(32),
ValidationHash: randBytes(32), ValidationHash: randBytes(32),
TxsHash: randBytes(32), TxsHash: randBytes(32),
}, },
Validation{ Validation: Validation{
Signatures: []Signature{randSig(), randSig()}, Signatures: []Signature{randSig(), randSig()},
Adjustments: []Adjustment{bond, unbond, timeout, dupeout}, Adjustments: []Adjustment{bond, unbond, timeout, dupeout},
}, },
Txs{ Txs: Txs{
Txs: []Tx{sendTx, nameTx}, Txs: []Tx{sendTx, nameTx},
hash: nil,
}, },
} }

View File

@ -18,7 +18,7 @@ func BenchmarkTestCustom(b *testing.B) {
Name: "Header", Name: "Header",
Height: 123, Height: 123,
Fees: 123, Fees: 123,
Time: 123, Time: TimeFromUnix(123),
PrevHash: ByteSlice("prevhash"), PrevHash: ByteSlice("prevhash"),
ValidationHash: ByteSlice("validationhash"), ValidationHash: ByteSlice("validationhash"),
TxsHash: ByteSlice("txshash"), TxsHash: ByteSlice("txshash"),
@ -83,7 +83,7 @@ func BenchmarkTestGob(b *testing.B) {
Name: "Header", Name: "Header",
Height: 123, Height: 123,
Fees: 123, Fees: 123,
Time: 123, Time: TimeFromUnix(123),
PrevHash: []byte("prevhash"), PrevHash: []byte("prevhash"),
ValidationHash: []byte("validationhash"), ValidationHash: []byte("validationhash"),
TxsHash: []byte("txshash"), TxsHash: []byte("txshash"),
@ -112,7 +112,7 @@ func BenchmarkTestMsgPack(b *testing.B) {
Name: "Header", Name: "Header",
Height: 123, Height: 123,
Fees: 123, Fees: 123,
Time: 123, Time: TimeFromUnix(123),
PrevHash: []byte("prevhash"), PrevHash: []byte("prevhash"),
ValidationHash: []byte("validationhash"), ValidationHash: []byte("validationhash"),
TxsHash: []byte("txshash"), TxsHash: []byte("txshash"),
@ -141,7 +141,7 @@ func BenchmarkTestMsgPack2(b *testing.B) {
Name: "Header", Name: "Header",
Height: 123, Height: 123,
Fees: 123, Fees: 123,
Time: 123, Time: TimeFromUnix(123),
PrevHash: []byte("prevhash"), PrevHash: []byte("prevhash"),
ValidationHash: []byte("validationhash"), ValidationHash: []byte("validationhash"),
TxsHash: []byte("txshash"), TxsHash: []byte("txshash"),

View File

@ -34,14 +34,14 @@ func ReadTx(r io.Reader) Tx {
switch t := ReadByte(r); t { switch t := ReadByte(r); t {
case TX_TYPE_SEND: case TX_TYPE_SEND:
return &SendTx{ return &SendTx{
Fee: ReadUInt64(r), Fee: Readuint64(r),
To: ReadUInt64(r), To: Readuint64(r),
Amount: ReadUInt64(r), Amount: Readuint64(r),
Signature: ReadSignature(r), Signature: ReadSignature(r),
} }
case TX_TYPE_NAME: case TX_TYPE_NAME:
return &NameTx{ return &NameTx{
Fee: ReadUInt64(r), Fee: Readuint64(r),
Name: ReadString(r), Name: ReadString(r),
PubKey: ReadByteSlice(r), PubKey: ReadByteSlice(r),
Signature: ReadSignature(r), Signature: ReadSignature(r),
@ -55,9 +55,9 @@ func ReadTx(r io.Reader) Tx {
/* SendTx < Tx */ /* SendTx < Tx */
type SendTx struct { type SendTx struct {
Fee UInt64 Fee uint64
To UInt64 To uint64
Amount UInt64 Amount uint64
Signature Signature
} }
@ -67,9 +67,9 @@ func (self *SendTx) Type() Byte {
func (self *SendTx) WriteTo(w io.Writer) (n int64, err error) { func (self *SendTx) WriteTo(w io.Writer) (n int64, err error) {
n, err = WriteTo(self.Type(), w, n, err) n, err = WriteTo(self.Type(), w, n, err)
n, err = WriteTo(self.Fee, w, n, err) n, err = WriteTo(UInt64(self.Fee), w, n, err)
n, err = WriteTo(self.To, w, n, err) n, err = WriteTo(UInt64(self.To), w, n, err)
n, err = WriteTo(self.Amount, w, n, err) n, err = WriteTo(UInt64(self.Amount), w, n, err)
n, err = WriteTo(self.Signature, w, n, err) n, err = WriteTo(self.Signature, w, n, err)
return return
} }
@ -77,7 +77,7 @@ func (self *SendTx) WriteTo(w io.Writer) (n int64, err error) {
/* NameTx < Tx */ /* NameTx < Tx */
type NameTx struct { type NameTx struct {
Fee UInt64 Fee uint64
Name String Name String
PubKey ByteSlice PubKey ByteSlice
Signature Signature
@ -89,7 +89,7 @@ func (self *NameTx) Type() Byte {
func (self *NameTx) WriteTo(w io.Writer) (n int64, err error) { func (self *NameTx) WriteTo(w io.Writer) (n int64, err error) {
n, err = WriteTo(self.Type(), w, n, err) n, err = WriteTo(self.Type(), w, n, err)
n, err = WriteTo(self.Fee, w, n, err) n, err = WriteTo(UInt64(self.Fee), w, n, err)
n, err = WriteTo(self.Name, w, n, err) n, err = WriteTo(self.Name, w, n, err)
n, err = WriteTo(self.PubKey, w, n, err) n, err = WriteTo(self.PubKey, w, n, err)
n, err = WriteTo(self.Signature, w, n, err) n, err = WriteTo(self.Signature, w, n, err)

View File

@ -8,6 +8,7 @@ var log = logging.MustGetLogger("p2p")
func init() { func init() {
logging.SetFormatter(logging.MustStringFormatter("[%{level:.1s}] %{message}")) logging.SetFormatter(logging.MustStringFormatter("[%{level:.1s}] %{message}"))
logging.SetLevel(logging.WARNING, "p2p")
} }
func SetP2PLogger(l *logging.Logger) { func SetP2PLogger(l *logging.Logger) {

View File

@ -88,6 +88,12 @@ func (na *NetAddress) Less(other interface{}) bool {
} }
func (na *NetAddress) String() string { func (na *NetAddress) String() string {
if na.str == "" {
na.str = net.JoinHostPort(
na.IP.String(),
strconv.FormatUint(uint64(na.Port), 10),
)
}
return na.str return na.str
} }

View File

@ -59,14 +59,14 @@ func (p *Peer) Send(chId byte, msg Binary) bool {
if atomic.LoadUint32(&p.stopped) == 1 { if atomic.LoadUint32(&p.stopped) == 1 {
return false return false
} }
return p.mconn.Send(chId, BinaryBytes(msg)) return p.mconn.Send(chId, msg)
} }
func (p *Peer) TrySend(chId byte, msg Binary) bool { func (p *Peer) TrySend(chId byte, msg Binary) bool {
if atomic.LoadUint32(&p.stopped) == 1 { if atomic.LoadUint32(&p.stopped) == 1 {
return false return false
} }
return p.mconn.TrySend(chId, BinaryBytes(msg)) return p.mconn.TrySend(chId, msg)
} }
func (p *Peer) CanSend(chId byte) bool { func (p *Peer) CanSend(chId byte) bool {

View File

@ -1,6 +1,7 @@
package p2p package p2p
import ( import (
"encoding/hex"
"testing" "testing"
"time" "time"
@ -89,7 +90,7 @@ func TestSwitches(t *testing.T) {
t.Errorf("Failed to receive from channel one") t.Errorf("Failed to receive from channel one")
} }
if ReadString(inMsg.Bytes.Reader()) != "channel one" { if ReadString(inMsg.Bytes.Reader()) != "channel one" {
t.Errorf("Unexpected received message bytes: %X = [%v]", inMsg.Bytes, ReadString(inMsg.Bytes.Reader())) t.Errorf("Unexpected received message bytes:\n%v", hex.Dump(inMsg.Bytes))
} }
// Receive message from channel 0 and check // Receive message from channel 0 and check
@ -98,7 +99,7 @@ func TestSwitches(t *testing.T) {
t.Errorf("Failed to receive from channel zero") t.Errorf("Failed to receive from channel zero")
} }
if ReadString(inMsg.Bytes.Reader()) != "channel zero" { if ReadString(inMsg.Bytes.Reader()) != "channel zero" {
t.Errorf("Unexpected received message bytes: %X = [%v]", inMsg.Bytes, ReadString(inMsg.Bytes.Reader())) t.Errorf("Unexpected received message bytes:\n%v", hex.Dump(inMsg.Bytes))
} }
} }