From 135894ea88b038f97b7f6ed779da17badbc74c1a Mon Sep 17 00:00:00 2001 From: Jae Kwon Date: Wed, 14 Jan 2015 20:34:53 -0800 Subject: [PATCH] Dot import -> named import Changed modulename_ to short module names Also removed Unreader, replaced with PrefixdReader in select locations --- account/account.go | 10 +- account/privkey.go | 6 +- account/pubkey.go | 10 +- account/signature.go | 6 +- account/signature_test.go | 8 +- binary/binary.go | 7 +- binary/codec.go | 4 +- binary/int.go | 10 -- binary/reflect.go | 6 +- binary/reflect_test.go | 6 +- block/block.go | 8 +- block/part_set.go | 4 +- block/store.go | 35 ++-- block/tx.go | 60 +++---- block/vote.go | 16 +- cmd/daemon.go | 26 +-- cmd/gen_account.go | 4 +- cmd/gen_tx.go | 34 ++-- cmd/gen_validator.go | 9 +- {binary => common}/bit_array.go | 4 +- {binary => common}/bit_array_test.go | 57 +----- common/io.go | 24 +++ consensus/pol.go | 24 +-- consensus/pol_test.go | 54 +++--- consensus/reactor.go | 80 ++++----- consensus/state.go | 162 ++++++++--------- consensus/state_test.go | 30 ++-- consensus/test.go | 34 ++-- consensus/types/proposal.go | 14 +- consensus/vote_set.go | 69 ++++---- consensus/vote_set_test.go | 62 +++---- mempool/mempool.go | 28 +-- mempool/reactor.go | 10 +- merkle/iavl_node.go | 20 +-- merkle/iavl_test.go | 12 +- merkle/iavl_tree.go | 16 +- merkle/simple_tree.go | 8 +- p2p/connection.go | 20 +-- p2p/peer.go | 4 +- p2p/pex_reactor.go | 4 +- p2p/switch_test.go | 14 +- rpc/blocks.go | 14 +- rpc/mempool.go | 8 +- rpc/rpc.go | 12 +- rpc/validators.go | 14 +- state/genesis.go | 38 ++-- state/priv_validator.go | 38 ++-- state/state.go | 248 +++++++++++++-------------- state/state_test.go | 120 +++---------- state/test.go | 24 +-- state/validator.go | 28 +-- state/validator_set_test.go | 4 +- 52 files changed, 728 insertions(+), 839 deletions(-) rename {binary => common}/bit_array.go (98%) rename {binary => common}/bit_array_test.go (64%) create mode 100644 common/io.go diff --git a/account/account.go b/account/account.go index 15333258c..3974413b6 100644 --- a/account/account.go +++ b/account/account.go @@ -5,7 +5,7 @@ import ( "fmt" "io" - . "github.com/tendermint/tendermint/binary" + "github.com/tendermint/tendermint/binary" ) // 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) { - 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, Decode: AccountDecoder, } diff --git a/account/privkey.go b/account/privkey.go index 87d634acb..5d9f49b74 100644 --- a/account/privkey.go +++ b/account/privkey.go @@ -4,7 +4,7 @@ import ( "errors" "github.com/tendermint/go-ed25519" - . "github.com/tendermint/tendermint/binary" + "github.com/tendermint/tendermint/binary" ) // PrivKey is part of PrivAccount and state.PrivValidator. @@ -19,9 +19,9 @@ const ( ) // for binary.readReflect -var _ = RegisterInterface( +var _ = binary.RegisterInterface( struct{ PrivKey }{}, - ConcreteType{PrivKeyEd25519{}}, + binary.ConcreteType{PrivKeyEd25519{}}, ) //------------------------------------- diff --git a/account/pubkey.go b/account/pubkey.go index d9e386707..f38c3e1a6 100644 --- a/account/pubkey.go +++ b/account/pubkey.go @@ -4,7 +4,7 @@ import ( "errors" "github.com/tendermint/go-ed25519" - . "github.com/tendermint/tendermint/binary" + "github.com/tendermint/tendermint/binary" ) // PubKey is part of Account and Validator. @@ -21,10 +21,10 @@ const ( ) // for binary.readReflect -var _ = RegisterInterface( +var _ = binary.RegisterInterface( 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 } // 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 { if len(key) != ed25519.PublicKeySize { diff --git a/account/signature.go b/account/signature.go index a61075da8..428889863 100644 --- a/account/signature.go +++ b/account/signature.go @@ -5,7 +5,7 @@ import ( "fmt" "github.com/tendermint/go-ed25519" - . "github.com/tendermint/tendermint/binary" + "github.com/tendermint/tendermint/binary" . "github.com/tendermint/tendermint/common" ) @@ -19,9 +19,9 @@ const ( ) // for binary.readReflect -var _ = RegisterInterface( +var _ = binary.RegisterInterface( struct{ Signature }{}, - ConcreteType{SignatureEd25519{}}, + binary.ConcreteType{SignatureEd25519{}}, ) //------------------------------------- diff --git a/account/signature_test.go b/account/signature_test.go index 3cba030aa..5c71d5826 100644 --- a/account/signature_test.go +++ b/account/signature_test.go @@ -4,8 +4,8 @@ import ( "bytes" "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" ) @@ -43,7 +43,7 @@ func TestBinaryDecode(t *testing.T) { t.Logf("msg: %X, sig: %X", msg, sig) buf, n, err := new(bytes.Buffer), new(int64), new(error) - WriteBinary(sig, buf, n, err) + binary.WriteBinary(sig, buf, n, err) if *err != nil { t.Fatalf("Failed to write Signature: %v", err) } @@ -56,7 +56,7 @@ func TestBinaryDecode(t *testing.T) { 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 { t.Fatalf("Failed to read Signature: %v", err) } diff --git a/binary/binary.go b/binary/binary.go index 92f2a90b5..2fb4bf34a 100644 --- a/binary/binary.go +++ b/binary/binary.go @@ -6,12 +6,7 @@ import ( "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) if rv.Kind() == reflect.Ptr { readReflect(rv.Elem(), rt.Elem(), r, n, err) diff --git a/binary/codec.go b/binary/codec.go index 79799d9f4..4f1fabe65 100644 --- a/binary/codec.go +++ b/binary/codec.go @@ -9,7 +9,7 @@ import ( ) 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 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) switch type_ { case typeByte: diff --git a/binary/int.go b/binary/int.go index 015b83a02..a63c9bb63 100644 --- a/binary/int.go +++ b/binary/int.go @@ -18,16 +18,6 @@ func ReadByte(r io.Reader, n *int64, err *error) byte { 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 func WriteInt8(i int8, w io.Writer, n *int64, err *error) { diff --git a/binary/reflect.go b/binary/reflect.go index 8193b1351..2300884fb 100644 --- a/binary/reflect.go +++ b/binary/reflect.go @@ -158,7 +158,7 @@ func RegisterType(info *TypeInfo) *TypeInfo { 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) @@ -197,7 +197,7 @@ func readReflect(rv reflect.Value, rt reflect.Type, r Unreader, n *int64, err *e switch rt.Kind() { case reflect.Interface: - typeByte := PeekByte(r, n, err) + typeByte := ReadByte(r, n, err) if *err != nil { 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)) } 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()) case reflect.Slice: diff --git a/binary/reflect_test.go b/binary/reflect_test.go index abe94054f..d8055f53a 100644 --- a/binary/reflect_test.go +++ b/binary/reflect_test.go @@ -287,9 +287,9 @@ func validateComplexArray(o interface{}, t *testing.T) { var testCases = []TestCase{} 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}) } diff --git a/block/block.go b/block/block.go index 1ff1378e7..32f041160 100644 --- a/block/block.go +++ b/block/block.go @@ -8,8 +8,8 @@ import ( "strings" "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/config" "github.com/tendermint/tendermint/merkle" @@ -125,7 +125,7 @@ type Header struct { func (h *Header) Hash() []byte { if h.hash == nil { hasher, n, err := sha256.New(), new(int64), new(error) - WriteBinary(h, hasher, n, err) + binary.WriteBinary(h, hasher, n, err) if *err != nil { panic(err) } @@ -161,7 +161,7 @@ func (h *Header) StringIndented(indent string) string { type Commit struct { Address []byte Round uint - Signature SignatureEd25519 + Signature account.SignatureEd25519 } func (commit Commit) IsZero() bool { diff --git a/block/part_set.go b/block/part_set.go index ecf3e36a2..046b95430 100644 --- a/block/part_set.go +++ b/block/part_set.go @@ -5,10 +5,10 @@ import ( "crypto/sha256" "errors" "fmt" + "io" "strings" "sync" - . "github.com/tendermint/tendermint/binary" . "github.com/tendermint/tendermint/common" "github.com/tendermint/tendermint/merkle" ) @@ -227,7 +227,7 @@ func (ps *PartSet) IsComplete() bool { return ps.count == ps.total } -func (ps *PartSet) GetReader() Unreader { +func (ps *PartSet) GetReader() io.Reader { if !ps.IsComplete() { panic("Cannot GetReader() on incomplete PartSet") } diff --git a/block/store.go b/block/store.go index 7c7df09b3..e49083ca1 100644 --- a/block/store.go +++ b/block/store.go @@ -4,10 +4,11 @@ import ( "bytes" "encoding/json" "fmt" + "io" - . "github.com/tendermint/tendermint/binary" + "github.com/tendermint/tendermint/binary" . "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 { height uint - db db_.DB + db dbm.DB } -func NewBlockStore(db db_.DB) *BlockStore { +func NewBlockStore(db dbm.DB) *BlockStore { bsjson := LoadBlockStoreStateJSON(db) return &BlockStore{ height: bsjson.Height, @@ -40,7 +41,7 @@ func (bs *BlockStore) Height() uint { return bs.height } -func (bs *BlockStore) GetReader(key []byte) Unreader { +func (bs *BlockStore) GetReader(key []byte) io.Reader { bytez := bs.db.Get(key) if bytez == nil { return nil @@ -55,7 +56,7 @@ func (bs *BlockStore) LoadBlock(height uint) *Block { if r == nil { 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 { panic(Fmt("Error reading block meta: %v", err)) } @@ -64,7 +65,7 @@ func (bs *BlockStore) LoadBlock(height uint) *Block { part := bs.LoadBlockPart(height, i) 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 { panic(Fmt("Error reading block: %v", err)) } @@ -78,7 +79,7 @@ func (bs *BlockStore) LoadBlockPart(height uint, index uint) *Part { if r == nil { 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 { panic(Fmt("Error reading block part: %v", err)) } @@ -92,7 +93,7 @@ func (bs *BlockStore) LoadBlockMeta(height uint) *BlockMeta { if r == nil { 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 { panic(Fmt("Error reading block meta: %v", err)) } @@ -109,7 +110,7 @@ func (bs *BlockStore) LoadBlockValidation(height uint) *Validation { if r == nil { 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 { panic(Fmt("Error reading validation: %v", err)) } @@ -124,7 +125,7 @@ func (bs *BlockStore) LoadSeenValidation(height uint) *Validation { if r == nil { 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 { panic(Fmt("Error reading validation: %v", err)) } @@ -149,7 +150,7 @@ func (bs *BlockStore) SaveBlock(block *Block, blockParts *PartSet, seenValidatio // Save block meta meta := makeBlockMeta(block, blockParts) - metaBytes := BinaryBytes(meta) + metaBytes := binary.BinaryBytes(meta) bs.db.Set(calcBlockMetaKey(height), metaBytes) // 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) - blockValidationBytes := BinaryBytes(block.Validation) + blockValidationBytes := binary.BinaryBytes(block.Validation) bs.db.Set(calcBlockValidationKey(height), blockValidationBytes) // Save seen validation (seen +2/3 commits) - seenValidationBytes := BinaryBytes(seenValidation) + seenValidationBytes := binary.BinaryBytes(seenValidation) bs.db.Set(calcSeenValidationKey(height), seenValidationBytes) // Save new BlockStoreStateJSON descriptor @@ -176,7 +177,7 @@ func (bs *BlockStore) saveBlockPart(height uint, index uint, part *Part) { if height != bs.height+1 { 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) } @@ -222,7 +223,7 @@ type BlockStoreStateJSON struct { Height uint } -func (bsj BlockStoreStateJSON) Save(db db_.DB) { +func (bsj BlockStoreStateJSON) Save(db dbm.DB) { bytes, err := json.Marshal(bsj) if err != nil { panic(Fmt("Could not marshal state bytes: %v", err)) @@ -230,7 +231,7 @@ func (bsj BlockStoreStateJSON) Save(db db_.DB) { db.Set(blockStoreKey, bytes) } -func LoadBlockStoreStateJSON(db db_.DB) BlockStoreStateJSON { +func LoadBlockStoreStateJSON(db dbm.DB) BlockStoreStateJSON { bytes := db.Get(blockStoreKey) if bytes == nil { return BlockStoreStateJSON{ diff --git a/block/tx.go b/block/tx.go index 3a841a39e..e9bec43be 100644 --- a/block/tx.go +++ b/block/tx.go @@ -4,8 +4,8 @@ import ( "errors" "io" - . "github.com/tendermint/tendermint/account" - . "github.com/tendermint/tendermint/binary" + "github.com/tendermint/tendermint/account" + "github.com/tendermint/tendermint/binary" ) var ( @@ -47,23 +47,23 @@ const ( ) // for binary.readReflect -var _ = RegisterInterface( +var _ = binary.RegisterInterface( 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 { - 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 { @@ -77,9 +77,9 @@ func (txIn *TxInput) ValidateBasic() 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) { - 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) 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 { 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 { out.WriteSignBytes(w, n, err) } @@ -127,7 +127,7 @@ func (tx *SendTx) WriteSignBytes(w io.Writer, n *int64, err *error) { //----------------------------------------------------------------------------- type BondTx struct { - PubKey PubKeyEd25519 + PubKey account.PubKeyEd25519 Inputs []*TxInput UnbondTo []*TxOutput } @@ -135,12 +135,12 @@ type BondTx struct { func (tx *BondTx) TypeByte() byte { return TxTypeBond } 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 { 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 { out.WriteSignBytes(w, n, err) } @@ -151,14 +151,14 @@ func (tx *BondTx) WriteSignBytes(w io.Writer, n *int64, err *error) { type UnbondTx struct { Address []byte Height uint - Signature SignatureEd25519 + Signature account.SignatureEd25519 } func (tx *UnbondTx) TypeByte() byte { return TxTypeUnbond } 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 { Address []byte Height uint - Signature SignatureEd25519 + Signature account.SignatureEd25519 } func (tx *RebondTx) TypeByte() byte { return TxTypeRebond } 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) } //----------------------------------------------------------------------------- diff --git a/block/vote.go b/block/vote.go index 0c0dafc72..079f8515d 100644 --- a/block/vote.go +++ b/block/vote.go @@ -5,8 +5,8 @@ import ( "fmt" "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" ) @@ -27,7 +27,7 @@ type Vote struct { Type byte BlockHash []byte // empty if vote is nil. BlockParts PartSetHeader // zero if vote is nil. - Signature SignatureEd25519 + Signature account.SignatureEd25519 } // Types of votes @@ -38,11 +38,11 @@ const ( ) 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 { diff --git a/cmd/daemon.go b/cmd/daemon.go index 9a281f1f1..75156007e 100644 --- a/cmd/daemon.go +++ b/cmd/daemon.go @@ -8,11 +8,11 @@ import ( . "github.com/tendermint/tendermint/common" "github.com/tendermint/tendermint/config" "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/rpc" - state_ "github.com/tendermint/tendermint/state" + sm "github.com/tendermint/tendermint/state" ) type Node struct { @@ -21,29 +21,29 @@ type Node struct { book *p2p.AddrBook pexReactor *p2p.PEXReactor blockStore *block.BlockStore - mempoolReactor *mempool_.MempoolReactor + mempoolReactor *mempl.MempoolReactor consensusState *consensus.ConsensusState consensusReactor *consensus.ConsensusReactor - privValidator *state_.PrivValidator + privValidator *sm.PrivValidator } func NewNode() *Node { // Get BlockStore - blockStoreDB := db_.GetDB("blockstore") + blockStoreDB := dbm.GetDB("blockstore") blockStore := block.NewBlockStore(blockStoreDB) // Get State - stateDB := db_.GetDB("state") - state := state_.LoadState(stateDB) + stateDB := dbm.GetDB("state") + state := sm.LoadState(stateDB) if state == nil { - state = state_.MakeGenesisStateFromFile(stateDB, config.App.GetString("GenesisFile")) + state = sm.MakeGenesisStateFromFile(stateDB, config.App.GetString("GenesisFile")) state.Save() } // Get PrivValidator - var privValidator *state_.PrivValidator + var privValidator *sm.PrivValidator 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) } else { log.Info("No PrivValidator found", "file", config.App.GetString("PrivValidatorFile")) @@ -54,8 +54,8 @@ func NewNode() *Node { pexReactor := p2p.NewPEXReactor(book) // Get MempoolReactor - mempool := mempool_.NewMempool(state.Copy()) - mempoolReactor := mempool_.NewMempoolReactor(mempool) + mempool := mempl.NewMempool(state.Copy()) + mempoolReactor := mempl.NewMempoolReactor(mempool) // Get ConsensusReactor consensusState := consensus.NewConsensusState(state, blockStore, mempoolReactor) diff --git a/cmd/gen_account.go b/cmd/gen_account.go index d7f4b2d46..aea29cf9d 100644 --- a/cmd/gen_account.go +++ b/cmd/gen_account.go @@ -4,12 +4,12 @@ import ( "fmt" "github.com/tendermint/tendermint/account" - . "github.com/tendermint/tendermint/binary" + "github.com/tendermint/tendermint/binary" ) func gen_account() { privAccount := account.GenPrivAccount() - privAccountJSONBytes := JSONBytes(privAccount) + privAccountJSONBytes := binary.JSONBytes(privAccount) fmt.Printf(`Generated a new account! %v diff --git a/cmd/gen_tx.go b/cmd/gen_tx.go index 802ee2ada..bc9756721 100644 --- a/cmd/gen_tx.go +++ b/cmd/gen_tx.go @@ -8,12 +8,12 @@ import ( "os" "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" - 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 { @@ -44,19 +44,19 @@ func getUint64(prompt string) uint64 { func gen_tx() { // 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 srcPubKeyBytes := getByteSliceFromHex("Enter source pubkey: ") 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 { Exit(Fmt("Invalid PubKey. Error: %v", err)) } // Get the state of the account. - var srcAccount *account_.Account + var srcAccount *account.Account var srcAccountAddress = srcPubKey.Address() var srcAccountBalanceStr = "unknown" var srcAccountSequenceStr = "unknown" @@ -80,18 +80,18 @@ func gen_tx() { dstSendAmount := getUint64(Fmt("Enter amount to send to %X: ", dstAddress)) // Construct SendTx - tx := &block_.SendTx{ - Inputs: []*block_.TxInput{ - &block_.TxInput{ + tx := &block.SendTx{ + Inputs: []*block.TxInput{ + &block.TxInput{ Address: srcAddress, Amount: srcSendAmount, Sequence: srcSendSequence, - Signature: account_.SignatureEd25519{}, + Signature: account.SignatureEd25519{}, PubKey: srcPubKey, }, }, - Outputs: []*block_.TxOutput{ - &block_.TxOutput{ + Outputs: []*block.TxOutput{ + &block.TxOutput{ Address: dstAddress, Amount: dstSendAmount, }, @@ -104,12 +104,12 @@ func gen_tx() { // Get source privkey (for signing) srcPrivKeyBytes := getByteSliceFromHex("Enter source privkey (for signing): ") 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 { Exit(Fmt("Invalid PrivKey. Error: %v", err)) } // 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)) } diff --git a/cmd/gen_validator.go b/cmd/gen_validator.go index 7f2b08fc5..f2777e557 100644 --- a/cmd/gen_validator.go +++ b/cmd/gen_validator.go @@ -3,16 +3,15 @@ package main import ( "fmt" + "github.com/tendermint/tendermint/binary" "github.com/tendermint/tendermint/config" - "github.com/tendermint/tendermint/state" - - . "github.com/tendermint/tendermint/binary" + sm "github.com/tendermint/tendermint/state" ) func gen_validator() { - privValidator := state.GenPrivValidator() - privValidatorJSONBytes := JSONBytes(privValidator) + privValidator := sm.GenPrivValidator() + privValidatorJSONBytes := binary.JSONBytes(privValidator) fmt.Printf(`Generated a new validator! Paste the following JSON into your %v file diff --git a/binary/bit_array.go b/common/bit_array.go similarity index 98% rename from binary/bit_array.go rename to common/bit_array.go index e3e4f7a25..62a4ef976 100644 --- a/binary/bit_array.go +++ b/common/bit_array.go @@ -1,12 +1,10 @@ -package binary +package common import ( "fmt" "math/rand" "strings" "sync" - - . "github.com/tendermint/tendermint/common" ) type BitArray struct { diff --git a/binary/bit_array_test.go b/common/bit_array_test.go similarity index 64% rename from binary/bit_array_test.go rename to common/bit_array_test.go index cf381d8b7..4802bbb9a 100644 --- a/binary/bit_array_test.go +++ b/common/bit_array_test.go @@ -1,11 +1,7 @@ -package binary +package common import ( - "bytes" - "fmt" "testing" - - . "github.com/tendermint/tendermint/common" ) func randBitArray(bits uint) (BitArray, []byte) { @@ -23,57 +19,6 @@ func randBitArray(bits uint) (BitArray, []byte) { return bA, src } -func TestReadWriteEmptyBitarray(t *testing.T) { - bA1 := BitArray{} - buf, n, err := new(bytes.Buffer), new(int64), new(error) - WriteBinary(bA1, buf, n, err) - if *err != nil { - t.Error("Failed to write empty bitarray") - } - - bA2 := ReadBinary(BitArray{}, buf, n, err).(BitArray) - if *err != nil { - t.Error("Failed to read empty bitarray") - } - if bA2.Bits != 0 { - t.Error("Expected to get bA2.Bits 0") - } -} - -func TestReadWriteBitarray(t *testing.T) { - - // Make random bA1 - bA1, testData := randBitArray(64*10 + 8) // not divisible by 64 - - // Write it - buf, n, err := new(bytes.Buffer), new(int64), new(error) - WriteBinary(bA1, buf, n, err) - if *err != nil { - t.Error("Failed to write bitarray") - } - - fmt.Printf("Bytes: %X", buf.Bytes()) - - // Read it - bA2 := ReadBinary(BitArray{}, buf, n, err).(BitArray) - if *err != nil { - t.Error("Failed to read bitarray") - } - testData2 := make([]byte, len(testData)) - for i := uint(0); i < uint(len(testData)); i++ { - for j := uint(0); j < 8; j++ { - if bA2.GetIndex(i*8 + j) { - testData2[i] |= 1 << j - } - } - } - - // Compare testData - if !bytes.Equal(testData, testData2) { - t.Errorf("Not the same:\n%X\n%X", testData, testData2) - } -} - func TestAnd(t *testing.T) { bA1, _ := randBitArray(51) diff --git a/common/io.go b/common/io.go new file mode 100644 index 000000000..0c33b0756 --- /dev/null +++ b/common/io.go @@ -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) + } +} diff --git a/consensus/pol.go b/consensus/pol.go index 20cb07897..25c769fac 100644 --- a/consensus/pol.go +++ b/consensus/pol.go @@ -3,10 +3,10 @@ package consensus import ( "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/state" + sm "github.com/tendermint/tendermint/state" ) // 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. type POLVoteSignature struct { Round uint - Signature SignatureEd25519 + Signature account.SignatureEd25519 } // Proof of lock. @@ -23,13 +23,13 @@ type POLVoteSignature struct { type POL struct { Height 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. -func (pol *POL) Verify(valSet *state.ValidatorSet) error { +func (pol *POL) Verify(valSet *sm.ValidatorSet) error { if uint(len(pol.Votes)) != valSet.Size() { 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) - 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, BlockParts: pol.BlockParts, }) @@ -54,8 +54,8 @@ func (pol *POL) Verify(valSet *state.ValidatorSet) error { // Commit vote? 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, BlockParts: pol.BlockParts, }) diff --git a/consensus/pol_test.go b/consensus/pol_test.go index d523965f2..4ec1082a7 100644 --- a/consensus/pol_test.go +++ b/consensus/pol_test.go @@ -1,10 +1,10 @@ package consensus 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" - state "github.com/tendermint/tendermint/state" + sm "github.com/tendermint/tendermint/state" "bytes" "testing" @@ -15,7 +15,7 @@ import ( // Convenience method. // Signs the vote and sets the POL's vote at the desired index // 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() err := val.SignVote(vote) if err != nil { @@ -28,7 +28,7 @@ func signAddPOLVoteSignature(val *state.PrivValidator, valSet *state.ValidatorSe func TestVerifyVotes(t *testing.T) { 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. blockHash := RandBytes(32) @@ -36,8 +36,8 @@ func TestVerifyVotes(t *testing.T) { Height: height, Round: round, BlockHash: blockHash, 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++ { signAddPOLVoteSignature(privValidators[i], valSet, voteProto, pol) @@ -59,7 +59,7 @@ func TestVerifyVotes(t *testing.T) { func TestVerifyInvalidVote(t *testing.T) { 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. blockHash := RandBytes(32) @@ -67,8 +67,8 @@ func TestVerifyInvalidVote(t *testing.T) { Height: height, Round: round, BlockHash: blockHash, 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++ { polVoteSig := signAddPOLVoteSignature(privValidators[i], valSet, voteProto, pol) @@ -83,7 +83,7 @@ func TestVerifyInvalidVote(t *testing.T) { func TestVerifyCommits(t *testing.T) { 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. blockHash := RandBytes(32) @@ -91,8 +91,8 @@ func TestVerifyCommits(t *testing.T) { Height: height, Round: round, BlockHash: blockHash, 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++ { signAddPOLVoteSignature(privValidators[i], valSet, voteProto, pol) @@ -106,7 +106,7 @@ func TestVerifyCommits(t *testing.T) { func TestVerifyInvalidCommits(t *testing.T) { 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. blockHash := RandBytes(32) @@ -114,8 +114,8 @@ func TestVerifyInvalidCommits(t *testing.T) { Height: height, Round: round, BlockHash: blockHash, 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++ { polVoteSig := signAddPOLVoteSignature(privValidators[i], valSet, voteProto, pol) @@ -130,7 +130,7 @@ func TestVerifyInvalidCommits(t *testing.T) { func TestVerifyInvalidCommitRounds(t *testing.T) { 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. blockHash := RandBytes(32) @@ -138,8 +138,8 @@ func TestVerifyInvalidCommitRounds(t *testing.T) { Height: height, Round: round, BlockHash: blockHash, 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++ { signAddPOLVoteSignature(privValidators[i], valSet, voteProto, pol) @@ -153,7 +153,7 @@ func TestVerifyInvalidCommitRounds(t *testing.T) { func TestVerifyInvalidCommitRounds2(t *testing.T) { 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. blockHash := RandBytes(32) @@ -161,8 +161,8 @@ func TestVerifyInvalidCommitRounds2(t *testing.T) { Height: height, Round: round, BlockHash: blockHash, 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++ { polVoteSig := signAddPOLVoteSignature(privValidators[i], valSet, voteProto, pol) @@ -177,7 +177,7 @@ func TestVerifyInvalidCommitRounds2(t *testing.T) { func TestReadWrite(t *testing.T) { 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. blockHash := RandBytes(32) @@ -185,8 +185,8 @@ func TestReadWrite(t *testing.T) { Height: height, Round: round, BlockHash: blockHash, 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++ { signAddPOLVoteSignature(privValidators[i], valSet, voteProto, pol) @@ -194,13 +194,13 @@ func TestReadWrite(t *testing.T) { // Write it to a buffer. buf, n, err := new(bytes.Buffer), new(int64), new(error) - WriteBinary(pol, buf, n, err) + binary.WriteBinary(pol, buf, n, err) if *err != nil { t.Fatalf("Failed to write POL: %v", *err) } // Read from buffer. - pol2 := ReadBinary(&POL{}, buf, n, err).(*POL) + pol2 := binary.ReadBinary(&POL{}, buf, n, err).(*POL) if *err != nil { t.Fatalf("Failed to read POL") } diff --git a/consensus/reactor.go b/consensus/reactor.go index a671b05c3..16c27bef1 100644 --- a/consensus/reactor.go +++ b/consensus/reactor.go @@ -8,12 +8,12 @@ import ( "sync/atomic" "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/consensus/types" "github.com/tendermint/tendermint/p2p" - "github.com/tendermint/tendermint/state" + sm "github.com/tendermint/tendermint/state" ) const ( @@ -34,11 +34,11 @@ type ConsensusReactor struct { stopped uint32 quit chan struct{} - blockStore *BlockStore + blockStore *block.BlockStore conS *ConsensusState } -func NewConsensusReactor(consensusState *ConsensusState, blockStore *BlockStore) *ConsensusReactor { +func NewConsensusReactor(consensusState *ConsensusState, blockStore *block.BlockStore) *ConsensusReactor { conR := &ConsensusReactor{ blockStore: blockStore, 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. -func (conR *ConsensusReactor) SetPrivValidator(priv *state.PrivValidator) { +func (conR *ConsensusReactor) SetPrivValidator(priv *sm.PrivValidator) { conR.conS.SetPrivValidator(priv) } @@ -398,7 +398,7 @@ OUTER_LOOP: 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 ps.EnsureVoteBitArrays(prs.Height, uint(len(validation.Commits))) @@ -406,10 +406,10 @@ OUTER_LOOP: commit := validation.Commits[index] log.Debug("Picked commit to send", "index", index, "commit", commit) // Reconstruct vote. - vote := &Vote{ + vote := &block.Vote{ Height: prs.Height, Round: commit.Round, - Type: VoteTypeCommit, + Type: block.VoteTypeCommit, BlockHash: blockMeta.Hash, BlockParts: blockMeta.Parts, Signature: commit.Signature, @@ -509,20 +509,20 @@ OUTER_LOOP: // Read only when returned by PeerState.GetRoundState(). 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() 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) { - if ps.Height == height+1 && type_ == VoteTypeCommit { + if ps.Height == height+1 && type_ == block.VoteTypeCommit { // Special case for LastCommits. ps.LastCommits.SetIndex(index, true) return @@ -628,11 +628,11 @@ func (ps *PeerState) setHasVote(height uint, round uint, type_ byte, index uint) } switch type_ { - case VoteTypePrevote: + case block.VoteTypePrevote: ps.Prevotes.SetIndex(index, true) - case VoteTypePrecommit: + case block.VoteTypePrecommit: ps.Precommits.SetIndex(index, true) - case VoteTypeCommit: + case block.VoteTypeCommit: if round < ps.Round { ps.Prevotes.SetIndex(index, true) ps.Precommits.SetIndex(index, true) @@ -670,9 +670,9 @@ func (ps *PeerState) ApplyNewRoundStepMessage(msg *NewRoundStepMessage, rs *Roun ps.StartTime = startTime if psHeight != msg.Height || psRound != msg.Round { ps.Proposal = false - ps.ProposalBlockParts = PartSetHeader{} + ps.ProposalBlockParts = block.PartSetHeader{} ps.ProposalBlockBitArray = BitArray{} - ps.ProposalPOLParts = PartSetHeader{} + ps.ProposalPOLParts = block.PartSetHeader{} ps.ProposalPOLBitArray = BitArray{} // We'll update the BitArray capacity later. ps.Prevotes = BitArray{} @@ -708,7 +708,7 @@ func (ps *PeerState) ApplyHasVoteMessage(msg *HasVoteMessage) { defer ps.mtx.Unlock() // 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) return } else if ps.Height != msg.Height { @@ -740,19 +740,19 @@ func DecodeMessage(bz []byte) (msgType byte, msg interface{}, err error) { switch msgType { // Messages for communicating state changes case msgTypeNewRoundStep: - msg = ReadBinary(&NewRoundStepMessage{}, r, n, &err) + msg = binary.ReadBinary(&NewRoundStepMessage{}, r, n, &err) case msgTypeCommitStep: - msg = ReadBinary(&CommitStepMessage{}, r, n, &err) + msg = binary.ReadBinary(&CommitStepMessage{}, r, n, &err) // Messages of data case msgTypeProposal: r.ReadByte() // Consume the byte - msg = ReadBinary(&Proposal{}, r, n, &err) + msg = binary.ReadBinary(&Proposal{}, r, n, &err) case msgTypePart: - msg = ReadBinary(&PartMessage{}, r, n, &err) + msg = binary.ReadBinary(&PartMessage{}, r, n, &err) case msgTypeVote: - msg = ReadBinary(&VoteMessage{}, r, n, &err) + msg = binary.ReadBinary(&VoteMessage{}, r, n, &err) case msgTypeHasVote: - msg = ReadBinary(&HasVoteMessage{}, r, n, &err) + msg = binary.ReadBinary(&HasVoteMessage{}, r, n, &err) default: msg = nil } @@ -778,7 +778,7 @@ func (m *NewRoundStepMessage) String() string { type CommitStepMessage struct { Height uint - BlockParts PartSetHeader + BlockParts block.PartSetHeader BlockBitArray BitArray } @@ -799,7 +799,7 @@ type PartMessage struct { Height uint Round uint Type byte - Part *Part + Part *block.Part } func (m *PartMessage) TypeByte() byte { return msgTypePart } @@ -812,7 +812,7 @@ func (m *PartMessage) String() string { type VoteMessage struct { ValidatorIndex uint - Vote *Vote + Vote *block.Vote } func (m *VoteMessage) TypeByte() byte { return msgTypeVote } diff --git a/consensus/state.go b/consensus/state.go index fcc538222..ce1b5a505 100644 --- a/consensus/state.go +++ b/consensus/state.go @@ -60,14 +60,14 @@ import ( "sync/atomic" "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/config" . "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 ( @@ -169,20 +169,20 @@ type RoundState struct { Step RoundStep StartTime time.Time CommitTime time.Time // Time when +2/3 commits were found - Validators *state.ValidatorSet + Validators *sm.ValidatorSet Proposal *Proposal - ProposalBlock *Block - ProposalBlockParts *PartSet + ProposalBlock *block.Block + ProposalBlockParts *block.PartSet ProposalPOL *POL - ProposalPOLParts *PartSet - LockedBlock *Block - LockedBlockParts *PartSet + ProposalPOLParts *block.PartSet + LockedBlock *block.Block + LockedBlockParts *block.PartSet LockedPOL *POL // Rarely needed, so no LockedPOLParts. Prevotes *VoteSet Precommits *VoteSet Commits *VoteSet LastCommits *VoteSet - PrivValidator *state.PrivValidator + PrivValidator *sm.PrivValidator } func (rs *RoundState) String() string { @@ -234,20 +234,20 @@ type ConsensusState struct { stopped uint32 quit chan struct{} - blockStore *BlockStore - mempoolReactor *mempool.MempoolReactor + blockStore *block.BlockStore + mempoolReactor *mempl.MempoolReactor runActionCh chan RoundAction newStepCh chan *RoundState mtx sync.Mutex 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. } -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{ quit: make(chan struct{}), blockStore: blockStore, @@ -259,7 +259,7 @@ func NewConsensusState(state *state.State, blockStore *BlockStore, mempoolReacto return cs } -func (cs *ConsensusState) GetState() *state.State { +func (cs *ConsensusState) GetState() *sm.State { cs.mtx.Lock() defer cs.mtx.Unlock() return cs.state.Copy() @@ -456,7 +456,7 @@ ACTION_LOOP: // 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. // 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. if cs.Height > 0 && cs.Height != state.LastBlockHeight { 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.LockedBlockParts = 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.Commits = NewVoteSet(height, 0, VoteTypeCommit, validators) + cs.Commits = NewVoteSet(height, 0, block.VoteTypeCommit, validators) cs.state = state cs.stagedBlock = nil @@ -501,7 +501,7 @@ func (cs *ConsensusState) updateToState(state *state.State) { // If we've timed out, then send rebond tx. if cs.PrivValidator != nil && cs.state.UnbondingValidators.HasAddress(cs.PrivValidator.Address) { - rebondTx := &RebondTx{ + rebondTx := &block.RebondTx{ Address: cs.PrivValidator.Address, Height: cs.Height + 1, } @@ -534,13 +534,13 @@ func (cs *ConsensusState) setupNewRound(round uint) { cs.ProposalBlockParts = nil cs.ProposalPOL = 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.Precommits = NewVoteSet(cs.Height, round, VoteTypePrecommit, validators) + cs.Precommits = NewVoteSet(cs.Height, round, block.VoteTypePrecommit, validators) cs.Precommits.AddFromCommits(cs.Commits) } -func (cs *ConsensusState) SetPrivValidator(priv *state.PrivValidator) { +func (cs *ConsensusState) SetPrivValidator(priv *sm.PrivValidator) { cs.mtx.Lock() defer cs.mtx.Unlock() 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) } - var block *Block - var blockParts *PartSet + var block_ *block.Block + var blockParts *block.PartSet var pol *POL - var polParts *PartSet + var polParts *block.PartSet // Decide on block and POL if cs.LockedBlock != nil { // If we're locked onto a block, just choose that. - block = cs.LockedBlock + block_ = cs.LockedBlock blockParts = cs.LockedBlockParts pol = cs.LockedPOL } else { // Otherwise we should create a new proposal. - var validation *Validation + var validation *block.Validation if cs.Height == 1 { // We're creating a proposal for the first block. // The validation is empty. - validation = &Validation{} + validation = &block.Validation{} } else if cs.LastCommits.HasTwoThirdsMajority() { // Make the validation from LastCommits validation = cs.LastCommits.MakeValidation() @@ -617,8 +617,8 @@ func (cs *ConsensusState) RunActionPropose(height uint, round uint) { } } txs := cs.mempoolReactor.Mempool.GetProposalTxs() - block = &Block{ - Header: &Header{ + block_ = &block.Block{ + Header: &block.Header{ Network: config.App.GetString("Network"), Height: cs.Height, Time: time.Now(), @@ -629,7 +629,7 @@ func (cs *ConsensusState) RunActionPropose(height uint, round uint) { StateHash: nil, // Will set afterwards. }, Validation: validation, - Data: &Data{ + Data: &block.Data{ Txs: txs, }, } @@ -637,14 +637,14 @@ func (cs *ConsensusState) RunActionPropose(height uint, round uint) { // Set the block.Header.StateHash. // TODO: we could cache the resulting state to cs.stagedState. // 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. } if pol != nil { - polParts = NewPartSetFromData(BinaryBytes(pol)) + polParts = block.NewPartSetFromData(binary.BinaryBytes(pol)) } // Make proposal @@ -652,10 +652,10 @@ func (cs *ConsensusState) RunActionPropose(height uint, round uint) { err := cs.PrivValidator.SignProposal(proposal) if err == nil { 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 cs.Proposal = proposal - cs.ProposalBlock = block + cs.ProposalBlock = block_ cs.ProposalBlockParts = blockParts cs.ProposalPOL = pol cs.ProposalPOLParts = polParts @@ -679,14 +679,14 @@ func (cs *ConsensusState) RunActionPrevote(height uint, round uint) { // If a block is locked, prevote that. if cs.LockedBlock != nil { - cs.signAddVote(VoteTypePrevote, cs.LockedBlock.Hash(), cs.LockedBlockParts.Header()) + cs.signAddVote(block.VoteTypePrevote, cs.LockedBlock.Hash(), cs.LockedBlockParts.Header()) return } // If ProposalBlock is nil, prevote nil. if cs.ProposalBlock == nil { log.Warn("ProposalBlock is nil") - cs.signAddVote(VoteTypePrevote, nil, PartSetHeader{}) + cs.signAddVote(block.VoteTypePrevote, nil, block.PartSetHeader{}) return } @@ -695,12 +695,12 @@ func (cs *ConsensusState) RunActionPrevote(height uint, round uint) { if err != nil { // ProposalBlock is invalid, prevote nil. log.Warn("ProposalBlock is invalid", "error", err) - cs.signAddVote(VoteTypePrevote, nil, PartSetHeader{}) + cs.signAddVote(block.VoteTypePrevote, nil, block.PartSetHeader{}) return } // Prevote cs.ProposalBlock - cs.signAddVote(VoteTypePrevote, cs.ProposalBlock.Hash(), cs.ProposalBlockParts.Header()) + cs.signAddVote(block.VoteTypePrevote, cs.ProposalBlock.Hash(), cs.ProposalBlockParts.Header()) return } @@ -736,7 +736,7 @@ func (cs *ConsensusState) RunActionPrecommit(height uint, round uint) { // If +2/3 prevoted for already locked block, precommit it. if cs.LockedBlock.HashesTo(hash) { - cs.signAddVote(VoteTypePrecommit, hash, partsHeader) + cs.signAddVote(block.VoteTypePrecommit, hash, partsHeader) return } @@ -750,7 +750,7 @@ func (cs *ConsensusState) RunActionPrecommit(height uint, round uint) { } cs.LockedBlock = cs.ProposalBlock cs.LockedBlockParts = cs.ProposalBlockParts - cs.signAddVote(VoteTypePrecommit, hash, partsHeader) + cs.signAddVote(block.VoteTypePrecommit, hash, partsHeader) return } @@ -804,7 +804,7 @@ func (cs *ConsensusState) RunActionCommit(height uint) { // We're getting the wrong block. // Set up ProposalBlockParts and keep waiting. cs.ProposalBlock = nil - cs.ProposalBlockParts = NewPartSetFromHeader(partsHeader) + cs.ProposalBlockParts = block.NewPartSetFromHeader(partsHeader) } else { // We just need to keep waiting. @@ -889,19 +889,19 @@ func (cs *ConsensusState) SetProposal(proposal *Proposal) error { } // 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 } 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 } // NOTE: block is not necessarily valid. // 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() defer cs.mtx.Unlock() @@ -922,7 +922,7 @@ func (cs *ConsensusState) AddProposalBlockPart(height uint, round uint, part *Pa if added && cs.ProposalBlockParts.IsComplete() { var n int64 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 cs.Step == RoundStepCommit { 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. -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() defer cs.mtx.Unlock() @@ -954,13 +954,13 @@ func (cs *ConsensusState) AddProposalPOLPart(height uint, round uint, part *Part if added && cs.ProposalPOLParts.IsComplete() { var n int64 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, 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() 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 { - case VoteTypePrevote: + case block.VoteTypePrevote: // Prevotes checks for height+round match. return cs.Prevotes.Add(address, vote) - case VoteTypePrecommit: + case block.VoteTypePrecommit: // Precommits checks for height+round match. return cs.Precommits.Add(address, vote) - case VoteTypeCommit: + case block.VoteTypeCommit: if vote.Height == cs.Height { // No need to check if vote.Round < cs.Round ... // 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") } // Already staged? - if cs.stagedBlock == block { + if cs.stagedBlock == block_ { return nil } @@ -1019,21 +1019,21 @@ func (cs *ConsensusState) stageBlock(block *Block, blockParts *PartSet) error { // Commit block onto the copied state. // 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 { return err } else { - cs.stagedBlock = block + cs.stagedBlock = block_ cs.stagedState = stateCopy 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) { return nil } - vote := &Vote{ + vote := &block.Vote{ Height: cs.Height, Round: cs.Round, Type: type_, @@ -1052,51 +1052,51 @@ func (cs *ConsensusState) signAddVote(type_ byte, hash []byte, header PartSetHea } // 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. - if err := cs.stageBlock(block, blockParts); err != nil { + if err := cs.stageBlock(block_, blockParts); err != nil { // Prevent zombies. log.Warn("commitVoteBlock() an invalid block", "error", err) return } // 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 { - 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, // 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. - if err := cs.stageBlock(block, blockParts); err != nil { + if err := cs.stageBlock(block_, blockParts); err != nil { // Prevent zombies. log.Warn("saveCommitVoteBlock() an invalid block", "error", err) return } // Save to blockStore. - if cs.blockStore.Height() < block.Height { + if cs.blockStore.Height() < block_.Height { seenValidation := commits.MakeValidation() - cs.blockStore.SaveBlock(block, blockParts, seenValidation) + cs.blockStore.SaveBlock(block_, blockParts, seenValidation) } // Save the state. cs.stagedState.Save() // Update mempool. - cs.mempoolReactor.Mempool.ResetForBlockAndState(block, cs.stagedState) + cs.mempoolReactor.Mempool.ResetForBlockAndState(block_, cs.stagedState) // 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 } } diff --git a/consensus/state_test.go b/consensus/state_test.go index 25dd9b1ec..d68adb4e8 100644 --- a/consensus/state_test.go +++ b/consensus/state_test.go @@ -4,7 +4,7 @@ import ( "bytes" "testing" - . "github.com/tendermint/tendermint/block" + "github.com/tendermint/tendermint/block" ) func TestSetupRound(t *testing.T) { @@ -12,9 +12,9 @@ func TestSetupRound(t *testing.T) { val0 := privValidators[0] // 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 { - 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) if err != nil { t.Error("Error signing vote: %v", err) @@ -24,13 +24,13 @@ func TestSetupRound(t *testing.T) { // Ensure that vote appears in RoundState. 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) } - 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) } - 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) } @@ -40,13 +40,13 @@ func TestSetupRound(t *testing.T) { // Now the commit should be copied over to prevotes and precommits. 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) } - 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) } - 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) } @@ -116,10 +116,10 @@ func TestRunActionPrecommitCommitFinalize(t *testing.T) { // Add at least +2/3 prevotes. for i := 0; i < 7; i++ { - vote := &Vote{ + vote := &block.Vote{ Height: 1, Round: 0, - Type: VoteTypePrevote, + Type: block.VoteTypePrevote, BlockHash: cs.ProposalBlock.Hash(), BlockParts: cs.ProposalBlockParts.Header(), } @@ -146,10 +146,10 @@ func TestRunActionPrecommitCommitFinalize(t *testing.T) { } continue } - vote := &Vote{ + vote := &block.Vote{ Height: 1, Round: 0, - Type: VoteTypePrecommit, + Type: block.VoteTypePrecommit, BlockHash: cs.ProposalBlock.Hash(), BlockParts: cs.ProposalBlockParts.Header(), } @@ -184,10 +184,10 @@ func TestRunActionPrecommitCommitFinalize(t *testing.T) { } continue } - vote := &Vote{ + vote := &block.Vote{ Height: 1, Round: uint(i), // Doesn't matter what round - Type: VoteTypeCommit, + Type: block.VoteTypeCommit, BlockHash: cs.ProposalBlock.Hash(), BlockParts: cs.ProposalBlockParts.Header(), } diff --git a/consensus/test.go b/consensus/test.go index fe09aa3a9..f1447994a 100644 --- a/consensus/test.go +++ b/consensus/test.go @@ -3,16 +3,16 @@ package consensus import ( "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 -func makeValidator(valInfo *state.ValidatorInfo) *state.Validator { - return &state.Validator{ +func makeValidator(valInfo *sm.ValidatorInfo) *sm.Validator { + return &sm.Validator{ Address: valInfo.Address, PubKey: valInfo.PubKey, 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++ { - valInfo, privValidator := state.RandValidator(false, votingPower) + valInfo, privValidator := sm.RandValidator(false, votingPower) val := makeValidator(valInfo) vals[i] = val 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 } -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) return cs, privValidators } diff --git a/consensus/types/proposal.go b/consensus/types/proposal.go index 5d1414c0d..143be81b6 100644 --- a/consensus/types/proposal.go +++ b/consensus/types/proposal.go @@ -5,8 +5,8 @@ import ( "fmt" "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" ) @@ -20,7 +20,7 @@ type Proposal struct { Round uint BlockParts PartSetHeader POLParts PartSetHeader - Signature SignatureEd25519 + Signature account.SignatureEd25519 } 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) { - 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) } diff --git a/consensus/vote_set.go b/consensus/vote_set.go index fb0d09fd8..361ca4f59 100644 --- a/consensus/vote_set.go +++ b/consensus/vote_set.go @@ -6,10 +6,11 @@ import ( "strings" "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 @@ -23,22 +24,22 @@ type VoteSet struct { type_ byte 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? votesByBlock map[string]uint64 // string(blockHash)+string(blockParts) -> vote sum. totalVotes uint64 maj23Hash []byte - maj23Parts PartSetHeader + maj23Parts block.PartSetHeader maj23Exists bool } // 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 { 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") } return &VoteSet{ @@ -46,7 +47,7 @@ func NewVoteSet(height uint, round uint, type_ byte, valSet *state.ValidatorSet) round: round, type_: type_, valSet: valSet, - votes: make([]*Vote, valSet.Size()), + votes: make([]*block.Vote, valSet.Size()), votesBitArray: NewBitArray(valSet.Size()), votesByBlock: make(map[string]uint64), totalVotes: 0, @@ -64,40 +65,40 @@ func (voteSet *VoteSet) Size() uint { // True if added, false if not. // Returns ErrVote[UnexpectedStep|InvalidAccount|InvalidSignature|InvalidBlockHash|ConflictingSignature] // 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() defer voteSet.mtx.Unlock() // Make sure the step matches. (or that vote is commit && round < voteSet.round) 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. valIndex, val := voteSet.valSet.GetByAddress(address) if val == nil { - return false, 0, ErrVoteInvalidAccount + return false, 0, block.ErrVoteInvalidAccount } // Check signature. - if !val.PubKey.VerifyBytes(SignBytes(vote), vote.Signature) { + if !val.PubKey.VerifyBytes(account.SignBytes(vote), vote.Signature) { // Bad signature. - return false, 0, ErrVoteInvalidSignature + return false, 0, block.ErrVoteInvalidSignature } 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 existingVote := voteSet.votes[valIndex]; existingVote != nil { if bytes.Equal(existingVote.BlockHash, vote.BlockHash) { return false, 0, nil } 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.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 voteSet.votesByBlock[blockKey] = totalBlockHashVotes voteSet.totalVotes += val.VotingPower @@ -145,13 +146,13 @@ func (voteSet *VoteSet) BitArray() BitArray { return voteSet.votesBitArray.Copy() } -func (voteSet *VoteSet) GetByIndex(valIndex uint) *Vote { +func (voteSet *VoteSet) GetByIndex(valIndex uint) *block.Vote { voteSet.mtx.Lock() defer voteSet.mtx.Unlock() return voteSet.votes[valIndex] } -func (voteSet *VoteSet) GetByAddress(address []byte) *Vote { +func (voteSet *VoteSet) GetByAddress(address []byte) *block.Vote { voteSet.mtx.Lock() defer voteSet.mtx.Unlock() 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. // 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() defer voteSet.mtx.Unlock() if voteSet.maj23Exists { return voteSet.maj23Hash, voteSet.maj23Parts, true } else { - return nil, PartSetHeader{}, false + return nil, block.PartSetHeader{}, false } } 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() defer voteSet.mtx.Unlock() @@ -216,17 +217,17 @@ func (voteSet *VoteSet) MakePOL() *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() defer voteSet.mtx.Unlock() if len(voteSet.maj23Hash) == 0 { 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] if vote == nil { return false @@ -237,10 +238,10 @@ func (voteSet *VoteSet) MakeValidation() *Validation { if !vote.BlockParts.Equals(voteSet.maj23Parts) { return false } - commits[valIndex] = Commit{val.Address, vote.Round, vote.Signature} + commits[valIndex] = block.Commit{val.Address, vote.Round, vote.Signature} return false }) - return &Validation{ + return &block.Validation{ Commits: commits, } } diff --git a/consensus/vote_set_test.go b/consensus/vote_set_test.go index 6ae240101..01878f6c3 100644 --- a/consensus/vote_set_test.go +++ b/consensus/vote_set_test.go @@ -3,10 +3,10 @@ package consensus import ( "bytes" - . "github.com/tendermint/tendermint/block" + "github.com/tendermint/tendermint/block" . "github.com/tendermint/tendermint/common" . "github.com/tendermint/tendermint/common/test" - "github.com/tendermint/tendermint/state" + sm "github.com/tendermint/tendermint/state" "testing" ) @@ -14,41 +14,41 @@ import ( // NOTE: see consensus/test.go for common test methods. // 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.Height = height return vote } // 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.Round = round return vote } // 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.Type = type_ return vote } // 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.BlockHash = blockHash return vote } // 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.BlockParts = blockParts 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) added, _, err := voteSet.Add(privVal.Address, vote) return added, err @@ -56,7 +56,7 @@ func signAddVote(privVal *state.PrivValidator, vote *Vote, voteSet *VoteSet) (bo func TestAddVote(t *testing.T) { 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] // t.Logf(">> %v", voteSet) @@ -72,7 +72,7 @@ func TestAddVote(t *testing.T) { 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) if voteSet.GetByAddress(val0.Address) == nil { @@ -89,9 +89,9 @@ func TestAddVote(t *testing.T) { func Test2_3Majority(t *testing.T) { 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. for i := 0; i < 6; i++ { @@ -123,13 +123,13 @@ func Test2_3Majority(t *testing.T) { func Test2_3MajorityRedux(t *testing.T) { 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) 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. for i := 0; i < 66; i++ { @@ -151,7 +151,7 @@ func Test2_3MajorityRedux(t *testing.T) { // 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) hash, header, ok = voteSet.TwoThirdsMajority() if hash != nil || !header.IsZero() || ok { @@ -161,7 +161,7 @@ func Test2_3MajorityRedux(t *testing.T) { // 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) hash, header, ok = voteSet.TwoThirdsMajority() if hash != nil || !header.IsZero() || ok { @@ -190,10 +190,10 @@ func Test2_3MajorityRedux(t *testing.T) { func TestBadVotes(t *testing.T) { 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. - 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) if !added || err != nil { t.Errorf("Expected Add() to succeed") @@ -218,7 +218,7 @@ func TestBadVotes(t *testing.T) { } // 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 { t.Errorf("Expected Add() to fail, wrong type") } @@ -226,10 +226,10 @@ func TestBadVotes(t *testing.T) { func TestAddCommitsToPrevoteVotes(t *testing.T) { 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. - 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++ { 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 - 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) if added { t.Errorf("Expected Add() to fail, wrong height.") } // 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) if added { t.Errorf("Expected Add() to fail, cannot add future round vote.") } // 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) if added || err == nil { t.Errorf("Expected Add() to fail, only prior round commits can be added.") } // 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) if !added || err != nil { t.Errorf("Expected Add() to succeed, commit for prior rounds are relevant.") } // 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) if !added || err != nil { t.Errorf("Expected Add() to succeed. err: %v", err) @@ -283,10 +283,10 @@ func TestAddCommitsToPrevoteVotes(t *testing.T) { func TestMakeValidation(t *testing.T) { 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} // 6 out of 10 voted for some block. @@ -300,7 +300,7 @@ func TestMakeValidation(t *testing.T) { // 7th voted for some other block. { 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) } diff --git a/mempool/mempool.go b/mempool/mempool.go index 37dc82334..9cb22156e 100644 --- a/mempool/mempool.go +++ b/mempool/mempool.go @@ -11,25 +11,25 @@ package mempool import ( "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 { 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{ state: state, } } // 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() defer mem.mtx.Unlock() 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() defer mem.mtx.Unlock() log.Debug("GetProposalTxs:", "txs", mem.txs) @@ -54,22 +54,22 @@ func (mem *Mempool) GetProposalTxs() []Tx { // "state" is the result of state.AppendBlock("block"). // Txs that are present in "block" are discarded from mempool. // 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() defer mem.mtx.Unlock() mem.state = state.Copy() // First, create a lookup map of txns in new block. 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{}{} } // Next, filter all txs from mem.txs that are in blockTxsMap - txs := []Tx{} + txs := []block.Tx{} for _, tx := range mem.txs { - txHash := BinarySha256(tx) + txHash := binary.BinarySha256(tx) if _, ok := blockTxsMap[string(txHash)]; ok { log.Debug("Filter out, already committed", "tx", tx, "txHash", txHash) 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. - validTxs := []Tx{} + validTxs := []block.Tx{} for _, tx := range txs { err := mem.state.ExecTx(tx) if err == nil { diff --git a/mempool/reactor.go b/mempool/reactor.go index c380a44c1..d945adde3 100644 --- a/mempool/reactor.go +++ b/mempool/reactor.go @@ -5,8 +5,8 @@ import ( "fmt" "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" ) @@ -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) if err != nil { return err @@ -126,7 +126,7 @@ func DecodeMessage(bz []byte) (msgType byte, msg interface{}, err error) { r := bytes.NewReader(bz) switch msgType { case msgTypeTx: - msg = ReadBinary(&TxMessage{}, r, n, &err) + msg = binary.ReadBinary(&TxMessage{}, r, n, &err) default: msg = nil } @@ -136,7 +136,7 @@ func DecodeMessage(bz []byte) (msgType byte, msg interface{}, err error) { //------------------------------------- type TxMessage struct { - Tx Tx + Tx block.Tx } func (m *TxMessage) TypeByte() byte { return msgTypeTx } diff --git a/merkle/iavl_node.go b/merkle/iavl_node.go index 8ffc719d2..6d7d5d634 100644 --- a/merkle/iavl_node.go +++ b/merkle/iavl_node.go @@ -4,7 +4,7 @@ import ( "crypto/sha256" "io" - . "github.com/tendermint/tendermint/binary" + "github.com/tendermint/tendermint/binary" ) // 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 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) if *err != nil { panic(*err) @@ -45,8 +45,8 @@ func ReadIAVLNode(t *IAVLTree, r Unreader, n *int64, err *error) *IAVLNode { if node.height == 0 { node.value = t.valueCodec.Decode(r, n, err) } 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 { panic(*err) @@ -254,8 +254,8 @@ func (node *IAVLNode) remove(t *IAVLTree, key interface{}) ( // NOTE: sets hashes recursively func (node *IAVLNode) writeToCountHashes(t *IAVLTree, w io.Writer) (n int64, hashCount uint64, err error) { // 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) if err != nil { return @@ -274,7 +274,7 @@ func (node *IAVLNode) writeToCountHashes(t *IAVLTree, w io.Writer) (n int64, has if node.leftHash == nil { panic("node.leftHash was nil in save") } - WriteByteSlice(node.leftHash, w, &n, &err) + binary.WriteByteSlice(node.leftHash, w, &n, &err) // right if node.rightNode != nil { 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 { panic("node.rightHash was nil in save") } - WriteByteSlice(node.rightHash, w, &n, &err) + binary.WriteByteSlice(node.rightHash, w, &n, &err) } return } diff --git a/merkle/iavl_test.go b/merkle/iavl_test.go index b498336ec..dd7897e86 100644 --- a/merkle/iavl_test.go +++ b/merkle/iavl_test.go @@ -4,7 +4,7 @@ import ( "bytes" "fmt" - . "github.com/tendermint/tendermint/binary" + "github.com/tendermint/tendermint/binary" . "github.com/tendermint/tendermint/common" "github.com/tendermint/tendermint/db" @@ -46,7 +46,7 @@ func N(l, r interface{}) *IAVLNode { // Setup a deep node func T(n *IAVLNode) *IAVLTree { - t := NewIAVLTree(BasicCodec, BasicCodec, 0, nil) + t := NewIAVLTree(binary.BasicCodec, binary.BasicCodec, 0, nil) n.hashWithCount(t) t.root = n return t @@ -152,7 +152,7 @@ func TestIntegration(t *testing.T) { } 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 { return &record{randstr(20), randstr(20)} @@ -222,7 +222,7 @@ func TestPersistence(t *testing.T) { } // 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 { t1.Set(key, value) } @@ -231,7 +231,7 @@ func TestPersistence(t *testing.T) { hash, _ := t1.HashWithCount() // Load a tree - t2 := NewIAVLTree(BasicCodec, BasicCodec, 0, db) + t2 := NewIAVLTree(binary.BasicCodec, binary.BasicCodec, 0, db) t2.Load(hash) for key, value := range records { _, t2value := t2.Get(key) @@ -244,7 +244,7 @@ func TestPersistence(t *testing.T) { func BenchmarkImmutableAvlTree(b *testing.B) { b.StopTimer() - t := NewIAVLTree(BasicCodec, BasicCodec, 0, nil) + t := NewIAVLTree(binary.BasicCodec, binary.BasicCodec, 0, nil) for i := 0; i < 1000000; i++ { t.Set(RandUint64(), "") } diff --git a/merkle/iavl_tree.go b/merkle/iavl_tree.go index d42031122..7318e0889 100644 --- a/merkle/iavl_tree.go +++ b/merkle/iavl_tree.go @@ -5,9 +5,9 @@ import ( "container/list" "sync" - . "github.com/tendermint/tendermint/binary" + "github.com/tendermint/tendermint/binary" . "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. */ type IAVLTree struct { - keyCodec Codec - valueCodec Codec + keyCodec binary.Codec + valueCodec binary.Codec root *IAVLNode 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 { // In-memory IAVLTree return &IAVLTree{ @@ -182,10 +182,10 @@ type nodeDB struct { cache map[string]nodeElement cacheSize int 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{ cache: make(map[string]nodeElement), cacheSize: cacheSize, @@ -207,7 +207,7 @@ func (ndb *nodeDB) GetNode(t *IAVLTree, hash []byte) *IAVLNode { // Doesn't exist, load. buf := ndb.db.Get(hash) if len(buf) == 0 { - ndb.db.(*db_.LevelDB).Print() + ndb.db.(*dbm.LevelDB).Print() panic(Fmt("Value missing for key %X", hash)) } r := bytes.NewReader(buf) diff --git a/merkle/simple_tree.go b/merkle/simple_tree.go index 5433dbfd7..f31e36e54 100644 --- a/merkle/simple_tree.go +++ b/merkle/simple_tree.go @@ -28,15 +28,15 @@ import ( "bytes" "crypto/sha256" - . "github.com/tendermint/tendermint/binary" + "github.com/tendermint/tendermint/binary" ) func HashFromTwoHashes(left []byte, right []byte) []byte { var n int64 var err error 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 { panic(err) } @@ -62,7 +62,7 @@ func HashFromBinaries(items []interface{}) []byte { hashes := [][]byte{} for _, item := range items { hasher, n, err := sha256.New(), new(int64), new(error) - WriteBinary(item, hasher, n, err) + binary.WriteBinary(item, hasher, n, err) if *err != nil { panic(err) } diff --git a/p2p/connection.go b/p2p/connection.go index e48e00660..2cb3c5160 100644 --- a/p2p/connection.go +++ b/p2p/connection.go @@ -12,7 +12,7 @@ import ( flow "code.google.com/p/mxk/go1/flowcontrol" "github.com/tendermint/log15" - . "github.com/tendermint/tendermint/binary" + "github.com/tendermint/tendermint/binary" . "github.com/tendermint/tendermint/common" ) @@ -182,7 +182,7 @@ func (c *MConnection) Send(chId byte, msg interface{}) bool { 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. channel, ok := c.channelsIdx[chId] @@ -191,7 +191,7 @@ func (c *MConnection) Send(chId byte, msg interface{}) bool { return false } - channel.sendBytes(BinaryBytes(msg)) + channel.sendBytes(binary.BinaryBytes(msg)) // Wake up sendRoutine if necessary select { @@ -218,7 +218,7 @@ func (c *MConnection) TrySend(chId byte, msg interface{}) bool { return false } - ok = channel.trySendBytes(BinaryBytes(msg)) + ok = channel.trySendBytes(binary.BinaryBytes(msg)) if ok { // Wake up sendRoutine if necessary select { @@ -262,12 +262,12 @@ FOR_LOOP: } case <-c.pingTimer.Ch: log.Debug("Send Ping") - WriteByte(packetTypePing, c.bufWriter, &n, &err) + binary.WriteByte(packetTypePing, c.bufWriter, &n, &err) c.sendMonitor.Update(int(n)) c.flush() case <-c.pong: log.Debug("Send Pong") - WriteByte(packetTypePong, c.bufWriter, &n, &err) + binary.WriteByte(packetTypePong, c.bufWriter, &n, &err) c.sendMonitor.Update(int(n)) c.flush() case <-c.quit: @@ -379,7 +379,7 @@ FOR_LOOP: // Read packet type var n int64 var err error - pktType := ReadByte(c.bufReader, &n, &err) + pktType := binary.ReadByte(c.bufReader, &n, &err) c.recvMonitor.Update(int(n)) if err != nil { if atomic.LoadUint32(&c.stopped) != 1 { @@ -400,7 +400,7 @@ FOR_LOOP: log.Debug("Receive Pong") case packetTypeMsg: 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)) if *err != nil { if atomic.LoadUint32(&c.stopped) != 1 { @@ -533,8 +533,8 @@ func (ch *Channel) nextMsgPacket() msgPacket { // Not goroutine-safe func (ch *Channel) writeMsgPacketTo(w io.Writer) (n int64, err error) { 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 { ch.recentlySent += n } diff --git a/p2p/peer.go b/p2p/peer.go index ae5f7f537..68137a63a 100644 --- a/p2p/peer.go +++ b/p2p/peer.go @@ -6,7 +6,7 @@ import ( "net" "sync/atomic" - . "github.com/tendermint/tendermint/binary" + "github.com/tendermint/tendermint/binary" . "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) { - WriteString(p.Key, w, &n, &err) + binary.WriteString(p.Key, w, &n, &err) return } diff --git a/p2p/pex_reactor.go b/p2p/pex_reactor.go index 2a018d07b..88372e7ae 100644 --- a/p2p/pex_reactor.go +++ b/p2p/pex_reactor.go @@ -7,7 +7,7 @@ import ( "sync/atomic" "time" - . "github.com/tendermint/tendermint/binary" + "github.com/tendermint/tendermint/binary" . "github.com/tendermint/tendermint/common" ) @@ -215,7 +215,7 @@ func DecodeMessage(bz []byte) (msg interface{}, err error) { case msgTypeRequest: msg = &pexRequestMessage{} case msgTypeAddrs: - msg = ReadBinary(&pexAddrsMessage{}, r, n, &err) + msg = binary.ReadBinary(&pexAddrsMessage{}, r, n, &err) default: msg = nil } diff --git a/p2p/switch_test.go b/p2p/switch_test.go index a5b4b6ede..9f2106c81 100644 --- a/p2p/switch_test.go +++ b/p2p/switch_test.go @@ -6,7 +6,7 @@ import ( "testing" "time" - . "github.com/tendermint/tendermint/binary" + "github.com/tendermint/tendermint/binary" . "github.com/tendermint/tendermint/common" ) @@ -144,8 +144,8 @@ func TestSwitches(t *testing.T) { if len(ch0Msgs) != 1 { 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 @@ -153,8 +153,8 @@ func TestSwitches(t *testing.T) { if len(ch1Msgs) != 1 { 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 @@ -162,8 +162,8 @@ func TestSwitches(t *testing.T) { if len(ch2Msgs) != 1 { 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) } } diff --git a/rpc/blocks.go b/rpc/blocks.go index 6427b4e28..497ab7a36 100644 --- a/rpc/blocks.go +++ b/rpc/blocks.go @@ -3,7 +3,7 @@ package rpc import ( "net/http" - . "github.com/tendermint/tendermint/block" + "github.com/tendermint/tendermint/block" . "github.com/tendermint/tendermint/common" ) @@ -20,7 +20,7 @@ func BlockchainInfoHandler(w http.ResponseWriter, r *http.Request) { } log.Debug("BlockchainInfoHandler", "maxHeight", maxHeight, "minHeight", minHeight) - blockMetas := []*BlockMeta{} + blockMetas := []*block.BlockMeta{} for height := maxHeight; height >= minHeight; height-- { blockMeta := blockStore.LoadBlockMeta(height) blockMetas = append(blockMetas, blockMeta) @@ -28,7 +28,7 @@ func BlockchainInfoHandler(w http.ResponseWriter, r *http.Request) { WriteAPIResponse(w, API_OK, struct { LastHeight uint - BlockMetas []*BlockMeta + BlockMetas []*block.BlockMeta }{blockStore.Height(), blockMetas}) } @@ -46,10 +46,10 @@ func GetBlockHandler(w http.ResponseWriter, r *http.Request) { } blockMeta := blockStore.LoadBlockMeta(height) - block := blockStore.LoadBlock(height) + block_ := blockStore.LoadBlock(height) WriteAPIResponse(w, API_OK, struct { - BlockMeta *BlockMeta - Block *Block - }{blockMeta, block}) + BlockMeta *block.BlockMeta + Block *block.Block + }{blockMeta, block_}) } diff --git a/rpc/mempool.go b/rpc/mempool.go index 6bd795c3a..469b031eb 100644 --- a/rpc/mempool.go +++ b/rpc/mempool.go @@ -3,16 +3,16 @@ package rpc import ( "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" ) func BroadcastTxHandler(w http.ResponseWriter, r *http.Request) { txJSON := GetParam(r, "tx") var err error - var tx Tx - ReadJSON(&tx, []byte(txJSON), &err) + var tx block.Tx + binary.ReadJSON(&tx, []byte(txJSON), &err) if err != nil { WriteAPIResponse(w, API_INVALID_PARAM, Fmt("Invalid tx: %v", err)) return diff --git a/rpc/rpc.go b/rpc/rpc.go index 61c8ad567..39732cea4 100644 --- a/rpc/rpc.go +++ b/rpc/rpc.go @@ -1,16 +1,16 @@ package rpc import ( - block_ "github.com/tendermint/tendermint/block" + "github.com/tendermint/tendermint/block" "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 mempoolReactor *mempool_.MempoolReactor +var mempoolReactor *mempl.MempoolReactor -func SetRPCBlockStore(bs *block_.BlockStore) { +func SetRPCBlockStore(bs *block.BlockStore) { blockStore = bs } @@ -18,6 +18,6 @@ func SetRPCConsensusState(cs *consensus.ConsensusState) { consensusState = cs } -func SetRPCMempoolReactor(mr *mempool_.MempoolReactor) { +func SetRPCMempoolReactor(mr *mempl.MempoolReactor) { mempoolReactor = mr } diff --git a/rpc/validators.go b/rpc/validators.go index 6e1ba6013..9e2775158 100644 --- a/rpc/validators.go +++ b/rpc/validators.go @@ -3,28 +3,28 @@ package rpc import ( "net/http" - state_ "github.com/tendermint/tendermint/state" + sm "github.com/tendermint/tendermint/state" ) func ListValidatorsHandler(w http.ResponseWriter, r *http.Request) { var blockHeight uint - var bondedValidators []*state_.Validator - var unbondingValidators []*state_.Validator + var bondedValidators []*sm.Validator + var unbondingValidators []*sm.Validator state := consensusState.GetState() 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) 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) return false }) WriteAPIResponse(w, API_OK, struct { BlockHeight uint - BondedValidators []*state_.Validator - UnbondingValidators []*state_.Validator + BondedValidators []*sm.Validator + UnbondingValidators []*sm.Validator }{blockHeight, bondedValidators, unbondingValidators}) } diff --git a/state/genesis.go b/state/genesis.go index ba92bbb12..dd3b95cb0 100644 --- a/state/genesis.go +++ b/state/genesis.go @@ -4,11 +4,11 @@ import ( "io/ioutil" "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" - db_ "github.com/tendermint/tendermint/db" + dbm "github.com/tendermint/tendermint/db" "github.com/tendermint/tendermint/merkle" ) @@ -18,7 +18,7 @@ type GenesisAccount struct { } type GenesisValidator struct { - PubKey PubKeyEd25519 + PubKey account.PubKeyEd25519 Amount uint64 UnbondTo []GenesisAccount } @@ -31,14 +31,14 @@ type GenesisDoc struct { func GenesisDocFromJSON(jsonBlob []byte) (genState *GenesisDoc) { var err error - ReadJSON(&genState, jsonBlob, &err) + binary.ReadJSON(&genState, jsonBlob, &err) if err != nil { panic(Fmt("Couldn't read GenesisDoc: %v", err)) } return } -func MakeGenesisStateFromFile(db db_.DB, genDocFile string) *State { +func MakeGenesisStateFromFile(db dbm.DB, genDocFile string) *State { jsonBlob, err := ioutil.ReadFile(genDocFile) if err != nil { 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) } -func MakeGenesisState(db db_.DB, genDoc *GenesisDoc) *State { +func MakeGenesisState(db dbm.DB, genDoc *GenesisDoc) *State { if len(genDoc.Validators) == 0 { Exit(Fmt("The genesis file has no validators")) } @@ -57,19 +57,19 @@ func MakeGenesisState(db db_.DB, genDoc *GenesisDoc) *State { } // 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, - Balance: acc.Amount, + Balance: genAcc.Amount, } - accounts.Set(acc.Address, account) + accounts.Set(acc.Address, acc) } // 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)) for i, val := range genDoc.Validators { pubKey := val.PubKey @@ -79,12 +79,12 @@ func MakeGenesisState(db db_.DB, genDoc *GenesisDoc) *State { valInfo := &ValidatorInfo{ Address: address, PubKey: pubKey, - UnbondTo: make([]*TxOutput, len(val.UnbondTo)), + UnbondTo: make([]*block.TxOutput, len(val.UnbondTo)), FirstBondHeight: 0, FirstBondAmount: val.Amount, } for i, unbondTo := range val.UnbondTo { - valInfo.UnbondTo[i] = &TxOutput{ + valInfo.UnbondTo[i] = &block.TxOutput{ Address: unbondTo.Address, Amount: unbondTo.Amount, } @@ -107,7 +107,7 @@ func MakeGenesisState(db db_.DB, genDoc *GenesisDoc) *State { DB: db, LastBlockHeight: 0, LastBlockHash: nil, - LastBlockParts: PartSetHeader{}, + LastBlockParts: block.PartSetHeader{}, LastBlockTime: genDoc.GenesisTime, BondedValidators: NewValidatorSet(validators), UnbondingValidators: NewValidatorSet(nil), diff --git a/state/priv_validator.go b/state/priv_validator.go index 7d235c40f..f97e781bb 100644 --- a/state/priv_validator.go +++ b/state/priv_validator.go @@ -9,9 +9,9 @@ import ( "math" "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/config" . "github.com/tendermint/tendermint/consensus/types" @@ -27,13 +27,13 @@ const ( stepCommit = 4 ) -func voteToStep(vote *Vote) uint8 { +func voteToStep(vote *block.Vote) uint8 { switch vote.Type { - case VoteTypePrevote: + case block.VoteTypePrevote: return stepPrevote - case VoteTypePrecommit: + case block.VoteTypePrecommit: return stepPrecommit - case VoteTypeCommit: + case block.VoteTypeCommit: return stepCommit default: panic("Unknown vote type") @@ -42,8 +42,8 @@ func voteToStep(vote *Vote) uint8 { type PrivValidator struct { Address []byte - PubKey PubKeyEd25519 - PrivKey PrivKeyEd25519 + PubKey account.PubKeyEd25519 + PrivKey account.PrivKeyEd25519 LastHeight uint LastRound uint LastStep uint8 @@ -58,8 +58,8 @@ type PrivValidator struct { func GenPrivValidator() *PrivValidator { privKeyBytes := CRandBytes(32) pubKeyBytes := ed25519.MakePubKey(privKeyBytes) - pubKey := PubKeyEd25519(pubKeyBytes) - privKey := PrivKeyEd25519(privKeyBytes) + pubKey := account.PubKeyEd25519(pubKeyBytes) + privKey := account.PrivKeyEd25519(privKeyBytes) return &PrivValidator{ Address: pubKey.Address(), PubKey: pubKey, @@ -76,7 +76,7 @@ func LoadPrivValidator(filename string) *PrivValidator { if err != nil { panic(err) } - privVal := ReadJSON(&PrivValidator{}, privValJSONBytes, &err).(*PrivValidator) + privVal := binary.ReadJSON(&PrivValidator{}, privValJSONBytes, &err).(*PrivValidator) if err != nil { Exit(Fmt("Error reading PrivValidator from %v: %v\n", filename, err)) } @@ -91,7 +91,7 @@ func (privVal *PrivValidator) Save() { } func (privVal *PrivValidator) save() { - jsonBytes := JSONBytes(privVal) + jsonBytes := binary.JSONBytes(privVal) err := ioutil.WriteFile(privVal.filename, jsonBytes, 0700) if err != nil { panic(err) @@ -99,7 +99,7 @@ func (privVal *PrivValidator) save() { } // TODO: test -func (privVal *PrivValidator) SignVote(vote *Vote) error { +func (privVal *PrivValidator) SignVote(vote *block.Vote) error { privVal.mtx.Lock() defer privVal.mtx.Unlock() @@ -134,8 +134,8 @@ func (privVal *PrivValidator) SignVote(vote *Vote) error { 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 { @@ -152,14 +152,14 @@ func (privVal *PrivValidator) SignProposal(proposal *Proposal) error { privVal.save() // Sign - proposal.Signature = privVal.PrivKey.Sign(SignBytes(proposal)).(SignatureEd25519) + proposal.Signature = privVal.PrivKey.Sign(account.SignBytes(proposal)).(account.SignatureEd25519) return nil } else { 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() defer privVal.mtx.Unlock() if privVal.LastHeight < rebondTx.Height { @@ -171,7 +171,7 @@ func (privVal *PrivValidator) SignRebondTx(rebondTx *RebondTx) error { privVal.save() // Sign - rebondTx.Signature = privVal.PrivKey.Sign(SignBytes(rebondTx)).(SignatureEd25519) + rebondTx.Signature = privVal.PrivKey.Sign(account.SignBytes(rebondTx)).(account.SignatureEd25519) return nil } else { return errors.New(fmt.Sprintf("Attempt of duplicate signing of rebondTx: Height %v", rebondTx.Height)) diff --git a/state/state.go b/state/state.go index 0477568f1..23cae5bd0 100644 --- a/state/state.go +++ b/state/state.go @@ -6,11 +6,11 @@ import ( "fmt" "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" - db_ "github.com/tendermint/tendermint/db" + dbm "github.com/tendermint/tendermint/db" "github.com/tendermint/tendermint/merkle" ) @@ -25,7 +25,7 @@ var ( //----------------------------------------------------------------------------- type InvalidTxError struct { - Tx Tx + Tx block.Tx Reason error } @@ -37,10 +37,10 @@ func (txErr InvalidTxError) Error() string { // NOTE: not goroutine-safe. type State struct { - DB db_.DB + DB dbm.DB LastBlockHeight uint LastBlockHash []byte - LastBlockParts PartSetHeader + LastBlockParts block.PartSetHeader LastBlockTime time.Time BondedValidators *ValidatorSet UnbondingValidators *ValidatorSet @@ -48,24 +48,24 @@ type State struct { validatorInfos merkle.Tree // Shouldn't be accessed directly. } -func LoadState(db db_.DB) *State { +func LoadState(db dbm.DB) *State { s := &State{DB: db} buf := db.Get(stateKey) if len(buf) == 0 { return nil } else { 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) - 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) if *err != nil { panic(*err) @@ -80,14 +80,14 @@ func (s *State) Save() { s.accounts.Save() s.validatorInfos.Save() 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 { panic(*err) } @@ -112,55 +112,55 @@ func (s *State) Copy() *State { // account.PubKey.(type) != PubKeyNil, (it must be known), // or it must be specified in the TxInput. If redeclared, // 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 { // Account shouldn't be duplicated 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" - 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 { - in.PubKey = PubKeyNil{} + in.PubKey = account.PubKeyNil{} } - accounts[string(in.Address)] = account + accounts[string(in.Address)] = acc } for _, out := range outs { // Account shouldn't be duplicated 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) - if account == nil { - account = &Account{ + if acc == nil { + acc = &account.Account{ Address: out.Address, - PubKey: PubKeyNil{}, + PubKey: account.PubKeyNil{}, Sequence: 0, Balance: 0, } } - accounts[string(out.Address)] = account + accounts[string(out.Address)] = acc } 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 { - account := accounts[string(in.Address)] - if account == nil { + acc := accounts[string(in.Address)] + if acc == nil { panic("ValidateInputs() expects account in accounts") } // Check TxInput basic @@ -168,16 +168,16 @@ func (s *State) ValidateInputs(accounts map[string]*Account, signBytes []byte, i return 0, err } // 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 - if account.Sequence+1 != in.Sequence { - return 0, ErrTxInvalidSequence + if acc.Sequence+1 != in.Sequence { + return 0, block.ErrTxInvalidSequence } // Check amount - if account.Balance < in.Amount { - return 0, ErrTxInsufficientFunds + if acc.Balance < in.Amount { + return 0, block.ErrTxInsufficientFunds } // Good. Add amount to total total += in.Amount @@ -185,7 +185,7 @@ func (s *State) ValidateInputs(accounts map[string]*Account, signBytes []byte, i 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 { // Check TxOutput basic if err := out.ValidateBasic(); err != nil { @@ -197,46 +197,46 @@ func (s *State) ValidateOutputs(outs []*TxOutput) (total uint64, err error) { 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 { - account := accounts[string(in.Address)] - if account == nil { + acc := accounts[string(in.Address)] + if acc == nil { panic("AdjustByInputs() expects account in accounts") } - if account.Balance < in.Amount { + if acc.Balance < in.Amount { 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 { - account := accounts[string(out.Address)] - if account == nil { + acc := accounts[string(out.Address)] + if acc == nil { panic("AdjustByOutputs() expects account in accounts") } - account.Balance += out.Amount + acc.Balance += out.Amount } } // If the tx is invalid, an error will be returned. // 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 fees := uint64(0) // Exec tx switch tx_.(type) { - case *SendTx: - tx := tx_.(*SendTx) + case *block.SendTx: + tx := tx_.(*block.SendTx) accounts, err := s.GetOrMakeAccounts(tx.Inputs, tx.Outputs) if err != nil { return err } - signBytes := SignBytes(tx) + signBytes := account.SignBytes(tx) inTotal, err := s.ValidateInputs(accounts, signBytes, tx.Inputs) if err != nil { return err @@ -246,7 +246,7 @@ func (s *State) ExecTx(tx_ Tx) error { return err } if outTotal > inTotal { - return ErrTxInsufficientFunds + return block.ErrTxInsufficientFunds } fee := inTotal - outTotal fees += fee @@ -257,8 +257,8 @@ func (s *State) ExecTx(tx_ Tx) error { s.UpdateAccounts(accounts) return nil - case *BondTx: - tx := tx_.(*BondTx) + case *block.BondTx: + tx := tx_.(*block.BondTx) valInfo := s.GetValidatorInfo(tx.PubKey.Address()) if valInfo != nil { // 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 { return err } - signBytes := SignBytes(tx) + signBytes := account.SignBytes(tx) inTotal, err := s.ValidateInputs(accounts, signBytes, tx.Inputs) if err != nil { return err @@ -282,7 +282,7 @@ func (s *State) ExecTx(tx_ Tx) error { return err } if outTotal > inTotal { - return ErrTxInsufficientFunds + return block.ErrTxInsufficientFunds } fee := inTotal - outTotal fees += fee @@ -311,19 +311,19 @@ func (s *State) ExecTx(tx_ Tx) error { } return nil - case *UnbondTx: - tx := tx_.(*UnbondTx) + case *block.UnbondTx: + tx := tx_.(*block.UnbondTx) // The validator must be active _, val := s.BondedValidators.GetByAddress(tx.Address) if val == nil { - return ErrTxInvalidAddress + return block.ErrTxInvalidAddress } // Verify the signature - signBytes := SignBytes(tx) + signBytes := account.SignBytes(tx) if !val.PubKey.VerifyBytes(signBytes, tx.Signature) { - return ErrTxInvalidSignature + return block.ErrTxInvalidSignature } // tx.Height must be greater than val.LastCommitHeight @@ -335,19 +335,19 @@ func (s *State) ExecTx(tx_ Tx) error { s.unbondValidator(val) return nil - case *RebondTx: - tx := tx_.(*RebondTx) + case *block.RebondTx: + tx := tx_.(*block.RebondTx) // The validator must be inactive _, val := s.UnbondingValidators.GetByAddress(tx.Address) if val == nil { - return ErrTxInvalidAddress + return block.ErrTxInvalidAddress } // Verify the signature - signBytes := SignBytes(tx) + signBytes := account.SignBytes(tx) if !val.PubKey.VerifyBytes(signBytes, tx.Signature) { - return ErrTxInvalidSignature + return block.ErrTxInvalidSignature } // tx.Height must be equal to the next height @@ -359,16 +359,16 @@ func (s *State) ExecTx(tx_ Tx) error { s.rebondValidator(val) return nil - case *DupeoutTx: - tx := tx_.(*DupeoutTx) + case *block.DupeoutTx: + tx := tx_.(*block.DupeoutTx) // Verify the signatures _, 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) || !accused.PubKey.VerifyBytes(voteBSignBytes, tx.VoteB.Signature) { - return ErrTxInvalidSignature + return block.ErrTxInvalidSignature } // Verify equivocation @@ -377,7 +377,7 @@ func (s *State) ExecTx(tx_ Tx) error { if tx.VoteA.Height != tx.VoteB.Height { 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. // Validators should not sign another vote after committing. } else { @@ -477,36 +477,36 @@ func (s *State) destroyValidator(val *Validator) { // (used for constructing a new proposal) // NOTE: If an error occurs during block execution, state will be left // 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. - 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 { return err } // 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") } } 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") } var sumVotingPower uint64 s.BondedValidators.Iterate(func(index uint, val *Validator) bool { - commit := block.Validation.Commits[index] + commit := block_.Validation.Commits[index] if commit.IsZero() { return false } else { - vote := &Vote{ - Height: block.Height - 1, + vote := &block.Vote{ + Height: block_.Height - 1, 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 return false } else { @@ -525,7 +525,7 @@ func (s *State) AppendBlock(block *Block, blockPartsHeader PartSetHeader, checkS } // Commit each tx - for _, tx := range block.Data.Txs { + for _, tx := range block_.Data.Txs { err := s.ExecTx(tx) if err != nil { return InvalidTxError{tx, err} @@ -533,7 +533,7 @@ func (s *State) AppendBlock(block *Block, blockPartsHeader PartSetHeader, checkS } // Update Validator.LastCommitHeight as necessary. - for i, commit := range block.Validation.Commits { + for i, commit := range block_.Validation.Commits { if commit.IsZero() { continue } @@ -541,7 +541,7 @@ func (s *State) AppendBlock(block *Block, blockPartsHeader PartSetHeader, checkS if val == nil { 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) if !updated { panic("Failed to update validator LastCommitHeight") @@ -552,7 +552,7 @@ func (s *State) AppendBlock(block *Block, blockPartsHeader PartSetHeader, checkS // reward account with bonded coins. toRelease := []*Validator{} 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) } return false @@ -565,7 +565,7 @@ func (s *State) AppendBlock(block *Block, blockPartsHeader PartSetHeader, checkS // unbond them, they have timed out. toTimeout := []*Validator{} 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) } return false @@ -581,33 +581,33 @@ func (s *State) AppendBlock(block *Block, blockPartsHeader PartSetHeader, checkS stateHash := s.Hash() if checkStateHash { // 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", - stateHash, block.StateHash) + stateHash, block_.StateHash) } } else { // 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.LastBlockTime = block.Time + s.LastBlockTime = block_.Time return nil } // The returned Account is a copy, so mutating it // 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 account.(*Account).Copy() + return acc.(*account.Account).Copy() } // 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 // 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()) } // The accounts are copied before setting, so mutating it // 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()) } } diff --git a/state/state_test.go b/state/state_test.go index 4158e1d43..c0622d196 100644 --- a/state/state_test.go +++ b/state/state_test.go @@ -1,9 +1,9 @@ package state 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" "bytes" @@ -58,8 +58,8 @@ func TestGenesisSaveLoad(t *testing.T) { s0, _, _ := RandGenesisState(10, true, 1000, 5, true, 1000) // Mutate the state to append one empty block. - block := &Block{ - Header: &Header{ + block := &blk.Block{ + Header: &blk.Header{ Network: config.App.GetString("Network"), Height: 1, Time: s0.LastBlockTime.Add(time.Minute), @@ -69,12 +69,12 @@ func TestGenesisSaveLoad(t *testing.T) { LastBlockParts: s0.LastBlockParts, 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`, // which sets Block.Header.StateHash. @@ -97,7 +97,7 @@ func TestGenesisSaveLoad(t *testing.T) { s0.Save() // Sanity check s0 - //s0.DB.(*db_.MemDB).Print() + //s0.DB.(*dbm.MemDB).Print() if s0.BondedValidators.TotalVotingPower() == 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) { - - // 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()) // 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, Amount: 1, Sequence: sequence, PubKey: acc0PubKey, }, }, - Outputs: []*TxOutput{ - &TxOutput{ + Outputs: []*blk.TxOutput{ + &blk.TxOutput{ Address: acc1.Address, Amount: 1, }, @@ -287,17 +223,17 @@ func TestTxs(t *testing.T) { // SendTx. { state := state.Copy() - tx := &SendTx{ - Inputs: []*TxInput{ - &TxInput{ + tx := &blk.SendTx{ + Inputs: []*blk.TxInput{ + &blk.TxInput{ Address: acc0.Address, Amount: 1, Sequence: acc0.Sequence + 1, PubKey: acc0PubKey, }, }, - Outputs: []*TxOutput{ - &TxOutput{ + Outputs: []*blk.TxOutput{ + &blk.TxOutput{ Address: acc1.Address, Amount: 1, }, @@ -324,18 +260,18 @@ func TestTxs(t *testing.T) { // BondTx. { 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, Amount: 1, Sequence: acc0.Sequence + 1, PubKey: acc0PubKey, }, }, - UnbondTo: []*TxOutput{ - &TxOutput{ + UnbondTo: []*blk.TxOutput{ + &blk.TxOutput{ Address: acc0.Address, Amount: 1, }, diff --git a/state/test.go b/state/test.go index 931dfece0..b72c64b76 100644 --- a/state/test.go +++ b/state/test.go @@ -4,10 +4,10 @@ import ( "bytes" "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" - db_ "github.com/tendermint/tendermint/db" + dbm "github.com/tendermint/tendermint/db" "io/ioutil" "os" @@ -22,18 +22,18 @@ func Tempfile(prefix string) (*os.File, string) { 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(), PubKey: privAccount.PubKey, Sequence: RandUint(), Balance: minBalance, } if randBalance { - account.Balance += uint64(RandUint32()) + acc.Balance += uint64(RandUint32()) } - return account, privAccount + return acc, privAccount } func RandValidator(randBonded bool, minBonded uint64) (*ValidatorInfo, *PrivValidator) { @@ -46,7 +46,7 @@ func RandValidator(randBonded bool, minBonded uint64) (*ValidatorInfo, *PrivVali valInfo := &ValidatorInfo{ Address: privVal.Address, PubKey: privVal.PubKey, - UnbondTo: []*TxOutput{&TxOutput{ + UnbondTo: []*block.TxOutput{&block.TxOutput{ Amount: bonded, Address: privVal.Address, }}, @@ -56,10 +56,10 @@ func RandValidator(randBonded bool, minBonded uint64) (*ValidatorInfo, *PrivVali 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) - privAccounts := make([]*PrivAccount, numAccounts) + privAccounts := make([]*account.PrivAccount, numAccounts) for i := 0; i < numAccounts; i++ { account, privAccount := RandAccount(randBalance, minBalance) accounts[i] = GenesisAccount{ diff --git a/state/validator.go b/state/validator.go index 405518a87..dd6b1731b 100644 --- a/state/validator.go +++ b/state/validator.go @@ -5,16 +5,16 @@ import ( "fmt" "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 type ValidatorInfo struct { Address []byte - PubKey PubKeyEd25519 - UnbondTo []*TxOutput + PubKey account.PubKeyEd25519 + UnbondTo []*block.TxOutput FirstBondHeight uint FirstBondAmount uint64 @@ -32,14 +32,14 @@ func (valInfo *ValidatorInfo) Copy() *ValidatorInfo { } 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, Decode: ValidatorInfoDecoder, } @@ -51,7 +51,7 @@ var ValidatorInfoCodec = Codec{ // every height|round so they don't go in merkle.Tree type Validator struct { Address []byte - PubKey PubKeyEd25519 + PubKey account.PubKeyEd25519 BondHeight uint UnbondHeight uint LastCommitHeight uint @@ -97,7 +97,7 @@ func (v *Validator) String() string { } func (v *Validator) Hash() []byte { - return BinarySha256(v) + return binary.BinarySha256(v) } //------------------------------------- @@ -107,11 +107,11 @@ var ValidatorCodec = validatorCodec{} type validatorCodec struct{} 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 { diff --git a/state/validator_set_test.go b/state/validator_set_test.go index ee1034676..3c9b3d943 100644 --- a/state/validator_set_test.go +++ b/state/validator_set_test.go @@ -1,7 +1,7 @@ package state import ( - . "github.com/tendermint/tendermint/account" + "github.com/tendermint/tendermint/account" . "github.com/tendermint/tendermint/common" "bytes" @@ -11,7 +11,7 @@ import ( func randValidator_() *Validator { return &Validator{ Address: RandBytes(20), - PubKey: PubKeyEd25519(RandBytes(64)), + PubKey: account.PubKeyEd25519(RandBytes(64)), BondHeight: uint(RandUint32()), VotingPower: RandUint64(), Accum: int64(RandUint64()),