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
}
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)

View File

@ -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

View File

@ -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,
},
}

View File

@ -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"),

View File

@ -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)

View File

@ -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) {

View File

@ -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
}

View File

@ -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 {

View File

@ -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))
}
}