From 805e1f51dd274965ebf147cb530af0130c2d6c86 Mon Sep 17 00:00:00 2001 From: Jae Kwon Date: Thu, 5 Jun 2014 11:45:18 -0700 Subject: [PATCH] simplify writing --- binary/binary.go | 9 +++++++- blocks/account.go | 18 ++++++---------- blocks/adjustment.go | 50 ++++++++++++++++---------------------------- blocks/block.go | 43 +++++++++++++------------------------ blocks/signature.go | 10 ++++----- blocks/tx.go | 34 +++++++++++------------------- blocks/vote.go | 2 +- 7 files changed, 63 insertions(+), 103 deletions(-) diff --git a/binary/binary.go b/binary/binary.go index 0ad636bf7..8d13cd35f 100644 --- a/binary/binary.go +++ b/binary/binary.go @@ -3,5 +3,12 @@ package binary import "io" type Binary interface { - WriteTo(io.Writer) (int64, error) + WriteTo(w io.Writer) (int64, error) +} + +func WriteOnto(b Binary, w io.Writer, n int64, err error) (int64, error) { + if err != nil { return n, err } + var n_ int64 + n_, err = b.WriteTo(w) + return n+n_, err } diff --git a/blocks/account.go b/blocks/account.go index b4f2dd3d6..8f1250849 100644 --- a/blocks/account.go +++ b/blocks/account.go @@ -31,19 +31,13 @@ func ReadAccountId(r io.Reader) AccountId { } } -func (self *AccountId) WriteTo(w io.Writer) (n int64, err error) { - var n_ int64 - n_, err = self.Type.WriteTo(w) - n += n_; if err != nil { return n, err } - if self.Type == ACCOUNT_TYPE_NUMBER || - self.Type == ACCOUNT_TYPE_BOTH { - n_, err = self.Number.WriteTo(w) - n += n_; if err != nil { return n, err } +func (self AccountId) WriteTo(w io.Writer) (n int64, err error) { + n, err = WriteOnto(self.Type, w, n, err) + if self.Type == ACCOUNT_TYPE_NUMBER || self.Type == ACCOUNT_TYPE_BOTH { + n, err = WriteOnto(self.Number, w, n, err) } - if self.Type == ACCOUNT_TYPE_PUBKEY || - self.Type == ACCOUNT_TYPE_BOTH { - n_, err = self.PubKey.WriteTo(w) - n += n_; if err != nil { return n, err } + if self.Type == ACCOUNT_TYPE_PUBKEY || self.Type == ACCOUNT_TYPE_BOTH { + n, err = WriteOnto(self.PubKey, w, n, err) } return } diff --git a/blocks/adjustment.go b/blocks/adjustment.go index a7fe705c3..0c451a4ab 100644 --- a/blocks/adjustment.go +++ b/blocks/adjustment.go @@ -73,17 +73,12 @@ func (self *Bond) Type() Byte { } func (self *Bond) WriteTo(w io.Writer) (n int64, err error) { - var n_ int64 - n_, err = self.Type().WriteTo(w) - n += n_; if err != nil { return n, err } - n_, err = self.Fee.WriteTo(w) - n += n_; if err != nil { return n, err } - n_, err = self.UnbondTo.WriteTo(w) - n += n_; if err != nil { return n, err } - n_, err = self.Amount.WriteTo(w) - n += n_; if err != nil { return n, err } - n_, err = self.Signature.WriteTo(w) - n += n_; return + n, err = WriteOnto(self.Type(), w, n, err) + n, err = WriteOnto(self.Fee, w, n, err) + n, err = WriteOnto(self.UnbondTo, w, n, err) + n, err = WriteOnto(self.Amount, w, n, err) + n, err = WriteOnto(self.Signature, w, n, err) + return } @@ -100,15 +95,11 @@ func (self *Unbond) Type() Byte { } func (self *Unbond) WriteTo(w io.Writer) (n int64, err error) { - var n_ int64 - n_, err = self.Type().WriteTo(w) - n += n_; if err != nil { return n, err } - n_, err = self.Fee.WriteTo(w) - n += n_; if err != nil { return n, err } - n_, err = self.Amount.WriteTo(w) - n += n_; if err != nil { return n, err } - n_, err = self.Signature.WriteTo(w) - n += n_; return + n, err = WriteOnto(self.Type(), w, n, err) + n, err = WriteOnto(self.Fee, w, n, err) + n, err = WriteOnto(self.Amount, w, n, err) + n, err = WriteOnto(self.Signature, w, n, err) + return } @@ -124,13 +115,10 @@ func (self *Timeout) Type() Byte { } func (self *Timeout) WriteTo(w io.Writer) (n int64, err error) { - var n_ int64 - n_, err = self.Type().WriteTo(w) - n += n_; if err != nil { return n, err } - n_, err = self.Account.WriteTo(w) - n += n_; if err != nil { return n, err } - n_, err = self.Penalty.WriteTo(w) - n += n_; return + n, err = WriteOnto(self.Type(), w, n, err) + n, err = WriteOnto(self.Account, w, n, err) + n, err = WriteOnto(self.Penalty, w, n, err) + return } @@ -146,9 +134,7 @@ func (self *Dupeout) Type() Byte { } func (self *Dupeout) WriteTo(w io.Writer) (n int64, err error) { - var n_ int64 - n_, err = self.VoteA.WriteTo(w) - n += n_; if err != nil { return n, err } - n_, err = self.VoteB.WriteTo(w) - n += n_; return + n, err = WriteOnto(self.VoteA, w, n, err) + n, err = WriteOnto(self.VoteB, w, n, err) + return } diff --git a/blocks/block.go b/blocks/block.go index ad649fb90..b3e7b265d 100644 --- a/blocks/block.go +++ b/blocks/block.go @@ -54,21 +54,14 @@ func ReadHeader(r io.Reader) Header { } func (self *Header) WriteTo(w io.Writer) (n int64, err error) { - var n_ int64 - n_, err = self.Name.WriteTo(w) - n += n_; if err != nil { return n, err } - n_, err = self.Height.WriteTo(w) - n += n_; if err != nil { return n, err } - n_, err = self.Fees.WriteTo(w) - n += n_; if err != nil { return n, err } - n_, err = self.Time.WriteTo(w) - n += n_; if err != nil { return n, err } - n_, err = self.PrevHash.WriteTo(w) - n += n_; if err != nil { return n, err } - n_, err = self.ValidationHash.WriteTo(w) - n += n_; if err != nil { return n, err } - n_, err = self.DataHash.WriteTo(w) - n += n_; return + n, err = WriteOnto(self.Name, w, n, err) + n, err = WriteOnto(self.Height, w, n, err) + n, err = WriteOnto(self.Fees, w, n, err) + n, err = WriteOnto(self.Time, w, n, err) + n, err = WriteOnto(self.PrevHash, w, n, err) + n, err = WriteOnto(self.ValidationHash, w, n, err) + n, err = WriteOnto(self.DataHash, w, n, err) + return } @@ -97,18 +90,13 @@ func ReadValidation(r io.Reader) Validation { } func (self *Validation) WriteTo(w io.Writer) (n int64, err error) { - var n_ int64 - n_, err = UInt64(len(self.Signatures)).WriteTo(w) - n += n_; if err != nil { return n, err } - n_, err = UInt64(len(self.Adjustments)).WriteTo(w) - n += n_; if err != nil { return n, err } + n, err = WriteOnto(UInt64(len(self.Signatures)), w, n, err) + n, err = WriteOnto(UInt64(len(self.Adjustments)), w, n, err) for _, sig := range self.Signatures { - n_, err = sig.WriteTo(w) - n += n_; if err != nil { return n, err } + n, err = WriteOnto(sig, w, n, err) } for _, adj := range self.Adjustments { - n_, err = adj.WriteTo(w) - n += n_; if err != nil { return n, err } + n, err = WriteOnto(adj, w, n, err) } return } @@ -129,12 +117,9 @@ func ReadData(r io.Reader) Data { } func (self *Data) WriteTo(w io.Writer) (n int64, err error) { - var n_ int64 - n_, err = UInt64(len(self.Txs)).WriteTo(w) - n += n_; if err != nil { return n, err } + n, err = WriteOnto(UInt64(len(self.Txs)), w, n, err) for _, tx := range self.Txs { - n_, err = tx.WriteTo(w) - n += n_; if err != nil { return } + n, err = WriteOnto(tx, w, n, err) } return } diff --git a/blocks/signature.go b/blocks/signature.go index d58f81433..d32a66681 100644 --- a/blocks/signature.go +++ b/blocks/signature.go @@ -30,12 +30,10 @@ func ReadSignature(r io.Reader) Signature { } } -func (self *Signature) WriteTo(w io.Writer) (n int64, err error) { - var n_ int64 - n_, err = self.Signer.WriteTo(w) - n += n_; if err != nil { return n, err } - n_, err = self.SigBytes.WriteTo(w) - n += n_; return +func (self Signature) WriteTo(w io.Writer) (n int64, err error) { + n, err = WriteOnto(self.Signer, w, n, err) + n, err = WriteOnto(self.SigBytes, w, n, err) + return } func (self *Signature) Verify(msg ByteSlice) bool { diff --git a/blocks/tx.go b/blocks/tx.go index 29e5a4244..ce940b9f3 100644 --- a/blocks/tx.go +++ b/blocks/tx.go @@ -66,17 +66,12 @@ func (self *SendTx) Type() Byte { } func (self *SendTx) WriteTo(w io.Writer) (n int64, err error) { - var n_ int64 - n_, err = self.Type().WriteTo(w) - n += n_; if err != nil { return n, err } - n_, err = self.Fee.WriteTo(w) - n += n_; if err != nil { return n, err } - n_, err = self.To.WriteTo(w) - n += n_; if err != nil { return n, err } - n_, err = self.Amount.WriteTo(w) - n += n_; if err != nil { return n, err } - n_, err = self.Signature.WriteTo(w) - n += n_; return + n, err = WriteOnto(self.Type(), w, n, err) + n, err = WriteOnto(self.Fee, w, n, err) + n, err = WriteOnto(self.To, w, n, err) + n, err = WriteOnto(self.Amount, w, n, err) + n, err = WriteOnto(self.Signature, w, n, err) + return } @@ -94,15 +89,10 @@ func (self *NameTx) Type() Byte { } func (self *NameTx) WriteTo(w io.Writer) (n int64, err error) { - var n_ int64 - n_, err = self.Type().WriteTo(w) - n += n_; if err != nil { return n, err } - n_, err = self.Fee.WriteTo(w) - n += n_; if err != nil { return n, err } - n_, err = self.Name.WriteTo(w) - n += n_; if err != nil { return n, err } - n_, err = self.PubKey.WriteTo(w) - n += n_; if err != nil { return n, err } - n_, err = self.Signature.WriteTo(w) - n += n_; return + n, err = WriteOnto(self.Type(), w, n, err) + n, err = WriteOnto(self.Fee, w, n, err) + n, err = WriteOnto(self.Name, w, n, err) + n, err = WriteOnto(self.PubKey, w, n, err) + n, err = WriteOnto(self.Signature, w, n, err) + return } diff --git a/blocks/vote.go b/blocks/vote.go index ab8f2434c..9d219d6a0 100644 --- a/blocks/vote.go +++ b/blocks/vote.go @@ -24,7 +24,7 @@ func ReadVote(r io.Reader) Vote { } } -func (self *Vote) WriteTo(w io.Writer) (n int64, err error) { +func (self Vote) WriteTo(w io.Writer) (n int64, err error) { var n_ int64 n_, err = self.Height.WriteTo(w) n += n_; if err != nil { return n, err }