fill in blocks methods

This commit is contained in:
Jae Kwon 2014-06-05 11:04:56 -07:00
parent eda3965d6a
commit 380019949e
8 changed files with 207 additions and 92 deletions

View File

@ -5,49 +5,49 @@ import (
"io" "io"
) )
type AccountId interface { type AccountId struct {
Binary Type Byte
Type() Byte Number UInt64
PubKey ByteSlice
} }
const ( const (
ACCOUNT_TYPE_NUMBER = Byte(0x00) ACCOUNT_TYPE_NUMBER = Byte(0x01)
ACCOUNT_TYPE_PUBKEY = Byte(0x01) ACCOUNT_TYPE_PUBKEY = Byte(0x02)
ACCOUNT_TYPE_BOTH = Byte(0x03)
) )
func ReadAccountId(r io.Reader) AccountId { func ReadAccountId(r io.Reader) AccountId {
return nil switch t := ReadByte(r); t {
case ACCOUNT_TYPE_NUMBER:
return AccountId{t, ReadUInt64(r), nil}
case ACCOUNT_TYPE_PUBKEY:
return AccountId{t, 0, ReadByteSlice(r)}
case ACCOUNT_TYPE_BOTH:
return AccountId{t, ReadUInt64(r), ReadByteSlice(r)}
default:
panicf("Unknown AccountId type %x", t)
return AccountId{}
}
} }
/* AccountNumber < AccountId */ func (self *AccountId) WriteTo(w io.Writer) (n int64, err error) {
type AccountNumber uint64
func (self AccountNumber) Type() Byte {
return ACCOUNT_TYPE_NUMBER
}
func (self AccountNumber) WriteTo(w io.Writer) (n int64, err error) {
var n_ int64 var n_ int64
n_, err = self.Type().WriteTo(w) n_, err = self.Type.WriteTo(w)
n += n_; if err != nil { return n, err } n += n_; if err != nil { return n, err }
n_, err = UInt64(self).WriteTo(w) if self.Type == ACCOUNT_TYPE_NUMBER ||
n += n_; return self.Type == ACCOUNT_TYPE_BOTH {
n_, err = self.Number.WriteTo(w)
n += n_; if err != nil { return 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 }
}
return
} }
func AccountNumber(n UInt64) AccountId {
/* AccountPubKey < AccountId */ return AccountId{ACCOUNT_TYPE_NUMBER, n, nil}
type AccountPubKey []byte
func (self AccountPubKey) Type() Byte {
return ACCOUNT_TYPE_PUBKEY
}
func (self AccountPubKey) 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 = ByteSlice(self).WriteTo(w)
n += n_; return
} }

View File

@ -24,21 +24,48 @@ const (
ADJ_TYPE_BOND = Byte(0x01) ADJ_TYPE_BOND = Byte(0x01)
ADJ_TYPE_UNBOND = Byte(0x02) ADJ_TYPE_UNBOND = Byte(0x02)
ADJ_TYPE_TIMEOUT = Byte(0x03) ADJ_TYPE_TIMEOUT = Byte(0x03)
ADJ_TYPE_GUILTOUT = Byte(0x04) ADJ_TYPE_DUPEOUT = Byte(0x04)
) )
func ReadAdjustment(r io.Reader) Adjustment { func ReadAdjustment(r io.Reader) Adjustment {
return nil switch t := ReadByte(r); t {
case ADJ_TYPE_BOND:
return &Bond{
Fee: ReadUInt64(r),
UnbondTo: ReadAccountId(r),
Amount: ReadUInt64(r),
Signature: ReadSignature(r),
}
case ADJ_TYPE_UNBOND:
return &Unbond{
Fee: ReadUInt64(r),
Amount: ReadUInt64(r),
Signature: ReadSignature(r),
}
case ADJ_TYPE_TIMEOUT:
return &Timeout{
Account: ReadAccountId(r),
Penalty: ReadUInt64(r),
}
case ADJ_TYPE_DUPEOUT:
return &Dupeout{
VoteA: ReadVote(r),
VoteB: ReadVote(r),
}
default:
panicf("Unknown Adjustment type %x", t)
return nil
}
} }
/* Bond < Adjustment */ /* Bond < Adjustment */
type Bond struct { type Bond struct {
Signature
Fee UInt64 Fee UInt64
UnbondTo AccountId UnbondTo AccountId
Amount UInt64 Amount UInt64
Signature
} }
func (self *Bond) Type() Byte { func (self *Bond) Type() Byte {
@ -49,13 +76,13 @@ func (self *Bond) WriteTo(w io.Writer) (n int64, err error) {
var n_ int64 var n_ int64
n_, err = self.Type().WriteTo(w) n_, err = self.Type().WriteTo(w)
n += n_; if err != nil { return n, err } n += n_; if err != nil { return n, err }
n_, err = self.Signature.WriteTo(w)
n += n_; if err != nil { return n, err }
n_, err = self.Fee.WriteTo(w) n_, err = self.Fee.WriteTo(w)
n += n_; if err != nil { return n, err } n += n_; if err != nil { return n, err }
n_, err = self.UnbondTo.WriteTo(w) n_, err = self.UnbondTo.WriteTo(w)
n += n_; if err != nil { return n, err } n += n_; if err != nil { return n, err }
n_, err = self.Amount.WriteTo(w) n_, err = self.Amount.WriteTo(w)
n += n_; if err != nil { return n, err }
n_, err = self.Signature.WriteTo(w)
n += n_; return n += n_; return
} }
@ -63,9 +90,9 @@ func (self *Bond) WriteTo(w io.Writer) (n int64, err error) {
/* Unbond < Adjustment */ /* Unbond < Adjustment */
type Unbond struct { type Unbond struct {
Signature
Fee UInt64 Fee UInt64
Amount UInt64 Amount UInt64
Signature
} }
func (self *Unbond) Type() Byte { func (self *Unbond) Type() Byte {
@ -76,11 +103,11 @@ func (self *Unbond) WriteTo(w io.Writer) (n int64, err error) {
var n_ int64 var n_ int64
n_, err = self.Type().WriteTo(w) n_, err = self.Type().WriteTo(w)
n += n_; if err != nil { return n, err } n += n_; if err != nil { return n, err }
n_, err = self.Signature.WriteTo(w)
n += n_; if err != nil { return n, err }
n_, err = self.Fee.WriteTo(w) n_, err = self.Fee.WriteTo(w)
n += n_; if err != nil { return n, err } n += n_; if err != nil { return n, err }
n_, err = self.Amount.WriteTo(w) n_, err = self.Amount.WriteTo(w)
n += n_; if err != nil { return n, err }
n_, err = self.Signature.WriteTo(w)
n += n_; return n += n_; return
} }
@ -120,10 +147,8 @@ 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 var n_ int64
n_, err = self.Type().WriteTo(w) n_, err = self.VoteA.WriteTo(w)
n += n_; if err != nil { return n, err } n += n_; if err != nil { return n, err }
n_, err = self.Account.WriteTo(w) n_, err = self.VoteB.WriteTo(w)
n += n_; if err != nil { return n, err }
n_, err = self.Penalty.WriteTo(w)
n += n_; return n += n_; return
} }

View File

@ -2,6 +2,7 @@ package blocks
import ( import (
. "github.com/tendermint/tendermint/binary" . "github.com/tendermint/tendermint/binary"
"github.com/tendermint/tendermint/merkle"
"io" "io"
) )
@ -16,7 +17,11 @@ type Block struct {
} }
func ReadBlock(r io.Reader) *Block { func ReadBlock(r io.Reader) *Block {
return nil return &Block{
Header: ReadHeader(r),
Validation: ReadValidation(r),
Data: ReadData(r),
}
} }
func (self *Block) Validate() bool { func (self *Block) Validate() bool {
@ -36,8 +41,16 @@ type Header struct {
DataHash ByteSlice DataHash ByteSlice
} }
func ReadHeader(r io.Reader) *Header { func ReadHeader(r io.Reader) Header {
return nil return Header{
Name: ReadString(r),
Height: ReadUInt64(r),
Fees: ReadUInt64(r),
Time: ReadUInt64(r),
PrevHash: ReadByteSlice(r),
ValidationHash: ReadByteSlice(r),
DataHash: ReadByteSlice(r),
}
} }
func (self *Header) WriteTo(w io.Writer) (n int64, err error) { func (self *Header) WriteTo(w io.Writer) (n int64, err error) {
@ -62,14 +75,43 @@ func (self *Header) WriteTo(w io.Writer) (n int64, err error) {
/* Block > Validation */ /* Block > Validation */
type Validation struct { type Validation struct {
Signatures []*Signature Signatures []Signature
Adjustments []Adjustment Adjustments []Adjustment
} }
func ReadValidation(r io.Reader) *Validation { func ReadValidation(r io.Reader) Validation {
return nil numSigs := int(ReadUInt64(r))
numAdjs := int(ReadUInt64(r))
sigs := make([]Signature, 0, numSigs)
for i:=0; i<numSigs; i++ {
sigs = append(sigs, ReadSignature(r))
}
adjs := make([]Adjustment, 0, numAdjs)
for i:=0; i<numSigs; i++ {
adjs = append(adjs, ReadAdjustment(r))
}
return Validation{
Signatures: sigs,
Adjustments: adjs,
}
} }
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 }
for _, sig := range self.Signatures {
n_, err = sig.WriteTo(w)
n += n_; if err != nil { return n, err }
}
for _, adj := range self.Adjustments {
n_, err = adj.WriteTo(w)
n += n_; if err != nil { return n, err }
}
return
}
/* Block > Data */ /* Block > Data */
@ -77,16 +119,22 @@ type Data struct {
Txs []Tx Txs []Tx
} }
func ReadData(r io.Reader) *Data { func ReadData(r io.Reader) Data {
return nil numTxs := int(ReadUInt64(r))
txs := make([]Tx, 0, numTxs)
for i:=0; i<numTxs; i++ {
txs = append(txs, ReadTx(r))
}
return Data{txs}
} }
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 var n_ int64
n_, err = UInt64(len(self.Txs)).WriteTo(w)
n += n_; if err != nil { return n, err }
for _, tx := range self.Txs { for _, tx := range self.Txs {
n_, err = tx.WriteTo(w) n_, err = tx.WriteTo(w)
n += n_ n += n_; if err != nil { return }
if err != nil { return }
} }
return return
} }

View File

@ -3,7 +3,6 @@ package blocks
import ( import (
. "github.com/tendermint/tendermint/binary" . "github.com/tendermint/tendermint/binary"
"testing" "testing"
"bytes"
"fmt" "fmt"
"math/rand" "math/rand"
) )
@ -26,7 +25,6 @@ func randBytes(n int) ByteSlice {
} }
func TestBlock(t *testing.T) { func TestBlock(t *testing.T) {
var block = NewBlock()
sendTx := &SendTx{ sendTx := &SendTx{
Signature: Signature{AccountNumber(randVar()), randBytes(32)}, Signature: Signature{AccountNumber(randVar()), randBytes(32)},
@ -35,6 +33,7 @@ func TestBlock(t *testing.T) {
Amount: randVar(), Amount: randVar(),
} }
/*
bondTx := &BondTx{ bondTx := &BondTx{
Signature: Signature{AccountNumber(randVar()), randBytes(32)}, Signature: Signature{AccountNumber(randVar()), randBytes(32)},
Fee: randVar(), Fee: randVar(),
@ -47,6 +46,7 @@ func TestBlock(t *testing.T) {
Fee: randVar(), Fee: randVar(),
Amount: randVar(), Amount: randVar(),
} }
*/
nameTx := &NameTx{ nameTx := &NameTx{
Signature: Signature{AccountNumber(randVar()), randBytes(32)}, Signature: Signature{AccountNumber(randVar()), randBytes(32)},
@ -55,13 +55,9 @@ func TestBlock(t *testing.T) {
PubKey: randBytes(32), PubKey: randBytes(32),
} }
block.AddTx(sendTx) txs := []Tx{}
block.AddTx(bondTx) txs = append(txs, sendTx)
block.AddTx(unbondTx) txs = append(txs, nameTx)
block.AddTx(nameTx)
blockBytes := block.Bytes()
fmt.Println(buf.Bytes(), len(buf.Bytes()))
fmt.Println(txs)
} }

View File

@ -23,16 +23,10 @@ type Signature struct {
SigBytes ByteSlice SigBytes ByteSlice
} }
func ReadSignature(r io.Reader) *Signature { func ReadSignature(r io.Reader) Signature {
return nil return Signature{
} Signer: ReadAccountId(r),
SigBytes: ReadByteSlice(r),
func (self *Signature) Equals(other Binary) bool {
if o, ok := other.(*Signature); ok {
return self.Signer.Equals(o.Signer) &&
self.SigBytes.Equals(o.SigBytes)
} else {
return false
} }
} }

View File

@ -22,8 +22,6 @@ Tx wire format:
type Tx interface { type Tx interface {
Type() Byte Type() Byte
Binary Binary
Signature() *Signature
} }
const ( const (
@ -32,17 +30,35 @@ const (
) )
func ReadTx(r io.Reader) Tx { func ReadTx(r io.Reader) Tx {
return nil switch t := ReadByte(r); t {
case TX_TYPE_SEND:
return &SendTx{
Fee: ReadUInt64(r),
To: ReadAccountId(r),
Amount: ReadUInt64(r),
Signature: ReadSignature(r),
}
case TX_TYPE_NAME:
return &NameTx{
Fee: ReadUInt64(r),
Name: ReadString(r),
PubKey: ReadByteSlice(r),
Signature: ReadSignature(r),
}
default:
panicf("Unknown Tx type %x", t)
return nil
}
} }
/* SendTx < Tx */ /* SendTx < Tx */
type SendTx struct { type SendTx struct {
Signature
Fee UInt64 Fee UInt64
To AccountId To AccountId
Amount UInt64 Amount UInt64
Signature
} }
func (self *SendTx) Type() Byte { func (self *SendTx) Type() Byte {
@ -53,28 +69,24 @@ func (self *SendTx) WriteTo(w io.Writer) (n int64, err error) {
var n_ int64 var n_ int64
n_, err = self.Type().WriteTo(w) n_, err = self.Type().WriteTo(w)
n += n_; if err != nil { return n, err } n += n_; if err != nil { return n, err }
n_, err = self.Signature.WriteTo(w)
n += n_; if err != nil { return n, err }
n_, err = self.Fee.WriteTo(w) n_, err = self.Fee.WriteTo(w)
n += n_; if err != nil { return n, err } n += n_; if err != nil { return n, err }
n_, err = self.To.WriteTo(w) n_, err = self.To.WriteTo(w)
n += n_; if err != nil { return n, err } n += n_; if err != nil { return n, err }
n_, err = self.Amount.WriteTo(w) n_, err = self.Amount.WriteTo(w)
n += n_; if err != nil { return n, err }
n_, err = self.Signature.WriteTo(w)
n += n_; return n += n_; return
} }
func (self *SendTx) Signature() *Signature {
return self.Signature
}
/* NameTx < Tx */ /* NameTx < Tx */
type NameTx struct { type NameTx struct {
Signature
Fee UInt64 Fee UInt64
Name String Name String
PubKey ByteSlice PubKey ByteSlice
Signature
} }
func (self *NameTx) Type() Byte { func (self *NameTx) Type() Byte {
@ -85,16 +97,12 @@ func (self *NameTx) WriteTo(w io.Writer) (n int64, err error) {
var n_ int64 var n_ int64
n_, err = self.Type().WriteTo(w) n_, err = self.Type().WriteTo(w)
n += n_; if err != nil { return n, err } n += n_; if err != nil { return n, err }
n_, err = self.Signature.WriteTo(w)
n += n_; if err != nil { return n, err }
n_, err = self.Fee.WriteTo(w) n_, err = self.Fee.WriteTo(w)
n += n_; if err != nil { return n, err } n += n_; if err != nil { return n, err }
n_, err = self.Name.WriteTo(w) n_, err = self.Name.WriteTo(w)
n += n_; if err != nil { return n, err } n += n_; if err != nil { return n, err }
n_, err = self.PubKey.WriteTo(w) n_, err = self.PubKey.WriteTo(w)
n += n_; if err != nil { return n, err }
n_, err = self.Signature.WriteTo(w)
n += n_; return n += n_; return
} }
func (self *NameTx) Signature() *Signature {
return self.Signature
}

9
blocks/util.go Normal file
View File

@ -0,0 +1,9 @@
package blocks
import (
"fmt"
)
func panicf(s string, args ...interface{}) {
panic(fmt.Sprintf(s, args...))
}

35
blocks/vote.go Normal file
View File

@ -0,0 +1,35 @@
package blocks
import (
. "github.com/tendermint/tendermint/binary"
"io"
)
/*
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 Vote struct {
Height UInt64
BlockHash ByteSlice
Signature
}
func ReadVote(r io.Reader) Vote {
return Vote{
Height: ReadUInt64(r),
BlockHash: ReadByteSlice(r),
Signature: ReadSignature(r),
}
}
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 }
n_, err = self.BlockHash.WriteTo(w)
n += n_; if err != nil { return n, err }
n_, err = self.Signature.WriteTo(w)
n += n_; return
}