Browse Source

Dot import -> named import

Changed modulename_ to short module names
Also removed Unreader, replaced with PrefixdReader in select locations
pull/39/head
Jae Kwon 10 years ago
parent
commit
135894ea88
52 changed files with 728 additions and 839 deletions
  1. +5
    -5
      account/account.go
  2. +3
    -3
      account/privkey.go
  3. +5
    -5
      account/pubkey.go
  4. +3
    -3
      account/signature.go
  5. +4
    -4
      account/signature_test.go
  6. +1
    -6
      binary/binary.go
  7. +2
    -2
      binary/codec.go
  8. +0
    -10
      binary/int.go
  9. +3
    -3
      binary/reflect.go
  10. +3
    -3
      binary/reflect_test.go
  11. +4
    -4
      block/block.go
  12. +2
    -2
      block/part_set.go
  13. +18
    -17
      block/store.go
  14. +30
    -30
      block/tx.go
  15. +8
    -8
      block/vote.go
  16. +13
    -13
      cmd/daemon.go
  17. +2
    -2
      cmd/gen_account.go
  18. +17
    -17
      cmd/gen_tx.go
  19. +4
    -5
      cmd/gen_validator.go
  20. +1
    -3
      common/bit_array.go
  21. +1
    -56
      common/bit_array_test.go
  22. +24
    -0
      common/io.go
  23. +12
    -12
      consensus/pol.go
  24. +27
    -27
      consensus/pol_test.go
  25. +40
    -40
      consensus/reactor.go
  26. +81
    -81
      consensus/state.go
  27. +15
    -15
      consensus/state_test.go
  28. +17
    -17
      consensus/test.go
  29. +7
    -7
      consensus/types/proposal.go
  30. +35
    -34
      consensus/vote_set.go
  31. +31
    -31
      consensus/vote_set_test.go
  32. +14
    -14
      mempool/mempool.go
  33. +5
    -5
      mempool/reactor.go
  34. +10
    -10
      merkle/iavl_node.go
  35. +6
    -6
      merkle/iavl_test.go
  36. +8
    -8
      merkle/iavl_tree.go
  37. +4
    -4
      merkle/simple_tree.go
  38. +10
    -10
      p2p/connection.go
  39. +2
    -2
      p2p/peer.go
  40. +2
    -2
      p2p/pex_reactor.go
  41. +7
    -7
      p2p/switch_test.go
  42. +7
    -7
      rpc/blocks.go
  43. +4
    -4
      rpc/mempool.go
  44. +6
    -6
      rpc/rpc.go
  45. +7
    -7
      rpc/validators.go
  46. +19
    -19
      state/genesis.go
  47. +19
    -19
      state/priv_validator.go
  48. +124
    -124
      state/state.go
  49. +28
    -92
      state/state_test.go
  50. +12
    -12
      state/test.go
  51. +14
    -14
      state/validator.go
  52. +2
    -2
      state/validator_set_test.go

+ 5
- 5
account/account.go View File

@ -5,7 +5,7 @@ import (
"fmt" "fmt"
"io" "io"
. "github.com/tendermint/tendermint/binary"
"github.com/tendermint/tendermint/binary"
) )
// Signable is an interface for all signable things. // Signable is an interface for all signable things.
@ -46,14 +46,14 @@ func (account *Account) String() string {
} }
func AccountEncoder(o interface{}, w io.Writer, n *int64, err *error) { func AccountEncoder(o interface{}, w io.Writer, n *int64, err *error) {
WriteBinary(o.(*Account), w, n, err)
binary.WriteBinary(o.(*Account), w, n, err)
} }
func AccountDecoder(r Unreader, n *int64, err *error) interface{} {
return ReadBinary(&Account{}, r, n, err)
func AccountDecoder(r io.Reader, n *int64, err *error) interface{} {
return binary.ReadBinary(&Account{}, r, n, err)
} }
var AccountCodec = Codec{
var AccountCodec = binary.Codec{
Encode: AccountEncoder, Encode: AccountEncoder,
Decode: AccountDecoder, Decode: AccountDecoder,
} }

+ 3
- 3
account/privkey.go View File

@ -4,7 +4,7 @@ import (
"errors" "errors"
"github.com/tendermint/go-ed25519" "github.com/tendermint/go-ed25519"
. "github.com/tendermint/tendermint/binary"
"github.com/tendermint/tendermint/binary"
) )
// PrivKey is part of PrivAccount and state.PrivValidator. // PrivKey is part of PrivAccount and state.PrivValidator.
@ -19,9 +19,9 @@ const (
) )
// for binary.readReflect // for binary.readReflect
var _ = RegisterInterface(
var _ = binary.RegisterInterface(
struct{ PrivKey }{}, struct{ PrivKey }{},
ConcreteType{PrivKeyEd25519{}},
binary.ConcreteType{PrivKeyEd25519{}},
) )
//------------------------------------- //-------------------------------------


+ 5
- 5
account/pubkey.go View File

@ -4,7 +4,7 @@ import (
"errors" "errors"
"github.com/tendermint/go-ed25519" "github.com/tendermint/go-ed25519"
. "github.com/tendermint/tendermint/binary"
"github.com/tendermint/tendermint/binary"
) )
// PubKey is part of Account and Validator. // PubKey is part of Account and Validator.
@ -21,10 +21,10 @@ const (
) )
// for binary.readReflect // for binary.readReflect
var _ = RegisterInterface(
var _ = binary.RegisterInterface(
struct{ PubKey }{}, struct{ PubKey }{},
ConcreteType{PubKeyNil{}},
ConcreteType{PubKeyEd25519{}},
binary.ConcreteType{PubKeyNil{}},
binary.ConcreteType{PubKeyEd25519{}},
) )
//------------------------------------- //-------------------------------------
@ -50,7 +50,7 @@ type PubKeyEd25519 []byte
func (key PubKeyEd25519) TypeByte() byte { return PubKeyTypeEd25519 } func (key PubKeyEd25519) TypeByte() byte { return PubKeyTypeEd25519 }
// TODO: Or should this just be BinaryRipemd160(key)? (The difference is the TypeByte.) // TODO: Or should this just be BinaryRipemd160(key)? (The difference is the TypeByte.)
func (key PubKeyEd25519) Address() []byte { return BinaryRipemd160(key) }
func (key PubKeyEd25519) Address() []byte { return binary.BinaryRipemd160(key) }
func (key PubKeyEd25519) ValidateBasic() error { func (key PubKeyEd25519) ValidateBasic() error {
if len(key) != ed25519.PublicKeySize { if len(key) != ed25519.PublicKeySize {


+ 3
- 3
account/signature.go View File

@ -5,7 +5,7 @@ import (
"fmt" "fmt"
"github.com/tendermint/go-ed25519" "github.com/tendermint/go-ed25519"
. "github.com/tendermint/tendermint/binary"
"github.com/tendermint/tendermint/binary"
. "github.com/tendermint/tendermint/common" . "github.com/tendermint/tendermint/common"
) )
@ -19,9 +19,9 @@ const (
) )
// for binary.readReflect // for binary.readReflect
var _ = RegisterInterface(
var _ = binary.RegisterInterface(
struct{ Signature }{}, struct{ Signature }{},
ConcreteType{SignatureEd25519{}},
binary.ConcreteType{SignatureEd25519{}},
) )
//------------------------------------- //-------------------------------------


+ 4
- 4
account/signature_test.go View File

@ -4,8 +4,8 @@ import (
"bytes" "bytes"
"testing" "testing"
ed25519 "github.com/tendermint/go-ed25519"
. "github.com/tendermint/tendermint/binary"
"github.com/tendermint/go-ed25519"
"github.com/tendermint/tendermint/binary"
. "github.com/tendermint/tendermint/common" . "github.com/tendermint/tendermint/common"
) )
@ -43,7 +43,7 @@ func TestBinaryDecode(t *testing.T) {
t.Logf("msg: %X, sig: %X", msg, sig) t.Logf("msg: %X, sig: %X", msg, sig)
buf, n, err := new(bytes.Buffer), new(int64), new(error) buf, n, err := new(bytes.Buffer), new(int64), new(error)
WriteBinary(sig, buf, n, err)
binary.WriteBinary(sig, buf, n, err)
if *err != nil { if *err != nil {
t.Fatalf("Failed to write Signature: %v", err) t.Fatalf("Failed to write Signature: %v", err)
} }
@ -56,7 +56,7 @@ func TestBinaryDecode(t *testing.T) {
t.Fatalf("Unexpected signature type byte") t.Fatalf("Unexpected signature type byte")
} }
sig2, ok := ReadBinary(SignatureEd25519{}, buf, n, err).(SignatureEd25519)
sig2, ok := binary.ReadBinary(SignatureEd25519{}, buf, n, err).(SignatureEd25519)
if !ok || *err != nil { if !ok || *err != nil {
t.Fatalf("Failed to read Signature: %v", err) t.Fatalf("Failed to read Signature: %v", err)
} }


+ 1
- 6
binary/binary.go View File

@ -6,12 +6,7 @@ import (
"reflect" "reflect"
) )
type Unreader interface {
io.Reader
UnreadByte() error
}
func ReadBinary(o interface{}, r Unreader, n *int64, err *error) interface{} {
func ReadBinary(o interface{}, r io.Reader, n *int64, err *error) interface{} {
rv, rt := reflect.ValueOf(o), reflect.TypeOf(o) rv, rt := reflect.ValueOf(o), reflect.TypeOf(o)
if rv.Kind() == reflect.Ptr { if rv.Kind() == reflect.Ptr {
readReflect(rv.Elem(), rt.Elem(), r, n, err) readReflect(rv.Elem(), rt.Elem(), r, n, err)


+ 2
- 2
binary/codec.go View File

@ -9,7 +9,7 @@ import (
) )
type Encoder func(o interface{}, w io.Writer, n *int64, err *error) type Encoder func(o interface{}, w io.Writer, n *int64, err *error)
type Decoder func(r Unreader, n *int64, err *error) interface{}
type Decoder func(r io.Reader, n *int64, err *error) interface{}
type Comparator func(o1 interface{}, o2 interface{}) int type Comparator func(o1 interface{}, o2 interface{}) int
type Codec struct { type Codec struct {
@ -86,7 +86,7 @@ func BasicCodecEncoder(o interface{}, w io.Writer, n *int64, err *error) {
} }
} }
func BasicCodecDecoder(r Unreader, n *int64, err *error) (o interface{}) {
func BasicCodecDecoder(r io.Reader, n *int64, err *error) (o interface{}) {
type_ := ReadByte(r, n, err) type_ := ReadByte(r, n, err)
switch type_ { switch type_ {
case typeByte: case typeByte:


+ 0
- 10
binary/int.go View File

@ -18,16 +18,6 @@ func ReadByte(r io.Reader, n *int64, err *error) byte {
return buf[0] return buf[0]
} }
// NOTE: may end up advancing the reader upon error.
func PeekByte(r Unreader, n *int64, err *error) byte {
byte_ := ReadByte(r, n, err)
if *err != nil {
return 0
}
*err = r.UnreadByte()
return byte_
}
// Int8 // Int8
func WriteInt8(i int8, w io.Writer, n *int64, err *error) { func WriteInt8(i int8, w io.Writer, n *int64, err *error) {


+ 3
- 3
binary/reflect.go View File

@ -158,7 +158,7 @@ func RegisterType(info *TypeInfo) *TypeInfo {
return info return info
} }
func readReflect(rv reflect.Value, rt reflect.Type, r Unreader, n *int64, err *error) {
func readReflect(rv reflect.Value, rt reflect.Type, r io.Reader, n *int64, err *error) {
log.Debug("Read reflect", "type", rt) log.Debug("Read reflect", "type", rt)
@ -197,7 +197,7 @@ func readReflect(rv reflect.Value, rt reflect.Type, r Unreader, n *int64, err *e
switch rt.Kind() { switch rt.Kind() {
case reflect.Interface: case reflect.Interface:
typeByte := PeekByte(r, n, err)
typeByte := ReadByte(r, n, err)
if *err != nil { if *err != nil {
return return
} }
@ -206,7 +206,7 @@ func readReflect(rv reflect.Value, rt reflect.Type, r Unreader, n *int64, err *e
panic(Fmt("TypeByte %X not registered for interface %v", typeByte, rt)) panic(Fmt("TypeByte %X not registered for interface %v", typeByte, rt))
} }
newRv := reflect.New(concreteType) newRv := reflect.New(concreteType)
readReflect(newRv.Elem(), concreteType, r, n, err)
readReflect(newRv.Elem(), concreteType, NewPrefixedReader([]byte{typeByte}, r), n, err)
rv.Set(newRv.Elem()) rv.Set(newRv.Elem())
case reflect.Slice: case reflect.Slice:


+ 3
- 3
binary/reflect_test.go View File

@ -287,9 +287,9 @@ func validateComplexArray(o interface{}, t *testing.T) {
var testCases = []TestCase{} var testCases = []TestCase{}
func init() { func init() {
testCases = append(testCases, TestCase{constructBasic, instantiateBasic, validateBasic})
testCases = append(testCases, TestCase{constructComplex, instantiateComplex, validateComplex})
testCases = append(testCases, TestCase{constructComplex2, instantiateComplex2, validateComplex2})
//testCases = append(testCases, TestCase{constructBasic, instantiateBasic, validateBasic})
//testCases = append(testCases, TestCase{constructComplex, instantiateComplex, validateComplex})
//testCases = append(testCases, TestCase{constructComplex2, instantiateComplex2, validateComplex2})
testCases = append(testCases, TestCase{constructComplexArray, instantiateComplexArray, validateComplexArray}) testCases = append(testCases, TestCase{constructComplexArray, instantiateComplexArray, validateComplexArray})
} }


+ 4
- 4
block/block.go View File

@ -8,8 +8,8 @@ import (
"strings" "strings"
"time" "time"
. "github.com/tendermint/tendermint/account"
. "github.com/tendermint/tendermint/binary"
"github.com/tendermint/tendermint/account"
"github.com/tendermint/tendermint/binary"
. "github.com/tendermint/tendermint/common" . "github.com/tendermint/tendermint/common"
"github.com/tendermint/tendermint/config" "github.com/tendermint/tendermint/config"
"github.com/tendermint/tendermint/merkle" "github.com/tendermint/tendermint/merkle"
@ -125,7 +125,7 @@ type Header struct {
func (h *Header) Hash() []byte { func (h *Header) Hash() []byte {
if h.hash == nil { if h.hash == nil {
hasher, n, err := sha256.New(), new(int64), new(error) hasher, n, err := sha256.New(), new(int64), new(error)
WriteBinary(h, hasher, n, err)
binary.WriteBinary(h, hasher, n, err)
if *err != nil { if *err != nil {
panic(err) panic(err)
} }
@ -161,7 +161,7 @@ func (h *Header) StringIndented(indent string) string {
type Commit struct { type Commit struct {
Address []byte Address []byte
Round uint Round uint
Signature SignatureEd25519
Signature account.SignatureEd25519
} }
func (commit Commit) IsZero() bool { func (commit Commit) IsZero() bool {


+ 2
- 2
block/part_set.go View File

@ -5,10 +5,10 @@ import (
"crypto/sha256" "crypto/sha256"
"errors" "errors"
"fmt" "fmt"
"io"
"strings" "strings"
"sync" "sync"
. "github.com/tendermint/tendermint/binary"
. "github.com/tendermint/tendermint/common" . "github.com/tendermint/tendermint/common"
"github.com/tendermint/tendermint/merkle" "github.com/tendermint/tendermint/merkle"
) )
@ -227,7 +227,7 @@ func (ps *PartSet) IsComplete() bool {
return ps.count == ps.total return ps.count == ps.total
} }
func (ps *PartSet) GetReader() Unreader {
func (ps *PartSet) GetReader() io.Reader {
if !ps.IsComplete() { if !ps.IsComplete() {
panic("Cannot GetReader() on incomplete PartSet") panic("Cannot GetReader() on incomplete PartSet")
} }


+ 18
- 17
block/store.go View File

@ -4,10 +4,11 @@ import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"fmt" "fmt"
"io"
. "github.com/tendermint/tendermint/binary"
"github.com/tendermint/tendermint/binary"
. "github.com/tendermint/tendermint/common" . "github.com/tendermint/tendermint/common"
db_ "github.com/tendermint/tendermint/db"
dbm "github.com/tendermint/tendermint/db"
) )
/* /*
@ -24,10 +25,10 @@ the Validation data outside the Block.
*/ */
type BlockStore struct { type BlockStore struct {
height uint height uint
db db_.DB
db dbm.DB
} }
func NewBlockStore(db db_.DB) *BlockStore {
func NewBlockStore(db dbm.DB) *BlockStore {
bsjson := LoadBlockStoreStateJSON(db) bsjson := LoadBlockStoreStateJSON(db)
return &BlockStore{ return &BlockStore{
height: bsjson.Height, height: bsjson.Height,
@ -40,7 +41,7 @@ func (bs *BlockStore) Height() uint {
return bs.height return bs.height
} }
func (bs *BlockStore) GetReader(key []byte) Unreader {
func (bs *BlockStore) GetReader(key []byte) io.Reader {
bytez := bs.db.Get(key) bytez := bs.db.Get(key)
if bytez == nil { if bytez == nil {
return nil return nil
@ -55,7 +56,7 @@ func (bs *BlockStore) LoadBlock(height uint) *Block {
if r == nil { if r == nil {
panic(Fmt("Block does not exist at height %v", height)) panic(Fmt("Block does not exist at height %v", height))
} }
meta := ReadBinary(&BlockMeta{}, r, &n, &err).(*BlockMeta)
meta := binary.ReadBinary(&BlockMeta{}, r, &n, &err).(*BlockMeta)
if err != nil { if err != nil {
panic(Fmt("Error reading block meta: %v", err)) panic(Fmt("Error reading block meta: %v", err))
} }
@ -64,7 +65,7 @@ func (bs *BlockStore) LoadBlock(height uint) *Block {
part := bs.LoadBlockPart(height, i) part := bs.LoadBlockPart(height, i)
bytez = append(bytez, part.Bytes...) bytez = append(bytez, part.Bytes...)
} }
block := ReadBinary(&Block{}, bytes.NewReader(bytez), &n, &err).(*Block)
block := binary.ReadBinary(&Block{}, bytes.NewReader(bytez), &n, &err).(*Block)
if err != nil { if err != nil {
panic(Fmt("Error reading block: %v", err)) panic(Fmt("Error reading block: %v", err))
} }
@ -78,7 +79,7 @@ func (bs *BlockStore) LoadBlockPart(height uint, index uint) *Part {
if r == nil { if r == nil {
panic(Fmt("BlockPart does not exist for height %v index %v", height, index)) panic(Fmt("BlockPart does not exist for height %v index %v", height, index))
} }
part := ReadBinary(&Part{}, r, &n, &err).(*Part)
part := binary.ReadBinary(&Part{}, r, &n, &err).(*Part)
if err != nil { if err != nil {
panic(Fmt("Error reading block part: %v", err)) panic(Fmt("Error reading block part: %v", err))
} }
@ -92,7 +93,7 @@ func (bs *BlockStore) LoadBlockMeta(height uint) *BlockMeta {
if r == nil { if r == nil {
panic(Fmt("BlockMeta does not exist for height %v", height)) panic(Fmt("BlockMeta does not exist for height %v", height))
} }
meta := ReadBinary(&BlockMeta{}, r, &n, &err).(*BlockMeta)
meta := binary.ReadBinary(&BlockMeta{}, r, &n, &err).(*BlockMeta)
if err != nil { if err != nil {
panic(Fmt("Error reading block meta: %v", err)) panic(Fmt("Error reading block meta: %v", err))
} }
@ -109,7 +110,7 @@ func (bs *BlockStore) LoadBlockValidation(height uint) *Validation {
if r == nil { if r == nil {
panic(Fmt("BlockValidation does not exist for height %v", height)) panic(Fmt("BlockValidation does not exist for height %v", height))
} }
validation := ReadBinary(&Validation{}, r, &n, &err).(*Validation)
validation := binary.ReadBinary(&Validation{}, r, &n, &err).(*Validation)
if err != nil { if err != nil {
panic(Fmt("Error reading validation: %v", err)) panic(Fmt("Error reading validation: %v", err))
} }
@ -124,7 +125,7 @@ func (bs *BlockStore) LoadSeenValidation(height uint) *Validation {
if r == nil { if r == nil {
panic(Fmt("SeenValidation does not exist for height %v", height)) panic(Fmt("SeenValidation does not exist for height %v", height))
} }
validation := ReadBinary(&Validation{}, r, &n, &err).(*Validation)
validation := binary.ReadBinary(&Validation{}, r, &n, &err).(*Validation)
if err != nil { if err != nil {
panic(Fmt("Error reading validation: %v", err)) panic(Fmt("Error reading validation: %v", err))
} }
@ -149,7 +150,7 @@ func (bs *BlockStore) SaveBlock(block *Block, blockParts *PartSet, seenValidatio
// Save block meta // Save block meta
meta := makeBlockMeta(block, blockParts) meta := makeBlockMeta(block, blockParts)
metaBytes := BinaryBytes(meta)
metaBytes := binary.BinaryBytes(meta)
bs.db.Set(calcBlockMetaKey(height), metaBytes) bs.db.Set(calcBlockMetaKey(height), metaBytes)
// Save block parts // Save block parts
@ -158,11 +159,11 @@ func (bs *BlockStore) SaveBlock(block *Block, blockParts *PartSet, seenValidatio
} }
// Save block validation (duplicate and separate from the Block) // Save block validation (duplicate and separate from the Block)
blockValidationBytes := BinaryBytes(block.Validation)
blockValidationBytes := binary.BinaryBytes(block.Validation)
bs.db.Set(calcBlockValidationKey(height), blockValidationBytes) bs.db.Set(calcBlockValidationKey(height), blockValidationBytes)
// Save seen validation (seen +2/3 commits) // Save seen validation (seen +2/3 commits)
seenValidationBytes := BinaryBytes(seenValidation)
seenValidationBytes := binary.BinaryBytes(seenValidation)
bs.db.Set(calcSeenValidationKey(height), seenValidationBytes) bs.db.Set(calcSeenValidationKey(height), seenValidationBytes)
// Save new BlockStoreStateJSON descriptor // Save new BlockStoreStateJSON descriptor
@ -176,7 +177,7 @@ func (bs *BlockStore) saveBlockPart(height uint, index uint, part *Part) {
if height != bs.height+1 { if height != bs.height+1 {
panic(Fmt("BlockStore can only save contiguous blocks. Wanted %v, got %v", bs.height+1, height)) panic(Fmt("BlockStore can only save contiguous blocks. Wanted %v, got %v", bs.height+1, height))
} }
partBytes := BinaryBytes(part)
partBytes := binary.BinaryBytes(part)
bs.db.Set(calcBlockPartKey(height, index), partBytes) bs.db.Set(calcBlockPartKey(height, index), partBytes)
} }
@ -222,7 +223,7 @@ type BlockStoreStateJSON struct {
Height uint Height uint
} }
func (bsj BlockStoreStateJSON) Save(db db_.DB) {
func (bsj BlockStoreStateJSON) Save(db dbm.DB) {
bytes, err := json.Marshal(bsj) bytes, err := json.Marshal(bsj)
if err != nil { if err != nil {
panic(Fmt("Could not marshal state bytes: %v", err)) panic(Fmt("Could not marshal state bytes: %v", err))
@ -230,7 +231,7 @@ func (bsj BlockStoreStateJSON) Save(db db_.DB) {
db.Set(blockStoreKey, bytes) db.Set(blockStoreKey, bytes)
} }
func LoadBlockStoreStateJSON(db db_.DB) BlockStoreStateJSON {
func LoadBlockStoreStateJSON(db dbm.DB) BlockStoreStateJSON {
bytes := db.Get(blockStoreKey) bytes := db.Get(blockStoreKey)
if bytes == nil { if bytes == nil {
return BlockStoreStateJSON{ return BlockStoreStateJSON{


+ 30
- 30
block/tx.go View File

@ -4,8 +4,8 @@ import (
"errors" "errors"
"io" "io"
. "github.com/tendermint/tendermint/account"
. "github.com/tendermint/tendermint/binary"
"github.com/tendermint/tendermint/account"
"github.com/tendermint/tendermint/binary"
) )
var ( var (
@ -47,23 +47,23 @@ const (
) )
// for binary.readReflect // for binary.readReflect
var _ = RegisterInterface(
var _ = binary.RegisterInterface(
struct{ Tx }{}, struct{ Tx }{},
ConcreteType{&SendTx{}},
ConcreteType{&BondTx{}},
ConcreteType{&UnbondTx{}},
ConcreteType{&RebondTx{}},
ConcreteType{&DupeoutTx{}},
binary.ConcreteType{&SendTx{}},
binary.ConcreteType{&BondTx{}},
binary.ConcreteType{&UnbondTx{}},
binary.ConcreteType{&RebondTx{}},
binary.ConcreteType{&DupeoutTx{}},
) )
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
type TxInput struct { type TxInput struct {
Address []byte // Hash of the PubKey
Amount uint64 // Must not exceed account balance
Sequence uint // Must be 1 greater than the last committed TxInput
Signature Signature // Depends on the PubKey type and the whole Tx
PubKey PubKey // Must not be nil, may be PubKeyNil.
Address []byte // Hash of the PubKey
Amount uint64 // Must not exceed account balance
Sequence uint // Must be 1 greater than the last committed TxInput
Signature account.Signature // Depends on the PubKey type and the whole Tx
PubKey account.PubKey // Must not be nil, may be PubKeyNil.
} }
func (txIn *TxInput) ValidateBasic() error { func (txIn *TxInput) ValidateBasic() error {
@ -77,9 +77,9 @@ func (txIn *TxInput) ValidateBasic() error {
} }
func (txIn *TxInput) WriteSignBytes(w io.Writer, n *int64, err *error) { func (txIn *TxInput) WriteSignBytes(w io.Writer, n *int64, err *error) {
WriteByteSlice(txIn.Address, w, n, err)
WriteUint64(txIn.Amount, w, n, err)
WriteUvarint(txIn.Sequence, w, n, err)
binary.WriteByteSlice(txIn.Address, w, n, err)
binary.WriteUint64(txIn.Amount, w, n, err)
binary.WriteUvarint(txIn.Sequence, w, n, err)
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
@ -100,8 +100,8 @@ func (txOut *TxOutput) ValidateBasic() error {
} }
func (txOut *TxOutput) WriteSignBytes(w io.Writer, n *int64, err *error) { func (txOut *TxOutput) WriteSignBytes(w io.Writer, n *int64, err *error) {
WriteByteSlice(txOut.Address, w, n, err)
WriteUint64(txOut.Amount, w, n, err)
binary.WriteByteSlice(txOut.Address, w, n, err)
binary.WriteUint64(txOut.Amount, w, n, err)
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
@ -114,11 +114,11 @@ type SendTx struct {
func (tx *SendTx) TypeByte() byte { return TxTypeSend } func (tx *SendTx) TypeByte() byte { return TxTypeSend }
func (tx *SendTx) WriteSignBytes(w io.Writer, n *int64, err *error) { func (tx *SendTx) WriteSignBytes(w io.Writer, n *int64, err *error) {
WriteUvarint(uint(len(tx.Inputs)), w, n, err)
binary.WriteUvarint(uint(len(tx.Inputs)), w, n, err)
for _, in := range tx.Inputs { for _, in := range tx.Inputs {
in.WriteSignBytes(w, n, err) in.WriteSignBytes(w, n, err)
} }
WriteUvarint(uint(len(tx.Outputs)), w, n, err)
binary.WriteUvarint(uint(len(tx.Outputs)), w, n, err)
for _, out := range tx.Outputs { for _, out := range tx.Outputs {
out.WriteSignBytes(w, n, err) out.WriteSignBytes(w, n, err)
} }
@ -127,7 +127,7 @@ func (tx *SendTx) WriteSignBytes(w io.Writer, n *int64, err *error) {
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
type BondTx struct { type BondTx struct {
PubKey PubKeyEd25519
PubKey account.PubKeyEd25519
Inputs []*TxInput Inputs []*TxInput
UnbondTo []*TxOutput UnbondTo []*TxOutput
} }
@ -135,12 +135,12 @@ type BondTx struct {
func (tx *BondTx) TypeByte() byte { return TxTypeBond } func (tx *BondTx) TypeByte() byte { return TxTypeBond }
func (tx *BondTx) WriteSignBytes(w io.Writer, n *int64, err *error) { func (tx *BondTx) WriteSignBytes(w io.Writer, n *int64, err *error) {
WriteBinary(tx.PubKey, w, n, err)
WriteUvarint(uint(len(tx.Inputs)), w, n, err)
binary.WriteBinary(tx.PubKey, w, n, err)
binary.WriteUvarint(uint(len(tx.Inputs)), w, n, err)
for _, in := range tx.Inputs { for _, in := range tx.Inputs {
in.WriteSignBytes(w, n, err) in.WriteSignBytes(w, n, err)
} }
WriteUvarint(uint(len(tx.UnbondTo)), w, n, err)
binary.WriteUvarint(uint(len(tx.UnbondTo)), w, n, err)
for _, out := range tx.UnbondTo { for _, out := range tx.UnbondTo {
out.WriteSignBytes(w, n, err) out.WriteSignBytes(w, n, err)
} }
@ -151,14 +151,14 @@ func (tx *BondTx) WriteSignBytes(w io.Writer, n *int64, err *error) {
type UnbondTx struct { type UnbondTx struct {
Address []byte Address []byte
Height uint Height uint
Signature SignatureEd25519
Signature account.SignatureEd25519
} }
func (tx *UnbondTx) TypeByte() byte { return TxTypeUnbond } func (tx *UnbondTx) TypeByte() byte { return TxTypeUnbond }
func (tx *UnbondTx) WriteSignBytes(w io.Writer, n *int64, err *error) { func (tx *UnbondTx) WriteSignBytes(w io.Writer, n *int64, err *error) {
WriteByteSlice(tx.Address, w, n, err)
WriteUvarint(tx.Height, w, n, err)
binary.WriteByteSlice(tx.Address, w, n, err)
binary.WriteUvarint(tx.Height, w, n, err)
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
@ -166,14 +166,14 @@ func (tx *UnbondTx) WriteSignBytes(w io.Writer, n *int64, err *error) {
type RebondTx struct { type RebondTx struct {
Address []byte Address []byte
Height uint Height uint
Signature SignatureEd25519
Signature account.SignatureEd25519
} }
func (tx *RebondTx) TypeByte() byte { return TxTypeRebond } func (tx *RebondTx) TypeByte() byte { return TxTypeRebond }
func (tx *RebondTx) WriteSignBytes(w io.Writer, n *int64, err *error) { func (tx *RebondTx) WriteSignBytes(w io.Writer, n *int64, err *error) {
WriteByteSlice(tx.Address, w, n, err)
WriteUvarint(tx.Height, w, n, err)
binary.WriteByteSlice(tx.Address, w, n, err)
binary.WriteUvarint(tx.Height, w, n, err)
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------


+ 8
- 8
block/vote.go View File

@ -5,8 +5,8 @@ import (
"fmt" "fmt"
"io" "io"
. "github.com/tendermint/tendermint/account"
. "github.com/tendermint/tendermint/binary"
"github.com/tendermint/tendermint/account"
"github.com/tendermint/tendermint/binary"
. "github.com/tendermint/tendermint/common" . "github.com/tendermint/tendermint/common"
) )
@ -27,7 +27,7 @@ type Vote struct {
Type byte Type byte
BlockHash []byte // empty if vote is nil. BlockHash []byte // empty if vote is nil.
BlockParts PartSetHeader // zero if vote is nil. BlockParts PartSetHeader // zero if vote is nil.
Signature SignatureEd25519
Signature account.SignatureEd25519
} }
// Types of votes // Types of votes
@ -38,11 +38,11 @@ const (
) )
func (vote *Vote) WriteSignBytes(w io.Writer, n *int64, err *error) { func (vote *Vote) WriteSignBytes(w io.Writer, n *int64, err *error) {
WriteUvarint(vote.Height, w, n, err)
WriteUvarint(vote.Round, w, n, err)
WriteByte(vote.Type, w, n, err)
WriteByteSlice(vote.BlockHash, w, n, err)
WriteBinary(vote.BlockParts, w, n, err)
binary.WriteUvarint(vote.Height, w, n, err)
binary.WriteUvarint(vote.Round, w, n, err)
binary.WriteByte(vote.Type, w, n, err)
binary.WriteByteSlice(vote.BlockHash, w, n, err)
binary.WriteBinary(vote.BlockParts, w, n, err)
} }
func (vote *Vote) Copy() *Vote { func (vote *Vote) Copy() *Vote {


+ 13
- 13
cmd/daemon.go View File

@ -8,11 +8,11 @@ import (
. "github.com/tendermint/tendermint/common" . "github.com/tendermint/tendermint/common"
"github.com/tendermint/tendermint/config" "github.com/tendermint/tendermint/config"
"github.com/tendermint/tendermint/consensus" "github.com/tendermint/tendermint/consensus"
db_ "github.com/tendermint/tendermint/db"
mempool_ "github.com/tendermint/tendermint/mempool"
dbm "github.com/tendermint/tendermint/db"
mempl "github.com/tendermint/tendermint/mempool"
"github.com/tendermint/tendermint/p2p" "github.com/tendermint/tendermint/p2p"
"github.com/tendermint/tendermint/rpc" "github.com/tendermint/tendermint/rpc"
state_ "github.com/tendermint/tendermint/state"
sm "github.com/tendermint/tendermint/state"
) )
type Node struct { type Node struct {
@ -21,29 +21,29 @@ type Node struct {
book *p2p.AddrBook book *p2p.AddrBook
pexReactor *p2p.PEXReactor pexReactor *p2p.PEXReactor
blockStore *block.BlockStore blockStore *block.BlockStore
mempoolReactor *mempool_.MempoolReactor
mempoolReactor *mempl.MempoolReactor
consensusState *consensus.ConsensusState consensusState *consensus.ConsensusState
consensusReactor *consensus.ConsensusReactor consensusReactor *consensus.ConsensusReactor
privValidator *state_.PrivValidator
privValidator *sm.PrivValidator
} }
func NewNode() *Node { func NewNode() *Node {
// Get BlockStore // Get BlockStore
blockStoreDB := db_.GetDB("blockstore")
blockStoreDB := dbm.GetDB("blockstore")
blockStore := block.NewBlockStore(blockStoreDB) blockStore := block.NewBlockStore(blockStoreDB)
// Get State // Get State
stateDB := db_.GetDB("state")
state := state_.LoadState(stateDB)
stateDB := dbm.GetDB("state")
state := sm.LoadState(stateDB)
if state == nil { if state == nil {
state = state_.MakeGenesisStateFromFile(stateDB, config.App.GetString("GenesisFile"))
state = sm.MakeGenesisStateFromFile(stateDB, config.App.GetString("GenesisFile"))
state.Save() state.Save()
} }
// Get PrivValidator // Get PrivValidator
var privValidator *state_.PrivValidator
var privValidator *sm.PrivValidator
if _, err := os.Stat(config.App.GetString("PrivValidatorFile")); err == nil { if _, err := os.Stat(config.App.GetString("PrivValidatorFile")); err == nil {
privValidator = state_.LoadPrivValidator(config.App.GetString("PrivValidatorFile"))
privValidator = sm.LoadPrivValidator(config.App.GetString("PrivValidatorFile"))
log.Info("Loaded PrivValidator", "file", config.App.GetString("PrivValidatorFile"), "privValidator", privValidator) log.Info("Loaded PrivValidator", "file", config.App.GetString("PrivValidatorFile"), "privValidator", privValidator)
} else { } else {
log.Info("No PrivValidator found", "file", config.App.GetString("PrivValidatorFile")) log.Info("No PrivValidator found", "file", config.App.GetString("PrivValidatorFile"))
@ -54,8 +54,8 @@ func NewNode() *Node {
pexReactor := p2p.NewPEXReactor(book) pexReactor := p2p.NewPEXReactor(book)
// Get MempoolReactor // Get MempoolReactor
mempool := mempool_.NewMempool(state.Copy())
mempoolReactor := mempool_.NewMempoolReactor(mempool)
mempool := mempl.NewMempool(state.Copy())
mempoolReactor := mempl.NewMempoolReactor(mempool)
// Get ConsensusReactor // Get ConsensusReactor
consensusState := consensus.NewConsensusState(state, blockStore, mempoolReactor) consensusState := consensus.NewConsensusState(state, blockStore, mempoolReactor)


+ 2
- 2
cmd/gen_account.go View File

@ -4,12 +4,12 @@ import (
"fmt" "fmt"
"github.com/tendermint/tendermint/account" "github.com/tendermint/tendermint/account"
. "github.com/tendermint/tendermint/binary"
"github.com/tendermint/tendermint/binary"
) )
func gen_account() { func gen_account() {
privAccount := account.GenPrivAccount() privAccount := account.GenPrivAccount()
privAccountJSONBytes := JSONBytes(privAccount)
privAccountJSONBytes := binary.JSONBytes(privAccount)
fmt.Printf(`Generated a new account! fmt.Printf(`Generated a new account!
%v %v


+ 17
- 17
cmd/gen_tx.go View File

@ -8,12 +8,12 @@ import (
"os" "os"
"strconv" "strconv"
account_ "github.com/tendermint/tendermint/account"
binary "github.com/tendermint/tendermint/binary"
block_ "github.com/tendermint/tendermint/block"
"github.com/tendermint/tendermint/account"
"github.com/tendermint/tendermint/binary"
"github.com/tendermint/tendermint/block"
. "github.com/tendermint/tendermint/common" . "github.com/tendermint/tendermint/common"
db_ "github.com/tendermint/tendermint/db"
state_ "github.com/tendermint/tendermint/state"
dbm "github.com/tendermint/tendermint/db"
sm "github.com/tendermint/tendermint/state"
) )
func getString(prompt string) string { func getString(prompt string) string {
@ -44,19 +44,19 @@ func getUint64(prompt string) uint64 {
func gen_tx() { func gen_tx() {
// Get State, which may be nil. // Get State, which may be nil.
stateDB := db_.GetDB("state")
state := state_.LoadState(stateDB)
stateDB := dbm.GetDB("state")
state := sm.LoadState(stateDB)
// Get source pubkey // Get source pubkey
srcPubKeyBytes := getByteSliceFromHex("Enter source pubkey: ") srcPubKeyBytes := getByteSliceFromHex("Enter source pubkey: ")
r, n, err := bytes.NewReader(srcPubKeyBytes), new(int64), new(error) r, n, err := bytes.NewReader(srcPubKeyBytes), new(int64), new(error)
srcPubKey := binary.ReadBinary(struct{ account_.PubKey }{}, r, n, err).(struct{ account_.PubKey }).PubKey
srcPubKey := binary.ReadBinary(struct{ account.PubKey }{}, r, n, err).(struct{ account.PubKey }).PubKey
if *err != nil { if *err != nil {
Exit(Fmt("Invalid PubKey. Error: %v", err)) Exit(Fmt("Invalid PubKey. Error: %v", err))
} }
// Get the state of the account. // Get the state of the account.
var srcAccount *account_.Account
var srcAccount *account.Account
var srcAccountAddress = srcPubKey.Address() var srcAccountAddress = srcPubKey.Address()
var srcAccountBalanceStr = "unknown" var srcAccountBalanceStr = "unknown"
var srcAccountSequenceStr = "unknown" var srcAccountSequenceStr = "unknown"
@ -80,18 +80,18 @@ func gen_tx() {
dstSendAmount := getUint64(Fmt("Enter amount to send to %X: ", dstAddress)) dstSendAmount := getUint64(Fmt("Enter amount to send to %X: ", dstAddress))
// Construct SendTx // Construct SendTx
tx := &block_.SendTx{
Inputs: []*block_.TxInput{
&block_.TxInput{
tx := &block.SendTx{
Inputs: []*block.TxInput{
&block.TxInput{
Address: srcAddress, Address: srcAddress,
Amount: srcSendAmount, Amount: srcSendAmount,
Sequence: srcSendSequence, Sequence: srcSendSequence,
Signature: account_.SignatureEd25519{},
Signature: account.SignatureEd25519{},
PubKey: srcPubKey, PubKey: srcPubKey,
}, },
}, },
Outputs: []*block_.TxOutput{
&block_.TxOutput{
Outputs: []*block.TxOutput{
&block.TxOutput{
Address: dstAddress, Address: dstAddress,
Amount: dstSendAmount, Amount: dstSendAmount,
}, },
@ -104,12 +104,12 @@ func gen_tx() {
// Get source privkey (for signing) // Get source privkey (for signing)
srcPrivKeyBytes := getByteSliceFromHex("Enter source privkey (for signing): ") srcPrivKeyBytes := getByteSliceFromHex("Enter source privkey (for signing): ")
r, n, err = bytes.NewReader(srcPrivKeyBytes), new(int64), new(error) r, n, err = bytes.NewReader(srcPrivKeyBytes), new(int64), new(error)
srcPrivKey := binary.ReadBinary(struct{ account_.PrivKey }{}, r, n, err).(struct{ account_.PrivKey }).PrivKey
srcPrivKey := binary.ReadBinary(struct{ account.PrivKey }{}, r, n, err).(struct{ account.PrivKey }).PrivKey
if *err != nil { if *err != nil {
Exit(Fmt("Invalid PrivKey. Error: %v", err)) Exit(Fmt("Invalid PrivKey. Error: %v", err))
} }
// Sign // Sign
tx.Inputs[0].Signature = srcPrivKey.Sign(account_.SignBytes(tx))
tx.Inputs[0].Signature = srcPrivKey.Sign(account.SignBytes(tx))
fmt.Printf("Signed tx: %X\n", binary.BinaryBytes(tx)) fmt.Printf("Signed tx: %X\n", binary.BinaryBytes(tx))
} }

+ 4
- 5
cmd/gen_validator.go View File

@ -3,16 +3,15 @@ package main
import ( import (
"fmt" "fmt"
"github.com/tendermint/tendermint/binary"
"github.com/tendermint/tendermint/config" "github.com/tendermint/tendermint/config"
"github.com/tendermint/tendermint/state"
. "github.com/tendermint/tendermint/binary"
sm "github.com/tendermint/tendermint/state"
) )
func gen_validator() { func gen_validator() {
privValidator := state.GenPrivValidator()
privValidatorJSONBytes := JSONBytes(privValidator)
privValidator := sm.GenPrivValidator()
privValidatorJSONBytes := binary.JSONBytes(privValidator)
fmt.Printf(`Generated a new validator! fmt.Printf(`Generated a new validator!
Paste the following JSON into your %v file Paste the following JSON into your %v file


binary/bit_array.go → common/bit_array.go View File


binary/bit_array_test.go → common/bit_array_test.go View File


+ 24
- 0
common/io.go View File

@ -0,0 +1,24 @@
package common
import (
"io"
)
type PrefixedReader struct {
Prefix []byte
reader io.Reader
}
func NewPrefixedReader(prefix []byte, reader io.Reader) *PrefixedReader {
return &PrefixedReader{prefix, reader}
}
func (pr *PrefixedReader) Read(p []byte) (n int, err error) {
if len(pr.Prefix) > 0 {
read := copy(p, pr.Prefix)
pr.Prefix = pr.Prefix[read:]
return read, nil
} else {
return pr.reader.Read(p)
}
}

+ 12
- 12
consensus/pol.go View File

@ -3,10 +3,10 @@ package consensus
import ( import (
"fmt" "fmt"
. "github.com/tendermint/tendermint/account"
. "github.com/tendermint/tendermint/block"
"github.com/tendermint/tendermint/account"
"github.com/tendermint/tendermint/block"
. "github.com/tendermint/tendermint/common" . "github.com/tendermint/tendermint/common"
"github.com/tendermint/tendermint/state"
sm "github.com/tendermint/tendermint/state"
) )
// Each signature of a POL (proof-of-lock, see whitepaper) is // Each signature of a POL (proof-of-lock, see whitepaper) is
@ -15,7 +15,7 @@ import (
// the POL round. Prevote rounds are equal to the POL round. // the POL round. Prevote rounds are equal to the POL round.
type POLVoteSignature struct { type POLVoteSignature struct {
Round uint Round uint
Signature SignatureEd25519
Signature account.SignatureEd25519
} }
// Proof of lock. // Proof of lock.
@ -23,13 +23,13 @@ type POLVoteSignature struct {
type POL struct { type POL struct {
Height uint Height uint
Round uint Round uint
BlockHash []byte // Could be nil, which makes this a proof of unlock.
BlockParts PartSetHeader // When BlockHash is nil, this is zero.
Votes []POLVoteSignature // Prevote and commit signatures in ValidatorSet order.
BlockHash []byte // Could be nil, which makes this a proof of unlock.
BlockParts block.PartSetHeader // When BlockHash is nil, this is zero.
Votes []POLVoteSignature // Prevote and commit signatures in ValidatorSet order.
} }
// Returns whether +2/3 have prevoted/committed for BlockHash. // Returns whether +2/3 have prevoted/committed for BlockHash.
func (pol *POL) Verify(valSet *state.ValidatorSet) error {
func (pol *POL) Verify(valSet *sm.ValidatorSet) error {
if uint(len(pol.Votes)) != valSet.Size() { if uint(len(pol.Votes)) != valSet.Size() {
return Errorf("Invalid POL votes count: Expected %v, got %v", return Errorf("Invalid POL votes count: Expected %v, got %v",
@ -37,8 +37,8 @@ func (pol *POL) Verify(valSet *state.ValidatorSet) error {
} }
talliedVotingPower := uint64(0) talliedVotingPower := uint64(0)
prevoteDoc := SignBytes(&Vote{
Height: pol.Height, Round: pol.Round, Type: VoteTypePrevote,
prevoteDoc := account.SignBytes(&block.Vote{
Height: pol.Height, Round: pol.Round, Type: block.VoteTypePrevote,
BlockHash: pol.BlockHash, BlockHash: pol.BlockHash,
BlockParts: pol.BlockParts, BlockParts: pol.BlockParts,
}) })
@ -54,8 +54,8 @@ func (pol *POL) Verify(valSet *state.ValidatorSet) error {
// Commit vote? // Commit vote?
if vote.Round < pol.Round { if vote.Round < pol.Round {
voteDoc = SignBytes(&Vote{
Height: pol.Height, Round: vote.Round, Type: VoteTypeCommit,
voteDoc = account.SignBytes(&block.Vote{
Height: pol.Height, Round: vote.Round, Type: block.VoteTypeCommit,
BlockHash: pol.BlockHash, BlockHash: pol.BlockHash,
BlockParts: pol.BlockParts, BlockParts: pol.BlockParts,
}) })


+ 27
- 27
consensus/pol_test.go View File

@ -1,10 +1,10 @@
package consensus package consensus
import ( import (
. "github.com/tendermint/tendermint/binary"
. "github.com/tendermint/tendermint/block"
"github.com/tendermint/tendermint/binary"
"github.com/tendermint/tendermint/block"
. "github.com/tendermint/tendermint/common" . "github.com/tendermint/tendermint/common"
state "github.com/tendermint/tendermint/state"
sm "github.com/tendermint/tendermint/state"
"bytes" "bytes"
"testing" "testing"
@ -15,7 +15,7 @@ import (
// Convenience method. // Convenience method.
// Signs the vote and sets the POL's vote at the desired index // Signs the vote and sets the POL's vote at the desired index
// Returns the POLVoteSignature pointer, so you can modify it afterwards. // Returns the POLVoteSignature pointer, so you can modify it afterwards.
func signAddPOLVoteSignature(val *state.PrivValidator, valSet *state.ValidatorSet, vote *Vote, pol *POL) *POLVoteSignature {
func signAddPOLVoteSignature(val *sm.PrivValidator, valSet *sm.ValidatorSet, vote *block.Vote, pol *POL) *POLVoteSignature {
vote = vote.Copy() vote = vote.Copy()
err := val.SignVote(vote) err := val.SignVote(vote)
if err != nil { if err != nil {
@ -28,7 +28,7 @@ func signAddPOLVoteSignature(val *state.PrivValidator, valSet *state.ValidatorSe
func TestVerifyVotes(t *testing.T) { func TestVerifyVotes(t *testing.T) {
height, round := uint(1), uint(0) height, round := uint(1), uint(0)
_, valSet, privValidators := randVoteSet(height, round, VoteTypePrevote, 10, 1)
_, valSet, privValidators := randVoteSet(height, round, block.VoteTypePrevote, 10, 1)
// Make a POL with -2/3 votes. // Make a POL with -2/3 votes.
blockHash := RandBytes(32) blockHash := RandBytes(32)
@ -36,8 +36,8 @@ func TestVerifyVotes(t *testing.T) {
Height: height, Round: round, BlockHash: blockHash, Height: height, Round: round, BlockHash: blockHash,
Votes: make([]POLVoteSignature, valSet.Size()), Votes: make([]POLVoteSignature, valSet.Size()),
} }
voteProto := &Vote{
Height: height, Round: round, Type: VoteTypePrevote, BlockHash: blockHash,
voteProto := &block.Vote{
Height: height, Round: round, Type: block.VoteTypePrevote, BlockHash: blockHash,
} }
for i := 0; i < 6; i++ { for i := 0; i < 6; i++ {
signAddPOLVoteSignature(privValidators[i], valSet, voteProto, pol) signAddPOLVoteSignature(privValidators[i], valSet, voteProto, pol)
@ -59,7 +59,7 @@ func TestVerifyVotes(t *testing.T) {
func TestVerifyInvalidVote(t *testing.T) { func TestVerifyInvalidVote(t *testing.T) {
height, round := uint(1), uint(0) height, round := uint(1), uint(0)
_, valSet, privValidators := randVoteSet(height, round, VoteTypePrevote, 10, 1)
_, valSet, privValidators := randVoteSet(height, round, block.VoteTypePrevote, 10, 1)
// Make a POL with +2/3 votes with the wrong signature. // Make a POL with +2/3 votes with the wrong signature.
blockHash := RandBytes(32) blockHash := RandBytes(32)
@ -67,8 +67,8 @@ func TestVerifyInvalidVote(t *testing.T) {
Height: height, Round: round, BlockHash: blockHash, Height: height, Round: round, BlockHash: blockHash,
Votes: make([]POLVoteSignature, valSet.Size()), Votes: make([]POLVoteSignature, valSet.Size()),
} }
voteProto := &Vote{
Height: height, Round: round, Type: VoteTypePrevote, BlockHash: blockHash,
voteProto := &block.Vote{
Height: height, Round: round, Type: block.VoteTypePrevote, BlockHash: blockHash,
} }
for i := 0; i < 7; i++ { for i := 0; i < 7; i++ {
polVoteSig := signAddPOLVoteSignature(privValidators[i], valSet, voteProto, pol) polVoteSig := signAddPOLVoteSignature(privValidators[i], valSet, voteProto, pol)
@ -83,7 +83,7 @@ func TestVerifyInvalidVote(t *testing.T) {
func TestVerifyCommits(t *testing.T) { func TestVerifyCommits(t *testing.T) {
height, round := uint(1), uint(2) height, round := uint(1), uint(2)
_, valSet, privValidators := randVoteSet(height, round, VoteTypePrevote, 10, 1)
_, valSet, privValidators := randVoteSet(height, round, block.VoteTypePrevote, 10, 1)
// Make a POL with +2/3 votes. // Make a POL with +2/3 votes.
blockHash := RandBytes(32) blockHash := RandBytes(32)
@ -91,8 +91,8 @@ func TestVerifyCommits(t *testing.T) {
Height: height, Round: round, BlockHash: blockHash, Height: height, Round: round, BlockHash: blockHash,
Votes: make([]POLVoteSignature, valSet.Size()), Votes: make([]POLVoteSignature, valSet.Size()),
} }
voteProto := &Vote{
Height: height, Round: round - 1, Type: VoteTypeCommit, BlockHash: blockHash,
voteProto := &block.Vote{
Height: height, Round: round - 1, Type: block.VoteTypeCommit, BlockHash: blockHash,
} }
for i := 0; i < 7; i++ { for i := 0; i < 7; i++ {
signAddPOLVoteSignature(privValidators[i], valSet, voteProto, pol) signAddPOLVoteSignature(privValidators[i], valSet, voteProto, pol)
@ -106,7 +106,7 @@ func TestVerifyCommits(t *testing.T) {
func TestVerifyInvalidCommits(t *testing.T) { func TestVerifyInvalidCommits(t *testing.T) {
height, round := uint(1), uint(2) height, round := uint(1), uint(2)
_, valSet, privValidators := randVoteSet(height, round, VoteTypePrevote, 10, 1)
_, valSet, privValidators := randVoteSet(height, round, block.VoteTypePrevote, 10, 1)
// Make a POL with +2/3 votes with the wrong signature. // Make a POL with +2/3 votes with the wrong signature.
blockHash := RandBytes(32) blockHash := RandBytes(32)
@ -114,8 +114,8 @@ func TestVerifyInvalidCommits(t *testing.T) {
Height: height, Round: round, BlockHash: blockHash, Height: height, Round: round, BlockHash: blockHash,
Votes: make([]POLVoteSignature, valSet.Size()), Votes: make([]POLVoteSignature, valSet.Size()),
} }
voteProto := &Vote{
Height: height, Round: round - 1, Type: VoteTypeCommit, BlockHash: blockHash,
voteProto := &block.Vote{
Height: height, Round: round - 1, Type: block.VoteTypeCommit, BlockHash: blockHash,
} }
for i := 0; i < 7; i++ { for i := 0; i < 7; i++ {
polVoteSig := signAddPOLVoteSignature(privValidators[i], valSet, voteProto, pol) polVoteSig := signAddPOLVoteSignature(privValidators[i], valSet, voteProto, pol)
@ -130,7 +130,7 @@ func TestVerifyInvalidCommits(t *testing.T) {
func TestVerifyInvalidCommitRounds(t *testing.T) { func TestVerifyInvalidCommitRounds(t *testing.T) {
height, round := uint(1), uint(2) height, round := uint(1), uint(2)
_, valSet, privValidators := randVoteSet(height, round, VoteTypePrevote, 10, 1)
_, valSet, privValidators := randVoteSet(height, round, block.VoteTypePrevote, 10, 1)
// Make a POL with +2/3 commits for the current round. // Make a POL with +2/3 commits for the current round.
blockHash := RandBytes(32) blockHash := RandBytes(32)
@ -138,8 +138,8 @@ func TestVerifyInvalidCommitRounds(t *testing.T) {
Height: height, Round: round, BlockHash: blockHash, Height: height, Round: round, BlockHash: blockHash,
Votes: make([]POLVoteSignature, valSet.Size()), Votes: make([]POLVoteSignature, valSet.Size()),
} }
voteProto := &Vote{
Height: height, Round: round, Type: VoteTypeCommit, BlockHash: blockHash,
voteProto := &block.Vote{
Height: height, Round: round, Type: block.VoteTypeCommit, BlockHash: blockHash,
} }
for i := 0; i < 7; i++ { for i := 0; i < 7; i++ {
signAddPOLVoteSignature(privValidators[i], valSet, voteProto, pol) signAddPOLVoteSignature(privValidators[i], valSet, voteProto, pol)
@ -153,7 +153,7 @@ func TestVerifyInvalidCommitRounds(t *testing.T) {
func TestVerifyInvalidCommitRounds2(t *testing.T) { func TestVerifyInvalidCommitRounds2(t *testing.T) {
height, round := uint(1), uint(2) height, round := uint(1), uint(2)
_, valSet, privValidators := randVoteSet(height, round, VoteTypePrevote, 10, 1)
_, valSet, privValidators := randVoteSet(height, round, block.VoteTypePrevote, 10, 1)
// Make a POL with +2/3 commits for future round. // Make a POL with +2/3 commits for future round.
blockHash := RandBytes(32) blockHash := RandBytes(32)
@ -161,8 +161,8 @@ func TestVerifyInvalidCommitRounds2(t *testing.T) {
Height: height, Round: round, BlockHash: blockHash, Height: height, Round: round, BlockHash: blockHash,
Votes: make([]POLVoteSignature, valSet.Size()), Votes: make([]POLVoteSignature, valSet.Size()),
} }
voteProto := &Vote{
Height: height, Round: round + 1, Type: VoteTypeCommit, BlockHash: blockHash,
voteProto := &block.Vote{
Height: height, Round: round + 1, Type: block.VoteTypeCommit, BlockHash: blockHash,
} }
for i := 0; i < 7; i++ { for i := 0; i < 7; i++ {
polVoteSig := signAddPOLVoteSignature(privValidators[i], valSet, voteProto, pol) polVoteSig := signAddPOLVoteSignature(privValidators[i], valSet, voteProto, pol)
@ -177,7 +177,7 @@ func TestVerifyInvalidCommitRounds2(t *testing.T) {
func TestReadWrite(t *testing.T) { func TestReadWrite(t *testing.T) {
height, round := uint(1), uint(2) height, round := uint(1), uint(2)
_, valSet, privValidators := randVoteSet(height, round, VoteTypePrevote, 10, 1)
_, valSet, privValidators := randVoteSet(height, round, block.VoteTypePrevote, 10, 1)
// Make a POL with +2/3 votes. // Make a POL with +2/3 votes.
blockHash := RandBytes(32) blockHash := RandBytes(32)
@ -185,8 +185,8 @@ func TestReadWrite(t *testing.T) {
Height: height, Round: round, BlockHash: blockHash, Height: height, Round: round, BlockHash: blockHash,
Votes: make([]POLVoteSignature, valSet.Size()), Votes: make([]POLVoteSignature, valSet.Size()),
} }
voteProto := &Vote{
Height: height, Round: round, Type: VoteTypePrevote, BlockHash: blockHash,
voteProto := &block.Vote{
Height: height, Round: round, Type: block.VoteTypePrevote, BlockHash: blockHash,
} }
for i := 0; i < 7; i++ { for i := 0; i < 7; i++ {
signAddPOLVoteSignature(privValidators[i], valSet, voteProto, pol) signAddPOLVoteSignature(privValidators[i], valSet, voteProto, pol)
@ -194,13 +194,13 @@ func TestReadWrite(t *testing.T) {
// Write it to a buffer. // Write it to a buffer.
buf, n, err := new(bytes.Buffer), new(int64), new(error) buf, n, err := new(bytes.Buffer), new(int64), new(error)
WriteBinary(pol, buf, n, err)
binary.WriteBinary(pol, buf, n, err)
if *err != nil { if *err != nil {
t.Fatalf("Failed to write POL: %v", *err) t.Fatalf("Failed to write POL: %v", *err)
} }
// Read from buffer. // Read from buffer.
pol2 := ReadBinary(&POL{}, buf, n, err).(*POL)
pol2 := binary.ReadBinary(&POL{}, buf, n, err).(*POL)
if *err != nil { if *err != nil {
t.Fatalf("Failed to read POL") t.Fatalf("Failed to read POL")
} }


+ 40
- 40
consensus/reactor.go View File

@ -8,12 +8,12 @@ import (
"sync/atomic" "sync/atomic"
"time" "time"
. "github.com/tendermint/tendermint/binary"
. "github.com/tendermint/tendermint/block"
"github.com/tendermint/tendermint/binary"
"github.com/tendermint/tendermint/block"
. "github.com/tendermint/tendermint/common" . "github.com/tendermint/tendermint/common"
. "github.com/tendermint/tendermint/consensus/types" . "github.com/tendermint/tendermint/consensus/types"
"github.com/tendermint/tendermint/p2p" "github.com/tendermint/tendermint/p2p"
"github.com/tendermint/tendermint/state"
sm "github.com/tendermint/tendermint/state"
) )
const ( const (
@ -34,11 +34,11 @@ type ConsensusReactor struct {
stopped uint32 stopped uint32
quit chan struct{} quit chan struct{}
blockStore *BlockStore
blockStore *block.BlockStore
conS *ConsensusState conS *ConsensusState
} }
func NewConsensusReactor(consensusState *ConsensusState, blockStore *BlockStore) *ConsensusReactor {
func NewConsensusReactor(consensusState *ConsensusState, blockStore *block.BlockStore) *ConsensusReactor {
conR := &ConsensusReactor{ conR := &ConsensusReactor{
blockStore: blockStore, blockStore: blockStore,
quit: make(chan struct{}), quit: make(chan struct{}),
@ -204,7 +204,7 @@ func (conR *ConsensusReactor) Receive(chId byte, peer *p2p.Peer, msgBytes []byte
} }
// Sets our private validator account for signing votes. // Sets our private validator account for signing votes.
func (conR *ConsensusReactor) SetPrivValidator(priv *state.PrivValidator) {
func (conR *ConsensusReactor) SetPrivValidator(priv *sm.PrivValidator) {
conR.conS.SetPrivValidator(priv) conR.conS.SetPrivValidator(priv)
} }
@ -398,7 +398,7 @@ OUTER_LOOP:
return false return false
} }
trySendCommitFromValidation := func(blockMeta *BlockMeta, validation *Validation, peerVoteSet BitArray) (sent bool) {
trySendCommitFromValidation := func(blockMeta *block.BlockMeta, validation *block.Validation, peerVoteSet BitArray) (sent bool) {
// Initialize Commits if needed // Initialize Commits if needed
ps.EnsureVoteBitArrays(prs.Height, uint(len(validation.Commits))) ps.EnsureVoteBitArrays(prs.Height, uint(len(validation.Commits)))
@ -406,10 +406,10 @@ OUTER_LOOP:
commit := validation.Commits[index] commit := validation.Commits[index]
log.Debug("Picked commit to send", "index", index, "commit", commit) log.Debug("Picked commit to send", "index", index, "commit", commit)
// Reconstruct vote. // Reconstruct vote.
vote := &Vote{
vote := &block.Vote{
Height: prs.Height, Height: prs.Height,
Round: commit.Round, Round: commit.Round,
Type: VoteTypeCommit,
Type: block.VoteTypeCommit,
BlockHash: blockMeta.Hash, BlockHash: blockMeta.Hash,
BlockParts: blockMeta.Parts, BlockParts: blockMeta.Parts,
Signature: commit.Signature, Signature: commit.Signature,
@ -509,20 +509,20 @@ OUTER_LOOP:
// Read only when returned by PeerState.GetRoundState(). // Read only when returned by PeerState.GetRoundState().
type PeerRoundState struct { type PeerRoundState struct {
Height uint // Height peer is at
Round uint // Round peer is at
Step RoundStep // Step peer is at
StartTime time.Time // Estimated start of round 0 at this height
Proposal bool // True if peer has proposal for this round
ProposalBlockParts PartSetHeader //
ProposalBlockBitArray BitArray // True bit -> has part
ProposalPOLParts PartSetHeader //
ProposalPOLBitArray BitArray // True bit -> has part
Prevotes BitArray // All votes peer has for this round
Precommits BitArray // All precommits peer has for this round
Commits BitArray // All commits peer has for this height
LastCommits BitArray // All commits peer has for last height
HasAllCatchupCommits bool // Used for catch-up
Height uint // Height peer is at
Round uint // Round peer is at
Step RoundStep // Step peer is at
StartTime time.Time // Estimated start of round 0 at this height
Proposal bool // True if peer has proposal for this round
ProposalBlockParts block.PartSetHeader //
ProposalBlockBitArray BitArray // True bit -> has part
ProposalPOLParts block.PartSetHeader //
ProposalPOLBitArray BitArray // True bit -> has part
Prevotes BitArray // All votes peer has for this round
Precommits BitArray // All precommits peer has for this round
Commits BitArray // All commits peer has for this height
LastCommits BitArray // All commits peer has for last height
HasAllCatchupCommits bool // Used for catch-up
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
@ -610,7 +610,7 @@ func (ps *PeerState) EnsureVoteBitArrays(height uint, numValidators uint) {
} }
} }
func (ps *PeerState) SetHasVote(vote *Vote, index uint) {
func (ps *PeerState) SetHasVote(vote *block.Vote, index uint) {
ps.mtx.Lock() ps.mtx.Lock()
defer ps.mtx.Unlock() defer ps.mtx.Unlock()
@ -618,7 +618,7 @@ func (ps *PeerState) SetHasVote(vote *Vote, index uint) {
} }
func (ps *PeerState) setHasVote(height uint, round uint, type_ byte, index uint) { func (ps *PeerState) setHasVote(height uint, round uint, type_ byte, index uint) {
if ps.Height == height+1 && type_ == VoteTypeCommit {
if ps.Height == height+1 && type_ == block.VoteTypeCommit {
// Special case for LastCommits. // Special case for LastCommits.
ps.LastCommits.SetIndex(index, true) ps.LastCommits.SetIndex(index, true)
return return
@ -628,11 +628,11 @@ func (ps *PeerState) setHasVote(height uint, round uint, type_ byte, index uint)
} }
switch type_ { switch type_ {
case VoteTypePrevote:
case block.VoteTypePrevote:
ps.Prevotes.SetIndex(index, true) ps.Prevotes.SetIndex(index, true)
case VoteTypePrecommit:
case block.VoteTypePrecommit:
ps.Precommits.SetIndex(index, true) ps.Precommits.SetIndex(index, true)
case VoteTypeCommit:
case block.VoteTypeCommit:
if round < ps.Round { if round < ps.Round {
ps.Prevotes.SetIndex(index, true) ps.Prevotes.SetIndex(index, true)
ps.Precommits.SetIndex(index, true) ps.Precommits.SetIndex(index, true)
@ -670,9 +670,9 @@ func (ps *PeerState) ApplyNewRoundStepMessage(msg *NewRoundStepMessage, rs *Roun
ps.StartTime = startTime ps.StartTime = startTime
if psHeight != msg.Height || psRound != msg.Round { if psHeight != msg.Height || psRound != msg.Round {
ps.Proposal = false ps.Proposal = false
ps.ProposalBlockParts = PartSetHeader{}
ps.ProposalBlockParts = block.PartSetHeader{}
ps.ProposalBlockBitArray = BitArray{} ps.ProposalBlockBitArray = BitArray{}
ps.ProposalPOLParts = PartSetHeader{}
ps.ProposalPOLParts = block.PartSetHeader{}
ps.ProposalPOLBitArray = BitArray{} ps.ProposalPOLBitArray = BitArray{}
// We'll update the BitArray capacity later. // We'll update the BitArray capacity later.
ps.Prevotes = BitArray{} ps.Prevotes = BitArray{}
@ -708,7 +708,7 @@ func (ps *PeerState) ApplyHasVoteMessage(msg *HasVoteMessage) {
defer ps.mtx.Unlock() defer ps.mtx.Unlock()
// Special case for LastCommits // Special case for LastCommits
if ps.Height == msg.Height+1 && msg.Type == VoteTypeCommit {
if ps.Height == msg.Height+1 && msg.Type == block.VoteTypeCommit {
ps.LastCommits.SetIndex(msg.Index, true) ps.LastCommits.SetIndex(msg.Index, true)
return return
} else if ps.Height != msg.Height { } else if ps.Height != msg.Height {
@ -740,19 +740,19 @@ func DecodeMessage(bz []byte) (msgType byte, msg interface{}, err error) {
switch msgType { switch msgType {
// Messages for communicating state changes // Messages for communicating state changes
case msgTypeNewRoundStep: case msgTypeNewRoundStep:
msg = ReadBinary(&NewRoundStepMessage{}, r, n, &err)
msg = binary.ReadBinary(&NewRoundStepMessage{}, r, n, &err)
case msgTypeCommitStep: case msgTypeCommitStep:
msg = ReadBinary(&CommitStepMessage{}, r, n, &err)
msg = binary.ReadBinary(&CommitStepMessage{}, r, n, &err)
// Messages of data // Messages of data
case msgTypeProposal: case msgTypeProposal:
r.ReadByte() // Consume the byte r.ReadByte() // Consume the byte
msg = ReadBinary(&Proposal{}, r, n, &err)
msg = binary.ReadBinary(&Proposal{}, r, n, &err)
case msgTypePart: case msgTypePart:
msg = ReadBinary(&PartMessage{}, r, n, &err)
msg = binary.ReadBinary(&PartMessage{}, r, n, &err)
case msgTypeVote: case msgTypeVote:
msg = ReadBinary(&VoteMessage{}, r, n, &err)
msg = binary.ReadBinary(&VoteMessage{}, r, n, &err)
case msgTypeHasVote: case msgTypeHasVote:
msg = ReadBinary(&HasVoteMessage{}, r, n, &err)
msg = binary.ReadBinary(&HasVoteMessage{}, r, n, &err)
default: default:
msg = nil msg = nil
} }
@ -778,7 +778,7 @@ func (m *NewRoundStepMessage) String() string {
type CommitStepMessage struct { type CommitStepMessage struct {
Height uint Height uint
BlockParts PartSetHeader
BlockParts block.PartSetHeader
BlockBitArray BitArray BlockBitArray BitArray
} }
@ -799,7 +799,7 @@ type PartMessage struct {
Height uint Height uint
Round uint Round uint
Type byte Type byte
Part *Part
Part *block.Part
} }
func (m *PartMessage) TypeByte() byte { return msgTypePart } func (m *PartMessage) TypeByte() byte { return msgTypePart }
@ -812,7 +812,7 @@ func (m *PartMessage) String() string {
type VoteMessage struct { type VoteMessage struct {
ValidatorIndex uint ValidatorIndex uint
Vote *Vote
Vote *block.Vote
} }
func (m *VoteMessage) TypeByte() byte { return msgTypeVote } func (m *VoteMessage) TypeByte() byte { return msgTypeVote }


+ 81
- 81
consensus/state.go View File

@ -60,14 +60,14 @@ import (
"sync/atomic" "sync/atomic"
"time" "time"
. "github.com/tendermint/tendermint/account"
. "github.com/tendermint/tendermint/binary"
. "github.com/tendermint/tendermint/block"
"github.com/tendermint/tendermint/account"
"github.com/tendermint/tendermint/binary"
"github.com/tendermint/tendermint/block"
. "github.com/tendermint/tendermint/common" . "github.com/tendermint/tendermint/common"
"github.com/tendermint/tendermint/config" "github.com/tendermint/tendermint/config"
. "github.com/tendermint/tendermint/consensus/types" . "github.com/tendermint/tendermint/consensus/types"
"github.com/tendermint/tendermint/mempool"
"github.com/tendermint/tendermint/state"
mempl "github.com/tendermint/tendermint/mempool"
sm "github.com/tendermint/tendermint/state"
) )
const ( const (
@ -169,20 +169,20 @@ type RoundState struct {
Step RoundStep Step RoundStep
StartTime time.Time StartTime time.Time
CommitTime time.Time // Time when +2/3 commits were found CommitTime time.Time // Time when +2/3 commits were found
Validators *state.ValidatorSet
Validators *sm.ValidatorSet
Proposal *Proposal Proposal *Proposal
ProposalBlock *Block
ProposalBlockParts *PartSet
ProposalBlock *block.Block
ProposalBlockParts *block.PartSet
ProposalPOL *POL ProposalPOL *POL
ProposalPOLParts *PartSet
LockedBlock *Block
LockedBlockParts *PartSet
ProposalPOLParts *block.PartSet
LockedBlock *block.Block
LockedBlockParts *block.PartSet
LockedPOL *POL // Rarely needed, so no LockedPOLParts. LockedPOL *POL // Rarely needed, so no LockedPOLParts.
Prevotes *VoteSet Prevotes *VoteSet
Precommits *VoteSet Precommits *VoteSet
Commits *VoteSet Commits *VoteSet
LastCommits *VoteSet LastCommits *VoteSet
PrivValidator *state.PrivValidator
PrivValidator *sm.PrivValidator
} }
func (rs *RoundState) String() string { func (rs *RoundState) String() string {
@ -234,20 +234,20 @@ type ConsensusState struct {
stopped uint32 stopped uint32
quit chan struct{} quit chan struct{}
blockStore *BlockStore
mempoolReactor *mempool.MempoolReactor
blockStore *block.BlockStore
mempoolReactor *mempl.MempoolReactor
runActionCh chan RoundAction runActionCh chan RoundAction
newStepCh chan *RoundState newStepCh chan *RoundState
mtx sync.Mutex mtx sync.Mutex
RoundState RoundState
state *state.State // State until height-1.
stagedBlock *Block // Cache last staged block.
stagedState *state.State // Cache result of staged block.
state *sm.State // State until height-1.
stagedBlock *block.Block // Cache last staged block.
stagedState *sm.State // Cache result of staged block.
lastCommitVoteHeight uint // Last called commitVoteBlock() or saveCommitVoteBlock() on. lastCommitVoteHeight uint // Last called commitVoteBlock() or saveCommitVoteBlock() on.
} }
func NewConsensusState(state *state.State, blockStore *BlockStore, mempoolReactor *mempool.MempoolReactor) *ConsensusState {
func NewConsensusState(state *sm.State, blockStore *block.BlockStore, mempoolReactor *mempl.MempoolReactor) *ConsensusState {
cs := &ConsensusState{ cs := &ConsensusState{
quit: make(chan struct{}), quit: make(chan struct{}),
blockStore: blockStore, blockStore: blockStore,
@ -259,7 +259,7 @@ func NewConsensusState(state *state.State, blockStore *BlockStore, mempoolReacto
return cs return cs
} }
func (cs *ConsensusState) GetState() *state.State {
func (cs *ConsensusState) GetState() *sm.State {
cs.mtx.Lock() cs.mtx.Lock()
defer cs.mtx.Unlock() defer cs.mtx.Unlock()
return cs.state.Copy() return cs.state.Copy()
@ -456,7 +456,7 @@ ACTION_LOOP:
// If calculated round is greater than 0 (based on BlockTime or calculated StartTime) // If calculated round is greater than 0 (based on BlockTime or calculated StartTime)
// then also sets up the appropriate round, and cs.Step becomes RoundStepNewRound. // then also sets up the appropriate round, and cs.Step becomes RoundStepNewRound.
// Otherwise the round is 0 and cs.Step becomes RoundStepNewHeight. // Otherwise the round is 0 and cs.Step becomes RoundStepNewHeight.
func (cs *ConsensusState) updateToState(state *state.State) {
func (cs *ConsensusState) updateToState(state *sm.State) {
// Sanity check state. // Sanity check state.
if cs.Height > 0 && cs.Height != state.LastBlockHeight { if cs.Height > 0 && cs.Height != state.LastBlockHeight {
panic(Fmt("updateToState() expected state height of %v but found %v", panic(Fmt("updateToState() expected state height of %v but found %v",
@ -484,10 +484,10 @@ func (cs *ConsensusState) updateToState(state *state.State) {
cs.LockedBlock = nil cs.LockedBlock = nil
cs.LockedBlockParts = nil cs.LockedBlockParts = nil
cs.LockedPOL = nil cs.LockedPOL = nil
cs.Prevotes = NewVoteSet(height, 0, VoteTypePrevote, validators)
cs.Precommits = NewVoteSet(height, 0, VoteTypePrecommit, validators)
cs.Prevotes = NewVoteSet(height, 0, block.VoteTypePrevote, validators)
cs.Precommits = NewVoteSet(height, 0, block.VoteTypePrecommit, validators)
cs.LastCommits = cs.Commits cs.LastCommits = cs.Commits
cs.Commits = NewVoteSet(height, 0, VoteTypeCommit, validators)
cs.Commits = NewVoteSet(height, 0, block.VoteTypeCommit, validators)
cs.state = state cs.state = state
cs.stagedBlock = nil cs.stagedBlock = nil
@ -501,7 +501,7 @@ func (cs *ConsensusState) updateToState(state *state.State) {
// If we've timed out, then send rebond tx. // If we've timed out, then send rebond tx.
if cs.PrivValidator != nil && cs.state.UnbondingValidators.HasAddress(cs.PrivValidator.Address) { if cs.PrivValidator != nil && cs.state.UnbondingValidators.HasAddress(cs.PrivValidator.Address) {
rebondTx := &RebondTx{
rebondTx := &block.RebondTx{
Address: cs.PrivValidator.Address, Address: cs.PrivValidator.Address,
Height: cs.Height + 1, Height: cs.Height + 1,
} }
@ -534,13 +534,13 @@ func (cs *ConsensusState) setupNewRound(round uint) {
cs.ProposalBlockParts = nil cs.ProposalBlockParts = nil
cs.ProposalPOL = nil cs.ProposalPOL = nil
cs.ProposalPOLParts = nil cs.ProposalPOLParts = nil
cs.Prevotes = NewVoteSet(cs.Height, round, VoteTypePrevote, validators)
cs.Prevotes = NewVoteSet(cs.Height, round, block.VoteTypePrevote, validators)
cs.Prevotes.AddFromCommits(cs.Commits) cs.Prevotes.AddFromCommits(cs.Commits)
cs.Precommits = NewVoteSet(cs.Height, round, VoteTypePrecommit, validators)
cs.Precommits = NewVoteSet(cs.Height, round, block.VoteTypePrecommit, validators)
cs.Precommits.AddFromCommits(cs.Commits) cs.Precommits.AddFromCommits(cs.Commits)
} }
func (cs *ConsensusState) SetPrivValidator(priv *state.PrivValidator) {
func (cs *ConsensusState) SetPrivValidator(priv *sm.PrivValidator) {
cs.mtx.Lock() cs.mtx.Lock()
defer cs.mtx.Unlock() defer cs.mtx.Unlock()
cs.PrivValidator = priv cs.PrivValidator = priv
@ -586,24 +586,24 @@ func (cs *ConsensusState) RunActionPropose(height uint, round uint) {
log.Debug("Our turn to propose", "proposer", cs.Validators.Proposer().Address, "privValidator", cs.PrivValidator) log.Debug("Our turn to propose", "proposer", cs.Validators.Proposer().Address, "privValidator", cs.PrivValidator)
} }
var block *Block
var blockParts *PartSet
var block_ *block.Block
var blockParts *block.PartSet
var pol *POL var pol *POL
var polParts *PartSet
var polParts *block.PartSet
// Decide on block and POL // Decide on block and POL
if cs.LockedBlock != nil { if cs.LockedBlock != nil {
// If we're locked onto a block, just choose that. // If we're locked onto a block, just choose that.
block = cs.LockedBlock
block_ = cs.LockedBlock
blockParts = cs.LockedBlockParts blockParts = cs.LockedBlockParts
pol = cs.LockedPOL pol = cs.LockedPOL
} else { } else {
// Otherwise we should create a new proposal. // Otherwise we should create a new proposal.
var validation *Validation
var validation *block.Validation
if cs.Height == 1 { if cs.Height == 1 {
// We're creating a proposal for the first block. // We're creating a proposal for the first block.
// The validation is empty. // The validation is empty.
validation = &Validation{}
validation = &block.Validation{}
} else if cs.LastCommits.HasTwoThirdsMajority() { } else if cs.LastCommits.HasTwoThirdsMajority() {
// Make the validation from LastCommits // Make the validation from LastCommits
validation = cs.LastCommits.MakeValidation() validation = cs.LastCommits.MakeValidation()
@ -617,8 +617,8 @@ func (cs *ConsensusState) RunActionPropose(height uint, round uint) {
} }
} }
txs := cs.mempoolReactor.Mempool.GetProposalTxs() txs := cs.mempoolReactor.Mempool.GetProposalTxs()
block = &Block{
Header: &Header{
block_ = &block.Block{
Header: &block.Header{
Network: config.App.GetString("Network"), Network: config.App.GetString("Network"),
Height: cs.Height, Height: cs.Height,
Time: time.Now(), Time: time.Now(),
@ -629,7 +629,7 @@ func (cs *ConsensusState) RunActionPropose(height uint, round uint) {
StateHash: nil, // Will set afterwards. StateHash: nil, // Will set afterwards.
}, },
Validation: validation, Validation: validation,
Data: &Data{
Data: &block.Data{
Txs: txs, Txs: txs,
}, },
} }
@ -637,14 +637,14 @@ func (cs *ConsensusState) RunActionPropose(height uint, round uint) {
// Set the block.Header.StateHash. // Set the block.Header.StateHash.
// TODO: we could cache the resulting state to cs.stagedState. // TODO: we could cache the resulting state to cs.stagedState.
// TODO: This is confusing, not clear that we're mutating block. // TODO: This is confusing, not clear that we're mutating block.
cs.state.Copy().AppendBlock(block, PartSetHeader{}, false)
cs.state.Copy().AppendBlock(block_, block.PartSetHeader{}, false)
blockParts = NewPartSetFromData(BinaryBytes(block))
blockParts = block.NewPartSetFromData(binary.BinaryBytes(block_))
pol = cs.LockedPOL // If exists, is a PoUnlock. pol = cs.LockedPOL // If exists, is a PoUnlock.
} }
if pol != nil { if pol != nil {
polParts = NewPartSetFromData(BinaryBytes(pol))
polParts = block.NewPartSetFromData(binary.BinaryBytes(pol))
} }
// Make proposal // Make proposal
@ -652,10 +652,10 @@ func (cs *ConsensusState) RunActionPropose(height uint, round uint) {
err := cs.PrivValidator.SignProposal(proposal) err := cs.PrivValidator.SignProposal(proposal)
if err == nil { if err == nil {
log.Info("Signed and set proposal", "height", cs.Height, "round", cs.Round, "proposal", proposal) log.Info("Signed and set proposal", "height", cs.Height, "round", cs.Round, "proposal", proposal)
log.Debug(Fmt("Signed and set proposal block: %v", block))
log.Debug(Fmt("Signed and set proposal block: %v", block_))
// Set fields // Set fields
cs.Proposal = proposal cs.Proposal = proposal
cs.ProposalBlock = block
cs.ProposalBlock = block_
cs.ProposalBlockParts = blockParts cs.ProposalBlockParts = blockParts
cs.ProposalPOL = pol cs.ProposalPOL = pol
cs.ProposalPOLParts = polParts cs.ProposalPOLParts = polParts
@ -679,14 +679,14 @@ func (cs *ConsensusState) RunActionPrevote(height uint, round uint) {
// If a block is locked, prevote that. // If a block is locked, prevote that.
if cs.LockedBlock != nil { if cs.LockedBlock != nil {
cs.signAddVote(VoteTypePrevote, cs.LockedBlock.Hash(), cs.LockedBlockParts.Header())
cs.signAddVote(block.VoteTypePrevote, cs.LockedBlock.Hash(), cs.LockedBlockParts.Header())
return return
} }
// If ProposalBlock is nil, prevote nil. // If ProposalBlock is nil, prevote nil.
if cs.ProposalBlock == nil { if cs.ProposalBlock == nil {
log.Warn("ProposalBlock is nil") log.Warn("ProposalBlock is nil")
cs.signAddVote(VoteTypePrevote, nil, PartSetHeader{})
cs.signAddVote(block.VoteTypePrevote, nil, block.PartSetHeader{})
return return
} }
@ -695,12 +695,12 @@ func (cs *ConsensusState) RunActionPrevote(height uint, round uint) {
if err != nil { if err != nil {
// ProposalBlock is invalid, prevote nil. // ProposalBlock is invalid, prevote nil.
log.Warn("ProposalBlock is invalid", "error", err) log.Warn("ProposalBlock is invalid", "error", err)
cs.signAddVote(VoteTypePrevote, nil, PartSetHeader{})
cs.signAddVote(block.VoteTypePrevote, nil, block.PartSetHeader{})
return return
} }
// Prevote cs.ProposalBlock // Prevote cs.ProposalBlock
cs.signAddVote(VoteTypePrevote, cs.ProposalBlock.Hash(), cs.ProposalBlockParts.Header())
cs.signAddVote(block.VoteTypePrevote, cs.ProposalBlock.Hash(), cs.ProposalBlockParts.Header())
return return
} }
@ -736,7 +736,7 @@ func (cs *ConsensusState) RunActionPrecommit(height uint, round uint) {
// If +2/3 prevoted for already locked block, precommit it. // If +2/3 prevoted for already locked block, precommit it.
if cs.LockedBlock.HashesTo(hash) { if cs.LockedBlock.HashesTo(hash) {
cs.signAddVote(VoteTypePrecommit, hash, partsHeader)
cs.signAddVote(block.VoteTypePrecommit, hash, partsHeader)
return return
} }
@ -750,7 +750,7 @@ func (cs *ConsensusState) RunActionPrecommit(height uint, round uint) {
} }
cs.LockedBlock = cs.ProposalBlock cs.LockedBlock = cs.ProposalBlock
cs.LockedBlockParts = cs.ProposalBlockParts cs.LockedBlockParts = cs.ProposalBlockParts
cs.signAddVote(VoteTypePrecommit, hash, partsHeader)
cs.signAddVote(block.VoteTypePrecommit, hash, partsHeader)
return return
} }
@ -804,7 +804,7 @@ func (cs *ConsensusState) RunActionCommit(height uint) {
// We're getting the wrong block. // We're getting the wrong block.
// Set up ProposalBlockParts and keep waiting. // Set up ProposalBlockParts and keep waiting.
cs.ProposalBlock = nil cs.ProposalBlock = nil
cs.ProposalBlockParts = NewPartSetFromHeader(partsHeader)
cs.ProposalBlockParts = block.NewPartSetFromHeader(partsHeader)
} else { } else {
// We just need to keep waiting. // We just need to keep waiting.
@ -889,19 +889,19 @@ func (cs *ConsensusState) SetProposal(proposal *Proposal) error {
} }
// Verify signature // Verify signature
if !cs.Validators.Proposer().PubKey.VerifyBytes(SignBytes(proposal), proposal.Signature) {
if !cs.Validators.Proposer().PubKey.VerifyBytes(account.SignBytes(proposal), proposal.Signature) {
return ErrInvalidProposalSignature return ErrInvalidProposalSignature
} }
cs.Proposal = proposal cs.Proposal = proposal
cs.ProposalBlockParts = NewPartSetFromHeader(proposal.BlockParts)
cs.ProposalPOLParts = NewPartSetFromHeader(proposal.POLParts)
cs.ProposalBlockParts = block.NewPartSetFromHeader(proposal.BlockParts)
cs.ProposalPOLParts = block.NewPartSetFromHeader(proposal.POLParts)
return nil return nil
} }
// NOTE: block is not necessarily valid. // NOTE: block is not necessarily valid.
// NOTE: This function may increment the height. // NOTE: This function may increment the height.
func (cs *ConsensusState) AddProposalBlockPart(height uint, round uint, part *Part) (added bool, err error) {
func (cs *ConsensusState) AddProposalBlockPart(height uint, round uint, part *block.Part) (added bool, err error) {
cs.mtx.Lock() cs.mtx.Lock()
defer cs.mtx.Unlock() defer cs.mtx.Unlock()
@ -922,7 +922,7 @@ func (cs *ConsensusState) AddProposalBlockPart(height uint, round uint, part *Pa
if added && cs.ProposalBlockParts.IsComplete() { if added && cs.ProposalBlockParts.IsComplete() {
var n int64 var n int64
var err error var err error
cs.ProposalBlock = ReadBinary(&Block{}, cs.ProposalBlockParts.GetReader(), &n, &err).(*Block)
cs.ProposalBlock = binary.ReadBinary(&block.Block{}, cs.ProposalBlockParts.GetReader(), &n, &err).(*block.Block)
// If we're already in the commit step, try to finalize round. // If we're already in the commit step, try to finalize round.
if cs.Step == RoundStepCommit { if cs.Step == RoundStepCommit {
cs.queueAction(RoundAction{cs.Height, cs.Round, RoundActionTryFinalize}) cs.queueAction(RoundAction{cs.Height, cs.Round, RoundActionTryFinalize})
@ -934,7 +934,7 @@ func (cs *ConsensusState) AddProposalBlockPart(height uint, round uint, part *Pa
} }
// NOTE: POL is not necessarily valid. // NOTE: POL is not necessarily valid.
func (cs *ConsensusState) AddProposalPOLPart(height uint, round uint, part *Part) (added bool, err error) {
func (cs *ConsensusState) AddProposalPOLPart(height uint, round uint, part *block.Part) (added bool, err error) {
cs.mtx.Lock() cs.mtx.Lock()
defer cs.mtx.Unlock() defer cs.mtx.Unlock()
@ -954,13 +954,13 @@ func (cs *ConsensusState) AddProposalPOLPart(height uint, round uint, part *Part
if added && cs.ProposalPOLParts.IsComplete() { if added && cs.ProposalPOLParts.IsComplete() {
var n int64 var n int64
var err error var err error
cs.ProposalPOL = ReadBinary(&POL{}, cs.ProposalPOLParts.GetReader(), &n, &err).(*POL)
cs.ProposalPOL = binary.ReadBinary(&POL{}, cs.ProposalPOLParts.GetReader(), &n, &err).(*POL)
return true, err return true, err
} }
return true, nil return true, nil
} }
func (cs *ConsensusState) AddVote(address []byte, vote *Vote) (added bool, index uint, err error) {
func (cs *ConsensusState) AddVote(address []byte, vote *block.Vote) (added bool, index uint, err error) {
cs.mtx.Lock() cs.mtx.Lock()
defer cs.mtx.Unlock() defer cs.mtx.Unlock()
@ -969,15 +969,15 @@ func (cs *ConsensusState) AddVote(address []byte, vote *Vote) (added bool, index
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
func (cs *ConsensusState) addVote(address []byte, vote *Vote) (added bool, index uint, err error) {
func (cs *ConsensusState) addVote(address []byte, vote *block.Vote) (added bool, index uint, err error) {
switch vote.Type { switch vote.Type {
case VoteTypePrevote:
case block.VoteTypePrevote:
// Prevotes checks for height+round match. // Prevotes checks for height+round match.
return cs.Prevotes.Add(address, vote) return cs.Prevotes.Add(address, vote)
case VoteTypePrecommit:
case block.VoteTypePrecommit:
// Precommits checks for height+round match. // Precommits checks for height+round match.
return cs.Precommits.Add(address, vote) return cs.Precommits.Add(address, vote)
case VoteTypeCommit:
case block.VoteTypeCommit:
if vote.Height == cs.Height { if vote.Height == cs.Height {
// No need to check if vote.Round < cs.Round ... // No need to check if vote.Round < cs.Round ...
// Prevotes && Precommits already checks that. // Prevotes && Precommits already checks that.
@ -1004,13 +1004,13 @@ func (cs *ConsensusState) addVote(address []byte, vote *Vote) (added bool, index
} }
} }
func (cs *ConsensusState) stageBlock(block *Block, blockParts *PartSet) error {
if block == nil {
func (cs *ConsensusState) stageBlock(block_ *block.Block, blockParts *block.PartSet) error {
if block_ == nil {
panic("Cannot stage nil block") panic("Cannot stage nil block")
} }
// Already staged? // Already staged?
if cs.stagedBlock == block {
if cs.stagedBlock == block_ {
return nil return nil
} }
@ -1019,21 +1019,21 @@ func (cs *ConsensusState) stageBlock(block *Block, blockParts *PartSet) error {
// Commit block onto the copied state. // Commit block onto the copied state.
// NOTE: Basic validation is done in state.AppendBlock(). // NOTE: Basic validation is done in state.AppendBlock().
err := stateCopy.AppendBlock(block, blockParts.Header(), true)
err := stateCopy.AppendBlock(block_, blockParts.Header(), true)
if err != nil { if err != nil {
return err return err
} else { } else {
cs.stagedBlock = block
cs.stagedBlock = block_
cs.stagedState = stateCopy cs.stagedState = stateCopy
return nil return nil
} }
} }
func (cs *ConsensusState) signAddVote(type_ byte, hash []byte, header PartSetHeader) *Vote {
func (cs *ConsensusState) signAddVote(type_ byte, hash []byte, header block.PartSetHeader) *block.Vote {
if cs.PrivValidator == nil || !cs.Validators.HasAddress(cs.PrivValidator.Address) { if cs.PrivValidator == nil || !cs.Validators.HasAddress(cs.PrivValidator.Address) {
return nil return nil
} }
vote := &Vote{
vote := &block.Vote{
Height: cs.Height, Height: cs.Height,
Round: cs.Round, Round: cs.Round,
Type: type_, Type: type_,
@ -1052,51 +1052,51 @@ func (cs *ConsensusState) signAddVote(type_ byte, hash []byte, header PartSetHea
} }
// sign a Commit-Vote // sign a Commit-Vote
func (cs *ConsensusState) commitVoteBlock(block *Block, blockParts *PartSet) {
func (cs *ConsensusState) commitVoteBlock(block_ *block.Block, blockParts *block.PartSet) {
// The proposal must be valid. // The proposal must be valid.
if err := cs.stageBlock(block, blockParts); err != nil {
if err := cs.stageBlock(block_, blockParts); err != nil {
// Prevent zombies. // Prevent zombies.
log.Warn("commitVoteBlock() an invalid block", "error", err) log.Warn("commitVoteBlock() an invalid block", "error", err)
return return
} }
// Commit-vote. // Commit-vote.
if cs.lastCommitVoteHeight < block.Height {
cs.signAddVote(VoteTypeCommit, block.Hash(), blockParts.Header())
cs.lastCommitVoteHeight = block.Height
if cs.lastCommitVoteHeight < block_.Height {
cs.signAddVote(block.VoteTypeCommit, block_.Hash(), blockParts.Header())
cs.lastCommitVoteHeight = block_.Height
} else { } else {
log.Error("Duplicate commitVoteBlock() attempt", "lastCommitVoteHeight", cs.lastCommitVoteHeight, "block.Height", block.Height)
log.Error("Duplicate commitVoteBlock() attempt", "lastCommitVoteHeight", cs.lastCommitVoteHeight, "block.Height", block_.Height)
} }
} }
// Save Block, save the +2/3 Commits we've seen, // Save Block, save the +2/3 Commits we've seen,
// and sign a Commit-Vote if we haven't already // and sign a Commit-Vote if we haven't already
func (cs *ConsensusState) saveCommitVoteBlock(block *Block, blockParts *PartSet, commits *VoteSet) {
func (cs *ConsensusState) saveCommitVoteBlock(block_ *block.Block, blockParts *block.PartSet, commits *VoteSet) {
// The proposal must be valid. // The proposal must be valid.
if err := cs.stageBlock(block, blockParts); err != nil {
if err := cs.stageBlock(block_, blockParts); err != nil {
// Prevent zombies. // Prevent zombies.
log.Warn("saveCommitVoteBlock() an invalid block", "error", err) log.Warn("saveCommitVoteBlock() an invalid block", "error", err)
return return
} }
// Save to blockStore. // Save to blockStore.
if cs.blockStore.Height() < block.Height {
if cs.blockStore.Height() < block_.Height {
seenValidation := commits.MakeValidation() seenValidation := commits.MakeValidation()
cs.blockStore.SaveBlock(block, blockParts, seenValidation)
cs.blockStore.SaveBlock(block_, blockParts, seenValidation)
} }
// Save the state. // Save the state.
cs.stagedState.Save() cs.stagedState.Save()
// Update mempool. // Update mempool.
cs.mempoolReactor.Mempool.ResetForBlockAndState(block, cs.stagedState)
cs.mempoolReactor.Mempool.ResetForBlockAndState(block_, cs.stagedState)
// Commit-vote if we haven't already. // Commit-vote if we haven't already.
if cs.lastCommitVoteHeight < block.Height {
cs.signAddVote(VoteTypeCommit, block.Hash(), blockParts.Header())
cs.lastCommitVoteHeight = block.Height
if cs.lastCommitVoteHeight < block_.Height {
cs.signAddVote(block.VoteTypeCommit, block_.Hash(), blockParts.Header())
cs.lastCommitVoteHeight = block_.Height
} }
} }


+ 15
- 15
consensus/state_test.go View File

@ -4,7 +4,7 @@ import (
"bytes" "bytes"
"testing" "testing"
. "github.com/tendermint/tendermint/block"
"github.com/tendermint/tendermint/block"
) )
func TestSetupRound(t *testing.T) { func TestSetupRound(t *testing.T) {
@ -12,9 +12,9 @@ func TestSetupRound(t *testing.T) {
val0 := privValidators[0] val0 := privValidators[0]
// Add a vote, precommit, and commit by val0. // Add a vote, precommit, and commit by val0.
voteTypes := []byte{VoteTypePrevote, VoteTypePrecommit, VoteTypeCommit}
voteTypes := []byte{block.VoteTypePrevote, block.VoteTypePrecommit, block.VoteTypeCommit}
for _, voteType := range voteTypes { for _, voteType := range voteTypes {
vote := &Vote{Height: 1, Round: 0, Type: voteType} // nil vote
vote := &block.Vote{Height: 1, Round: 0, Type: voteType} // nil vote
err := val0.SignVote(vote) err := val0.SignVote(vote)
if err != nil { if err != nil {
t.Error("Error signing vote: %v", err) t.Error("Error signing vote: %v", err)
@ -24,13 +24,13 @@ func TestSetupRound(t *testing.T) {
// Ensure that vote appears in RoundState. // Ensure that vote appears in RoundState.
rs0 := cs.GetRoundState() rs0 := cs.GetRoundState()
if vote := rs0.Prevotes.GetByAddress(val0.Address); vote == nil || vote.Type != VoteTypePrevote {
if vote := rs0.Prevotes.GetByAddress(val0.Address); vote == nil || vote.Type != block.VoteTypePrevote {
t.Errorf("Expected to find prevote but got %v", vote) t.Errorf("Expected to find prevote but got %v", vote)
} }
if vote := rs0.Precommits.GetByAddress(val0.Address); vote == nil || vote.Type != VoteTypePrecommit {
if vote := rs0.Precommits.GetByAddress(val0.Address); vote == nil || vote.Type != block.VoteTypePrecommit {
t.Errorf("Expected to find precommit but got %v", vote) t.Errorf("Expected to find precommit but got %v", vote)
} }
if vote := rs0.Commits.GetByAddress(val0.Address); vote == nil || vote.Type != VoteTypeCommit {
if vote := rs0.Commits.GetByAddress(val0.Address); vote == nil || vote.Type != block.VoteTypeCommit {
t.Errorf("Expected to find commit but got %v", vote) t.Errorf("Expected to find commit but got %v", vote)
} }
@ -40,13 +40,13 @@ func TestSetupRound(t *testing.T) {
// Now the commit should be copied over to prevotes and precommits. // Now the commit should be copied over to prevotes and precommits.
rs1 := cs.GetRoundState() rs1 := cs.GetRoundState()
if vote := rs1.Prevotes.GetByAddress(val0.Address); vote == nil || vote.Type != VoteTypeCommit {
if vote := rs1.Prevotes.GetByAddress(val0.Address); vote == nil || vote.Type != block.VoteTypeCommit {
t.Errorf("Expected to find commit but got %v", vote) t.Errorf("Expected to find commit but got %v", vote)
} }
if vote := rs1.Precommits.GetByAddress(val0.Address); vote == nil || vote.Type != VoteTypeCommit {
if vote := rs1.Precommits.GetByAddress(val0.Address); vote == nil || vote.Type != block.VoteTypeCommit {
t.Errorf("Expected to find commit but got %v", vote) t.Errorf("Expected to find commit but got %v", vote)
} }
if vote := rs1.Commits.GetByAddress(val0.Address); vote == nil || vote.Type != VoteTypeCommit {
if vote := rs1.Commits.GetByAddress(val0.Address); vote == nil || vote.Type != block.VoteTypeCommit {
t.Errorf("Expected to find commit but got %v", vote) t.Errorf("Expected to find commit but got %v", vote)
} }
@ -116,10 +116,10 @@ func TestRunActionPrecommitCommitFinalize(t *testing.T) {
// Add at least +2/3 prevotes. // Add at least +2/3 prevotes.
for i := 0; i < 7; i++ { for i := 0; i < 7; i++ {
vote := &Vote{
vote := &block.Vote{
Height: 1, Height: 1,
Round: 0, Round: 0,
Type: VoteTypePrevote,
Type: block.VoteTypePrevote,
BlockHash: cs.ProposalBlock.Hash(), BlockHash: cs.ProposalBlock.Hash(),
BlockParts: cs.ProposalBlockParts.Header(), BlockParts: cs.ProposalBlockParts.Header(),
} }
@ -146,10 +146,10 @@ func TestRunActionPrecommitCommitFinalize(t *testing.T) {
} }
continue continue
} }
vote := &Vote{
vote := &block.Vote{
Height: 1, Height: 1,
Round: 0, Round: 0,
Type: VoteTypePrecommit,
Type: block.VoteTypePrecommit,
BlockHash: cs.ProposalBlock.Hash(), BlockHash: cs.ProposalBlock.Hash(),
BlockParts: cs.ProposalBlockParts.Header(), BlockParts: cs.ProposalBlockParts.Header(),
} }
@ -184,10 +184,10 @@ func TestRunActionPrecommitCommitFinalize(t *testing.T) {
} }
continue continue
} }
vote := &Vote{
vote := &block.Vote{
Height: 1, Height: 1,
Round: uint(i), // Doesn't matter what round Round: uint(i), // Doesn't matter what round
Type: VoteTypeCommit,
Type: block.VoteTypeCommit,
BlockHash: cs.ProposalBlock.Hash(), BlockHash: cs.ProposalBlock.Hash(),
BlockParts: cs.ProposalBlockParts.Header(), BlockParts: cs.ProposalBlockParts.Header(),
} }


+ 17
- 17
consensus/test.go View File

@ -3,16 +3,16 @@ package consensus
import ( import (
"sort" "sort"
. "github.com/tendermint/tendermint/block"
db_ "github.com/tendermint/tendermint/db"
mempool_ "github.com/tendermint/tendermint/mempool"
"github.com/tendermint/tendermint/state"
"github.com/tendermint/tendermint/block"
dbm "github.com/tendermint/tendermint/db"
mempl "github.com/tendermint/tendermint/mempool"
sm "github.com/tendermint/tendermint/state"
) )
// Common test methods // Common test methods
func makeValidator(valInfo *state.ValidatorInfo) *state.Validator {
return &state.Validator{
func makeValidator(valInfo *sm.ValidatorInfo) *sm.Validator {
return &sm.Validator{
Address: valInfo.Address, Address: valInfo.Address,
PubKey: valInfo.PubKey, PubKey: valInfo.PubKey,
BondHeight: 0, BondHeight: 0,
@ -23,25 +23,25 @@ func makeValidator(valInfo *state.ValidatorInfo) *state.Validator {
} }
} }
func randVoteSet(height uint, round uint, type_ byte, numValidators int, votingPower uint64) (*VoteSet, *state.ValidatorSet, []*state.PrivValidator) {
vals := make([]*state.Validator, numValidators)
privValidators := make([]*state.PrivValidator, numValidators)
func randVoteSet(height uint, round uint, type_ byte, numValidators int, votingPower uint64) (*VoteSet, *sm.ValidatorSet, []*sm.PrivValidator) {
vals := make([]*sm.Validator, numValidators)
privValidators := make([]*sm.PrivValidator, numValidators)
for i := 0; i < numValidators; i++ { for i := 0; i < numValidators; i++ {
valInfo, privValidator := state.RandValidator(false, votingPower)
valInfo, privValidator := sm.RandValidator(false, votingPower)
val := makeValidator(valInfo) val := makeValidator(valInfo)
vals[i] = val vals[i] = val
privValidators[i] = privValidator privValidators[i] = privValidator
} }
valSet := state.NewValidatorSet(vals)
sort.Sort(state.PrivValidatorsByAddress(privValidators))
valSet := sm.NewValidatorSet(vals)
sort.Sort(sm.PrivValidatorsByAddress(privValidators))
return NewVoteSet(height, round, type_, valSet), valSet, privValidators return NewVoteSet(height, round, type_, valSet), valSet, privValidators
} }
func randConsensusState() (*ConsensusState, []*state.PrivValidator) {
state, _, privValidators := state.RandGenesisState(20, false, 1000, 10, false, 1000)
blockStore := NewBlockStore(db_.NewMemDB())
mempool := mempool_.NewMempool(state)
mempoolReactor := mempool_.NewMempoolReactor(mempool)
func randConsensusState() (*ConsensusState, []*sm.PrivValidator) {
state, _, privValidators := sm.RandGenesisState(20, false, 1000, 10, false, 1000)
blockStore := block.NewBlockStore(dbm.NewMemDB())
mempool := mempl.NewMempool(state)
mempoolReactor := mempl.NewMempoolReactor(mempool)
cs := NewConsensusState(state, blockStore, mempoolReactor) cs := NewConsensusState(state, blockStore, mempoolReactor)
return cs, privValidators return cs, privValidators
} }

+ 7
- 7
consensus/types/proposal.go View File

@ -5,8 +5,8 @@ import (
"fmt" "fmt"
"io" "io"
. "github.com/tendermint/tendermint/account"
. "github.com/tendermint/tendermint/binary"
"github.com/tendermint/tendermint/account"
"github.com/tendermint/tendermint/binary"
. "github.com/tendermint/tendermint/block" . "github.com/tendermint/tendermint/block"
) )
@ -20,7 +20,7 @@ type Proposal struct {
Round uint Round uint
BlockParts PartSetHeader BlockParts PartSetHeader
POLParts PartSetHeader POLParts PartSetHeader
Signature SignatureEd25519
Signature account.SignatureEd25519
} }
func NewProposal(height uint, round uint, blockParts, polParts PartSetHeader) *Proposal { func NewProposal(height uint, round uint, blockParts, polParts PartSetHeader) *Proposal {
@ -38,8 +38,8 @@ func (p *Proposal) String() string {
} }
func (p *Proposal) WriteSignBytes(w io.Writer, n *int64, err *error) { func (p *Proposal) WriteSignBytes(w io.Writer, n *int64, err *error) {
WriteUvarint(p.Height, w, n, err)
WriteUvarint(p.Round, w, n, err)
WriteBinary(p.BlockParts, w, n, err)
WriteBinary(p.POLParts, w, n, err)
binary.WriteUvarint(p.Height, w, n, err)
binary.WriteUvarint(p.Round, w, n, err)
binary.WriteBinary(p.BlockParts, w, n, err)
binary.WriteBinary(p.POLParts, w, n, err)
} }

+ 35
- 34
consensus/vote_set.go View File

@ -6,10 +6,11 @@ import (
"strings" "strings"
"sync" "sync"
. "github.com/tendermint/tendermint/account"
. "github.com/tendermint/tendermint/binary"
. "github.com/tendermint/tendermint/block"
"github.com/tendermint/tendermint/state"
"github.com/tendermint/tendermint/account"
"github.com/tendermint/tendermint/binary"
"github.com/tendermint/tendermint/block"
. "github.com/tendermint/tendermint/common"
sm "github.com/tendermint/tendermint/state"
) )
// VoteSet helps collect signatures from validators at each height+round // VoteSet helps collect signatures from validators at each height+round
@ -23,22 +24,22 @@ type VoteSet struct {
type_ byte type_ byte
mtx sync.Mutex mtx sync.Mutex
valSet *state.ValidatorSet
votes []*Vote // validator index -> vote
valSet *sm.ValidatorSet
votes []*block.Vote // validator index -> vote
votesBitArray BitArray // validator index -> has vote? votesBitArray BitArray // validator index -> has vote?
votesByBlock map[string]uint64 // string(blockHash)+string(blockParts) -> vote sum. votesByBlock map[string]uint64 // string(blockHash)+string(blockParts) -> vote sum.
totalVotes uint64 totalVotes uint64
maj23Hash []byte maj23Hash []byte
maj23Parts PartSetHeader
maj23Parts block.PartSetHeader
maj23Exists bool maj23Exists bool
} }
// Constructs a new VoteSet struct used to accumulate votes for each round. // Constructs a new VoteSet struct used to accumulate votes for each round.
func NewVoteSet(height uint, round uint, type_ byte, valSet *state.ValidatorSet) *VoteSet {
func NewVoteSet(height uint, round uint, type_ byte, valSet *sm.ValidatorSet) *VoteSet {
if height == 0 { if height == 0 {
panic("Cannot make VoteSet for height == 0, doesn't make sense.") panic("Cannot make VoteSet for height == 0, doesn't make sense.")
} }
if type_ == VoteTypeCommit && round != 0 {
if type_ == block.VoteTypeCommit && round != 0 {
panic("Expected round 0 for commit vote set") panic("Expected round 0 for commit vote set")
} }
return &VoteSet{ return &VoteSet{
@ -46,7 +47,7 @@ func NewVoteSet(height uint, round uint, type_ byte, valSet *state.ValidatorSet)
round: round, round: round,
type_: type_, type_: type_,
valSet: valSet, valSet: valSet,
votes: make([]*Vote, valSet.Size()),
votes: make([]*block.Vote, valSet.Size()),
votesBitArray: NewBitArray(valSet.Size()), votesBitArray: NewBitArray(valSet.Size()),
votesByBlock: make(map[string]uint64), votesByBlock: make(map[string]uint64),
totalVotes: 0, totalVotes: 0,
@ -64,40 +65,40 @@ func (voteSet *VoteSet) Size() uint {
// True if added, false if not. // True if added, false if not.
// Returns ErrVote[UnexpectedStep|InvalidAccount|InvalidSignature|InvalidBlockHash|ConflictingSignature] // Returns ErrVote[UnexpectedStep|InvalidAccount|InvalidSignature|InvalidBlockHash|ConflictingSignature]
// NOTE: vote should not be mutated after adding. // NOTE: vote should not be mutated after adding.
func (voteSet *VoteSet) Add(address []byte, vote *Vote) (bool, uint, error) {
func (voteSet *VoteSet) Add(address []byte, vote *block.Vote) (bool, uint, error) {
voteSet.mtx.Lock() voteSet.mtx.Lock()
defer voteSet.mtx.Unlock() defer voteSet.mtx.Unlock()
// Make sure the step matches. (or that vote is commit && round < voteSet.round) // Make sure the step matches. (or that vote is commit && round < voteSet.round)
if vote.Height != voteSet.height || if vote.Height != voteSet.height ||
(vote.Type != VoteTypeCommit && vote.Round != voteSet.round) ||
(vote.Type != VoteTypeCommit && vote.Type != voteSet.type_) ||
(vote.Type == VoteTypeCommit && voteSet.type_ != VoteTypeCommit && vote.Round >= voteSet.round) {
return false, 0, ErrVoteUnexpectedStep
(vote.Type != block.VoteTypeCommit && vote.Round != voteSet.round) ||
(vote.Type != block.VoteTypeCommit && vote.Type != voteSet.type_) ||
(vote.Type == block.VoteTypeCommit && voteSet.type_ != block.VoteTypeCommit && vote.Round >= voteSet.round) {
return false, 0, block.ErrVoteUnexpectedStep
} }
// Ensure that signer is a validator. // Ensure that signer is a validator.
valIndex, val := voteSet.valSet.GetByAddress(address) valIndex, val := voteSet.valSet.GetByAddress(address)
if val == nil { if val == nil {
return false, 0, ErrVoteInvalidAccount
return false, 0, block.ErrVoteInvalidAccount
} }
// Check signature. // Check signature.
if !val.PubKey.VerifyBytes(SignBytes(vote), vote.Signature) {
if !val.PubKey.VerifyBytes(account.SignBytes(vote), vote.Signature) {
// Bad signature. // Bad signature.
return false, 0, ErrVoteInvalidSignature
return false, 0, block.ErrVoteInvalidSignature
} }
return voteSet.addVote(valIndex, vote) return voteSet.addVote(valIndex, vote)
} }
func (voteSet *VoteSet) addVote(valIndex uint, vote *Vote) (bool, uint, error) {
func (voteSet *VoteSet) addVote(valIndex uint, vote *block.Vote) (bool, uint, error) {
// If vote already exists, return false. // If vote already exists, return false.
if existingVote := voteSet.votes[valIndex]; existingVote != nil { if existingVote := voteSet.votes[valIndex]; existingVote != nil {
if bytes.Equal(existingVote.BlockHash, vote.BlockHash) { if bytes.Equal(existingVote.BlockHash, vote.BlockHash) {
return false, 0, nil return false, 0, nil
} else { } else {
return false, 0, ErrVoteConflictingSignature
return false, 0, block.ErrVoteConflictingSignature
} }
} }
@ -108,7 +109,7 @@ func (voteSet *VoteSet) addVote(valIndex uint, vote *Vote) (bool, uint, error) {
} }
voteSet.votes[valIndex] = vote voteSet.votes[valIndex] = vote
voteSet.votesBitArray.SetIndex(valIndex, true) voteSet.votesBitArray.SetIndex(valIndex, true)
blockKey := string(vote.BlockHash) + string(BinaryBytes(vote.BlockParts))
blockKey := string(vote.BlockHash) + string(binary.BinaryBytes(vote.BlockParts))
totalBlockHashVotes := voteSet.votesByBlock[blockKey] + val.VotingPower totalBlockHashVotes := voteSet.votesByBlock[blockKey] + val.VotingPower
voteSet.votesByBlock[blockKey] = totalBlockHashVotes voteSet.votesByBlock[blockKey] = totalBlockHashVotes
voteSet.totalVotes += val.VotingPower voteSet.totalVotes += val.VotingPower
@ -145,13 +146,13 @@ func (voteSet *VoteSet) BitArray() BitArray {
return voteSet.votesBitArray.Copy() return voteSet.votesBitArray.Copy()
} }
func (voteSet *VoteSet) GetByIndex(valIndex uint) *Vote {
func (voteSet *VoteSet) GetByIndex(valIndex uint) *block.Vote {
voteSet.mtx.Lock() voteSet.mtx.Lock()
defer voteSet.mtx.Unlock() defer voteSet.mtx.Unlock()
return voteSet.votes[valIndex] return voteSet.votes[valIndex]
} }
func (voteSet *VoteSet) GetByAddress(address []byte) *Vote {
func (voteSet *VoteSet) GetByAddress(address []byte) *block.Vote {
voteSet.mtx.Lock() voteSet.mtx.Lock()
defer voteSet.mtx.Unlock() defer voteSet.mtx.Unlock()
valIndex, val := voteSet.valSet.GetByAddress(address) valIndex, val := voteSet.valSet.GetByAddress(address)
@ -172,19 +173,19 @@ func (voteSet *VoteSet) HasTwoThirdsMajority() bool {
// Returns either a blockhash (or nil) that received +2/3 majority. // Returns either a blockhash (or nil) that received +2/3 majority.
// If there exists no such majority, returns (nil, false). // If there exists no such majority, returns (nil, false).
func (voteSet *VoteSet) TwoThirdsMajority() (hash []byte, parts PartSetHeader, ok bool) {
func (voteSet *VoteSet) TwoThirdsMajority() (hash []byte, parts block.PartSetHeader, ok bool) {
voteSet.mtx.Lock() voteSet.mtx.Lock()
defer voteSet.mtx.Unlock() defer voteSet.mtx.Unlock()
if voteSet.maj23Exists { if voteSet.maj23Exists {
return voteSet.maj23Hash, voteSet.maj23Parts, true return voteSet.maj23Hash, voteSet.maj23Parts, true
} else { } else {
return nil, PartSetHeader{}, false
return nil, block.PartSetHeader{}, false
} }
} }
func (voteSet *VoteSet) MakePOL() *POL { func (voteSet *VoteSet) MakePOL() *POL {
if voteSet.type_ != VoteTypePrevote {
panic("Cannot MakePOL() unless VoteSet.Type is VoteTypePrevote")
if voteSet.type_ != block.VoteTypePrevote {
panic("Cannot MakePOL() unless VoteSet.Type is block.VoteTypePrevote")
} }
voteSet.mtx.Lock() voteSet.mtx.Lock()
defer voteSet.mtx.Unlock() defer voteSet.mtx.Unlock()
@ -216,17 +217,17 @@ func (voteSet *VoteSet) MakePOL() *POL {
return pol return pol
} }
func (voteSet *VoteSet) MakeValidation() *Validation {
if voteSet.type_ != VoteTypeCommit {
panic("Cannot MakeValidation() unless VoteSet.Type is VoteTypeCommit")
func (voteSet *VoteSet) MakeValidation() *block.Validation {
if voteSet.type_ != block.VoteTypeCommit {
panic("Cannot MakeValidation() unless VoteSet.Type is block.VoteTypeCommit")
} }
voteSet.mtx.Lock() voteSet.mtx.Lock()
defer voteSet.mtx.Unlock() defer voteSet.mtx.Unlock()
if len(voteSet.maj23Hash) == 0 { if len(voteSet.maj23Hash) == 0 {
panic("Cannot MakeValidation() unless a blockhash has +2/3") panic("Cannot MakeValidation() unless a blockhash has +2/3")
} }
commits := make([]Commit, voteSet.valSet.Size())
voteSet.valSet.Iterate(func(valIndex uint, val *state.Validator) bool {
commits := make([]block.Commit, voteSet.valSet.Size())
voteSet.valSet.Iterate(func(valIndex uint, val *sm.Validator) bool {
vote := voteSet.votes[valIndex] vote := voteSet.votes[valIndex]
if vote == nil { if vote == nil {
return false return false
@ -237,10 +238,10 @@ func (voteSet *VoteSet) MakeValidation() *Validation {
if !vote.BlockParts.Equals(voteSet.maj23Parts) { if !vote.BlockParts.Equals(voteSet.maj23Parts) {
return false return false
} }
commits[valIndex] = Commit{val.Address, vote.Round, vote.Signature}
commits[valIndex] = block.Commit{val.Address, vote.Round, vote.Signature}
return false return false
}) })
return &Validation{
return &block.Validation{
Commits: commits, Commits: commits,
} }
} }


+ 31
- 31
consensus/vote_set_test.go View File

@ -3,10 +3,10 @@ package consensus
import ( import (
"bytes" "bytes"
. "github.com/tendermint/tendermint/block"
"github.com/tendermint/tendermint/block"
. "github.com/tendermint/tendermint/common" . "github.com/tendermint/tendermint/common"
. "github.com/tendermint/tendermint/common/test" . "github.com/tendermint/tendermint/common/test"
"github.com/tendermint/tendermint/state"
sm "github.com/tendermint/tendermint/state"
"testing" "testing"
) )
@ -14,41 +14,41 @@ import (
// NOTE: see consensus/test.go for common test methods. // NOTE: see consensus/test.go for common test methods.
// Convenience: Return new vote with different height // Convenience: Return new vote with different height
func withHeight(vote *Vote, height uint) *Vote {
func withHeight(vote *block.Vote, height uint) *block.Vote {
vote = vote.Copy() vote = vote.Copy()
vote.Height = height vote.Height = height
return vote return vote
} }
// Convenience: Return new vote with different round // Convenience: Return new vote with different round
func withRound(vote *Vote, round uint) *Vote {
func withRound(vote *block.Vote, round uint) *block.Vote {
vote = vote.Copy() vote = vote.Copy()
vote.Round = round vote.Round = round
return vote return vote
} }
// Convenience: Return new vote with different type // Convenience: Return new vote with different type
func withType(vote *Vote, type_ byte) *Vote {
func withType(vote *block.Vote, type_ byte) *block.Vote {
vote = vote.Copy() vote = vote.Copy()
vote.Type = type_ vote.Type = type_
return vote return vote
} }
// Convenience: Return new vote with different blockHash // Convenience: Return new vote with different blockHash
func withBlockHash(vote *Vote, blockHash []byte) *Vote {
func withBlockHash(vote *block.Vote, blockHash []byte) *block.Vote {
vote = vote.Copy() vote = vote.Copy()
vote.BlockHash = blockHash vote.BlockHash = blockHash
return vote return vote
} }
// Convenience: Return new vote with different blockParts // Convenience: Return new vote with different blockParts
func withBlockParts(vote *Vote, blockParts PartSetHeader) *Vote {
func withBlockParts(vote *block.Vote, blockParts block.PartSetHeader) *block.Vote {
vote = vote.Copy() vote = vote.Copy()
vote.BlockParts = blockParts vote.BlockParts = blockParts
return vote return vote
} }
func signAddVote(privVal *state.PrivValidator, vote *Vote, voteSet *VoteSet) (bool, error) {
func signAddVote(privVal *sm.PrivValidator, vote *block.Vote, voteSet *VoteSet) (bool, error) {
privVal.SignVoteUnsafe(vote) privVal.SignVoteUnsafe(vote)
added, _, err := voteSet.Add(privVal.Address, vote) added, _, err := voteSet.Add(privVal.Address, vote)
return added, err return added, err
@ -56,7 +56,7 @@ func signAddVote(privVal *state.PrivValidator, vote *Vote, voteSet *VoteSet) (bo
func TestAddVote(t *testing.T) { func TestAddVote(t *testing.T) {
height, round := uint(1), uint(0) height, round := uint(1), uint(0)
voteSet, _, privValidators := randVoteSet(height, round, VoteTypePrevote, 10, 1)
voteSet, _, privValidators := randVoteSet(height, round, block.VoteTypePrevote, 10, 1)
val0 := privValidators[0] val0 := privValidators[0]
// t.Logf(">> %v", voteSet) // t.Logf(">> %v", voteSet)
@ -72,7 +72,7 @@ func TestAddVote(t *testing.T) {
t.Errorf("There should be no 2/3 majority") t.Errorf("There should be no 2/3 majority")
} }
vote := &Vote{Height: height, Round: round, Type: VoteTypePrevote, BlockHash: nil}
vote := &block.Vote{Height: height, Round: round, Type: block.VoteTypePrevote, BlockHash: nil}
signAddVote(val0, vote, voteSet) signAddVote(val0, vote, voteSet)
if voteSet.GetByAddress(val0.Address) == nil { if voteSet.GetByAddress(val0.Address) == nil {
@ -89,9 +89,9 @@ func TestAddVote(t *testing.T) {
func Test2_3Majority(t *testing.T) { func Test2_3Majority(t *testing.T) {
height, round := uint(1), uint(0) height, round := uint(1), uint(0)
voteSet, _, privValidators := randVoteSet(height, round, VoteTypePrevote, 10, 1)
voteSet, _, privValidators := randVoteSet(height, round, block.VoteTypePrevote, 10, 1)
vote := &Vote{Height: height, Round: round, Type: VoteTypePrevote, BlockHash: nil}
vote := &block.Vote{Height: height, Round: round, Type: block.VoteTypePrevote, BlockHash: nil}
// 6 out of 10 voted for nil. // 6 out of 10 voted for nil.
for i := 0; i < 6; i++ { for i := 0; i < 6; i++ {
@ -123,13 +123,13 @@ func Test2_3Majority(t *testing.T) {
func Test2_3MajorityRedux(t *testing.T) { func Test2_3MajorityRedux(t *testing.T) {
height, round := uint(1), uint(0) height, round := uint(1), uint(0)
voteSet, _, privValidators := randVoteSet(height, round, VoteTypePrevote, 100, 1)
voteSet, _, privValidators := randVoteSet(height, round, block.VoteTypePrevote, 100, 1)
blockHash := CRandBytes(32) blockHash := CRandBytes(32)
blockPartsTotal := uint(123) blockPartsTotal := uint(123)
blockParts := PartSetHeader{blockPartsTotal, CRandBytes(32)}
blockParts := block.PartSetHeader{blockPartsTotal, CRandBytes(32)}
vote := &Vote{Height: height, Round: round, Type: VoteTypePrevote, BlockHash: blockHash, BlockParts: blockParts}
vote := &block.Vote{Height: height, Round: round, Type: block.VoteTypePrevote, BlockHash: blockHash, BlockParts: blockParts}
// 66 out of 100 voted for nil. // 66 out of 100 voted for nil.
for i := 0; i < 66; i++ { for i := 0; i < 66; i++ {
@ -151,7 +151,7 @@ func Test2_3MajorityRedux(t *testing.T) {
// 68th validator voted for a different BlockParts PartSetHeader // 68th validator voted for a different BlockParts PartSetHeader
{ {
blockParts := PartSetHeader{blockPartsTotal, CRandBytes(32)}
blockParts := block.PartSetHeader{blockPartsTotal, CRandBytes(32)}
signAddVote(privValidators[67], withBlockParts(vote, blockParts), voteSet) signAddVote(privValidators[67], withBlockParts(vote, blockParts), voteSet)
hash, header, ok = voteSet.TwoThirdsMajority() hash, header, ok = voteSet.TwoThirdsMajority()
if hash != nil || !header.IsZero() || ok { if hash != nil || !header.IsZero() || ok {
@ -161,7 +161,7 @@ func Test2_3MajorityRedux(t *testing.T) {
// 69th validator voted for different BlockParts Total // 69th validator voted for different BlockParts Total
{ {
blockParts := PartSetHeader{blockPartsTotal + 1, blockParts.Hash}
blockParts := block.PartSetHeader{blockPartsTotal + 1, blockParts.Hash}
signAddVote(privValidators[68], withBlockParts(vote, blockParts), voteSet) signAddVote(privValidators[68], withBlockParts(vote, blockParts), voteSet)
hash, header, ok = voteSet.TwoThirdsMajority() hash, header, ok = voteSet.TwoThirdsMajority()
if hash != nil || !header.IsZero() || ok { if hash != nil || !header.IsZero() || ok {
@ -190,10 +190,10 @@ func Test2_3MajorityRedux(t *testing.T) {
func TestBadVotes(t *testing.T) { func TestBadVotes(t *testing.T) {
height, round := uint(1), uint(0) height, round := uint(1), uint(0)
voteSet, _, privValidators := randVoteSet(height, round, VoteTypePrevote, 10, 1)
voteSet, _, privValidators := randVoteSet(height, round, block.VoteTypePrevote, 10, 1)
// val0 votes for nil. // val0 votes for nil.
vote := &Vote{Height: height, Round: round, Type: VoteTypePrevote, BlockHash: nil}
vote := &block.Vote{Height: height, Round: round, Type: block.VoteTypePrevote, BlockHash: nil}
added, err := signAddVote(privValidators[0], vote, voteSet) added, err := signAddVote(privValidators[0], vote, voteSet)
if !added || err != nil { if !added || err != nil {
t.Errorf("Expected Add() to succeed") t.Errorf("Expected Add() to succeed")
@ -218,7 +218,7 @@ func TestBadVotes(t *testing.T) {
} }
// val3 votes of another type. // val3 votes of another type.
added, err = signAddVote(privValidators[3], withType(vote, VoteTypePrecommit), voteSet)
added, err = signAddVote(privValidators[3], withType(vote, block.VoteTypePrecommit), voteSet)
if added { if added {
t.Errorf("Expected Add() to fail, wrong type") t.Errorf("Expected Add() to fail, wrong type")
} }
@ -226,10 +226,10 @@ func TestBadVotes(t *testing.T) {
func TestAddCommitsToPrevoteVotes(t *testing.T) { func TestAddCommitsToPrevoteVotes(t *testing.T) {
height, round := uint(2), uint(5) height, round := uint(2), uint(5)
voteSet, _, privValidators := randVoteSet(height, round, VoteTypePrevote, 10, 1)
voteSet, _, privValidators := randVoteSet(height, round, block.VoteTypePrevote, 10, 1)
// val0, val1, val2, val3, val4, val5 vote for nil. // val0, val1, val2, val3, val4, val5 vote for nil.
vote := &Vote{Height: height, Round: round, Type: VoteTypePrevote, BlockHash: nil}
vote := &block.Vote{Height: height, Round: round, Type: block.VoteTypePrevote, BlockHash: nil}
for i := 0; i < 6; i++ { for i := 0; i < 6; i++ {
signAddVote(privValidators[i], vote, voteSet) signAddVote(privValidators[i], vote, voteSet)
} }
@ -239,35 +239,35 @@ func TestAddCommitsToPrevoteVotes(t *testing.T) {
} }
// Attempt to add a commit from val6 at a previous height // Attempt to add a commit from val6 at a previous height
vote = &Vote{Height: height - 1, Round: round, Type: VoteTypeCommit, BlockHash: nil}
vote = &block.Vote{Height: height - 1, Round: round, Type: block.VoteTypeCommit, BlockHash: nil}
added, _ := signAddVote(privValidators[6], vote, voteSet) added, _ := signAddVote(privValidators[6], vote, voteSet)
if added { if added {
t.Errorf("Expected Add() to fail, wrong height.") t.Errorf("Expected Add() to fail, wrong height.")
} }
// Attempt to add a commit from val6 at a later round // Attempt to add a commit from val6 at a later round
vote = &Vote{Height: height, Round: round + 1, Type: VoteTypeCommit, BlockHash: nil}
vote = &block.Vote{Height: height, Round: round + 1, Type: block.VoteTypeCommit, BlockHash: nil}
added, _ = signAddVote(privValidators[6], vote, voteSet) added, _ = signAddVote(privValidators[6], vote, voteSet)
if added { if added {
t.Errorf("Expected Add() to fail, cannot add future round vote.") t.Errorf("Expected Add() to fail, cannot add future round vote.")
} }
// Attempt to add a commit from val6 for currrent height/round. // Attempt to add a commit from val6 for currrent height/round.
vote = &Vote{Height: height, Round: round, Type: VoteTypeCommit, BlockHash: nil}
vote = &block.Vote{Height: height, Round: round, Type: block.VoteTypeCommit, BlockHash: nil}
added, err := signAddVote(privValidators[6], vote, voteSet) added, err := signAddVote(privValidators[6], vote, voteSet)
if added || err == nil { if added || err == nil {
t.Errorf("Expected Add() to fail, only prior round commits can be added.") t.Errorf("Expected Add() to fail, only prior round commits can be added.")
} }
// Add commit from val6 at a previous round // Add commit from val6 at a previous round
vote = &Vote{Height: height, Round: round - 1, Type: VoteTypeCommit, BlockHash: nil}
vote = &block.Vote{Height: height, Round: round - 1, Type: block.VoteTypeCommit, BlockHash: nil}
added, err = signAddVote(privValidators[6], vote, voteSet) added, err = signAddVote(privValidators[6], vote, voteSet)
if !added || err != nil { if !added || err != nil {
t.Errorf("Expected Add() to succeed, commit for prior rounds are relevant.") t.Errorf("Expected Add() to succeed, commit for prior rounds are relevant.")
} }
// Also add commit from val7 for previous round. // Also add commit from val7 for previous round.
vote = &Vote{Height: height, Round: round - 2, Type: VoteTypeCommit, BlockHash: nil}
vote = &block.Vote{Height: height, Round: round - 2, Type: block.VoteTypeCommit, BlockHash: nil}
added, err = signAddVote(privValidators[7], vote, voteSet) added, err = signAddVote(privValidators[7], vote, voteSet)
if !added || err != nil { if !added || err != nil {
t.Errorf("Expected Add() to succeed. err: %v", err) t.Errorf("Expected Add() to succeed. err: %v", err)
@ -283,10 +283,10 @@ func TestAddCommitsToPrevoteVotes(t *testing.T) {
func TestMakeValidation(t *testing.T) { func TestMakeValidation(t *testing.T) {
height, round := uint(1), uint(0) height, round := uint(1), uint(0)
voteSet, _, privValidators := randVoteSet(height, round, VoteTypeCommit, 10, 1)
blockHash, blockParts := CRandBytes(32), PartSetHeader{123, CRandBytes(32)}
voteSet, _, privValidators := randVoteSet(height, round, block.VoteTypeCommit, 10, 1)
blockHash, blockParts := CRandBytes(32), block.PartSetHeader{123, CRandBytes(32)}
vote := &Vote{Height: height, Round: round, Type: VoteTypeCommit,
vote := &block.Vote{Height: height, Round: round, Type: block.VoteTypeCommit,
BlockHash: blockHash, BlockParts: blockParts} BlockHash: blockHash, BlockParts: blockParts}
// 6 out of 10 voted for some block. // 6 out of 10 voted for some block.
@ -300,7 +300,7 @@ func TestMakeValidation(t *testing.T) {
// 7th voted for some other block. // 7th voted for some other block.
{ {
vote := withBlockHash(vote, RandBytes(32)) vote := withBlockHash(vote, RandBytes(32))
vote = withBlockParts(vote, PartSetHeader{123, RandBytes(32)})
vote = withBlockParts(vote, block.PartSetHeader{123, RandBytes(32)})
signAddVote(privValidators[6], vote, voteSet) signAddVote(privValidators[6], vote, voteSet)
} }


+ 14
- 14
mempool/mempool.go View File

@ -11,25 +11,25 @@ package mempool
import ( import (
"sync" "sync"
. "github.com/tendermint/tendermint/binary"
. "github.com/tendermint/tendermint/block"
"github.com/tendermint/tendermint/state"
"github.com/tendermint/tendermint/binary"
"github.com/tendermint/tendermint/block"
sm "github.com/tendermint/tendermint/state"
) )
type Mempool struct { type Mempool struct {
mtx sync.Mutex mtx sync.Mutex
state *state.State
txs []Tx
state *sm.State
txs []block.Tx
} }
func NewMempool(state *state.State) *Mempool {
func NewMempool(state *sm.State) *Mempool {
return &Mempool{ return &Mempool{
state: state, state: state,
} }
} }
// Apply tx to the state and remember it. // Apply tx to the state and remember it.
func (mem *Mempool) AddTx(tx Tx) (err error) {
func (mem *Mempool) AddTx(tx block.Tx) (err error) {
mem.mtx.Lock() mem.mtx.Lock()
defer mem.mtx.Unlock() defer mem.mtx.Unlock()
err = mem.state.ExecTx(tx) err = mem.state.ExecTx(tx)
@ -43,7 +43,7 @@ func (mem *Mempool) AddTx(tx Tx) (err error) {
} }
} }
func (mem *Mempool) GetProposalTxs() []Tx {
func (mem *Mempool) GetProposalTxs() []block.Tx {
mem.mtx.Lock() mem.mtx.Lock()
defer mem.mtx.Unlock() defer mem.mtx.Unlock()
log.Debug("GetProposalTxs:", "txs", mem.txs) log.Debug("GetProposalTxs:", "txs", mem.txs)
@ -54,22 +54,22 @@ func (mem *Mempool) GetProposalTxs() []Tx {
// "state" is the result of state.AppendBlock("block"). // "state" is the result of state.AppendBlock("block").
// Txs that are present in "block" are discarded from mempool. // Txs that are present in "block" are discarded from mempool.
// Txs that have become invalid in the new "state" are also discarded. // Txs that have become invalid in the new "state" are also discarded.
func (mem *Mempool) ResetForBlockAndState(block *Block, state *state.State) {
func (mem *Mempool) ResetForBlockAndState(block_ *block.Block, state *sm.State) {
mem.mtx.Lock() mem.mtx.Lock()
defer mem.mtx.Unlock() defer mem.mtx.Unlock()
mem.state = state.Copy() mem.state = state.Copy()
// First, create a lookup map of txns in new block. // First, create a lookup map of txns in new block.
blockTxsMap := make(map[string]struct{}) blockTxsMap := make(map[string]struct{})
for _, tx := range block.Data.Txs {
txHash := BinarySha256(tx)
for _, tx := range block_.Data.Txs {
txHash := binary.BinarySha256(tx)
blockTxsMap[string(txHash)] = struct{}{} blockTxsMap[string(txHash)] = struct{}{}
} }
// Next, filter all txs from mem.txs that are in blockTxsMap // Next, filter all txs from mem.txs that are in blockTxsMap
txs := []Tx{}
txs := []block.Tx{}
for _, tx := range mem.txs { for _, tx := range mem.txs {
txHash := BinarySha256(tx)
txHash := binary.BinarySha256(tx)
if _, ok := blockTxsMap[string(txHash)]; ok { if _, ok := blockTxsMap[string(txHash)]; ok {
log.Debug("Filter out, already committed", "tx", tx, "txHash", txHash) log.Debug("Filter out, already committed", "tx", tx, "txHash", txHash)
continue continue
@ -80,7 +80,7 @@ func (mem *Mempool) ResetForBlockAndState(block *Block, state *state.State) {
} }
// Next, filter all txs that aren't valid given new state. // Next, filter all txs that aren't valid given new state.
validTxs := []Tx{}
validTxs := []block.Tx{}
for _, tx := range txs { for _, tx := range txs {
err := mem.state.ExecTx(tx) err := mem.state.ExecTx(tx)
if err == nil { if err == nil {


+ 5
- 5
mempool/reactor.go View File

@ -5,8 +5,8 @@ import (
"fmt" "fmt"
"sync/atomic" "sync/atomic"
. "github.com/tendermint/tendermint/binary"
. "github.com/tendermint/tendermint/block"
"github.com/tendermint/tendermint/binary"
"github.com/tendermint/tendermint/block"
"github.com/tendermint/tendermint/p2p" "github.com/tendermint/tendermint/p2p"
) )
@ -101,7 +101,7 @@ func (memR *MempoolReactor) Receive(chId byte, src *p2p.Peer, msgBytes []byte) {
} }
} }
func (memR *MempoolReactor) BroadcastTx(tx Tx) error {
func (memR *MempoolReactor) BroadcastTx(tx block.Tx) error {
err := memR.Mempool.AddTx(tx) err := memR.Mempool.AddTx(tx)
if err != nil { if err != nil {
return err return err
@ -126,7 +126,7 @@ func DecodeMessage(bz []byte) (msgType byte, msg interface{}, err error) {
r := bytes.NewReader(bz) r := bytes.NewReader(bz)
switch msgType { switch msgType {
case msgTypeTx: case msgTypeTx:
msg = ReadBinary(&TxMessage{}, r, n, &err)
msg = binary.ReadBinary(&TxMessage{}, r, n, &err)
default: default:
msg = nil msg = nil
} }
@ -136,7 +136,7 @@ func DecodeMessage(bz []byte) (msgType byte, msg interface{}, err error) {
//------------------------------------- //-------------------------------------
type TxMessage struct { type TxMessage struct {
Tx Tx
Tx block.Tx
} }
func (m *TxMessage) TypeByte() byte { return msgTypeTx } func (m *TxMessage) TypeByte() byte { return msgTypeTx }


+ 10
- 10
merkle/iavl_node.go View File

@ -4,7 +4,7 @@ import (
"crypto/sha256" "crypto/sha256"
"io" "io"
. "github.com/tendermint/tendermint/binary"
"github.com/tendermint/tendermint/binary"
) )
// Node // Node
@ -30,12 +30,12 @@ func NewIAVLNode(key interface{}, value interface{}) *IAVLNode {
} }
} }
func ReadIAVLNode(t *IAVLTree, r Unreader, n *int64, err *error) *IAVLNode {
func ReadIAVLNode(t *IAVLTree, r io.Reader, n *int64, err *error) *IAVLNode {
node := &IAVLNode{} node := &IAVLNode{}
// node header & key // node header & key
node.height = ReadUint8(r, n, err)
node.size = ReadUint64(r, n, err)
node.height = binary.ReadUint8(r, n, err)
node.size = binary.ReadUint64(r, n, err)
node.key = t.keyCodec.Decode(r, n, err) node.key = t.keyCodec.Decode(r, n, err)
if *err != nil { if *err != nil {
panic(*err) panic(*err)
@ -45,8 +45,8 @@ func ReadIAVLNode(t *IAVLTree, r Unreader, n *int64, err *error) *IAVLNode {
if node.height == 0 { if node.height == 0 {
node.value = t.valueCodec.Decode(r, n, err) node.value = t.valueCodec.Decode(r, n, err)
} else { } else {
node.leftHash = ReadByteSlice(r, n, err)
node.rightHash = ReadByteSlice(r, n, err)
node.leftHash = binary.ReadByteSlice(r, n, err)
node.rightHash = binary.ReadByteSlice(r, n, err)
} }
if *err != nil { if *err != nil {
panic(*err) panic(*err)
@ -254,8 +254,8 @@ func (node *IAVLNode) remove(t *IAVLTree, key interface{}) (
// NOTE: sets hashes recursively // NOTE: sets hashes recursively
func (node *IAVLNode) writeToCountHashes(t *IAVLTree, w io.Writer) (n int64, hashCount uint64, err error) { func (node *IAVLNode) writeToCountHashes(t *IAVLTree, w io.Writer) (n int64, hashCount uint64, err error) {
// height & size & key // height & size & key
WriteUint8(node.height, w, &n, &err)
WriteUint64(node.size, w, &n, &err)
binary.WriteUint8(node.height, w, &n, &err)
binary.WriteUint64(node.size, w, &n, &err)
t.keyCodec.Encode(node.key, w, &n, &err) t.keyCodec.Encode(node.key, w, &n, &err)
if err != nil { if err != nil {
return return
@ -274,7 +274,7 @@ func (node *IAVLNode) writeToCountHashes(t *IAVLTree, w io.Writer) (n int64, has
if node.leftHash == nil { if node.leftHash == nil {
panic("node.leftHash was nil in save") panic("node.leftHash was nil in save")
} }
WriteByteSlice(node.leftHash, w, &n, &err)
binary.WriteByteSlice(node.leftHash, w, &n, &err)
// right // right
if node.rightNode != nil { if node.rightNode != nil {
rightHash, rightCount := node.rightNode.hashWithCount(t) rightHash, rightCount := node.rightNode.hashWithCount(t)
@ -284,7 +284,7 @@ func (node *IAVLNode) writeToCountHashes(t *IAVLTree, w io.Writer) (n int64, has
if node.rightHash == nil { if node.rightHash == nil {
panic("node.rightHash was nil in save") panic("node.rightHash was nil in save")
} }
WriteByteSlice(node.rightHash, w, &n, &err)
binary.WriteByteSlice(node.rightHash, w, &n, &err)
} }
return return
} }


+ 6
- 6
merkle/iavl_test.go View File

@ -4,7 +4,7 @@ import (
"bytes" "bytes"
"fmt" "fmt"
. "github.com/tendermint/tendermint/binary"
"github.com/tendermint/tendermint/binary"
. "github.com/tendermint/tendermint/common" . "github.com/tendermint/tendermint/common"
"github.com/tendermint/tendermint/db" "github.com/tendermint/tendermint/db"
@ -46,7 +46,7 @@ func N(l, r interface{}) *IAVLNode {
// Setup a deep node // Setup a deep node
func T(n *IAVLNode) *IAVLTree { func T(n *IAVLNode) *IAVLTree {
t := NewIAVLTree(BasicCodec, BasicCodec, 0, nil)
t := NewIAVLTree(binary.BasicCodec, binary.BasicCodec, 0, nil)
n.hashWithCount(t) n.hashWithCount(t)
t.root = n t.root = n
return t return t
@ -152,7 +152,7 @@ func TestIntegration(t *testing.T) {
} }
records := make([]*record, 400) records := make([]*record, 400)
var tree *IAVLTree = NewIAVLTree(BasicCodec, BasicCodec, 0, nil)
var tree *IAVLTree = NewIAVLTree(binary.BasicCodec, binary.BasicCodec, 0, nil)
randomRecord := func() *record { randomRecord := func() *record {
return &record{randstr(20), randstr(20)} return &record{randstr(20), randstr(20)}
@ -222,7 +222,7 @@ func TestPersistence(t *testing.T) {
} }
// Construct some tree and save it // Construct some tree and save it
t1 := NewIAVLTree(BasicCodec, BasicCodec, 0, db)
t1 := NewIAVLTree(binary.BasicCodec, binary.BasicCodec, 0, db)
for key, value := range records { for key, value := range records {
t1.Set(key, value) t1.Set(key, value)
} }
@ -231,7 +231,7 @@ func TestPersistence(t *testing.T) {
hash, _ := t1.HashWithCount() hash, _ := t1.HashWithCount()
// Load a tree // Load a tree
t2 := NewIAVLTree(BasicCodec, BasicCodec, 0, db)
t2 := NewIAVLTree(binary.BasicCodec, binary.BasicCodec, 0, db)
t2.Load(hash) t2.Load(hash)
for key, value := range records { for key, value := range records {
_, t2value := t2.Get(key) _, t2value := t2.Get(key)
@ -244,7 +244,7 @@ func TestPersistence(t *testing.T) {
func BenchmarkImmutableAvlTree(b *testing.B) { func BenchmarkImmutableAvlTree(b *testing.B) {
b.StopTimer() b.StopTimer()
t := NewIAVLTree(BasicCodec, BasicCodec, 0, nil)
t := NewIAVLTree(binary.BasicCodec, binary.BasicCodec, 0, nil)
for i := 0; i < 1000000; i++ { for i := 0; i < 1000000; i++ {
t.Set(RandUint64(), "") t.Set(RandUint64(), "")
} }


+ 8
- 8
merkle/iavl_tree.go View File

@ -5,9 +5,9 @@ import (
"container/list" "container/list"
"sync" "sync"
. "github.com/tendermint/tendermint/binary"
"github.com/tendermint/tendermint/binary"
. "github.com/tendermint/tendermint/common" . "github.com/tendermint/tendermint/common"
db_ "github.com/tendermint/tendermint/db"
dbm "github.com/tendermint/tendermint/db"
) )
/* /*
@ -15,13 +15,13 @@ Immutable AVL Tree (wraps the Node root)
This tree is not goroutine safe. This tree is not goroutine safe.
*/ */
type IAVLTree struct { type IAVLTree struct {
keyCodec Codec
valueCodec Codec
keyCodec binary.Codec
valueCodec binary.Codec
root *IAVLNode root *IAVLNode
ndb *nodeDB ndb *nodeDB
} }
func NewIAVLTree(keyCodec, valueCodec Codec, cacheSize int, db db_.DB) *IAVLTree {
func NewIAVLTree(keyCodec, valueCodec binary.Codec, cacheSize int, db dbm.DB) *IAVLTree {
if db == nil { if db == nil {
// In-memory IAVLTree // In-memory IAVLTree
return &IAVLTree{ return &IAVLTree{
@ -182,10 +182,10 @@ type nodeDB struct {
cache map[string]nodeElement cache map[string]nodeElement
cacheSize int cacheSize int
cacheQueue *list.List cacheQueue *list.List
db db_.DB
db dbm.DB
} }
func newNodeDB(cacheSize int, db db_.DB) *nodeDB {
func newNodeDB(cacheSize int, db dbm.DB) *nodeDB {
return &nodeDB{ return &nodeDB{
cache: make(map[string]nodeElement), cache: make(map[string]nodeElement),
cacheSize: cacheSize, cacheSize: cacheSize,
@ -207,7 +207,7 @@ func (ndb *nodeDB) GetNode(t *IAVLTree, hash []byte) *IAVLNode {
// Doesn't exist, load. // Doesn't exist, load.
buf := ndb.db.Get(hash) buf := ndb.db.Get(hash)
if len(buf) == 0 { if len(buf) == 0 {
ndb.db.(*db_.LevelDB).Print()
ndb.db.(*dbm.LevelDB).Print()
panic(Fmt("Value missing for key %X", hash)) panic(Fmt("Value missing for key %X", hash))
} }
r := bytes.NewReader(buf) r := bytes.NewReader(buf)


+ 4
- 4
merkle/simple_tree.go View File

@ -28,15 +28,15 @@ import (
"bytes" "bytes"
"crypto/sha256" "crypto/sha256"
. "github.com/tendermint/tendermint/binary"
"github.com/tendermint/tendermint/binary"
) )
func HashFromTwoHashes(left []byte, right []byte) []byte { func HashFromTwoHashes(left []byte, right []byte) []byte {
var n int64 var n int64
var err error var err error
var hasher = sha256.New() var hasher = sha256.New()
WriteByteSlice(left, hasher, &n, &err)
WriteByteSlice(right, hasher, &n, &err)
binary.WriteByteSlice(left, hasher, &n, &err)
binary.WriteByteSlice(right, hasher, &n, &err)
if err != nil { if err != nil {
panic(err) panic(err)
} }
@ -62,7 +62,7 @@ func HashFromBinaries(items []interface{}) []byte {
hashes := [][]byte{} hashes := [][]byte{}
for _, item := range items { for _, item := range items {
hasher, n, err := sha256.New(), new(int64), new(error) hasher, n, err := sha256.New(), new(int64), new(error)
WriteBinary(item, hasher, n, err)
binary.WriteBinary(item, hasher, n, err)
if *err != nil { if *err != nil {
panic(err) panic(err)
} }


+ 10
- 10
p2p/connection.go View File

@ -12,7 +12,7 @@ import (
flow "code.google.com/p/mxk/go1/flowcontrol" flow "code.google.com/p/mxk/go1/flowcontrol"
"github.com/tendermint/log15" "github.com/tendermint/log15"
. "github.com/tendermint/tendermint/binary"
"github.com/tendermint/tendermint/binary"
. "github.com/tendermint/tendermint/common" . "github.com/tendermint/tendermint/common"
) )
@ -182,7 +182,7 @@ func (c *MConnection) Send(chId byte, msg interface{}) bool {
return false return false
} }
log.Debug("Send", "channel", chId, "connection", c, "msg", msg, "bytes", BinaryBytes(msg))
log.Debug("Send", "channel", chId, "connection", c, "msg", msg, "bytes", binary.BinaryBytes(msg))
// Send message to channel. // Send message to channel.
channel, ok := c.channelsIdx[chId] channel, ok := c.channelsIdx[chId]
@ -191,7 +191,7 @@ func (c *MConnection) Send(chId byte, msg interface{}) bool {
return false return false
} }
channel.sendBytes(BinaryBytes(msg))
channel.sendBytes(binary.BinaryBytes(msg))
// Wake up sendRoutine if necessary // Wake up sendRoutine if necessary
select { select {
@ -218,7 +218,7 @@ func (c *MConnection) TrySend(chId byte, msg interface{}) bool {
return false return false
} }
ok = channel.trySendBytes(BinaryBytes(msg))
ok = channel.trySendBytes(binary.BinaryBytes(msg))
if ok { if ok {
// Wake up sendRoutine if necessary // Wake up sendRoutine if necessary
select { select {
@ -262,12 +262,12 @@ FOR_LOOP:
} }
case <-c.pingTimer.Ch: case <-c.pingTimer.Ch:
log.Debug("Send Ping") log.Debug("Send Ping")
WriteByte(packetTypePing, c.bufWriter, &n, &err)
binary.WriteByte(packetTypePing, c.bufWriter, &n, &err)
c.sendMonitor.Update(int(n)) c.sendMonitor.Update(int(n))
c.flush() c.flush()
case <-c.pong: case <-c.pong:
log.Debug("Send Pong") log.Debug("Send Pong")
WriteByte(packetTypePong, c.bufWriter, &n, &err)
binary.WriteByte(packetTypePong, c.bufWriter, &n, &err)
c.sendMonitor.Update(int(n)) c.sendMonitor.Update(int(n))
c.flush() c.flush()
case <-c.quit: case <-c.quit:
@ -379,7 +379,7 @@ FOR_LOOP:
// Read packet type // Read packet type
var n int64 var n int64
var err error var err error
pktType := ReadByte(c.bufReader, &n, &err)
pktType := binary.ReadByte(c.bufReader, &n, &err)
c.recvMonitor.Update(int(n)) c.recvMonitor.Update(int(n))
if err != nil { if err != nil {
if atomic.LoadUint32(&c.stopped) != 1 { if atomic.LoadUint32(&c.stopped) != 1 {
@ -400,7 +400,7 @@ FOR_LOOP:
log.Debug("Receive Pong") log.Debug("Receive Pong")
case packetTypeMsg: case packetTypeMsg:
pkt, n, err := msgPacket{}, new(int64), new(error) pkt, n, err := msgPacket{}, new(int64), new(error)
ReadBinary(&pkt, c.bufReader, n, err)
binary.ReadBinary(&pkt, c.bufReader, n, err)
c.recvMonitor.Update(int(*n)) c.recvMonitor.Update(int(*n))
if *err != nil { if *err != nil {
if atomic.LoadUint32(&c.stopped) != 1 { if atomic.LoadUint32(&c.stopped) != 1 {
@ -533,8 +533,8 @@ func (ch *Channel) nextMsgPacket() msgPacket {
// Not goroutine-safe // Not goroutine-safe
func (ch *Channel) writeMsgPacketTo(w io.Writer) (n int64, err error) { func (ch *Channel) writeMsgPacketTo(w io.Writer) (n int64, err error) {
packet := ch.nextMsgPacket() packet := ch.nextMsgPacket()
WriteByte(packetTypeMsg, w, &n, &err)
WriteBinary(packet, w, &n, &err)
binary.WriteByte(packetTypeMsg, w, &n, &err)
binary.WriteBinary(packet, w, &n, &err)
if err != nil { if err != nil {
ch.recentlySent += n ch.recentlySent += n
} }


+ 2
- 2
p2p/peer.go View File

@ -6,7 +6,7 @@ import (
"net" "net"
"sync/atomic" "sync/atomic"
. "github.com/tendermint/tendermint/binary"
"github.com/tendermint/tendermint/binary"
. "github.com/tendermint/tendermint/common" . "github.com/tendermint/tendermint/common"
) )
@ -92,7 +92,7 @@ func (p *Peer) CanSend(chId byte) bool {
} }
func (p *Peer) WriteTo(w io.Writer) (n int64, err error) { func (p *Peer) WriteTo(w io.Writer) (n int64, err error) {
WriteString(p.Key, w, &n, &err)
binary.WriteString(p.Key, w, &n, &err)
return return
} }


+ 2
- 2
p2p/pex_reactor.go View File

@ -7,7 +7,7 @@ import (
"sync/atomic" "sync/atomic"
"time" "time"
. "github.com/tendermint/tendermint/binary"
"github.com/tendermint/tendermint/binary"
. "github.com/tendermint/tendermint/common" . "github.com/tendermint/tendermint/common"
) )
@ -215,7 +215,7 @@ func DecodeMessage(bz []byte) (msg interface{}, err error) {
case msgTypeRequest: case msgTypeRequest:
msg = &pexRequestMessage{} msg = &pexRequestMessage{}
case msgTypeAddrs: case msgTypeAddrs:
msg = ReadBinary(&pexAddrsMessage{}, r, n, &err)
msg = binary.ReadBinary(&pexAddrsMessage{}, r, n, &err)
default: default:
msg = nil msg = nil
} }


+ 7
- 7
p2p/switch_test.go View File

@ -6,7 +6,7 @@ import (
"testing" "testing"
"time" "time"
. "github.com/tendermint/tendermint/binary"
"github.com/tendermint/tendermint/binary"
. "github.com/tendermint/tendermint/common" . "github.com/tendermint/tendermint/common"
) )
@ -144,8 +144,8 @@ func TestSwitches(t *testing.T) {
if len(ch0Msgs) != 1 { if len(ch0Msgs) != 1 {
t.Errorf("Expected to have received 1 message in ch0") t.Errorf("Expected to have received 1 message in ch0")
} }
if !bytes.Equal(ch0Msgs[0].Bytes, BinaryBytes(ch0Msg)) {
t.Errorf("Unexpected message bytes. Wanted: %X, Got: %X", BinaryBytes(ch0Msg), ch0Msgs[0].Bytes)
if !bytes.Equal(ch0Msgs[0].Bytes, binary.BinaryBytes(ch0Msg)) {
t.Errorf("Unexpected message bytes. Wanted: %X, Got: %X", binary.BinaryBytes(ch0Msg), ch0Msgs[0].Bytes)
} }
// Check message on ch1 // Check message on ch1
@ -153,8 +153,8 @@ func TestSwitches(t *testing.T) {
if len(ch1Msgs) != 1 { if len(ch1Msgs) != 1 {
t.Errorf("Expected to have received 1 message in ch1") t.Errorf("Expected to have received 1 message in ch1")
} }
if !bytes.Equal(ch1Msgs[0].Bytes, BinaryBytes(ch1Msg)) {
t.Errorf("Unexpected message bytes. Wanted: %X, Got: %X", BinaryBytes(ch1Msg), ch1Msgs[0].Bytes)
if !bytes.Equal(ch1Msgs[0].Bytes, binary.BinaryBytes(ch1Msg)) {
t.Errorf("Unexpected message bytes. Wanted: %X, Got: %X", binary.BinaryBytes(ch1Msg), ch1Msgs[0].Bytes)
} }
// Check message on ch2 // Check message on ch2
@ -162,8 +162,8 @@ func TestSwitches(t *testing.T) {
if len(ch2Msgs) != 1 { if len(ch2Msgs) != 1 {
t.Errorf("Expected to have received 1 message in ch2") t.Errorf("Expected to have received 1 message in ch2")
} }
if !bytes.Equal(ch2Msgs[0].Bytes, BinaryBytes(ch2Msg)) {
t.Errorf("Unexpected message bytes. Wanted: %X, Got: %X", BinaryBytes(ch2Msg), ch2Msgs[0].Bytes)
if !bytes.Equal(ch2Msgs[0].Bytes, binary.BinaryBytes(ch2Msg)) {
t.Errorf("Unexpected message bytes. Wanted: %X, Got: %X", binary.BinaryBytes(ch2Msg), ch2Msgs[0].Bytes)
} }
} }


+ 7
- 7
rpc/blocks.go View File

@ -3,7 +3,7 @@ package rpc
import ( import (
"net/http" "net/http"
. "github.com/tendermint/tendermint/block"
"github.com/tendermint/tendermint/block"
. "github.com/tendermint/tendermint/common" . "github.com/tendermint/tendermint/common"
) )
@ -20,7 +20,7 @@ func BlockchainInfoHandler(w http.ResponseWriter, r *http.Request) {
} }
log.Debug("BlockchainInfoHandler", "maxHeight", maxHeight, "minHeight", minHeight) log.Debug("BlockchainInfoHandler", "maxHeight", maxHeight, "minHeight", minHeight)
blockMetas := []*BlockMeta{}
blockMetas := []*block.BlockMeta{}
for height := maxHeight; height >= minHeight; height-- { for height := maxHeight; height >= minHeight; height-- {
blockMeta := blockStore.LoadBlockMeta(height) blockMeta := blockStore.LoadBlockMeta(height)
blockMetas = append(blockMetas, blockMeta) blockMetas = append(blockMetas, blockMeta)
@ -28,7 +28,7 @@ func BlockchainInfoHandler(w http.ResponseWriter, r *http.Request) {
WriteAPIResponse(w, API_OK, struct { WriteAPIResponse(w, API_OK, struct {
LastHeight uint LastHeight uint
BlockMetas []*BlockMeta
BlockMetas []*block.BlockMeta
}{blockStore.Height(), blockMetas}) }{blockStore.Height(), blockMetas})
} }
@ -46,10 +46,10 @@ func GetBlockHandler(w http.ResponseWriter, r *http.Request) {
} }
blockMeta := blockStore.LoadBlockMeta(height) blockMeta := blockStore.LoadBlockMeta(height)
block := blockStore.LoadBlock(height)
block_ := blockStore.LoadBlock(height)
WriteAPIResponse(w, API_OK, struct { WriteAPIResponse(w, API_OK, struct {
BlockMeta *BlockMeta
Block *Block
}{blockMeta, block})
BlockMeta *block.BlockMeta
Block *block.Block
}{blockMeta, block_})
} }

+ 4
- 4
rpc/mempool.go View File

@ -3,16 +3,16 @@ package rpc
import ( import (
"net/http" "net/http"
. "github.com/tendermint/tendermint/binary"
. "github.com/tendermint/tendermint/block"
"github.com/tendermint/tendermint/binary"
"github.com/tendermint/tendermint/block"
. "github.com/tendermint/tendermint/common" . "github.com/tendermint/tendermint/common"
) )
func BroadcastTxHandler(w http.ResponseWriter, r *http.Request) { func BroadcastTxHandler(w http.ResponseWriter, r *http.Request) {
txJSON := GetParam(r, "tx") txJSON := GetParam(r, "tx")
var err error var err error
var tx Tx
ReadJSON(&tx, []byte(txJSON), &err)
var tx block.Tx
binary.ReadJSON(&tx, []byte(txJSON), &err)
if err != nil { if err != nil {
WriteAPIResponse(w, API_INVALID_PARAM, Fmt("Invalid tx: %v", err)) WriteAPIResponse(w, API_INVALID_PARAM, Fmt("Invalid tx: %v", err))
return return


+ 6
- 6
rpc/rpc.go View File

@ -1,16 +1,16 @@
package rpc package rpc
import ( import (
block_ "github.com/tendermint/tendermint/block"
"github.com/tendermint/tendermint/block"
"github.com/tendermint/tendermint/consensus" "github.com/tendermint/tendermint/consensus"
mempool_ "github.com/tendermint/tendermint/mempool"
mempl "github.com/tendermint/tendermint/mempool"
) )
var blockStore *block_.BlockStore
var blockStore *block.BlockStore
var consensusState *consensus.ConsensusState var consensusState *consensus.ConsensusState
var mempoolReactor *mempool_.MempoolReactor
var mempoolReactor *mempl.MempoolReactor
func SetRPCBlockStore(bs *block_.BlockStore) {
func SetRPCBlockStore(bs *block.BlockStore) {
blockStore = bs blockStore = bs
} }
@ -18,6 +18,6 @@ func SetRPCConsensusState(cs *consensus.ConsensusState) {
consensusState = cs consensusState = cs
} }
func SetRPCMempoolReactor(mr *mempool_.MempoolReactor) {
func SetRPCMempoolReactor(mr *mempl.MempoolReactor) {
mempoolReactor = mr mempoolReactor = mr
} }

+ 7
- 7
rpc/validators.go View File

@ -3,28 +3,28 @@ package rpc
import ( import (
"net/http" "net/http"
state_ "github.com/tendermint/tendermint/state"
sm "github.com/tendermint/tendermint/state"
) )
func ListValidatorsHandler(w http.ResponseWriter, r *http.Request) { func ListValidatorsHandler(w http.ResponseWriter, r *http.Request) {
var blockHeight uint var blockHeight uint
var bondedValidators []*state_.Validator
var unbondingValidators []*state_.Validator
var bondedValidators []*sm.Validator
var unbondingValidators []*sm.Validator
state := consensusState.GetState() state := consensusState.GetState()
blockHeight = state.LastBlockHeight blockHeight = state.LastBlockHeight
state.BondedValidators.Iterate(func(index uint, val *state_.Validator) bool {
state.BondedValidators.Iterate(func(index uint, val *sm.Validator) bool {
bondedValidators = append(bondedValidators, val) bondedValidators = append(bondedValidators, val)
return false return false
}) })
state.UnbondingValidators.Iterate(func(index uint, val *state_.Validator) bool {
state.UnbondingValidators.Iterate(func(index uint, val *sm.Validator) bool {
unbondingValidators = append(unbondingValidators, val) unbondingValidators = append(unbondingValidators, val)
return false return false
}) })
WriteAPIResponse(w, API_OK, struct { WriteAPIResponse(w, API_OK, struct {
BlockHeight uint BlockHeight uint
BondedValidators []*state_.Validator
UnbondingValidators []*state_.Validator
BondedValidators []*sm.Validator
UnbondingValidators []*sm.Validator
}{blockHeight, bondedValidators, unbondingValidators}) }{blockHeight, bondedValidators, unbondingValidators})
} }

+ 19
- 19
state/genesis.go View File

@ -4,11 +4,11 @@ import (
"io/ioutil" "io/ioutil"
"time" "time"
. "github.com/tendermint/tendermint/account"
. "github.com/tendermint/tendermint/binary"
. "github.com/tendermint/tendermint/block"
"github.com/tendermint/tendermint/account"
"github.com/tendermint/tendermint/binary"
"github.com/tendermint/tendermint/block"
. "github.com/tendermint/tendermint/common" . "github.com/tendermint/tendermint/common"
db_ "github.com/tendermint/tendermint/db"
dbm "github.com/tendermint/tendermint/db"
"github.com/tendermint/tendermint/merkle" "github.com/tendermint/tendermint/merkle"
) )
@ -18,7 +18,7 @@ type GenesisAccount struct {
} }
type GenesisValidator struct { type GenesisValidator struct {
PubKey PubKeyEd25519
PubKey account.PubKeyEd25519
Amount uint64 Amount uint64
UnbondTo []GenesisAccount UnbondTo []GenesisAccount
} }
@ -31,14 +31,14 @@ type GenesisDoc struct {
func GenesisDocFromJSON(jsonBlob []byte) (genState *GenesisDoc) { func GenesisDocFromJSON(jsonBlob []byte) (genState *GenesisDoc) {
var err error var err error
ReadJSON(&genState, jsonBlob, &err)
binary.ReadJSON(&genState, jsonBlob, &err)
if err != nil { if err != nil {
panic(Fmt("Couldn't read GenesisDoc: %v", err)) panic(Fmt("Couldn't read GenesisDoc: %v", err))
} }
return return
} }
func MakeGenesisStateFromFile(db db_.DB, genDocFile string) *State {
func MakeGenesisStateFromFile(db dbm.DB, genDocFile string) *State {
jsonBlob, err := ioutil.ReadFile(genDocFile) jsonBlob, err := ioutil.ReadFile(genDocFile)
if err != nil { if err != nil {
panic(Fmt("Couldn't read GenesisDoc file: %v", err)) panic(Fmt("Couldn't read GenesisDoc file: %v", err))
@ -47,7 +47,7 @@ func MakeGenesisStateFromFile(db db_.DB, genDocFile string) *State {
return MakeGenesisState(db, genDoc) return MakeGenesisState(db, genDoc)
} }
func MakeGenesisState(db db_.DB, genDoc *GenesisDoc) *State {
func MakeGenesisState(db dbm.DB, genDoc *GenesisDoc) *State {
if len(genDoc.Validators) == 0 { if len(genDoc.Validators) == 0 {
Exit(Fmt("The genesis file has no validators")) Exit(Fmt("The genesis file has no validators"))
} }
@ -57,19 +57,19 @@ func MakeGenesisState(db db_.DB, genDoc *GenesisDoc) *State {
} }
// Make accounts state tree // Make accounts state tree
accounts := merkle.NewIAVLTree(BasicCodec, AccountCodec, defaultAccountsCacheCapacity, db)
for _, acc := range genDoc.Accounts {
account := &Account{
Address: acc.Address,
PubKey: PubKeyNil{},
accounts := merkle.NewIAVLTree(binary.BasicCodec, account.AccountCodec, defaultAccountsCacheCapacity, db)
for _, genAcc := range genDoc.Accounts {
acc := &account.Account{
Address: genAcc.Address,
PubKey: account.PubKeyNil{},
Sequence: 0, Sequence: 0,
Balance: acc.Amount,
Balance: genAcc.Amount,
} }
accounts.Set(acc.Address, account)
accounts.Set(acc.Address, acc)
} }
// Make validatorInfos state tree && validators slice // Make validatorInfos state tree && validators slice
validatorInfos := merkle.NewIAVLTree(BasicCodec, ValidatorInfoCodec, 0, db)
validatorInfos := merkle.NewIAVLTree(binary.BasicCodec, ValidatorInfoCodec, 0, db)
validators := make([]*Validator, len(genDoc.Validators)) validators := make([]*Validator, len(genDoc.Validators))
for i, val := range genDoc.Validators { for i, val := range genDoc.Validators {
pubKey := val.PubKey pubKey := val.PubKey
@ -79,12 +79,12 @@ func MakeGenesisState(db db_.DB, genDoc *GenesisDoc) *State {
valInfo := &ValidatorInfo{ valInfo := &ValidatorInfo{
Address: address, Address: address,
PubKey: pubKey, PubKey: pubKey,
UnbondTo: make([]*TxOutput, len(val.UnbondTo)),
UnbondTo: make([]*block.TxOutput, len(val.UnbondTo)),
FirstBondHeight: 0, FirstBondHeight: 0,
FirstBondAmount: val.Amount, FirstBondAmount: val.Amount,
} }
for i, unbondTo := range val.UnbondTo { for i, unbondTo := range val.UnbondTo {
valInfo.UnbondTo[i] = &TxOutput{
valInfo.UnbondTo[i] = &block.TxOutput{
Address: unbondTo.Address, Address: unbondTo.Address,
Amount: unbondTo.Amount, Amount: unbondTo.Amount,
} }
@ -107,7 +107,7 @@ func MakeGenesisState(db db_.DB, genDoc *GenesisDoc) *State {
DB: db, DB: db,
LastBlockHeight: 0, LastBlockHeight: 0,
LastBlockHash: nil, LastBlockHash: nil,
LastBlockParts: PartSetHeader{},
LastBlockParts: block.PartSetHeader{},
LastBlockTime: genDoc.GenesisTime, LastBlockTime: genDoc.GenesisTime,
BondedValidators: NewValidatorSet(validators), BondedValidators: NewValidatorSet(validators),
UnbondingValidators: NewValidatorSet(nil), UnbondingValidators: NewValidatorSet(nil),


+ 19
- 19
state/priv_validator.go View File

@ -9,9 +9,9 @@ import (
"math" "math"
"sync" "sync"
. "github.com/tendermint/tendermint/account"
. "github.com/tendermint/tendermint/binary"
. "github.com/tendermint/tendermint/block"
"github.com/tendermint/tendermint/account"
"github.com/tendermint/tendermint/binary"
"github.com/tendermint/tendermint/block"
. "github.com/tendermint/tendermint/common" . "github.com/tendermint/tendermint/common"
"github.com/tendermint/tendermint/config" "github.com/tendermint/tendermint/config"
. "github.com/tendermint/tendermint/consensus/types" . "github.com/tendermint/tendermint/consensus/types"
@ -27,13 +27,13 @@ const (
stepCommit = 4 stepCommit = 4
) )
func voteToStep(vote *Vote) uint8 {
func voteToStep(vote *block.Vote) uint8 {
switch vote.Type { switch vote.Type {
case VoteTypePrevote:
case block.VoteTypePrevote:
return stepPrevote return stepPrevote
case VoteTypePrecommit:
case block.VoteTypePrecommit:
return stepPrecommit return stepPrecommit
case VoteTypeCommit:
case block.VoteTypeCommit:
return stepCommit return stepCommit
default: default:
panic("Unknown vote type") panic("Unknown vote type")
@ -42,8 +42,8 @@ func voteToStep(vote *Vote) uint8 {
type PrivValidator struct { type PrivValidator struct {
Address []byte Address []byte
PubKey PubKeyEd25519
PrivKey PrivKeyEd25519
PubKey account.PubKeyEd25519
PrivKey account.PrivKeyEd25519
LastHeight uint LastHeight uint
LastRound uint LastRound uint
LastStep uint8 LastStep uint8
@ -58,8 +58,8 @@ type PrivValidator struct {
func GenPrivValidator() *PrivValidator { func GenPrivValidator() *PrivValidator {
privKeyBytes := CRandBytes(32) privKeyBytes := CRandBytes(32)
pubKeyBytes := ed25519.MakePubKey(privKeyBytes) pubKeyBytes := ed25519.MakePubKey(privKeyBytes)
pubKey := PubKeyEd25519(pubKeyBytes)
privKey := PrivKeyEd25519(privKeyBytes)
pubKey := account.PubKeyEd25519(pubKeyBytes)
privKey := account.PrivKeyEd25519(privKeyBytes)
return &PrivValidator{ return &PrivValidator{
Address: pubKey.Address(), Address: pubKey.Address(),
PubKey: pubKey, PubKey: pubKey,
@ -76,7 +76,7 @@ func LoadPrivValidator(filename string) *PrivValidator {
if err != nil { if err != nil {
panic(err) panic(err)
} }
privVal := ReadJSON(&PrivValidator{}, privValJSONBytes, &err).(*PrivValidator)
privVal := binary.ReadJSON(&PrivValidator{}, privValJSONBytes, &err).(*PrivValidator)
if err != nil { if err != nil {
Exit(Fmt("Error reading PrivValidator from %v: %v\n", filename, err)) Exit(Fmt("Error reading PrivValidator from %v: %v\n", filename, err))
} }
@ -91,7 +91,7 @@ func (privVal *PrivValidator) Save() {
} }
func (privVal *PrivValidator) save() { func (privVal *PrivValidator) save() {
jsonBytes := JSONBytes(privVal)
jsonBytes := binary.JSONBytes(privVal)
err := ioutil.WriteFile(privVal.filename, jsonBytes, 0700) err := ioutil.WriteFile(privVal.filename, jsonBytes, 0700)
if err != nil { if err != nil {
panic(err) panic(err)
@ -99,7 +99,7 @@ func (privVal *PrivValidator) save() {
} }
// TODO: test // TODO: test
func (privVal *PrivValidator) SignVote(vote *Vote) error {
func (privVal *PrivValidator) SignVote(vote *block.Vote) error {
privVal.mtx.Lock() privVal.mtx.Lock()
defer privVal.mtx.Unlock() defer privVal.mtx.Unlock()
@ -134,8 +134,8 @@ func (privVal *PrivValidator) SignVote(vote *Vote) error {
return nil return nil
} }
func (privVal *PrivValidator) SignVoteUnsafe(vote *Vote) {
vote.Signature = privVal.PrivKey.Sign(SignBytes(vote)).(SignatureEd25519)
func (privVal *PrivValidator) SignVoteUnsafe(vote *block.Vote) {
vote.Signature = privVal.PrivKey.Sign(account.SignBytes(vote)).(account.SignatureEd25519)
} }
func (privVal *PrivValidator) SignProposal(proposal *Proposal) error { func (privVal *PrivValidator) SignProposal(proposal *Proposal) error {
@ -152,14 +152,14 @@ func (privVal *PrivValidator) SignProposal(proposal *Proposal) error {
privVal.save() privVal.save()
// Sign // Sign
proposal.Signature = privVal.PrivKey.Sign(SignBytes(proposal)).(SignatureEd25519)
proposal.Signature = privVal.PrivKey.Sign(account.SignBytes(proposal)).(account.SignatureEd25519)
return nil return nil
} else { } else {
return errors.New(fmt.Sprintf("Attempt of duplicate signing of proposal: Height %v, Round %v", proposal.Height, proposal.Round)) return errors.New(fmt.Sprintf("Attempt of duplicate signing of proposal: Height %v, Round %v", proposal.Height, proposal.Round))
} }
} }
func (privVal *PrivValidator) SignRebondTx(rebondTx *RebondTx) error {
func (privVal *PrivValidator) SignRebondTx(rebondTx *block.RebondTx) error {
privVal.mtx.Lock() privVal.mtx.Lock()
defer privVal.mtx.Unlock() defer privVal.mtx.Unlock()
if privVal.LastHeight < rebondTx.Height { if privVal.LastHeight < rebondTx.Height {
@ -171,7 +171,7 @@ func (privVal *PrivValidator) SignRebondTx(rebondTx *RebondTx) error {
privVal.save() privVal.save()
// Sign // Sign
rebondTx.Signature = privVal.PrivKey.Sign(SignBytes(rebondTx)).(SignatureEd25519)
rebondTx.Signature = privVal.PrivKey.Sign(account.SignBytes(rebondTx)).(account.SignatureEd25519)
return nil return nil
} else { } else {
return errors.New(fmt.Sprintf("Attempt of duplicate signing of rebondTx: Height %v", rebondTx.Height)) return errors.New(fmt.Sprintf("Attempt of duplicate signing of rebondTx: Height %v", rebondTx.Height))


+ 124
- 124
state/state.go View File

@ -6,11 +6,11 @@ import (
"fmt" "fmt"
"time" "time"
. "github.com/tendermint/tendermint/account"
. "github.com/tendermint/tendermint/binary"
. "github.com/tendermint/tendermint/block"
"github.com/tendermint/tendermint/account"
"github.com/tendermint/tendermint/binary"
"github.com/tendermint/tendermint/block"
. "github.com/tendermint/tendermint/common" . "github.com/tendermint/tendermint/common"
db_ "github.com/tendermint/tendermint/db"
dbm "github.com/tendermint/tendermint/db"
"github.com/tendermint/tendermint/merkle" "github.com/tendermint/tendermint/merkle"
) )
@ -25,7 +25,7 @@ var (
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
type InvalidTxError struct { type InvalidTxError struct {
Tx Tx
Tx block.Tx
Reason error Reason error
} }
@ -37,10 +37,10 @@ func (txErr InvalidTxError) Error() string {
// NOTE: not goroutine-safe. // NOTE: not goroutine-safe.
type State struct { type State struct {
DB db_.DB
DB dbm.DB
LastBlockHeight uint LastBlockHeight uint
LastBlockHash []byte LastBlockHash []byte
LastBlockParts PartSetHeader
LastBlockParts block.PartSetHeader
LastBlockTime time.Time LastBlockTime time.Time
BondedValidators *ValidatorSet BondedValidators *ValidatorSet
UnbondingValidators *ValidatorSet UnbondingValidators *ValidatorSet
@ -48,24 +48,24 @@ type State struct {
validatorInfos merkle.Tree // Shouldn't be accessed directly. validatorInfos merkle.Tree // Shouldn't be accessed directly.
} }
func LoadState(db db_.DB) *State {
func LoadState(db dbm.DB) *State {
s := &State{DB: db} s := &State{DB: db}
buf := db.Get(stateKey) buf := db.Get(stateKey)
if len(buf) == 0 { if len(buf) == 0 {
return nil return nil
} else { } else {
r, n, err := bytes.NewReader(buf), new(int64), new(error) r, n, err := bytes.NewReader(buf), new(int64), new(error)
s.LastBlockHeight = ReadUvarint(r, n, err)
s.LastBlockHash = ReadByteSlice(r, n, err)
s.LastBlockParts = ReadBinary(PartSetHeader{}, r, n, err).(PartSetHeader)
s.LastBlockTime = ReadTime(r, n, err)
s.BondedValidators = ReadBinary(&ValidatorSet{}, r, n, err).(*ValidatorSet)
s.UnbondingValidators = ReadBinary(&ValidatorSet{}, r, n, err).(*ValidatorSet)
accountsHash := ReadByteSlice(r, n, err)
s.accounts = merkle.NewIAVLTree(BasicCodec, AccountCodec, defaultAccountsCacheCapacity, db)
s.LastBlockHeight = binary.ReadUvarint(r, n, err)
s.LastBlockHash = binary.ReadByteSlice(r, n, err)
s.LastBlockParts = binary.ReadBinary(block.PartSetHeader{}, r, n, err).(block.PartSetHeader)
s.LastBlockTime = binary.ReadTime(r, n, err)
s.BondedValidators = binary.ReadBinary(&ValidatorSet{}, r, n, err).(*ValidatorSet)
s.UnbondingValidators = binary.ReadBinary(&ValidatorSet{}, r, n, err).(*ValidatorSet)
accountsHash := binary.ReadByteSlice(r, n, err)
s.accounts = merkle.NewIAVLTree(binary.BasicCodec, account.AccountCodec, defaultAccountsCacheCapacity, db)
s.accounts.Load(accountsHash) s.accounts.Load(accountsHash)
validatorInfosHash := ReadByteSlice(r, n, err)
s.validatorInfos = merkle.NewIAVLTree(BasicCodec, ValidatorInfoCodec, 0, db)
validatorInfosHash := binary.ReadByteSlice(r, n, err)
s.validatorInfos = merkle.NewIAVLTree(binary.BasicCodec, ValidatorInfoCodec, 0, db)
s.validatorInfos.Load(validatorInfosHash) s.validatorInfos.Load(validatorInfosHash)
if *err != nil { if *err != nil {
panic(*err) panic(*err)
@ -80,14 +80,14 @@ func (s *State) Save() {
s.accounts.Save() s.accounts.Save()
s.validatorInfos.Save() s.validatorInfos.Save()
buf, n, err := new(bytes.Buffer), new(int64), new(error) buf, n, err := new(bytes.Buffer), new(int64), new(error)
WriteUvarint(s.LastBlockHeight, buf, n, err)
WriteByteSlice(s.LastBlockHash, buf, n, err)
WriteBinary(s.LastBlockParts, buf, n, err)
WriteTime(s.LastBlockTime, buf, n, err)
WriteBinary(s.BondedValidators, buf, n, err)
WriteBinary(s.UnbondingValidators, buf, n, err)
WriteByteSlice(s.accounts.Hash(), buf, n, err)
WriteByteSlice(s.validatorInfos.Hash(), buf, n, err)
binary.WriteUvarint(s.LastBlockHeight, buf, n, err)
binary.WriteByteSlice(s.LastBlockHash, buf, n, err)
binary.WriteBinary(s.LastBlockParts, buf, n, err)
binary.WriteTime(s.LastBlockTime, buf, n, err)
binary.WriteBinary(s.BondedValidators, buf, n, err)
binary.WriteBinary(s.UnbondingValidators, buf, n, err)
binary.WriteByteSlice(s.accounts.Hash(), buf, n, err)
binary.WriteByteSlice(s.validatorInfos.Hash(), buf, n, err)
if *err != nil { if *err != nil {
panic(*err) panic(*err)
} }
@ -112,55 +112,55 @@ func (s *State) Copy() *State {
// account.PubKey.(type) != PubKeyNil, (it must be known), // account.PubKey.(type) != PubKeyNil, (it must be known),
// or it must be specified in the TxInput. If redeclared, // or it must be specified in the TxInput. If redeclared,
// the TxInput is modified and input.PubKey set to PubKeyNil. // the TxInput is modified and input.PubKey set to PubKeyNil.
func (s *State) GetOrMakeAccounts(ins []*TxInput, outs []*TxOutput) (map[string]*Account, error) {
accounts := map[string]*Account{}
func (s *State) GetOrMakeAccounts(ins []*block.TxInput, outs []*block.TxOutput) (map[string]*account.Account, error) {
accounts := map[string]*account.Account{}
for _, in := range ins { for _, in := range ins {
// Account shouldn't be duplicated // Account shouldn't be duplicated
if _, ok := accounts[string(in.Address)]; ok { if _, ok := accounts[string(in.Address)]; ok {
return nil, ErrTxDuplicateAddress
return nil, block.ErrTxDuplicateAddress
} }
account := s.GetAccount(in.Address)
if account == nil {
return nil, ErrTxInvalidAddress
acc := s.GetAccount(in.Address)
if acc == nil {
return nil, block.ErrTxInvalidAddress
} }
// PubKey should be present in either "account" or "in" // PubKey should be present in either "account" or "in"
if _, isNil := account.PubKey.(PubKeyNil); isNil {
if _, isNil := in.PubKey.(PubKeyNil); isNil {
return nil, ErrTxUnknownPubKey
if _, isNil := acc.PubKey.(account.PubKeyNil); isNil {
if _, isNil := in.PubKey.(account.PubKeyNil); isNil {
return nil, block.ErrTxUnknownPubKey
} }
if !bytes.Equal(in.PubKey.Address(), account.Address) {
return nil, ErrTxInvalidPubKey
if !bytes.Equal(in.PubKey.Address(), acc.Address) {
return nil, block.ErrTxInvalidPubKey
} }
account.PubKey = in.PubKey
acc.PubKey = in.PubKey
} else { } else {
in.PubKey = PubKeyNil{}
in.PubKey = account.PubKeyNil{}
} }
accounts[string(in.Address)] = account
accounts[string(in.Address)] = acc
} }
for _, out := range outs { for _, out := range outs {
// Account shouldn't be duplicated // Account shouldn't be duplicated
if _, ok := accounts[string(out.Address)]; ok { if _, ok := accounts[string(out.Address)]; ok {
return nil, ErrTxDuplicateAddress
return nil, block.ErrTxDuplicateAddress
} }
account := s.GetAccount(out.Address)
acc := s.GetAccount(out.Address)
// output account may be nil (new) // output account may be nil (new)
if account == nil {
account = &Account{
if acc == nil {
acc = &account.Account{
Address: out.Address, Address: out.Address,
PubKey: PubKeyNil{},
PubKey: account.PubKeyNil{},
Sequence: 0, Sequence: 0,
Balance: 0, Balance: 0,
} }
} }
accounts[string(out.Address)] = account
accounts[string(out.Address)] = acc
} }
return accounts, nil return accounts, nil
} }
func (s *State) ValidateInputs(accounts map[string]*Account, signBytes []byte, ins []*TxInput) (total uint64, err error) {
func (s *State) ValidateInputs(accounts map[string]*account.Account, signBytes []byte, ins []*block.TxInput) (total uint64, err error) {
for _, in := range ins { for _, in := range ins {
account := accounts[string(in.Address)]
if account == nil {
acc := accounts[string(in.Address)]
if acc == nil {
panic("ValidateInputs() expects account in accounts") panic("ValidateInputs() expects account in accounts")
} }
// Check TxInput basic // Check TxInput basic
@ -168,16 +168,16 @@ func (s *State) ValidateInputs(accounts map[string]*Account, signBytes []byte, i
return 0, err return 0, err
} }
// Check signatures // Check signatures
if !account.PubKey.VerifyBytes(signBytes, in.Signature) {
return 0, ErrTxInvalidSignature
if !acc.PubKey.VerifyBytes(signBytes, in.Signature) {
return 0, block.ErrTxInvalidSignature
} }
// Check sequences // Check sequences
if account.Sequence+1 != in.Sequence {
return 0, ErrTxInvalidSequence
if acc.Sequence+1 != in.Sequence {
return 0, block.ErrTxInvalidSequence
} }
// Check amount // Check amount
if account.Balance < in.Amount {
return 0, ErrTxInsufficientFunds
if acc.Balance < in.Amount {
return 0, block.ErrTxInsufficientFunds
} }
// Good. Add amount to total // Good. Add amount to total
total += in.Amount total += in.Amount
@ -185,7 +185,7 @@ func (s *State) ValidateInputs(accounts map[string]*Account, signBytes []byte, i
return total, nil return total, nil
} }
func (s *State) ValidateOutputs(outs []*TxOutput) (total uint64, err error) {
func (s *State) ValidateOutputs(outs []*block.TxOutput) (total uint64, err error) {
for _, out := range outs { for _, out := range outs {
// Check TxOutput basic // Check TxOutput basic
if err := out.ValidateBasic(); err != nil { if err := out.ValidateBasic(); err != nil {
@ -197,46 +197,46 @@ func (s *State) ValidateOutputs(outs []*TxOutput) (total uint64, err error) {
return total, nil return total, nil
} }
func (s *State) AdjustByInputs(accounts map[string]*Account, ins []*TxInput) {
func (s *State) AdjustByInputs(accounts map[string]*account.Account, ins []*block.TxInput) {
for _, in := range ins { for _, in := range ins {
account := accounts[string(in.Address)]
if account == nil {
acc := accounts[string(in.Address)]
if acc == nil {
panic("AdjustByInputs() expects account in accounts") panic("AdjustByInputs() expects account in accounts")
} }
if account.Balance < in.Amount {
if acc.Balance < in.Amount {
panic("AdjustByInputs() expects sufficient funds") panic("AdjustByInputs() expects sufficient funds")
} }
account.Balance -= in.Amount
account.Sequence += 1
acc.Balance -= in.Amount
acc.Sequence += 1
} }
} }
func (s *State) AdjustByOutputs(accounts map[string]*Account, outs []*TxOutput) {
func (s *State) AdjustByOutputs(accounts map[string]*account.Account, outs []*block.TxOutput) {
for _, out := range outs { for _, out := range outs {
account := accounts[string(out.Address)]
if account == nil {
acc := accounts[string(out.Address)]
if acc == nil {
panic("AdjustByOutputs() expects account in accounts") panic("AdjustByOutputs() expects account in accounts")
} }
account.Balance += out.Amount
acc.Balance += out.Amount
} }
} }
// If the tx is invalid, an error will be returned. // If the tx is invalid, an error will be returned.
// Unlike AppendBlock(), state will not be altered. // Unlike AppendBlock(), state will not be altered.
func (s *State) ExecTx(tx_ Tx) error {
func (s *State) ExecTx(tx_ block.Tx) error {
// TODO: do something with fees // TODO: do something with fees
fees := uint64(0) fees := uint64(0)
// Exec tx // Exec tx
switch tx_.(type) { switch tx_.(type) {
case *SendTx:
tx := tx_.(*SendTx)
case *block.SendTx:
tx := tx_.(*block.SendTx)
accounts, err := s.GetOrMakeAccounts(tx.Inputs, tx.Outputs) accounts, err := s.GetOrMakeAccounts(tx.Inputs, tx.Outputs)
if err != nil { if err != nil {
return err return err
} }
signBytes := SignBytes(tx)
signBytes := account.SignBytes(tx)
inTotal, err := s.ValidateInputs(accounts, signBytes, tx.Inputs) inTotal, err := s.ValidateInputs(accounts, signBytes, tx.Inputs)
if err != nil { if err != nil {
return err return err
@ -246,7 +246,7 @@ func (s *State) ExecTx(tx_ Tx) error {
return err return err
} }
if outTotal > inTotal { if outTotal > inTotal {
return ErrTxInsufficientFunds
return block.ErrTxInsufficientFunds
} }
fee := inTotal - outTotal fee := inTotal - outTotal
fees += fee fees += fee
@ -257,8 +257,8 @@ func (s *State) ExecTx(tx_ Tx) error {
s.UpdateAccounts(accounts) s.UpdateAccounts(accounts)
return nil return nil
case *BondTx:
tx := tx_.(*BondTx)
case *block.BondTx:
tx := tx_.(*block.BondTx)
valInfo := s.GetValidatorInfo(tx.PubKey.Address()) valInfo := s.GetValidatorInfo(tx.PubKey.Address())
if valInfo != nil { if valInfo != nil {
// TODO: In the future, check that the validator wasn't destroyed, // TODO: In the future, check that the validator wasn't destroyed,
@ -269,7 +269,7 @@ func (s *State) ExecTx(tx_ Tx) error {
if err != nil { if err != nil {
return err return err
} }
signBytes := SignBytes(tx)
signBytes := account.SignBytes(tx)
inTotal, err := s.ValidateInputs(accounts, signBytes, tx.Inputs) inTotal, err := s.ValidateInputs(accounts, signBytes, tx.Inputs)
if err != nil { if err != nil {
return err return err
@ -282,7 +282,7 @@ func (s *State) ExecTx(tx_ Tx) error {
return err return err
} }
if outTotal > inTotal { if outTotal > inTotal {
return ErrTxInsufficientFunds
return block.ErrTxInsufficientFunds
} }
fee := inTotal - outTotal fee := inTotal - outTotal
fees += fee fees += fee
@ -311,19 +311,19 @@ func (s *State) ExecTx(tx_ Tx) error {
} }
return nil return nil
case *UnbondTx:
tx := tx_.(*UnbondTx)
case *block.UnbondTx:
tx := tx_.(*block.UnbondTx)
// The validator must be active // The validator must be active
_, val := s.BondedValidators.GetByAddress(tx.Address) _, val := s.BondedValidators.GetByAddress(tx.Address)
if val == nil { if val == nil {
return ErrTxInvalidAddress
return block.ErrTxInvalidAddress
} }
// Verify the signature // Verify the signature
signBytes := SignBytes(tx)
signBytes := account.SignBytes(tx)
if !val.PubKey.VerifyBytes(signBytes, tx.Signature) { if !val.PubKey.VerifyBytes(signBytes, tx.Signature) {
return ErrTxInvalidSignature
return block.ErrTxInvalidSignature
} }
// tx.Height must be greater than val.LastCommitHeight // tx.Height must be greater than val.LastCommitHeight
@ -335,19 +335,19 @@ func (s *State) ExecTx(tx_ Tx) error {
s.unbondValidator(val) s.unbondValidator(val)
return nil return nil
case *RebondTx:
tx := tx_.(*RebondTx)
case *block.RebondTx:
tx := tx_.(*block.RebondTx)
// The validator must be inactive // The validator must be inactive
_, val := s.UnbondingValidators.GetByAddress(tx.Address) _, val := s.UnbondingValidators.GetByAddress(tx.Address)
if val == nil { if val == nil {
return ErrTxInvalidAddress
return block.ErrTxInvalidAddress
} }
// Verify the signature // Verify the signature
signBytes := SignBytes(tx)
signBytes := account.SignBytes(tx)
if !val.PubKey.VerifyBytes(signBytes, tx.Signature) { if !val.PubKey.VerifyBytes(signBytes, tx.Signature) {
return ErrTxInvalidSignature
return block.ErrTxInvalidSignature
} }
// tx.Height must be equal to the next height // tx.Height must be equal to the next height
@ -359,16 +359,16 @@ func (s *State) ExecTx(tx_ Tx) error {
s.rebondValidator(val) s.rebondValidator(val)
return nil return nil
case *DupeoutTx:
tx := tx_.(*DupeoutTx)
case *block.DupeoutTx:
tx := tx_.(*block.DupeoutTx)
// Verify the signatures // Verify the signatures
_, accused := s.BondedValidators.GetByAddress(tx.Address) _, accused := s.BondedValidators.GetByAddress(tx.Address)
voteASignBytes := SignBytes(&tx.VoteA)
voteBSignBytes := SignBytes(&tx.VoteB)
voteASignBytes := account.SignBytes(&tx.VoteA)
voteBSignBytes := account.SignBytes(&tx.VoteB)
if !accused.PubKey.VerifyBytes(voteASignBytes, tx.VoteA.Signature) || if !accused.PubKey.VerifyBytes(voteASignBytes, tx.VoteA.Signature) ||
!accused.PubKey.VerifyBytes(voteBSignBytes, tx.VoteB.Signature) { !accused.PubKey.VerifyBytes(voteBSignBytes, tx.VoteB.Signature) {
return ErrTxInvalidSignature
return block.ErrTxInvalidSignature
} }
// Verify equivocation // Verify equivocation
@ -377,7 +377,7 @@ func (s *State) ExecTx(tx_ Tx) error {
if tx.VoteA.Height != tx.VoteB.Height { if tx.VoteA.Height != tx.VoteB.Height {
return errors.New("DupeoutTx heights don't match") return errors.New("DupeoutTx heights don't match")
} }
if tx.VoteA.Type == VoteTypeCommit && tx.VoteA.Round < tx.VoteB.Round {
if tx.VoteA.Type == block.VoteTypeCommit && tx.VoteA.Round < tx.VoteB.Round {
// Check special case. // Check special case.
// Validators should not sign another vote after committing. // Validators should not sign another vote after committing.
} else { } else {
@ -477,36 +477,36 @@ func (s *State) destroyValidator(val *Validator) {
// (used for constructing a new proposal) // (used for constructing a new proposal)
// NOTE: If an error occurs during block execution, state will be left // NOTE: If an error occurs during block execution, state will be left
// at an invalid state. Copy the state before calling AppendBlock! // at an invalid state. Copy the state before calling AppendBlock!
func (s *State) AppendBlock(block *Block, blockPartsHeader PartSetHeader, checkStateHash bool) error {
func (s *State) AppendBlock(block_ *block.Block, blockPartsHeader block.PartSetHeader, checkStateHash bool) error {
// Basic block validation. // Basic block validation.
err := block.ValidateBasic(s.LastBlockHeight, s.LastBlockHash, s.LastBlockParts, s.LastBlockTime)
err := block_.ValidateBasic(s.LastBlockHeight, s.LastBlockHash, s.LastBlockParts, s.LastBlockTime)
if err != nil { if err != nil {
return err return err
} }
// Validate block Validation. // Validate block Validation.
if block.Height == 1 {
if len(block.Validation.Commits) != 0 {
if block_.Height == 1 {
if len(block_.Validation.Commits) != 0 {
return errors.New("Block at height 1 (first block) should have no Validation commits") return errors.New("Block at height 1 (first block) should have no Validation commits")
} }
} else { } else {
if uint(len(block.Validation.Commits)) != s.BondedValidators.Size() {
if uint(len(block_.Validation.Commits)) != s.BondedValidators.Size() {
return errors.New("Invalid block validation size") return errors.New("Invalid block validation size")
} }
var sumVotingPower uint64 var sumVotingPower uint64
s.BondedValidators.Iterate(func(index uint, val *Validator) bool { s.BondedValidators.Iterate(func(index uint, val *Validator) bool {
commit := block.Validation.Commits[index]
commit := block_.Validation.Commits[index]
if commit.IsZero() { if commit.IsZero() {
return false return false
} else { } else {
vote := &Vote{
Height: block.Height - 1,
vote := &block.Vote{
Height: block_.Height - 1,
Round: commit.Round, Round: commit.Round,
Type: VoteTypeCommit,
BlockHash: block.LastBlockHash,
BlockParts: block.LastBlockParts,
Type: block.VoteTypeCommit,
BlockHash: block_.LastBlockHash,
BlockParts: block_.LastBlockParts,
} }
if val.PubKey.VerifyBytes(SignBytes(vote), commit.Signature) {
if val.PubKey.VerifyBytes(account.SignBytes(vote), commit.Signature) {
sumVotingPower += val.VotingPower sumVotingPower += val.VotingPower
return false return false
} else { } else {
@ -525,7 +525,7 @@ func (s *State) AppendBlock(block *Block, blockPartsHeader PartSetHeader, checkS
} }
// Commit each tx // Commit each tx
for _, tx := range block.Data.Txs {
for _, tx := range block_.Data.Txs {
err := s.ExecTx(tx) err := s.ExecTx(tx)
if err != nil { if err != nil {
return InvalidTxError{tx, err} return InvalidTxError{tx, err}
@ -533,7 +533,7 @@ func (s *State) AppendBlock(block *Block, blockPartsHeader PartSetHeader, checkS
} }
// Update Validator.LastCommitHeight as necessary. // Update Validator.LastCommitHeight as necessary.
for i, commit := range block.Validation.Commits {
for i, commit := range block_.Validation.Commits {
if commit.IsZero() { if commit.IsZero() {
continue continue
} }
@ -541,7 +541,7 @@ func (s *State) AppendBlock(block *Block, blockPartsHeader PartSetHeader, checkS
if val == nil { if val == nil {
panic(Fmt("Failed to fetch validator at index %v", i)) panic(Fmt("Failed to fetch validator at index %v", i))
} }
val.LastCommitHeight = block.Height - 1
val.LastCommitHeight = block_.Height - 1
updated := s.BondedValidators.Update(val) updated := s.BondedValidators.Update(val)
if !updated { if !updated {
panic("Failed to update validator LastCommitHeight") panic("Failed to update validator LastCommitHeight")
@ -552,7 +552,7 @@ func (s *State) AppendBlock(block *Block, blockPartsHeader PartSetHeader, checkS
// reward account with bonded coins. // reward account with bonded coins.
toRelease := []*Validator{} toRelease := []*Validator{}
s.UnbondingValidators.Iterate(func(index uint, val *Validator) bool { s.UnbondingValidators.Iterate(func(index uint, val *Validator) bool {
if val.UnbondHeight+unbondingPeriodBlocks < block.Height {
if val.UnbondHeight+unbondingPeriodBlocks < block_.Height {
toRelease = append(toRelease, val) toRelease = append(toRelease, val)
} }
return false return false
@ -565,7 +565,7 @@ func (s *State) AppendBlock(block *Block, blockPartsHeader PartSetHeader, checkS
// unbond them, they have timed out. // unbond them, they have timed out.
toTimeout := []*Validator{} toTimeout := []*Validator{}
s.BondedValidators.Iterate(func(index uint, val *Validator) bool { s.BondedValidators.Iterate(func(index uint, val *Validator) bool {
if val.LastCommitHeight+validatorTimeoutBlocks < block.Height {
if val.LastCommitHeight+validatorTimeoutBlocks < block_.Height {
toTimeout = append(toTimeout, val) toTimeout = append(toTimeout, val)
} }
return false return false
@ -581,33 +581,33 @@ func (s *State) AppendBlock(block *Block, blockPartsHeader PartSetHeader, checkS
stateHash := s.Hash() stateHash := s.Hash()
if checkStateHash { if checkStateHash {
// State hash should match // State hash should match
if !bytes.Equal(stateHash, block.StateHash) {
if !bytes.Equal(stateHash, block_.StateHash) {
return Errorf("Invalid state hash. Got %X, block says %X", return Errorf("Invalid state hash. Got %X, block says %X",
stateHash, block.StateHash)
stateHash, block_.StateHash)
} }
} else { } else {
// Set the state hash. // Set the state hash.
if block.StateHash != nil {
panic("Cannot overwrite block.StateHash")
if block_.StateHash != nil {
panic("Cannot overwrite block_.StateHash")
} }
block.StateHash = stateHash
block_.StateHash = stateHash
} }
s.LastBlockHeight = block.Height
s.LastBlockHash = block.Hash()
s.LastBlockHeight = block_.Height
s.LastBlockHash = block_.Hash()
s.LastBlockParts = blockPartsHeader s.LastBlockParts = blockPartsHeader
s.LastBlockTime = block.Time
s.LastBlockTime = block_.Time
return nil return nil
} }
// The returned Account is a copy, so mutating it // The returned Account is a copy, so mutating it
// has no side effects. // has no side effects.
func (s *State) GetAccount(address []byte) *Account {
_, account := s.accounts.Get(address)
if account == nil {
func (s *State) GetAccount(address []byte) *account.Account {
_, acc := s.accounts.Get(address)
if acc == nil {
return nil return nil
} }
return account.(*Account).Copy()
return acc.(*account.Account).Copy()
} }
// The returned Account is a copy, so mutating it // The returned Account is a copy, so mutating it
@ -618,15 +618,15 @@ func (s *State) GetAccounts() merkle.Tree {
// The account is copied before setting, so mutating it // The account is copied before setting, so mutating it
// afterwards has no side effects. // afterwards has no side effects.
func (s *State) UpdateAccount(account *Account) {
func (s *State) UpdateAccount(account *account.Account) {
s.accounts.Set(account.Address, account.Copy()) s.accounts.Set(account.Address, account.Copy())
} }
// The accounts are copied before setting, so mutating it // The accounts are copied before setting, so mutating it
// afterwards has no side effects. // afterwards has no side effects.
func (s *State) UpdateAccounts(accounts map[string]*Account) {
for _, account := range accounts {
s.accounts.Set(account.Address, account.Copy())
func (s *State) UpdateAccounts(accounts map[string]*account.Account) {
for _, acc := range accounts {
s.accounts.Set(acc.Address, acc.Copy())
} }
} }


+ 28
- 92
state/state_test.go View File

@ -1,9 +1,9 @@
package state package state
import ( import (
. "github.com/tendermint/tendermint/account"
. "github.com/tendermint/tendermint/binary"
. "github.com/tendermint/tendermint/block"
"github.com/tendermint/tendermint/account"
"github.com/tendermint/tendermint/binary"
blk "github.com/tendermint/tendermint/block"
"github.com/tendermint/tendermint/config" "github.com/tendermint/tendermint/config"
"bytes" "bytes"
@ -58,8 +58,8 @@ func TestGenesisSaveLoad(t *testing.T) {
s0, _, _ := RandGenesisState(10, true, 1000, 5, true, 1000) s0, _, _ := RandGenesisState(10, true, 1000, 5, true, 1000)
// Mutate the state to append one empty block. // Mutate the state to append one empty block.
block := &Block{
Header: &Header{
block := &blk.Block{
Header: &blk.Header{
Network: config.App.GetString("Network"), Network: config.App.GetString("Network"),
Height: 1, Height: 1,
Time: s0.LastBlockTime.Add(time.Minute), Time: s0.LastBlockTime.Add(time.Minute),
@ -69,12 +69,12 @@ func TestGenesisSaveLoad(t *testing.T) {
LastBlockParts: s0.LastBlockParts, LastBlockParts: s0.LastBlockParts,
StateHash: nil, StateHash: nil,
}, },
Validation: &Validation{},
Data: &Data{
Txs: []Tx{},
Validation: &blk.Validation{},
Data: &blk.Data{
Txs: []blk.Tx{},
}, },
} }
blockParts := NewPartSetFromData(BinaryBytes(block))
blockParts := blk.NewPartSetFromData(binary.BinaryBytes(block))
// The last argument to AppendBlock() is `false`, // The last argument to AppendBlock() is `false`,
// which sets Block.Header.StateHash. // which sets Block.Header.StateHash.
@ -97,7 +97,7 @@ func TestGenesisSaveLoad(t *testing.T) {
s0.Save() s0.Save()
// Sanity check s0 // Sanity check s0
//s0.DB.(*db_.MemDB).Print()
//s0.DB.(*dbm.MemDB).Print()
if s0.BondedValidators.TotalVotingPower() == 0 { if s0.BondedValidators.TotalVotingPower() == 0 {
t.Error("s0 BondedValidators TotalVotingPower should not be 0") t.Error("s0 BondedValidators TotalVotingPower should not be 0")
} }
@ -143,72 +143,8 @@ func TestGenesisSaveLoad(t *testing.T) {
} }
} }
/* TODO: Write better tests, and also refactor out common code
/* TODO
func TestSendTxStateSave(t *testing.T) { func TestSendTxStateSave(t *testing.T) {
// Generate a state, save & load it.
s0, privAccounts, _ := RandGenesisState(10, true, 1000, 5, true, 1000)
sendTx := &SendTx{
Inputs: []*TxInput{
&TxInput{
Address: privAccounts[0].PubKey.Address(),
Amount: 100,
Sequence: 1,
Signature: nil,
PubKey: privAccounts[0].PubKey,
},
},
Outputs: []*TxOutput{
&TxOutput{
Address: []byte("01234567890123456789"),
Amount: 100,
},
},
}
sendTx.Inputs[0].Signature = privAccounts[0].Sign(sendTx)
// Mutate the state to append block with sendTx
block := &Block{
Header: &Header{
Network: config.App.GetString("Network"),
Height: 1,
Time: s0.LastBlockTime.Add(time.Minute),
Fees: 0,
NumTxs: 1,
LastBlockHash: s0.LastBlockHash,
LastBlockParts: s0.LastBlockParts,
StateHash: nil,
},
Validation: &Validation{},
Data: &Data{
Txs: []Tx{sendTx},
},
}
blockParts := NewPartSetFromData(BinaryBytes(block))
// The last argument to AppendBlock() is `false`,
// which sets Block.Header.StateHash.
err := s0.Copy().AppendBlock(block, blockParts.Header(), false)
if err != nil {
t.Error("Error appending initial block:", err)
}
if len(block.Header.StateHash) == 0 {
t.Error("Expected StateHash but got nothing.")
}
// Now append the block to s0.
// This time we also check the StateHash (as computed above).
err = s0.AppendBlock(block, blockParts.Header(), true)
if err != nil {
t.Error("Error appending initial block:", err)
}
// Save s0
s0.Save()
fmt.Printf("s0.accounts.Hash(): %X\n", s0.accounts.Hash())
s0.DB.Print()
} }
*/ */
@ -220,18 +156,18 @@ func TestTxSequence(t *testing.T) {
acc1 := state.GetAccount(privAccounts[1].PubKey.Address()) acc1 := state.GetAccount(privAccounts[1].PubKey.Address())
// Try executing a SendTx with various sequence numbers. // Try executing a SendTx with various sequence numbers.
makeSendTx := func(sequence uint) *SendTx {
return &SendTx{
Inputs: []*TxInput{
&TxInput{
makeSendTx := func(sequence uint) *blk.SendTx {
return &blk.SendTx{
Inputs: []*blk.TxInput{
&blk.TxInput{
Address: acc0.Address, Address: acc0.Address,
Amount: 1, Amount: 1,
Sequence: sequence, Sequence: sequence,
PubKey: acc0PubKey, PubKey: acc0PubKey,
}, },
}, },
Outputs: []*TxOutput{
&TxOutput{
Outputs: []*blk.TxOutput{
&blk.TxOutput{
Address: acc1.Address, Address: acc1.Address,
Amount: 1, Amount: 1,
}, },
@ -287,17 +223,17 @@ func TestTxs(t *testing.T) {
// SendTx. // SendTx.
{ {
state := state.Copy() state := state.Copy()
tx := &SendTx{
Inputs: []*TxInput{
&TxInput{
tx := &blk.SendTx{
Inputs: []*blk.TxInput{
&blk.TxInput{
Address: acc0.Address, Address: acc0.Address,
Amount: 1, Amount: 1,
Sequence: acc0.Sequence + 1, Sequence: acc0.Sequence + 1,
PubKey: acc0PubKey, PubKey: acc0PubKey,
}, },
}, },
Outputs: []*TxOutput{
&TxOutput{
Outputs: []*blk.TxOutput{
&blk.TxOutput{
Address: acc1.Address, Address: acc1.Address,
Amount: 1, Amount: 1,
}, },
@ -324,18 +260,18 @@ func TestTxs(t *testing.T) {
// BondTx. // BondTx.
{ {
state := state.Copy() state := state.Copy()
tx := &BondTx{
PubKey: acc0PubKey.(PubKeyEd25519),
Inputs: []*TxInput{
&TxInput{
tx := &blk.BondTx{
PubKey: acc0PubKey.(account.PubKeyEd25519),
Inputs: []*blk.TxInput{
&blk.TxInput{
Address: acc0.Address, Address: acc0.Address,
Amount: 1, Amount: 1,
Sequence: acc0.Sequence + 1, Sequence: acc0.Sequence + 1,
PubKey: acc0PubKey, PubKey: acc0PubKey,
}, },
}, },
UnbondTo: []*TxOutput{
&TxOutput{
UnbondTo: []*blk.TxOutput{
&blk.TxOutput{
Address: acc0.Address, Address: acc0.Address,
Amount: 1, Amount: 1,
}, },


+ 12
- 12
state/test.go View File

@ -4,10 +4,10 @@ import (
"bytes" "bytes"
"sort" "sort"
. "github.com/tendermint/tendermint/account"
. "github.com/tendermint/tendermint/block"
"github.com/tendermint/tendermint/account"
"github.com/tendermint/tendermint/block"
. "github.com/tendermint/tendermint/common" . "github.com/tendermint/tendermint/common"
db_ "github.com/tendermint/tendermint/db"
dbm "github.com/tendermint/tendermint/db"
"io/ioutil" "io/ioutil"
"os" "os"
@ -22,18 +22,18 @@ func Tempfile(prefix string) (*os.File, string) {
return file, file.Name() return file, file.Name()
} }
func RandAccount(randBalance bool, minBalance uint64) (*Account, *PrivAccount) {
privAccount := GenPrivAccount()
account := &Account{
func RandAccount(randBalance bool, minBalance uint64) (*account.Account, *account.PrivAccount) {
privAccount := account.GenPrivAccount()
acc := &account.Account{
Address: privAccount.PubKey.Address(), Address: privAccount.PubKey.Address(),
PubKey: privAccount.PubKey, PubKey: privAccount.PubKey,
Sequence: RandUint(), Sequence: RandUint(),
Balance: minBalance, Balance: minBalance,
} }
if randBalance { if randBalance {
account.Balance += uint64(RandUint32())
acc.Balance += uint64(RandUint32())
} }
return account, privAccount
return acc, privAccount
} }
func RandValidator(randBonded bool, minBonded uint64) (*ValidatorInfo, *PrivValidator) { func RandValidator(randBonded bool, minBonded uint64) (*ValidatorInfo, *PrivValidator) {
@ -46,7 +46,7 @@ func RandValidator(randBonded bool, minBonded uint64) (*ValidatorInfo, *PrivVali
valInfo := &ValidatorInfo{ valInfo := &ValidatorInfo{
Address: privVal.Address, Address: privVal.Address,
PubKey: privVal.PubKey, PubKey: privVal.PubKey,
UnbondTo: []*TxOutput{&TxOutput{
UnbondTo: []*block.TxOutput{&block.TxOutput{
Amount: bonded, Amount: bonded,
Address: privVal.Address, Address: privVal.Address,
}}, }},
@ -56,10 +56,10 @@ func RandValidator(randBonded bool, minBonded uint64) (*ValidatorInfo, *PrivVali
return valInfo, privVal return valInfo, privVal
} }
func RandGenesisState(numAccounts int, randBalance bool, minBalance uint64, numValidators int, randBonded bool, minBonded uint64) (*State, []*PrivAccount, []*PrivValidator) {
db := db_.NewMemDB()
func RandGenesisState(numAccounts int, randBalance bool, minBalance uint64, numValidators int, randBonded bool, minBonded uint64) (*State, []*account.PrivAccount, []*PrivValidator) {
db := dbm.NewMemDB()
accounts := make([]GenesisAccount, numAccounts) accounts := make([]GenesisAccount, numAccounts)
privAccounts := make([]*PrivAccount, numAccounts)
privAccounts := make([]*account.PrivAccount, numAccounts)
for i := 0; i < numAccounts; i++ { for i := 0; i < numAccounts; i++ {
account, privAccount := RandAccount(randBalance, minBalance) account, privAccount := RandAccount(randBalance, minBalance)
accounts[i] = GenesisAccount{ accounts[i] = GenesisAccount{


+ 14
- 14
state/validator.go View File

@ -5,16 +5,16 @@ import (
"fmt" "fmt"
"io" "io"
. "github.com/tendermint/tendermint/account"
. "github.com/tendermint/tendermint/binary"
. "github.com/tendermint/tendermint/block"
"github.com/tendermint/tendermint/account"
"github.com/tendermint/tendermint/binary"
"github.com/tendermint/tendermint/block"
) )
// Persistent (mostly) static data for each Validator // Persistent (mostly) static data for each Validator
type ValidatorInfo struct { type ValidatorInfo struct {
Address []byte Address []byte
PubKey PubKeyEd25519
UnbondTo []*TxOutput
PubKey account.PubKeyEd25519
UnbondTo []*block.TxOutput
FirstBondHeight uint FirstBondHeight uint
FirstBondAmount uint64 FirstBondAmount uint64
@ -32,14 +32,14 @@ func (valInfo *ValidatorInfo) Copy() *ValidatorInfo {
} }
func ValidatorInfoEncoder(o interface{}, w io.Writer, n *int64, err *error) { func ValidatorInfoEncoder(o interface{}, w io.Writer, n *int64, err *error) {
WriteBinary(o.(*ValidatorInfo), w, n, err)
binary.WriteBinary(o.(*ValidatorInfo), w, n, err)
} }
func ValidatorInfoDecoder(r Unreader, n *int64, err *error) interface{} {
return ReadBinary(&ValidatorInfo{}, r, n, err)
func ValidatorInfoDecoder(r io.Reader, n *int64, err *error) interface{} {
return binary.ReadBinary(&ValidatorInfo{}, r, n, err)
} }
var ValidatorInfoCodec = Codec{
var ValidatorInfoCodec = binary.Codec{
Encode: ValidatorInfoEncoder, Encode: ValidatorInfoEncoder,
Decode: ValidatorInfoDecoder, Decode: ValidatorInfoDecoder,
} }
@ -51,7 +51,7 @@ var ValidatorInfoCodec = Codec{
// every height|round so they don't go in merkle.Tree // every height|round so they don't go in merkle.Tree
type Validator struct { type Validator struct {
Address []byte Address []byte
PubKey PubKeyEd25519
PubKey account.PubKeyEd25519
BondHeight uint BondHeight uint
UnbondHeight uint UnbondHeight uint
LastCommitHeight uint LastCommitHeight uint
@ -97,7 +97,7 @@ func (v *Validator) String() string {
} }
func (v *Validator) Hash() []byte { func (v *Validator) Hash() []byte {
return BinarySha256(v)
return binary.BinarySha256(v)
} }
//------------------------------------- //-------------------------------------
@ -107,11 +107,11 @@ var ValidatorCodec = validatorCodec{}
type validatorCodec struct{} type validatorCodec struct{}
func (vc validatorCodec) Encode(o interface{}, w io.Writer, n *int64, err *error) { func (vc validatorCodec) Encode(o interface{}, w io.Writer, n *int64, err *error) {
WriteBinary(o.(*Validator), w, n, err)
binary.WriteBinary(o.(*Validator), w, n, err)
} }
func (vc validatorCodec) Decode(r Unreader, n *int64, err *error) interface{} {
return ReadBinary(&Validator{}, r, n, err)
func (vc validatorCodec) Decode(r io.Reader, n *int64, err *error) interface{} {
return binary.ReadBinary(&Validator{}, r, n, err)
} }
func (vc validatorCodec) Compare(o1 interface{}, o2 interface{}) int { func (vc validatorCodec) Compare(o1 interface{}, o2 interface{}) int {


+ 2
- 2
state/validator_set_test.go View File

@ -1,7 +1,7 @@
package state package state
import ( import (
. "github.com/tendermint/tendermint/account"
"github.com/tendermint/tendermint/account"
. "github.com/tendermint/tendermint/common" . "github.com/tendermint/tendermint/common"
"bytes" "bytes"
@ -11,7 +11,7 @@ import (
func randValidator_() *Validator { func randValidator_() *Validator {
return &Validator{ return &Validator{
Address: RandBytes(20), Address: RandBytes(20),
PubKey: PubKeyEd25519(RandBytes(64)),
PubKey: account.PubKeyEd25519(RandBytes(64)),
BondHeight: uint(RandUint32()), BondHeight: uint(RandUint32()),
VotingPower: RandUint64(), VotingPower: RandUint64(),
Accum: int64(RandUint64()), Accum: int64(RandUint64()),


Loading…
Cancel
Save