proper string formatting for txs

This commit is contained in:
Jae Kwon 2015-01-16 00:31:34 -08:00
parent b1be787987
commit 3f159dab69
8 changed files with 63 additions and 8 deletions

View File

@ -23,3 +23,7 @@ func GenPrivAccount() *PrivAccount {
func (privAccount *PrivAccount) Sign(o Signable) Signature {
return privAccount.PrivKey.Sign(SignBytes(o))
}
func (privAccount *PrivAccount) String() string {
return Fmt("PrivAccount{%X}", privAccount.Address)
}

View File

@ -5,6 +5,7 @@ import (
"github.com/tendermint/go-ed25519"
"github.com/tendermint/tendermint/binary"
. "github.com/tendermint/tendermint/common"
)
// PrivKey is part of PrivAccount and state.PrivValidator.
@ -46,3 +47,7 @@ func (key PrivKeyEd25519) Sign(msg []byte) Signature {
func (key PrivKeyEd25519) PubKey() PubKey {
return PubKeyEd25519(ed25519.MakePubKey(key))
}
func (key PrivKeyEd25519) String() string {
return Fmt("PrivKeyEd25519{*****}")
}

View File

@ -5,6 +5,7 @@ import (
"github.com/tendermint/go-ed25519"
"github.com/tendermint/tendermint/binary"
. "github.com/tendermint/tendermint/common"
)
// PubKey is part of Account and Validator.
@ -42,6 +43,10 @@ func (key PubKeyNil) VerifyBytes(msg []byte, sig_ Signature) bool {
panic("PubKeyNil cannot verify messages")
}
func (key PubKeyNil) String() string {
return "PubKeyNil{}"
}
//-------------------------------------
// Implements PubKey
@ -71,3 +76,7 @@ func (key PubKeyEd25519) VerifyBytes(msg []byte, sig_ Signature) bool {
}
return ed25519.VerifyBatch([]*ed25519.Verify{v1})
}
func (key PubKeyEd25519) String() string {
return Fmt("PubKeyEd25519{%X}", []byte(key))
}

View File

@ -6,6 +6,7 @@ import (
"github.com/tendermint/tendermint/account"
"github.com/tendermint/tendermint/binary"
. "github.com/tendermint/tendermint/common"
)
var (
@ -82,6 +83,10 @@ func (txIn *TxInput) WriteSignBytes(w io.Writer, n *int64, err *error) {
binary.WriteUvarint(txIn.Sequence, w, n, err)
}
func (txIn *TxInput) String() string {
return Fmt("TxInput{%X,%v,%v,%v,%v}", txIn.Address, txIn.Amount, txIn.Sequence, txIn.Signature, txIn.PubKey)
}
//-----------------------------------------------------------------------------
type TxOutput struct {
@ -104,6 +109,10 @@ func (txOut *TxOutput) WriteSignBytes(w io.Writer, n *int64, err *error) {
binary.WriteUint64(txOut.Amount, w, n, err)
}
func (txOut *TxOutput) String() string {
return Fmt("TxOutput{%X,%v}", txOut.Address, txOut.Amount)
}
//-----------------------------------------------------------------------------
type SendTx struct {
@ -124,6 +133,10 @@ func (tx *SendTx) WriteSignBytes(w io.Writer, n *int64, err *error) {
}
}
func (tx *SendTx) String() string {
return Fmt("SendTx{%v -> %v}", tx.Inputs, tx.Outputs)
}
//-----------------------------------------------------------------------------
type BondTx struct {
@ -146,6 +159,10 @@ func (tx *BondTx) WriteSignBytes(w io.Writer, n *int64, err *error) {
}
}
func (tx *BondTx) String() string {
return Fmt("BondTx{%v: %v -> %v}", tx.PubKey, tx.Inputs, tx.UnbondTo)
}
//-----------------------------------------------------------------------------
type UnbondTx struct {
@ -161,6 +178,10 @@ func (tx *UnbondTx) WriteSignBytes(w io.Writer, n *int64, err *error) {
binary.WriteUvarint(tx.Height, w, n, err)
}
func (tx *UnbondTx) String() string {
return Fmt("UnbondTx{%X,%v,%v}", tx.Address, tx.Height, tx.Signature)
}
//-----------------------------------------------------------------------------
type RebondTx struct {
@ -176,6 +197,10 @@ func (tx *RebondTx) WriteSignBytes(w io.Writer, n *int64, err *error) {
binary.WriteUvarint(tx.Height, w, n, err)
}
func (tx *RebondTx) String() string {
return Fmt("RebondTx{%X,%v,%v}", tx.Address, tx.Height, tx.Signature)
}
//-----------------------------------------------------------------------------
type DupeoutTx struct {
@ -189,3 +214,7 @@ func (tx *DupeoutTx) TypeByte() byte { return TxTypeDupeout }
func (tx *DupeoutTx) WriteSignBytes(w io.Writer, n *int64, err *error) {
panic("DupeoutTx has no sign bytes")
}
func (tx *DupeoutTx) String() string {
return Fmt("DupeoutTx{%X,%v,%v}", tx.Address, tx.VoteA, tx.VoteB)
}

View File

@ -7,16 +7,17 @@ RepeatTimer repeatedly sends a struct{}{} to .Ch after each "dur" period.
It's good for keeping connections alive.
*/
type RepeatTimer struct {
Name string
Ch chan struct{}
quit chan struct{}
dur time.Duration
timer *time.Timer
}
func NewRepeatTimer(dur time.Duration) *RepeatTimer {
func NewRepeatTimer(name string, dur time.Duration) *RepeatTimer {
var ch = make(chan struct{})
var quit = make(chan struct{})
var t = &RepeatTimer{Ch: ch, dur: dur, quit: quit}
var t = &RepeatTimer{Name: name, Ch: ch, dur: dur, quit: quit}
t.timer = time.AfterFunc(dur, t.fireRoutine)
return t
}
@ -26,6 +27,9 @@ func (t *RepeatTimer) fireRoutine() {
case t.Ch <- struct{}{}:
t.timer.Reset(t.dur)
case <-t.quit:
// do nothing
default:
t.timer.Reset(t.dur)
}
}

View File

@ -12,6 +12,7 @@ If a long continuous burst of .Set() calls happens, ThrottleTimer fires
at most once every "dur".
*/
type ThrottleTimer struct {
Name string
Ch chan struct{}
quit chan struct{}
dur time.Duration
@ -19,10 +20,10 @@ type ThrottleTimer struct {
isSet uint32
}
func NewThrottleTimer(dur time.Duration) *ThrottleTimer {
func NewThrottleTimer(name string, dur time.Duration) *ThrottleTimer {
var ch = make(chan struct{})
var quit = make(chan struct{})
var t = &ThrottleTimer{Ch: ch, dur: dur, quit: quit}
var t = &ThrottleTimer{Name: name, Ch: ch, dur: dur, quit: quit}
t.timer = time.AfterFunc(dur, t.fireRoutine)
t.timer.Stop()
return t
@ -33,6 +34,9 @@ func (t *ThrottleTimer) fireRoutine() {
case t.Ch <- struct{}{}:
atomic.StoreUint32(&t.isSet, 0)
case <-t.quit:
// do nothing
default:
t.timer.Reset(t.dur)
}
}

View File

@ -93,12 +93,12 @@ func NewMConnection(conn net.Conn, chDescs []*ChannelDescriptor, onReceive recei
recvMonitor: flow.New(0, 0),
sendRate: defaultSendRate,
recvRate: defaultRecvRate,
flushTimer: NewThrottleTimer(flushThrottleMS * time.Millisecond),
flushTimer: NewThrottleTimer("flush", flushThrottleMS*time.Millisecond),
send: make(chan struct{}, 1),
quit: make(chan struct{}),
pingTimer: NewRepeatTimer(pingTimeoutMinutes * time.Minute),
pingTimer: NewRepeatTimer("ping", pingTimeoutMinutes*time.Minute),
pong: make(chan struct{}),
chStatsTimer: NewRepeatTimer(updateStatsSeconds * time.Second),
chStatsTimer: NewRepeatTimer("chStats", updateStatsSeconds*time.Second),
onReceive: onReceive,
onError: onError,
LocalAddress: NewNetAddress(conn.LocalAddr()),

View File

@ -133,7 +133,7 @@ func (pexR *PEXReactor) ensurePeersRoutine() {
// fire once immediately.
pexR.ensurePeers()
// fire periodically
timer := NewRepeatTimer(ensurePeersPeriodSeconds * time.Second)
timer := NewRepeatTimer("pex", ensurePeersPeriodSeconds*time.Second)
FOR_LOOP:
for {
select {