From a8ece216f08b75ca40868752a882c77c9d05165f Mon Sep 17 00:00:00 2001 From: Jae Kwon Date: Sun, 31 Aug 2014 01:48:40 -0700 Subject: [PATCH] fix test cases --- binary/time.go | 4 +++ blocks/adjustment.go | 48 ++++++++++++++--------------- blocks/block_test.go | 73 +++++++++++++++++++++++++++++--------------- blocks/codec_test.go | 8 ++--- blocks/tx.go | 24 +++++++-------- p2p/log.go | 1 + p2p/netaddress.go | 6 ++++ p2p/peer.go | 4 +-- p2p/switch_test.go | 5 +-- 9 files changed, 104 insertions(+), 69 deletions(-) diff --git a/binary/time.go b/binary/time.go index 4750694c..d6428444 100644 --- a/binary/time.go +++ b/binary/time.go @@ -9,6 +9,10 @@ type Time struct { time.Time } +func TimeFromUnix(secSinceEpoch int64) Time { + return Time{time.Unix(secSinceEpoch, 0)} +} + func (self Time) Equals(other interface{}) bool { if o, ok := other.(Time); ok { return self.Equal(o.Time) diff --git a/blocks/adjustment.go b/blocks/adjustment.go index 0b86703a..e8565105 100644 --- a/blocks/adjustment.go +++ b/blocks/adjustment.go @@ -31,21 +31,21 @@ func ReadAdjustment(r io.Reader) Adjustment { switch t := ReadByte(r); t { case ADJ_TYPE_BOND: return &Bond{ - Fee: ReadUInt64(r), - UnbondTo: ReadUInt64(r), - Amount: ReadUInt64(r), + Fee: Readuint64(r), + UnbondTo: Readuint64(r), + Amount: Readuint64(r), Signature: ReadSignature(r), } case ADJ_TYPE_UNBOND: return &Unbond{ - Fee: ReadUInt64(r), - Amount: ReadUInt64(r), + Fee: Readuint64(r), + Amount: Readuint64(r), Signature: ReadSignature(r), } case ADJ_TYPE_TIMEOUT: return &Timeout{ - Account: ReadUInt64(r), - Penalty: ReadUInt64(r), + AccountId: Readuint64(r), + Penalty: Readuint64(r), } case ADJ_TYPE_DUPEOUT: return &Dupeout{ @@ -62,9 +62,9 @@ func ReadAdjustment(r io.Reader) Adjustment { /* Bond < Adjustment */ type Bond struct { - Fee UInt64 - UnbondTo UInt64 - Amount UInt64 + Fee uint64 + UnbondTo uint64 + Amount uint64 Signature } @@ -74,9 +74,9 @@ func (self *Bond) Type() Byte { func (self *Bond) WriteTo(w io.Writer) (n int64, err error) { n, err = WriteTo(self.Type(), w, n, err) - n, err = WriteTo(self.Fee, w, n, err) - n, err = WriteTo(self.UnbondTo, w, n, err) - n, err = WriteTo(self.Amount, w, n, err) + n, err = WriteTo(UInt64(self.Fee), w, n, err) + n, err = WriteTo(UInt64(self.UnbondTo), w, n, err) + n, err = WriteTo(UInt64(self.Amount), w, n, err) n, err = WriteTo(self.Signature, w, n, err) return } @@ -85,8 +85,8 @@ func (self *Bond) WriteTo(w io.Writer) (n int64, err error) { /* Unbond < Adjustment */ type Unbond struct { - Fee UInt64 - Amount UInt64 + Fee uint64 + Amount uint64 Signature } @@ -96,8 +96,8 @@ func (self *Unbond) Type() Byte { func (self *Unbond) WriteTo(w io.Writer) (n int64, err error) { n, err = WriteTo(self.Type(), w, n, err) - n, err = WriteTo(self.Fee, w, n, err) - n, err = WriteTo(self.Amount, w, n, err) + n, err = WriteTo(UInt64(self.Fee), w, n, err) + n, err = WriteTo(UInt64(self.Amount), w, n, err) n, err = WriteTo(self.Signature, w, n, err) return } @@ -106,8 +106,8 @@ func (self *Unbond) WriteTo(w io.Writer) (n int64, err error) { /* Timeout < Adjustment */ type Timeout struct { - Account UInt64 - Penalty UInt64 + AccountId uint64 + Penalty uint64 } 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) { n, err = WriteTo(self.Type(), w, n, err) - n, err = WriteTo(self.Account, w, n, err) - n, err = WriteTo(self.Penalty, w, n, err) + n, err = WriteTo(UInt64(self.AccountId), w, n, err) + n, err = WriteTo(UInt64(self.Penalty), w, n, err) 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. */ type BlockVote struct { - Height UInt64 + Height uint64 BlockHash ByteSlice Signature } func ReadBlockVote(r io.Reader) BlockVote { return BlockVote{ - Height: ReadUInt64(r), + Height: Readuint64(r), BlockHash: ReadByteSlice(r), Signature: ReadSignature(r), } } 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.Signature, w, n, err) return diff --git a/blocks/block_test.go b/blocks/block_test.go index 744ce4a9..2dcdb9e2 100644 --- a/blocks/block_test.go +++ b/blocks/block_test.go @@ -5,17 +5,39 @@ import ( . "github.com/tendermint/tendermint/binary" "math/rand" "testing" + "time" ) // Distributed pseudo-exponentially to test for various cases -func randVar() UInt64 { +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 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 { @@ -27,7 +49,7 @@ func randBytes(n int) ByteSlice { } func randSig() Signature { - return Signature{AccountNumber(randVar()), randBytes(32)} + return Signature{randuint64(), randBytes(32)} } func TestBlock(t *testing.T) { @@ -36,14 +58,14 @@ func TestBlock(t *testing.T) { sendTx := &SendTx{ Signature: randSig(), - Fee: randVar(), - To: AccountNumber(randVar()), - Amount: randVar(), + Fee: randuint64(), + To: randuint64(), + Amount: randuint64(), } nameTx := &NameTx{ Signature: randSig(), - Fee: randVar(), + Fee: randuint64(), Name: String(randBytes(12)), PubKey: randBytes(32), } @@ -52,30 +74,30 @@ func TestBlock(t *testing.T) { bond := &Bond{ Signature: randSig(), - Fee: randVar(), - UnbondTo: AccountNumber(randVar()), - Amount: randVar(), + Fee: randuint64(), + UnbondTo: randuint64(), + Amount: randuint64(), } unbond := &Unbond{ Signature: randSig(), - Fee: randVar(), - Amount: randVar(), + Fee: randuint64(), + Amount: randuint64(), } timeout := &Timeout{ - Account: AccountNumber(randVar()), - Penalty: randVar(), + AccountId: randuint64(), + Penalty: randuint64(), } dupeout := &Dupeout{ - VoteA: Vote{ - Height: randVar(), + VoteA: BlockVote{ + Height: randuint64(), BlockHash: randBytes(32), Signature: randSig(), }, - VoteB: Vote{ - Height: randVar(), + VoteB: BlockVote{ + Height: randuint64(), BlockHash: randBytes(32), Signature: randSig(), }, @@ -84,21 +106,22 @@ func TestBlock(t *testing.T) { // Block block := &Block{ - Header{ + Header: Header{ Name: "Tendermint", - Height: randVar(), - Fees: randVar(), - Time: randVar(), + Height: randuint32(), + Fees: randuint64(), + Time: randTime(), PrevHash: randBytes(32), ValidationHash: randBytes(32), TxsHash: randBytes(32), }, - Validation{ + Validation: Validation{ Signatures: []Signature{randSig(), randSig()}, Adjustments: []Adjustment{bond, unbond, timeout, dupeout}, }, - Txs{ - Txs: []Tx{sendTx, nameTx}, + Txs: Txs{ + Txs: []Tx{sendTx, nameTx}, + hash: nil, }, } diff --git a/blocks/codec_test.go b/blocks/codec_test.go index 0b98a8a1..33c69c6f 100644 --- a/blocks/codec_test.go +++ b/blocks/codec_test.go @@ -18,7 +18,7 @@ func BenchmarkTestCustom(b *testing.B) { Name: "Header", Height: 123, Fees: 123, - Time: 123, + Time: TimeFromUnix(123), PrevHash: ByteSlice("prevhash"), ValidationHash: ByteSlice("validationhash"), TxsHash: ByteSlice("txshash"), @@ -83,7 +83,7 @@ func BenchmarkTestGob(b *testing.B) { Name: "Header", Height: 123, Fees: 123, - Time: 123, + Time: TimeFromUnix(123), PrevHash: []byte("prevhash"), ValidationHash: []byte("validationhash"), TxsHash: []byte("txshash"), @@ -112,7 +112,7 @@ func BenchmarkTestMsgPack(b *testing.B) { Name: "Header", Height: 123, Fees: 123, - Time: 123, + Time: TimeFromUnix(123), PrevHash: []byte("prevhash"), ValidationHash: []byte("validationhash"), TxsHash: []byte("txshash"), @@ -141,7 +141,7 @@ func BenchmarkTestMsgPack2(b *testing.B) { Name: "Header", Height: 123, Fees: 123, - Time: 123, + Time: TimeFromUnix(123), PrevHash: []byte("prevhash"), ValidationHash: []byte("validationhash"), TxsHash: []byte("txshash"), diff --git a/blocks/tx.go b/blocks/tx.go index d7c2adb1..f4f73a52 100644 --- a/blocks/tx.go +++ b/blocks/tx.go @@ -34,14 +34,14 @@ func ReadTx(r io.Reader) Tx { switch t := ReadByte(r); t { case TX_TYPE_SEND: return &SendTx{ - Fee: ReadUInt64(r), - To: ReadUInt64(r), - Amount: ReadUInt64(r), + Fee: Readuint64(r), + To: Readuint64(r), + Amount: Readuint64(r), Signature: ReadSignature(r), } case TX_TYPE_NAME: return &NameTx{ - Fee: ReadUInt64(r), + Fee: Readuint64(r), Name: ReadString(r), PubKey: ReadByteSlice(r), Signature: ReadSignature(r), @@ -55,9 +55,9 @@ func ReadTx(r io.Reader) Tx { /* SendTx < Tx */ type SendTx struct { - Fee UInt64 - To UInt64 - Amount UInt64 + Fee uint64 + To uint64 + Amount uint64 Signature } @@ -67,9 +67,9 @@ func (self *SendTx) Type() Byte { func (self *SendTx) WriteTo(w io.Writer) (n int64, err error) { n, err = WriteTo(self.Type(), w, n, err) - n, err = WriteTo(self.Fee, w, n, err) - n, err = WriteTo(self.To, w, n, err) - n, err = WriteTo(self.Amount, w, n, err) + n, err = WriteTo(UInt64(self.Fee), w, n, err) + n, err = WriteTo(UInt64(self.To), w, n, err) + n, err = WriteTo(UInt64(self.Amount), w, n, err) n, err = WriteTo(self.Signature, w, n, err) return } @@ -77,7 +77,7 @@ func (self *SendTx) WriteTo(w io.Writer) (n int64, err error) { /* NameTx < Tx */ type NameTx struct { - Fee UInt64 + Fee uint64 Name String PubKey ByteSlice Signature @@ -89,7 +89,7 @@ func (self *NameTx) Type() Byte { func (self *NameTx) WriteTo(w io.Writer) (n int64, err error) { 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.PubKey, w, n, err) n, err = WriteTo(self.Signature, w, n, err) diff --git a/p2p/log.go b/p2p/log.go index bcaa8fc5..c68c453d 100644 --- a/p2p/log.go +++ b/p2p/log.go @@ -8,6 +8,7 @@ var log = logging.MustGetLogger("p2p") func init() { logging.SetFormatter(logging.MustStringFormatter("[%{level:.1s}] %{message}")) + logging.SetLevel(logging.WARNING, "p2p") } func SetP2PLogger(l *logging.Logger) { diff --git a/p2p/netaddress.go b/p2p/netaddress.go index e5517f95..a13220b7 100644 --- a/p2p/netaddress.go +++ b/p2p/netaddress.go @@ -88,6 +88,12 @@ func (na *NetAddress) Less(other interface{}) bool { } func (na *NetAddress) String() string { + if na.str == "" { + na.str = net.JoinHostPort( + na.IP.String(), + strconv.FormatUint(uint64(na.Port), 10), + ) + } return na.str } diff --git a/p2p/peer.go b/p2p/peer.go index dffd52b1..7ebb7f18 100644 --- a/p2p/peer.go +++ b/p2p/peer.go @@ -59,14 +59,14 @@ func (p *Peer) Send(chId byte, msg Binary) bool { if atomic.LoadUint32(&p.stopped) == 1 { return false } - return p.mconn.Send(chId, BinaryBytes(msg)) + return p.mconn.Send(chId, msg) } func (p *Peer) TrySend(chId byte, msg Binary) bool { if atomic.LoadUint32(&p.stopped) == 1 { return false } - return p.mconn.TrySend(chId, BinaryBytes(msg)) + return p.mconn.TrySend(chId, msg) } func (p *Peer) CanSend(chId byte) bool { diff --git a/p2p/switch_test.go b/p2p/switch_test.go index 924fc3d2..508e7a4b 100644 --- a/p2p/switch_test.go +++ b/p2p/switch_test.go @@ -1,6 +1,7 @@ package p2p import ( + "encoding/hex" "testing" "time" @@ -89,7 +90,7 @@ func TestSwitches(t *testing.T) { t.Errorf("Failed to receive from 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 @@ -98,7 +99,7 @@ func TestSwitches(t *testing.T) { t.Errorf("Failed to receive from 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)) } }