mirror of
https://github.com/fluencelabs/tendermint
synced 2025-06-27 11:41:39 +00:00
go fmt
This commit is contained in:
@ -1,48 +1,48 @@
|
||||
package blocks
|
||||
|
||||
import (
|
||||
. "github.com/tendermint/tendermint/common"
|
||||
. "github.com/tendermint/tendermint/binary"
|
||||
"io"
|
||||
. "github.com/tendermint/tendermint/binary"
|
||||
. "github.com/tendermint/tendermint/common"
|
||||
"io"
|
||||
)
|
||||
|
||||
type AccountId struct {
|
||||
Type Byte
|
||||
Number UInt64
|
||||
PubKey ByteSlice
|
||||
Type Byte
|
||||
Number UInt64
|
||||
PubKey ByteSlice
|
||||
}
|
||||
|
||||
const (
|
||||
ACCOUNT_TYPE_NUMBER = Byte(0x01)
|
||||
ACCOUNT_TYPE_PUBKEY = Byte(0x02)
|
||||
ACCOUNT_TYPE_BOTH = Byte(0x03)
|
||||
ACCOUNT_TYPE_NUMBER = Byte(0x01)
|
||||
ACCOUNT_TYPE_PUBKEY = Byte(0x02)
|
||||
ACCOUNT_TYPE_BOTH = Byte(0x03)
|
||||
)
|
||||
|
||||
func ReadAccountId(r io.Reader) AccountId {
|
||||
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{}
|
||||
}
|
||||
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{}
|
||||
}
|
||||
}
|
||||
|
||||
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 = WriteOnto(self.PubKey, w, n, err)
|
||||
}
|
||||
return
|
||||
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 = WriteOnto(self.PubKey, w, n, err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func AccountNumber(n UInt64) AccountId {
|
||||
return AccountId{ACCOUNT_TYPE_NUMBER, n, nil}
|
||||
return AccountId{ACCOUNT_TYPE_NUMBER, n, nil}
|
||||
}
|
||||
|
@ -1,9 +1,9 @@
|
||||
package blocks
|
||||
|
||||
import (
|
||||
. "github.com/tendermint/tendermint/common"
|
||||
. "github.com/tendermint/tendermint/binary"
|
||||
"io"
|
||||
. "github.com/tendermint/tendermint/binary"
|
||||
. "github.com/tendermint/tendermint/common"
|
||||
"io"
|
||||
)
|
||||
|
||||
/* Adjustment
|
||||
@ -17,126 +17,122 @@ TODO: signing a bad checkpoint (block)
|
||||
*/
|
||||
|
||||
type Adjustment interface {
|
||||
Type() Byte
|
||||
Binary
|
||||
Type() Byte
|
||||
Binary
|
||||
}
|
||||
|
||||
const (
|
||||
ADJ_TYPE_BOND = Byte(0x01)
|
||||
ADJ_TYPE_UNBOND = Byte(0x02)
|
||||
ADJ_TYPE_TIMEOUT = Byte(0x03)
|
||||
ADJ_TYPE_DUPEOUT = Byte(0x04)
|
||||
ADJ_TYPE_BOND = Byte(0x01)
|
||||
ADJ_TYPE_UNBOND = Byte(0x02)
|
||||
ADJ_TYPE_TIMEOUT = Byte(0x03)
|
||||
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
|
||||
}
|
||||
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 {
|
||||
Fee UInt64
|
||||
UnbondTo AccountId
|
||||
Amount UInt64
|
||||
Signature
|
||||
Fee UInt64
|
||||
UnbondTo AccountId
|
||||
Amount UInt64
|
||||
Signature
|
||||
}
|
||||
|
||||
func (self *Bond) Type() Byte {
|
||||
return ADJ_TYPE_BOND
|
||||
return ADJ_TYPE_BOND
|
||||
}
|
||||
|
||||
func (self *Bond) WriteTo(w io.Writer) (n int64, err error) {
|
||||
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
|
||||
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
|
||||
}
|
||||
|
||||
|
||||
/* Unbond < Adjustment */
|
||||
|
||||
type Unbond struct {
|
||||
Fee UInt64
|
||||
Amount UInt64
|
||||
Signature
|
||||
Fee UInt64
|
||||
Amount UInt64
|
||||
Signature
|
||||
}
|
||||
|
||||
func (self *Unbond) Type() Byte {
|
||||
return ADJ_TYPE_UNBOND
|
||||
return ADJ_TYPE_UNBOND
|
||||
}
|
||||
|
||||
func (self *Unbond) WriteTo(w io.Writer) (n int64, err error) {
|
||||
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
|
||||
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
|
||||
}
|
||||
|
||||
|
||||
/* Timeout < Adjustment */
|
||||
|
||||
type Timeout struct {
|
||||
Account AccountId
|
||||
Penalty UInt64
|
||||
Account AccountId
|
||||
Penalty UInt64
|
||||
}
|
||||
|
||||
func (self *Timeout) Type() Byte {
|
||||
return ADJ_TYPE_TIMEOUT
|
||||
return ADJ_TYPE_TIMEOUT
|
||||
}
|
||||
|
||||
func (self *Timeout) WriteTo(w io.Writer) (n int64, err error) {
|
||||
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
|
||||
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
|
||||
}
|
||||
|
||||
|
||||
/* Dupeout < Adjustment */
|
||||
|
||||
type Dupeout struct {
|
||||
VoteA Vote
|
||||
VoteB Vote
|
||||
VoteA Vote
|
||||
VoteB Vote
|
||||
}
|
||||
|
||||
func (self *Dupeout) Type() Byte {
|
||||
return ADJ_TYPE_DUPEOUT
|
||||
return ADJ_TYPE_DUPEOUT
|
||||
}
|
||||
|
||||
func (self *Dupeout) WriteTo(w io.Writer) (n int64, err error) {
|
||||
n, err = WriteOnto(self.Type(), w, n, err)
|
||||
n, err = WriteOnto(self.VoteA, w, n, err)
|
||||
n, err = WriteOnto(self.VoteB, w, n, err)
|
||||
return
|
||||
n, err = WriteOnto(self.Type(), w, n, err)
|
||||
n, err = WriteOnto(self.VoteA, w, n, err)
|
||||
n, err = WriteOnto(self.VoteB, w, n, err)
|
||||
return
|
||||
}
|
||||
|
169
blocks/block.go
169
blocks/block.go
@ -1,140 +1,137 @@
|
||||
package blocks
|
||||
|
||||
import (
|
||||
. "github.com/tendermint/tendermint/binary"
|
||||
"github.com/tendermint/tendermint/merkle"
|
||||
"io"
|
||||
. "github.com/tendermint/tendermint/binary"
|
||||
"github.com/tendermint/tendermint/merkle"
|
||||
"io"
|
||||
)
|
||||
|
||||
|
||||
/* Block */
|
||||
|
||||
type Block struct {
|
||||
Header
|
||||
Validation
|
||||
Data
|
||||
// Checkpoint
|
||||
Header
|
||||
Validation
|
||||
Data
|
||||
// Checkpoint
|
||||
}
|
||||
|
||||
func ReadBlock(r io.Reader) *Block {
|
||||
return &Block{
|
||||
Header: ReadHeader(r),
|
||||
Validation: ReadValidation(r),
|
||||
Data: ReadData(r),
|
||||
}
|
||||
return &Block{
|
||||
Header: ReadHeader(r),
|
||||
Validation: ReadValidation(r),
|
||||
Data: ReadData(r),
|
||||
}
|
||||
}
|
||||
|
||||
func (self *Block) Validate() bool {
|
||||
return false
|
||||
return false
|
||||
}
|
||||
|
||||
func (self *Block) WriteTo(w io.Writer) (n int64, err error) {
|
||||
n, err = WriteOnto(&self.Header, w, n, err)
|
||||
n, err = WriteOnto(&self.Validation, w, n, err)
|
||||
n, err = WriteOnto(&self.Data, w, n, err)
|
||||
return
|
||||
n, err = WriteOnto(&self.Header, w, n, err)
|
||||
n, err = WriteOnto(&self.Validation, w, n, err)
|
||||
n, err = WriteOnto(&self.Data, w, n, err)
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
/* Block > Header */
|
||||
|
||||
type Header struct {
|
||||
Name String
|
||||
Height UInt64
|
||||
Fees UInt64
|
||||
Time UInt64
|
||||
PrevHash ByteSlice
|
||||
ValidationHash ByteSlice
|
||||
DataHash ByteSlice
|
||||
Name String
|
||||
Height UInt64
|
||||
Fees UInt64
|
||||
Time UInt64
|
||||
PrevHash ByteSlice
|
||||
ValidationHash ByteSlice
|
||||
DataHash ByteSlice
|
||||
}
|
||||
|
||||
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),
|
||||
}
|
||||
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) {
|
||||
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
|
||||
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
|
||||
}
|
||||
|
||||
|
||||
/* Block > Validation */
|
||||
|
||||
type Validation struct {
|
||||
Signatures []Signature
|
||||
Adjustments []Adjustment
|
||||
Signatures []Signature
|
||||
Adjustments []Adjustment
|
||||
}
|
||||
|
||||
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<numAdjs; i++ {
|
||||
adjs = append(adjs, ReadAdjustment(r))
|
||||
}
|
||||
return Validation{
|
||||
Signatures: sigs,
|
||||
Adjustments: adjs,
|
||||
}
|
||||
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 < numAdjs; i++ {
|
||||
adjs = append(adjs, ReadAdjustment(r))
|
||||
}
|
||||
return Validation{
|
||||
Signatures: sigs,
|
||||
Adjustments: adjs,
|
||||
}
|
||||
}
|
||||
|
||||
func (self *Validation) WriteTo(w io.Writer) (n int64, err error) {
|
||||
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 = WriteOnto(sig, w, n, err)
|
||||
}
|
||||
for _, adj := range self.Adjustments {
|
||||
n, err = WriteOnto(adj, w, n, err)
|
||||
}
|
||||
return
|
||||
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 = WriteOnto(sig, w, n, err)
|
||||
}
|
||||
for _, adj := range self.Adjustments {
|
||||
n, err = WriteOnto(adj, w, n, err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
/* Block > Data */
|
||||
|
||||
type Data struct {
|
||||
Txs []Tx
|
||||
Txs []Tx
|
||||
}
|
||||
|
||||
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}
|
||||
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) {
|
||||
n, err = WriteOnto(UInt64(len(self.Txs)), w, n, err)
|
||||
for _, tx := range self.Txs {
|
||||
n, err = WriteOnto(tx, w, n, err)
|
||||
}
|
||||
return
|
||||
n, err = WriteOnto(UInt64(len(self.Txs)), w, n, err)
|
||||
for _, tx := range self.Txs {
|
||||
n, err = WriteOnto(tx, w, n, err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (self *Data) MerkleHash() ByteSlice {
|
||||
bs := make([]Binary, 0, len(self.Txs))
|
||||
for i, tx := range self.Txs {
|
||||
bs[i] = Binary(tx)
|
||||
}
|
||||
return merkle.HashFromBinarySlice(bs)
|
||||
bs := make([]Binary, 0, len(self.Txs))
|
||||
for i, tx := range self.Txs {
|
||||
bs[i] = Binary(tx)
|
||||
}
|
||||
return merkle.HashFromBinarySlice(bs)
|
||||
}
|
||||
|
@ -1,113 +1,115 @@
|
||||
package blocks
|
||||
|
||||
import (
|
||||
. "github.com/tendermint/tendermint/binary"
|
||||
"testing"
|
||||
"math/rand"
|
||||
"bytes"
|
||||
"bytes"
|
||||
. "github.com/tendermint/tendermint/binary"
|
||||
"math/rand"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// Distributed pseudo-exponentially to test for various cases
|
||||
func randVar() UInt64 {
|
||||
bits := rand.Uint32() % 64
|
||||
if bits == 0 { return 0 }
|
||||
n := uint64(1 << (bits-1))
|
||||
n += uint64(rand.Int63()) & ((1 << (bits-1)) - 1)
|
||||
return UInt64(n)
|
||||
bits := rand.Uint32() % 64
|
||||
if bits == 0 {
|
||||
return 0
|
||||
}
|
||||
n := uint64(1 << (bits - 1))
|
||||
n += uint64(rand.Int63()) & ((1 << (bits - 1)) - 1)
|
||||
return UInt64(n)
|
||||
}
|
||||
|
||||
func randBytes(n int) ByteSlice {
|
||||
bs := make([]byte, n)
|
||||
for i:=0; i<n; i++ {
|
||||
bs[i] = byte(rand.Intn(256))
|
||||
}
|
||||
return bs
|
||||
bs := make([]byte, n)
|
||||
for i := 0; i < n; i++ {
|
||||
bs[i] = byte(rand.Intn(256))
|
||||
}
|
||||
return bs
|
||||
}
|
||||
|
||||
func randSig() Signature {
|
||||
return Signature{AccountNumber(randVar()), randBytes(32)}
|
||||
return Signature{AccountNumber(randVar()), randBytes(32)}
|
||||
}
|
||||
|
||||
func TestBlock(t *testing.T) {
|
||||
|
||||
// Txs
|
||||
// Txs
|
||||
|
||||
sendTx := &SendTx{
|
||||
Signature: randSig(),
|
||||
Fee: randVar(),
|
||||
To: AccountNumber(randVar()),
|
||||
Amount: randVar(),
|
||||
}
|
||||
sendTx := &SendTx{
|
||||
Signature: randSig(),
|
||||
Fee: randVar(),
|
||||
To: AccountNumber(randVar()),
|
||||
Amount: randVar(),
|
||||
}
|
||||
|
||||
nameTx := &NameTx{
|
||||
Signature: randSig(),
|
||||
Fee: randVar(),
|
||||
Name: String(randBytes(12)),
|
||||
PubKey: randBytes(32),
|
||||
}
|
||||
nameTx := &NameTx{
|
||||
Signature: randSig(),
|
||||
Fee: randVar(),
|
||||
Name: String(randBytes(12)),
|
||||
PubKey: randBytes(32),
|
||||
}
|
||||
|
||||
// Adjs
|
||||
// Adjs
|
||||
|
||||
bond := &Bond{
|
||||
Signature: randSig(),
|
||||
Fee: randVar(),
|
||||
UnbondTo: AccountNumber(randVar()),
|
||||
Amount: randVar(),
|
||||
}
|
||||
bond := &Bond{
|
||||
Signature: randSig(),
|
||||
Fee: randVar(),
|
||||
UnbondTo: AccountNumber(randVar()),
|
||||
Amount: randVar(),
|
||||
}
|
||||
|
||||
unbond := &Unbond{
|
||||
Signature: randSig(),
|
||||
Fee: randVar(),
|
||||
Amount: randVar(),
|
||||
}
|
||||
unbond := &Unbond{
|
||||
Signature: randSig(),
|
||||
Fee: randVar(),
|
||||
Amount: randVar(),
|
||||
}
|
||||
|
||||
timeout := &Timeout{
|
||||
Account: AccountNumber(randVar()),
|
||||
Penalty: randVar(),
|
||||
}
|
||||
timeout := &Timeout{
|
||||
Account: AccountNumber(randVar()),
|
||||
Penalty: randVar(),
|
||||
}
|
||||
|
||||
dupeout := &Dupeout{
|
||||
VoteA: Vote{
|
||||
Height: randVar(),
|
||||
BlockHash: randBytes(32),
|
||||
Signature: randSig(),
|
||||
},
|
||||
VoteB: Vote{
|
||||
Height: randVar(),
|
||||
BlockHash: randBytes(32),
|
||||
Signature: randSig(),
|
||||
},
|
||||
}
|
||||
dupeout := &Dupeout{
|
||||
VoteA: Vote{
|
||||
Height: randVar(),
|
||||
BlockHash: randBytes(32),
|
||||
Signature: randSig(),
|
||||
},
|
||||
VoteB: Vote{
|
||||
Height: randVar(),
|
||||
BlockHash: randBytes(32),
|
||||
Signature: randSig(),
|
||||
},
|
||||
}
|
||||
|
||||
// Block
|
||||
// Block
|
||||
|
||||
block := &Block{
|
||||
Header{
|
||||
Name: "Tendermint",
|
||||
Height: randVar(),
|
||||
Fees: randVar(),
|
||||
Time: randVar(),
|
||||
PrevHash: randBytes(32),
|
||||
ValidationHash: randBytes(32),
|
||||
DataHash: randBytes(32),
|
||||
},
|
||||
Validation{
|
||||
Signatures: []Signature{randSig(),randSig()},
|
||||
Adjustments:[]Adjustment{bond,unbond,timeout,dupeout},
|
||||
},
|
||||
Data{
|
||||
Txs: []Tx{sendTx, nameTx},
|
||||
},
|
||||
}
|
||||
block := &Block{
|
||||
Header{
|
||||
Name: "Tendermint",
|
||||
Height: randVar(),
|
||||
Fees: randVar(),
|
||||
Time: randVar(),
|
||||
PrevHash: randBytes(32),
|
||||
ValidationHash: randBytes(32),
|
||||
DataHash: randBytes(32),
|
||||
},
|
||||
Validation{
|
||||
Signatures: []Signature{randSig(), randSig()},
|
||||
Adjustments: []Adjustment{bond, unbond, timeout, dupeout},
|
||||
},
|
||||
Data{
|
||||
Txs: []Tx{sendTx, nameTx},
|
||||
},
|
||||
}
|
||||
|
||||
// Write the block, read it in again, write it again.
|
||||
// Then, compare.
|
||||
// Write the block, read it in again, write it again.
|
||||
// Then, compare.
|
||||
|
||||
blockBytes := BinaryBytes(block)
|
||||
block2 := ReadBlock(bytes.NewReader(blockBytes))
|
||||
blockBytes2 := BinaryBytes(block2)
|
||||
blockBytes := BinaryBytes(block)
|
||||
block2 := ReadBlock(bytes.NewReader(blockBytes))
|
||||
blockBytes2 := BinaryBytes(block2)
|
||||
|
||||
if !BinaryEqual(blockBytes, blockBytes2) {
|
||||
t.Fatal("Write->Read of block failed.")
|
||||
}
|
||||
if !BinaryEqual(blockBytes, blockBytes2) {
|
||||
t.Fatal("Write->Read of block failed.")
|
||||
}
|
||||
}
|
||||
|
@ -1,8 +1,8 @@
|
||||
package blocks
|
||||
|
||||
import (
|
||||
. "github.com/tendermint/tendermint/binary"
|
||||
"io"
|
||||
. "github.com/tendermint/tendermint/binary"
|
||||
"io"
|
||||
)
|
||||
|
||||
/*
|
||||
@ -19,23 +19,23 @@ It usually follows the message to be signed.
|
||||
*/
|
||||
|
||||
type Signature struct {
|
||||
Signer AccountId
|
||||
SigBytes ByteSlice
|
||||
Signer AccountId
|
||||
SigBytes ByteSlice
|
||||
}
|
||||
|
||||
func ReadSignature(r io.Reader) Signature {
|
||||
return Signature{
|
||||
Signer: ReadAccountId(r),
|
||||
SigBytes: ReadByteSlice(r),
|
||||
}
|
||||
return Signature{
|
||||
Signer: ReadAccountId(r),
|
||||
SigBytes: ReadByteSlice(r),
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
n, err = WriteOnto(self.Signer, w, n, err)
|
||||
n, err = WriteOnto(self.SigBytes, w, n, err)
|
||||
return
|
||||
}
|
||||
|
||||
func (self *Signature) Verify(msg ByteSlice) bool {
|
||||
return false
|
||||
return false
|
||||
}
|
||||
|
98
blocks/tx.go
98
blocks/tx.go
@ -1,9 +1,9 @@
|
||||
package blocks
|
||||
|
||||
import (
|
||||
. "github.com/tendermint/tendermint/common"
|
||||
. "github.com/tendermint/tendermint/binary"
|
||||
"io"
|
||||
. "github.com/tendermint/tendermint/binary"
|
||||
. "github.com/tendermint/tendermint/common"
|
||||
"io"
|
||||
)
|
||||
|
||||
/*
|
||||
@ -21,79 +21,77 @@ Tx wire format:
|
||||
*/
|
||||
|
||||
type Tx interface {
|
||||
Type() Byte
|
||||
Binary
|
||||
Type() Byte
|
||||
Binary
|
||||
}
|
||||
|
||||
const (
|
||||
TX_TYPE_SEND = Byte(0x01)
|
||||
TX_TYPE_NAME = Byte(0x02)
|
||||
TX_TYPE_SEND = Byte(0x01)
|
||||
TX_TYPE_NAME = Byte(0x02)
|
||||
)
|
||||
|
||||
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
|
||||
}
|
||||
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 {
|
||||
Fee UInt64
|
||||
To AccountId
|
||||
Amount UInt64
|
||||
Signature
|
||||
Fee UInt64
|
||||
To AccountId
|
||||
Amount UInt64
|
||||
Signature
|
||||
}
|
||||
|
||||
func (self *SendTx) Type() Byte {
|
||||
return TX_TYPE_SEND
|
||||
return TX_TYPE_SEND
|
||||
}
|
||||
|
||||
func (self *SendTx) WriteTo(w io.Writer) (n int64, err error) {
|
||||
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
|
||||
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
|
||||
}
|
||||
|
||||
|
||||
/* NameTx < Tx */
|
||||
|
||||
type NameTx struct {
|
||||
Fee UInt64
|
||||
Name String
|
||||
PubKey ByteSlice
|
||||
Signature
|
||||
Fee UInt64
|
||||
Name String
|
||||
PubKey ByteSlice
|
||||
Signature
|
||||
}
|
||||
|
||||
func (self *NameTx) Type() Byte {
|
||||
return TX_TYPE_NAME
|
||||
return TX_TYPE_NAME
|
||||
}
|
||||
|
||||
func (self *NameTx) WriteTo(w io.Writer) (n int64, err error) {
|
||||
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
|
||||
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,8 +1,8 @@
|
||||
package blocks
|
||||
|
||||
import (
|
||||
. "github.com/tendermint/tendermint/binary"
|
||||
"io"
|
||||
. "github.com/tendermint/tendermint/binary"
|
||||
"io"
|
||||
)
|
||||
|
||||
/*
|
||||
@ -11,22 +11,22 @@ Typically only the signature is passed around, as the hash & height are implied.
|
||||
*/
|
||||
|
||||
type Vote struct {
|
||||
Height UInt64
|
||||
BlockHash ByteSlice
|
||||
Signature
|
||||
Height UInt64
|
||||
BlockHash ByteSlice
|
||||
Signature
|
||||
}
|
||||
|
||||
func ReadVote(r io.Reader) Vote {
|
||||
return Vote{
|
||||
Height: ReadUInt64(r),
|
||||
BlockHash: ReadByteSlice(r),
|
||||
Signature: ReadSignature(r),
|
||||
}
|
||||
return Vote{
|
||||
Height: ReadUInt64(r),
|
||||
BlockHash: ReadByteSlice(r),
|
||||
Signature: ReadSignature(r),
|
||||
}
|
||||
}
|
||||
|
||||
func (self Vote) WriteTo(w io.Writer) (n int64, err error) {
|
||||
n, err = WriteOnto(self.Height, w, n, err)
|
||||
n, err = WriteOnto(self.BlockHash, w, n, err)
|
||||
n, err = WriteOnto(self.Signature, w, n, err)
|
||||
return
|
||||
n, err = WriteOnto(self.Height, w, n, err)
|
||||
n, err = WriteOnto(self.BlockHash, w, n, err)
|
||||
n, err = WriteOnto(self.Signature, w, n, err)
|
||||
return
|
||||
}
|
||||
|
Reference in New Issue
Block a user