From bdb34f9f4e81d99d9308cb930b9cbad195e7ce7d Mon Sep 17 00:00:00 2001 From: Ethan Buchman Date: Thu, 27 Apr 2017 19:01:18 -0400 Subject: [PATCH] types: []byte -> data.Bytes --- types/block.go | 23 ++++++++++++----------- types/canonical_json.go | 10 +++++++--- types/events.go | 9 +++++---- types/genesis.go | 7 ++++--- types/part_set.go | 27 ++++++++++++++------------- types/tx.go | 3 ++- types/tx_test.go | 6 +++--- types/validator.go | 13 +++++++------ types/vote.go | 11 ++++++----- 9 files changed, 60 insertions(+), 49 deletions(-) diff --git a/types/block.go b/types/block.go index 88288a2b..65d4031d 100644 --- a/types/block.go +++ b/types/block.go @@ -8,9 +8,10 @@ import ( "strings" "time" + "github.com/tendermint/go-wire" + "github.com/tendermint/go-wire/data" . "github.com/tendermint/tmlibs/common" "github.com/tendermint/tmlibs/merkle" - "github.com/tendermint/go-wire" ) const MaxBlockSize = 22020096 // 21MB TODO make it configurable @@ -150,15 +151,15 @@ func (b *Block) StringShort() string { //----------------------------------------------------------------------------- type Header struct { - ChainID string `json:"chain_id"` - Height int `json:"height"` - Time time.Time `json:"time"` - NumTxs int `json:"num_txs"` // XXX: Can we get rid of this? - LastBlockID BlockID `json:"last_block_id"` - LastCommitHash []byte `json:"last_commit_hash"` // commit from validators from the last block - DataHash []byte `json:"data_hash"` // transactions - ValidatorsHash []byte `json:"validators_hash"` // validators for the current block - AppHash []byte `json:"app_hash"` // state after txs from the previous block + ChainID string `json:"chain_id"` + Height int `json:"height"` + Time time.Time `json:"time"` + NumTxs int `json:"num_txs"` // XXX: Can we get rid of this? + LastBlockID BlockID `json:"last_block_id"` + LastCommitHash data.Bytes `json:"last_commit_hash"` // commit from validators from the last block + DataHash data.Bytes `json:"data_hash"` // transactions + ValidatorsHash data.Bytes `json:"validators_hash"` // validators for the current block + AppHash data.Bytes `json:"app_hash"` // state after txs from the previous block } // NOTE: hash is nil if required fields are missing. @@ -388,7 +389,7 @@ func (data *Data) StringIndented(indent string) string { //-------------------------------------------------------------------------------- type BlockID struct { - Hash []byte `json:"hash"` + Hash data.Bytes `json:"hash"` PartsHeader PartSetHeader `json:"parts"` } diff --git a/types/canonical_json.go b/types/canonical_json.go index 68dd6924..2e8583a4 100644 --- a/types/canonical_json.go +++ b/types/canonical_json.go @@ -1,15 +1,19 @@ package types +import ( + "github.com/tendermint/go-wire/data" +) + // canonical json is go-wire's json for structs with fields in alphabetical order type CanonicalJSONBlockID struct { - Hash []byte `json:"hash,omitempty"` + Hash data.Bytes `json:"hash,omitempty"` PartsHeader CanonicalJSONPartSetHeader `json:"parts,omitempty"` } type CanonicalJSONPartSetHeader struct { - Hash []byte `json:"hash"` - Total int `json:"total"` + Hash data.Bytes `json:"hash"` + Total int `json:"total"` } type CanonicalJSONProposal struct { diff --git a/types/events.go b/types/events.go index 17f6fc6e..953859d0 100644 --- a/types/events.go +++ b/types/events.go @@ -3,9 +3,10 @@ package types import ( // for registering TMEventData as events.EventData abci "github.com/tendermint/abci/types" - . "github.com/tendermint/tmlibs/common" - "github.com/tendermint/tmlibs/events" "github.com/tendermint/go-wire" + "github.com/tendermint/go-wire/data" + cmn "github.com/tendermint/tmlibs/common" + "github.com/tendermint/tmlibs/events" ) // Functions to generate eventId strings @@ -16,7 +17,7 @@ func EventStringUnbond() string { return "Unbond" } func EventStringRebond() string { return "Rebond" } func EventStringDupeout() string { return "Dupeout" } func EventStringFork() string { return "Fork" } -func EventStringTx(tx Tx) string { return Fmt("Tx:%X", tx.Hash()) } +func EventStringTx(tx Tx) string { return cmn.Fmt("Tx:%X", tx.Hash()) } func EventStringNewBlock() string { return "NewBlock" } func EventStringNewBlockHeader() string { return "NewBlockHeader" } @@ -75,7 +76,7 @@ type EventDataNewBlockHeader struct { type EventDataTx struct { Height int `json:"height"` Tx Tx `json:"tx"` - Data []byte `json:"data"` + Data data.Bytes `json:"data"` Log string `json:"log"` Code abci.CodeType `json:"code"` Error string `json:"error"` // this is redundant information for now diff --git a/types/genesis.go b/types/genesis.go index 6a500664..75999f63 100644 --- a/types/genesis.go +++ b/types/genesis.go @@ -4,8 +4,9 @@ import ( "encoding/json" "time" - . "github.com/tendermint/tmlibs/common" "github.com/tendermint/go-crypto" + "github.com/tendermint/go-wire/data" + cmn "github.com/tendermint/tmlibs/common" ) //------------------------------------------------------------ @@ -26,7 +27,7 @@ type GenesisDoc struct { GenesisTime time.Time `json:"genesis_time"` ChainID string `json:"chain_id"` Validators []GenesisValidator `json:"validators"` - AppHash []byte `json:"app_hash"` + AppHash data.Bytes `json:"app_hash"` } // Utility method for saving GenensisDoc as JSON file. @@ -35,7 +36,7 @@ func (genDoc *GenesisDoc) SaveAs(file string) error { if err != nil { return err } - return WriteFile(file, genDocBytes, 0644) + return cmn.WriteFile(file, genDocBytes, 0644) } //------------------------------------------------------------ diff --git a/types/part_set.go b/types/part_set.go index 96907aa5..e15d2cab 100644 --- a/types/part_set.go +++ b/types/part_set.go @@ -9,9 +9,10 @@ import ( "golang.org/x/crypto/ripemd160" - . "github.com/tendermint/tmlibs/common" - "github.com/tendermint/tmlibs/merkle" "github.com/tendermint/go-wire" + "github.com/tendermint/go-wire/data" + cmn "github.com/tendermint/tmlibs/common" + "github.com/tendermint/tmlibs/merkle" ) var ( @@ -21,7 +22,7 @@ var ( type Part struct { Index int `json:"index"` - Bytes []byte `json:"bytes"` + Bytes data.Bytes `json:"bytes"` Proof merkle.SimpleProof `json:"proof"` // Cache @@ -49,7 +50,7 @@ func (part *Part) StringIndented(indent string) string { %s Proof: %v %s}`, part.Index, - indent, Fingerprint(part.Bytes), + indent, cmn.Fingerprint(part.Bytes), indent, part.Proof.StringIndented(indent+" "), indent) } @@ -57,12 +58,12 @@ func (part *Part) StringIndented(indent string) string { //------------------------------------- type PartSetHeader struct { - Total int `json:"total"` - Hash []byte `json:"hash"` + Total int `json:"total"` + Hash data.Bytes `json:"hash"` } func (psh PartSetHeader) String() string { - return fmt.Sprintf("%v:%X", psh.Total, Fingerprint(psh.Hash)) + return fmt.Sprintf("%v:%X", psh.Total, cmn.Fingerprint(psh.Hash)) } func (psh PartSetHeader) IsZero() bool { @@ -85,7 +86,7 @@ type PartSet struct { mtx sync.Mutex parts []*Part - partsBitArray *BitArray + partsBitArray *cmn.BitArray count int } @@ -96,11 +97,11 @@ func NewPartSetFromData(data []byte, partSize int) *PartSet { total := (len(data) + partSize - 1) / partSize parts := make([]*Part, total) parts_ := make([]merkle.Hashable, total) - partsBitArray := NewBitArray(total) + partsBitArray := cmn.NewBitArray(total) for i := 0; i < total; i++ { part := &Part{ Index: i, - Bytes: data[i*partSize : MinInt(len(data), (i+1)*partSize)], + Bytes: data[i*partSize : cmn.MinInt(len(data), (i+1)*partSize)], } parts[i] = part parts_[i] = part @@ -126,7 +127,7 @@ func NewPartSetFromHeader(header PartSetHeader) *PartSet { total: header.Total, hash: header.Hash, parts: make([]*Part, header.Total), - partsBitArray: NewBitArray(header.Total), + partsBitArray: cmn.NewBitArray(header.Total), count: 0, } } @@ -150,7 +151,7 @@ func (ps *PartSet) HasHeader(header PartSetHeader) bool { } } -func (ps *PartSet) BitArray() *BitArray { +func (ps *PartSet) BitArray() *cmn.BitArray { ps.mtx.Lock() defer ps.mtx.Unlock() return ps.partsBitArray.Copy() @@ -224,7 +225,7 @@ func (ps *PartSet) IsComplete() bool { func (ps *PartSet) GetReader() io.Reader { if !ps.IsComplete() { - PanicSanity("Cannot GetReader() on incomplete PartSet") + cmn.PanicSanity("Cannot GetReader() on incomplete PartSet") } return NewPartSetReader(ps.parts) } diff --git a/types/tx.go b/types/tx.go index e62b5f66..ac3ec01d 100644 --- a/types/tx.go +++ b/types/tx.go @@ -5,6 +5,7 @@ import ( "errors" abci "github.com/tendermint/abci/types" + "github.com/tendermint/go-wire/data" "github.com/tendermint/tmlibs/merkle" ) @@ -79,7 +80,7 @@ func (txs Txs) Proof(i int) TxProof { type TxProof struct { Index, Total int - RootHash []byte + RootHash data.Bytes Data Tx Proof merkle.SimpleProof } diff --git a/types/tx_test.go b/types/tx_test.go index 3ed59e3c..91cddecf 100644 --- a/types/tx_test.go +++ b/types/tx_test.go @@ -60,9 +60,9 @@ func TestValidTxProof(t *testing.T) { proof := txs.Proof(i) assert.Equal(i, proof.Index, "%d: %d", h, i) assert.Equal(len(txs), proof.Total, "%d: %d", h, i) - assert.Equal(root, proof.RootHash, "%d: %d", h, i) - assert.Equal(leaf, proof.Data, "%d: %d", h, i) - assert.Equal(leafHash, proof.LeafHash(), "%d: %d", h, i) + assert.EqualValues(root, proof.RootHash, "%d: %d", h, i) + assert.EqualValues(leaf, proof.Data, "%d: %d", h, i) + assert.EqualValues(leafHash, proof.LeafHash(), "%d: %d", h, i) assert.Nil(proof.Validate(root), "%d: %d", h, i) assert.NotNil(proof.Validate([]byte("foobar")), "%d: %d", h, i) diff --git a/types/validator.go b/types/validator.go index 2a8795bf..595c52ff 100644 --- a/types/validator.go +++ b/types/validator.go @@ -5,16 +5,17 @@ import ( "fmt" "io" - . "github.com/tendermint/tmlibs/common" "github.com/tendermint/go-crypto" "github.com/tendermint/go-wire" + "github.com/tendermint/go-wire/data" + cmn "github.com/tendermint/tmlibs/common" ) // Volatile state for each Validator // TODO: make non-volatile identity // - Remove Accum - it can be computed, and now valset becomes identifying type Validator struct { - Address []byte `json:"address"` + Address data.Bytes `json:"address"` PubKey crypto.PubKey `json:"pub_key"` VotingPower int64 `json:"voting_power"` Accum int64 `json:"accum"` @@ -51,7 +52,7 @@ func (v *Validator) CompareAccum(other *Validator) *Validator { } else if bytes.Compare(v.Address, other.Address) > 0 { return other } else { - PanicSanity("Cannot compare identical validators") + cmn.PanicSanity("Cannot compare identical validators") return nil } } @@ -87,7 +88,7 @@ func (vc validatorCodec) Decode(r io.Reader, n *int, err *error) interface{} { } func (vc validatorCodec) Compare(o1 interface{}, o2 interface{}) int { - PanicSanity("ValidatorCodec.Compare not implemented") + cmn.PanicSanity("ValidatorCodec.Compare not implemented") return 0 } @@ -96,11 +97,11 @@ func (vc validatorCodec) Compare(o1 interface{}, o2 interface{}) int { func RandValidator(randPower bool, minPower int64) (*Validator, *PrivValidator) { privVal := GenPrivValidator() - _, tempFilePath := Tempfile("priv_validator_") + _, tempFilePath := cmn.Tempfile("priv_validator_") privVal.SetFile(tempFilePath) votePower := minPower if randPower { - votePower += int64(RandUint32()) + votePower += int64(cmn.RandUint32()) } val := NewValidator(privVal.PubKey, votePower) return val, privVal diff --git a/types/vote.go b/types/vote.go index 2ad9df0a..164293c5 100644 --- a/types/vote.go +++ b/types/vote.go @@ -5,9 +5,10 @@ import ( "fmt" "io" - . "github.com/tendermint/tmlibs/common" "github.com/tendermint/go-crypto" "github.com/tendermint/go-wire" + "github.com/tendermint/go-wire/data" + cmn "github.com/tendermint/tmlibs/common" ) var ( @@ -47,7 +48,7 @@ func IsVoteTypeValid(type_ byte) bool { // Represents a prevote, precommit, or commit vote from validators for consensus. type Vote struct { - ValidatorAddress []byte `json:"validator_address"` + ValidatorAddress data.Bytes `json:"validator_address"` ValidatorIndex int `json:"validator_index"` Height int `json:"height"` Round int `json:"round"` @@ -79,11 +80,11 @@ func (vote *Vote) String() string { case VoteTypePrecommit: typeString = "Precommit" default: - PanicSanity("Unknown vote type") + cmn.PanicSanity("Unknown vote type") } return fmt.Sprintf("Vote{%v:%X %v/%02d/%v(%v) %X %v}", - vote.ValidatorIndex, Fingerprint(vote.ValidatorAddress), + vote.ValidatorIndex, cmn.Fingerprint(vote.ValidatorAddress), vote.Height, vote.Round, vote.Type, typeString, - Fingerprint(vote.BlockID.Hash), vote.Signature) + cmn.Fingerprint(vote.BlockID.Hash), vote.Signature) }