mirror of
https://github.com/fluencelabs/tendermint
synced 2025-04-24 22:32:15 +00:00
fill in blocks methods
This commit is contained in:
parent
eda3965d6a
commit
380019949e
@ -5,49 +5,49 @@ import (
|
||||
"io"
|
||||
)
|
||||
|
||||
type AccountId interface {
|
||||
Binary
|
||||
Type() Byte
|
||||
type AccountId struct {
|
||||
Type Byte
|
||||
Number UInt64
|
||||
PubKey ByteSlice
|
||||
}
|
||||
|
||||
const (
|
||||
ACCOUNT_TYPE_NUMBER = Byte(0x00)
|
||||
ACCOUNT_TYPE_PUBKEY = Byte(0x01)
|
||||
ACCOUNT_TYPE_NUMBER = Byte(0x01)
|
||||
ACCOUNT_TYPE_PUBKEY = Byte(0x02)
|
||||
ACCOUNT_TYPE_BOTH = Byte(0x03)
|
||||
)
|
||||
|
||||
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 */
|
||||
|
||||
type AccountNumber uint64
|
||||
|
||||
func (self AccountNumber) Type() Byte {
|
||||
return ACCOUNT_TYPE_NUMBER
|
||||
}
|
||||
|
||||
func (self AccountNumber) WriteTo(w io.Writer) (n int64, err error) {
|
||||
func (self *AccountId) WriteTo(w io.Writer) (n int64, err error) {
|
||||
var n_ int64
|
||||
n_, err = self.Type().WriteTo(w)
|
||||
n_, err = self.Type.WriteTo(w)
|
||||
n += n_; if err != nil { return n, err }
|
||||
n_, err = UInt64(self).WriteTo(w)
|
||||
n += n_; return
|
||||
}
|
||||
|
||||
|
||||
/* AccountPubKey < AccountId */
|
||||
|
||||
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)
|
||||
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 }
|
||||
n_, err = ByteSlice(self).WriteTo(w)
|
||||
n += n_; return
|
||||
}
|
||||
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 {
|
||||
return AccountId{ACCOUNT_TYPE_NUMBER, n, nil}
|
||||
}
|
||||
|
@ -24,21 +24,48 @@ const (
|
||||
ADJ_TYPE_BOND = Byte(0x01)
|
||||
ADJ_TYPE_UNBOND = Byte(0x02)
|
||||
ADJ_TYPE_TIMEOUT = Byte(0x03)
|
||||
ADJ_TYPE_GUILTOUT = Byte(0x04)
|
||||
ADJ_TYPE_DUPEOUT = Byte(0x04)
|
||||
)
|
||||
|
||||
func ReadAdjustment(r io.Reader) Adjustment {
|
||||
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 */
|
||||
|
||||
type Bond struct {
|
||||
Signature
|
||||
Fee UInt64
|
||||
UnbondTo AccountId
|
||||
Amount UInt64
|
||||
Signature
|
||||
}
|
||||
|
||||
func (self *Bond) Type() Byte {
|
||||
@ -49,13 +76,13 @@ 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.Signature.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
|
||||
}
|
||||
|
||||
@ -63,9 +90,9 @@ func (self *Bond) WriteTo(w io.Writer) (n int64, err error) {
|
||||
/* Unbond < Adjustment */
|
||||
|
||||
type Unbond struct {
|
||||
Signature
|
||||
Fee UInt64
|
||||
Amount UInt64
|
||||
Signature
|
||||
}
|
||||
|
||||
func (self *Unbond) Type() Byte {
|
||||
@ -76,11 +103,11 @@ 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.Signature.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
|
||||
}
|
||||
|
||||
@ -120,10 +147,8 @@ func (self *Dupeout) Type() Byte {
|
||||
|
||||
func (self *Dupeout) WriteTo(w io.Writer) (n int64, err error) {
|
||||
var n_ int64
|
||||
n_, err = self.Type().WriteTo(w)
|
||||
n_, err = self.VoteA.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_, err = self.VoteB.WriteTo(w)
|
||||
n += n_; return
|
||||
}
|
||||
|
@ -2,6 +2,7 @@ package blocks
|
||||
|
||||
import (
|
||||
. "github.com/tendermint/tendermint/binary"
|
||||
"github.com/tendermint/tendermint/merkle"
|
||||
"io"
|
||||
)
|
||||
|
||||
@ -16,7 +17,11 @@ type Block struct {
|
||||
}
|
||||
|
||||
func ReadBlock(r io.Reader) *Block {
|
||||
return nil
|
||||
return &Block{
|
||||
Header: ReadHeader(r),
|
||||
Validation: ReadValidation(r),
|
||||
Data: ReadData(r),
|
||||
}
|
||||
}
|
||||
|
||||
func (self *Block) Validate() bool {
|
||||
@ -36,8 +41,16 @@ type Header struct {
|
||||
DataHash ByteSlice
|
||||
}
|
||||
|
||||
func ReadHeader(r io.Reader) *Header {
|
||||
return nil
|
||||
func ReadHeader(r io.Reader) Header {
|
||||
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) {
|
||||
@ -62,14 +75,43 @@ func (self *Header) WriteTo(w io.Writer) (n int64, err error) {
|
||||
/* Block > Validation */
|
||||
|
||||
type Validation struct {
|
||||
Signatures []*Signature
|
||||
Signatures []Signature
|
||||
Adjustments []Adjustment
|
||||
}
|
||||
|
||||
func ReadValidation(r io.Reader) *Validation {
|
||||
return nil
|
||||
func ReadValidation(r io.Reader) Validation {
|
||||
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 */
|
||||
|
||||
@ -77,16 +119,22 @@ type Data struct {
|
||||
Txs []Tx
|
||||
}
|
||||
|
||||
func ReadData(r io.Reader) *Data {
|
||||
return nil
|
||||
func ReadData(r io.Reader) Data {
|
||||
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) {
|
||||
var n_ int64
|
||||
n_, err = UInt64(len(self.Txs)).WriteTo(w)
|
||||
n += n_; if err != nil { return n, err }
|
||||
for _, tx := range self.Txs {
|
||||
n_, err = tx.WriteTo(w)
|
||||
n += n_
|
||||
if err != nil { return }
|
||||
n += n_; if err != nil { return }
|
||||
}
|
||||
return
|
||||
}
|
||||
|
@ -3,7 +3,6 @@ package blocks
|
||||
import (
|
||||
. "github.com/tendermint/tendermint/binary"
|
||||
"testing"
|
||||
"bytes"
|
||||
"fmt"
|
||||
"math/rand"
|
||||
)
|
||||
@ -26,7 +25,6 @@ func randBytes(n int) ByteSlice {
|
||||
}
|
||||
|
||||
func TestBlock(t *testing.T) {
|
||||
var block = NewBlock()
|
||||
|
||||
sendTx := &SendTx{
|
||||
Signature: Signature{AccountNumber(randVar()), randBytes(32)},
|
||||
@ -35,6 +33,7 @@ func TestBlock(t *testing.T) {
|
||||
Amount: randVar(),
|
||||
}
|
||||
|
||||
/*
|
||||
bondTx := &BondTx{
|
||||
Signature: Signature{AccountNumber(randVar()), randBytes(32)},
|
||||
Fee: randVar(),
|
||||
@ -47,6 +46,7 @@ func TestBlock(t *testing.T) {
|
||||
Fee: randVar(),
|
||||
Amount: randVar(),
|
||||
}
|
||||
*/
|
||||
|
||||
nameTx := &NameTx{
|
||||
Signature: Signature{AccountNumber(randVar()), randBytes(32)},
|
||||
@ -55,13 +55,9 @@ func TestBlock(t *testing.T) {
|
||||
PubKey: randBytes(32),
|
||||
}
|
||||
|
||||
block.AddTx(sendTx)
|
||||
block.AddTx(bondTx)
|
||||
block.AddTx(unbondTx)
|
||||
block.AddTx(nameTx)
|
||||
|
||||
blockBytes := block.Bytes()
|
||||
|
||||
fmt.Println(buf.Bytes(), len(buf.Bytes()))
|
||||
txs := []Tx{}
|
||||
txs = append(txs, sendTx)
|
||||
txs = append(txs, nameTx)
|
||||
|
||||
fmt.Println(txs)
|
||||
}
|
||||
|
@ -23,16 +23,10 @@ type Signature struct {
|
||||
SigBytes ByteSlice
|
||||
}
|
||||
|
||||
func ReadSignature(r io.Reader) *Signature {
|
||||
return nil
|
||||
}
|
||||
|
||||
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
|
||||
func ReadSignature(r io.Reader) Signature {
|
||||
return Signature{
|
||||
Signer: ReadAccountId(r),
|
||||
SigBytes: ReadByteSlice(r),
|
||||
}
|
||||
}
|
||||
|
||||
|
40
blocks/tx.go
40
blocks/tx.go
@ -22,8 +22,6 @@ Tx wire format:
|
||||
type Tx interface {
|
||||
Type() Byte
|
||||
Binary
|
||||
|
||||
Signature() *Signature
|
||||
}
|
||||
|
||||
const (
|
||||
@ -32,17 +30,35 @@ const (
|
||||
)
|
||||
|
||||
func ReadTx(r io.Reader) Tx {
|
||||
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 */
|
||||
|
||||
type SendTx struct {
|
||||
Signature
|
||||
Fee UInt64
|
||||
To AccountId
|
||||
Amount UInt64
|
||||
Signature
|
||||
}
|
||||
|
||||
func (self *SendTx) Type() Byte {
|
||||
@ -53,28 +69,24 @@ 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.Signature.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
|
||||
}
|
||||
|
||||
func (self *SendTx) Signature() *Signature {
|
||||
return self.Signature
|
||||
}
|
||||
|
||||
|
||||
/* NameTx < Tx */
|
||||
|
||||
type NameTx struct {
|
||||
Signature
|
||||
Fee UInt64
|
||||
Name String
|
||||
PubKey ByteSlice
|
||||
Signature
|
||||
}
|
||||
|
||||
func (self *NameTx) Type() Byte {
|
||||
@ -85,16 +97,12 @@ 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.Signature.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
|
||||
}
|
||||
|
||||
func (self *NameTx) Signature() *Signature {
|
||||
return self.Signature
|
||||
}
|
||||
|
9
blocks/util.go
Normal file
9
blocks/util.go
Normal 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
35
blocks/vote.go
Normal 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
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user