Browse Source

simplify writing

pull/9/head
Jae Kwon 11 years ago
parent
commit
805e1f51dd
7 changed files with 63 additions and 103 deletions
  1. +8
    -1
      binary/binary.go
  2. +6
    -12
      blocks/account.go
  3. +18
    -32
      blocks/adjustment.go
  4. +14
    -29
      blocks/block.go
  5. +4
    -6
      blocks/signature.go
  6. +12
    -22
      blocks/tx.go
  7. +1
    -1
      blocks/vote.go

+ 8
- 1
binary/binary.go View File

@ -3,5 +3,12 @@ package binary
import "io" import "io"
type Binary interface { 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
} }

+ 6
- 12
blocks/account.go View File

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


+ 18
- 32
blocks/adjustment.go View File

@ -73,17 +73,12 @@ 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) {
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) { 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) { 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) { 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
} }

+ 14
- 29
blocks/block.go View File

@ -54,21 +54,14 @@ func ReadHeader(r io.Reader) Header {
} }
func (self *Header) WriteTo(w io.Writer) (n int64, err error) { 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) { 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 { 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 { 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 return
} }
@ -129,12 +117,9 @@ func ReadData(r io.Reader) Data {
} }
func (self *Data) WriteTo(w io.Writer) (n int64, err error) { 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 { for _, tx := range self.Txs {
n_, err = tx.WriteTo(w)
n += n_; if err != nil { return }
n, err = WriteOnto(tx, w, n, err)
} }
return return
} }


+ 4
- 6
blocks/signature.go View File

@ -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 { func (self *Signature) Verify(msg ByteSlice) bool {


+ 12
- 22
blocks/tx.go View File

@ -66,17 +66,12 @@ 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) {
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) { 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
} }

+ 1
- 1
blocks/vote.go View File

@ -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 var n_ int64
n_, err = self.Height.WriteTo(w) n_, err = self.Height.WriteTo(w)
n += n_; if err != nil { return n, err } n += n_; if err != nil { return n, err }


Loading…
Cancel
Save