types: []byte -> data.Bytes

This commit is contained in:
Ethan Buchman 2017-04-27 19:01:18 -04:00
parent 1310c72647
commit bdb34f9f4e
9 changed files with 60 additions and 49 deletions

View File

@ -8,9 +8,10 @@ import (
"strings" "strings"
"time" "time"
"github.com/tendermint/go-wire"
"github.com/tendermint/go-wire/data"
. "github.com/tendermint/tmlibs/common" . "github.com/tendermint/tmlibs/common"
"github.com/tendermint/tmlibs/merkle" "github.com/tendermint/tmlibs/merkle"
"github.com/tendermint/go-wire"
) )
const MaxBlockSize = 22020096 // 21MB TODO make it configurable const MaxBlockSize = 22020096 // 21MB TODO make it configurable
@ -150,15 +151,15 @@ func (b *Block) StringShort() string {
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
type Header struct { type Header struct {
ChainID string `json:"chain_id"` ChainID string `json:"chain_id"`
Height int `json:"height"` Height int `json:"height"`
Time time.Time `json:"time"` Time time.Time `json:"time"`
NumTxs int `json:"num_txs"` // XXX: Can we get rid of this? NumTxs int `json:"num_txs"` // XXX: Can we get rid of this?
LastBlockID BlockID `json:"last_block_id"` LastBlockID BlockID `json:"last_block_id"`
LastCommitHash []byte `json:"last_commit_hash"` // commit from validators from the last block LastCommitHash data.Bytes `json:"last_commit_hash"` // commit from validators from the last block
DataHash []byte `json:"data_hash"` // transactions DataHash data.Bytes `json:"data_hash"` // transactions
ValidatorsHash []byte `json:"validators_hash"` // validators for the current block ValidatorsHash data.Bytes `json:"validators_hash"` // validators for the current block
AppHash []byte `json:"app_hash"` // state after txs from the previous block AppHash data.Bytes `json:"app_hash"` // state after txs from the previous block
} }
// NOTE: hash is nil if required fields are missing. // NOTE: hash is nil if required fields are missing.
@ -388,7 +389,7 @@ func (data *Data) StringIndented(indent string) string {
//-------------------------------------------------------------------------------- //--------------------------------------------------------------------------------
type BlockID struct { type BlockID struct {
Hash []byte `json:"hash"` Hash data.Bytes `json:"hash"`
PartsHeader PartSetHeader `json:"parts"` PartsHeader PartSetHeader `json:"parts"`
} }

View File

@ -1,15 +1,19 @@
package types package types
import (
"github.com/tendermint/go-wire/data"
)
// canonical json is go-wire's json for structs with fields in alphabetical order // canonical json is go-wire's json for structs with fields in alphabetical order
type CanonicalJSONBlockID struct { type CanonicalJSONBlockID struct {
Hash []byte `json:"hash,omitempty"` Hash data.Bytes `json:"hash,omitempty"`
PartsHeader CanonicalJSONPartSetHeader `json:"parts,omitempty"` PartsHeader CanonicalJSONPartSetHeader `json:"parts,omitempty"`
} }
type CanonicalJSONPartSetHeader struct { type CanonicalJSONPartSetHeader struct {
Hash []byte `json:"hash"` Hash data.Bytes `json:"hash"`
Total int `json:"total"` Total int `json:"total"`
} }
type CanonicalJSONProposal struct { type CanonicalJSONProposal struct {

View File

@ -3,9 +3,10 @@ package types
import ( import (
// for registering TMEventData as events.EventData // for registering TMEventData as events.EventData
abci "github.com/tendermint/abci/types" 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"
"github.com/tendermint/go-wire/data"
cmn "github.com/tendermint/tmlibs/common"
"github.com/tendermint/tmlibs/events"
) )
// Functions to generate eventId strings // Functions to generate eventId strings
@ -16,7 +17,7 @@ func EventStringUnbond() string { return "Unbond" }
func EventStringRebond() string { return "Rebond" } func EventStringRebond() string { return "Rebond" }
func EventStringDupeout() string { return "Dupeout" } func EventStringDupeout() string { return "Dupeout" }
func EventStringFork() string { return "Fork" } 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 EventStringNewBlock() string { return "NewBlock" }
func EventStringNewBlockHeader() string { return "NewBlockHeader" } func EventStringNewBlockHeader() string { return "NewBlockHeader" }
@ -75,7 +76,7 @@ type EventDataNewBlockHeader struct {
type EventDataTx struct { type EventDataTx struct {
Height int `json:"height"` Height int `json:"height"`
Tx Tx `json:"tx"` Tx Tx `json:"tx"`
Data []byte `json:"data"` Data data.Bytes `json:"data"`
Log string `json:"log"` Log string `json:"log"`
Code abci.CodeType `json:"code"` Code abci.CodeType `json:"code"`
Error string `json:"error"` // this is redundant information for now Error string `json:"error"` // this is redundant information for now

View File

@ -4,8 +4,9 @@ import (
"encoding/json" "encoding/json"
"time" "time"
. "github.com/tendermint/tmlibs/common"
"github.com/tendermint/go-crypto" "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"` GenesisTime time.Time `json:"genesis_time"`
ChainID string `json:"chain_id"` ChainID string `json:"chain_id"`
Validators []GenesisValidator `json:"validators"` Validators []GenesisValidator `json:"validators"`
AppHash []byte `json:"app_hash"` AppHash data.Bytes `json:"app_hash"`
} }
// Utility method for saving GenensisDoc as JSON file. // Utility method for saving GenensisDoc as JSON file.
@ -35,7 +36,7 @@ func (genDoc *GenesisDoc) SaveAs(file string) error {
if err != nil { if err != nil {
return err return err
} }
return WriteFile(file, genDocBytes, 0644) return cmn.WriteFile(file, genDocBytes, 0644)
} }
//------------------------------------------------------------ //------------------------------------------------------------

View File

@ -9,9 +9,10 @@ import (
"golang.org/x/crypto/ripemd160" "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"
"github.com/tendermint/go-wire/data"
cmn "github.com/tendermint/tmlibs/common"
"github.com/tendermint/tmlibs/merkle"
) )
var ( var (
@ -21,7 +22,7 @@ var (
type Part struct { type Part struct {
Index int `json:"index"` Index int `json:"index"`
Bytes []byte `json:"bytes"` Bytes data.Bytes `json:"bytes"`
Proof merkle.SimpleProof `json:"proof"` Proof merkle.SimpleProof `json:"proof"`
// Cache // Cache
@ -49,7 +50,7 @@ func (part *Part) StringIndented(indent string) string {
%s Proof: %v %s Proof: %v
%s}`, %s}`,
part.Index, part.Index,
indent, Fingerprint(part.Bytes), indent, cmn.Fingerprint(part.Bytes),
indent, part.Proof.StringIndented(indent+" "), indent, part.Proof.StringIndented(indent+" "),
indent) indent)
} }
@ -57,12 +58,12 @@ func (part *Part) StringIndented(indent string) string {
//------------------------------------- //-------------------------------------
type PartSetHeader struct { type PartSetHeader struct {
Total int `json:"total"` Total int `json:"total"`
Hash []byte `json:"hash"` Hash data.Bytes `json:"hash"`
} }
func (psh PartSetHeader) String() string { 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 { func (psh PartSetHeader) IsZero() bool {
@ -85,7 +86,7 @@ type PartSet struct {
mtx sync.Mutex mtx sync.Mutex
parts []*Part parts []*Part
partsBitArray *BitArray partsBitArray *cmn.BitArray
count int count int
} }
@ -96,11 +97,11 @@ func NewPartSetFromData(data []byte, partSize int) *PartSet {
total := (len(data) + partSize - 1) / partSize total := (len(data) + partSize - 1) / partSize
parts := make([]*Part, total) parts := make([]*Part, total)
parts_ := make([]merkle.Hashable, total) parts_ := make([]merkle.Hashable, total)
partsBitArray := NewBitArray(total) partsBitArray := cmn.NewBitArray(total)
for i := 0; i < total; i++ { for i := 0; i < total; i++ {
part := &Part{ part := &Part{
Index: i, 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
parts_[i] = part parts_[i] = part
@ -126,7 +127,7 @@ func NewPartSetFromHeader(header PartSetHeader) *PartSet {
total: header.Total, total: header.Total,
hash: header.Hash, hash: header.Hash,
parts: make([]*Part, header.Total), parts: make([]*Part, header.Total),
partsBitArray: NewBitArray(header.Total), partsBitArray: cmn.NewBitArray(header.Total),
count: 0, 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() ps.mtx.Lock()
defer ps.mtx.Unlock() defer ps.mtx.Unlock()
return ps.partsBitArray.Copy() return ps.partsBitArray.Copy()
@ -224,7 +225,7 @@ func (ps *PartSet) IsComplete() bool {
func (ps *PartSet) GetReader() io.Reader { func (ps *PartSet) GetReader() io.Reader {
if !ps.IsComplete() { if !ps.IsComplete() {
PanicSanity("Cannot GetReader() on incomplete PartSet") cmn.PanicSanity("Cannot GetReader() on incomplete PartSet")
} }
return NewPartSetReader(ps.parts) return NewPartSetReader(ps.parts)
} }

View File

@ -5,6 +5,7 @@ import (
"errors" "errors"
abci "github.com/tendermint/abci/types" abci "github.com/tendermint/abci/types"
"github.com/tendermint/go-wire/data"
"github.com/tendermint/tmlibs/merkle" "github.com/tendermint/tmlibs/merkle"
) )
@ -79,7 +80,7 @@ func (txs Txs) Proof(i int) TxProof {
type TxProof struct { type TxProof struct {
Index, Total int Index, Total int
RootHash []byte RootHash data.Bytes
Data Tx Data Tx
Proof merkle.SimpleProof Proof merkle.SimpleProof
} }

View File

@ -60,9 +60,9 @@ func TestValidTxProof(t *testing.T) {
proof := txs.Proof(i) proof := txs.Proof(i)
assert.Equal(i, proof.Index, "%d: %d", h, i) assert.Equal(i, proof.Index, "%d: %d", h, i)
assert.Equal(len(txs), proof.Total, "%d: %d", h, i) assert.Equal(len(txs), proof.Total, "%d: %d", h, i)
assert.Equal(root, proof.RootHash, "%d: %d", h, i) assert.EqualValues(root, proof.RootHash, "%d: %d", h, i)
assert.Equal(leaf, proof.Data, "%d: %d", h, i) assert.EqualValues(leaf, proof.Data, "%d: %d", h, i)
assert.Equal(leafHash, proof.LeafHash(), "%d: %d", h, i) assert.EqualValues(leafHash, proof.LeafHash(), "%d: %d", h, i)
assert.Nil(proof.Validate(root), "%d: %d", h, i) assert.Nil(proof.Validate(root), "%d: %d", h, i)
assert.NotNil(proof.Validate([]byte("foobar")), "%d: %d", h, i) assert.NotNil(proof.Validate([]byte("foobar")), "%d: %d", h, i)

View File

@ -5,16 +5,17 @@ import (
"fmt" "fmt"
"io" "io"
. "github.com/tendermint/tmlibs/common"
"github.com/tendermint/go-crypto" "github.com/tendermint/go-crypto"
"github.com/tendermint/go-wire" "github.com/tendermint/go-wire"
"github.com/tendermint/go-wire/data"
cmn "github.com/tendermint/tmlibs/common"
) )
// Volatile state for each Validator // Volatile state for each Validator
// TODO: make non-volatile identity // TODO: make non-volatile identity
// - Remove Accum - it can be computed, and now valset becomes identifying // - Remove Accum - it can be computed, and now valset becomes identifying
type Validator struct { type Validator struct {
Address []byte `json:"address"` Address data.Bytes `json:"address"`
PubKey crypto.PubKey `json:"pub_key"` PubKey crypto.PubKey `json:"pub_key"`
VotingPower int64 `json:"voting_power"` VotingPower int64 `json:"voting_power"`
Accum int64 `json:"accum"` Accum int64 `json:"accum"`
@ -51,7 +52,7 @@ func (v *Validator) CompareAccum(other *Validator) *Validator {
} else if bytes.Compare(v.Address, other.Address) > 0 { } else if bytes.Compare(v.Address, other.Address) > 0 {
return other return other
} else { } else {
PanicSanity("Cannot compare identical validators") cmn.PanicSanity("Cannot compare identical validators")
return nil 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 { func (vc validatorCodec) Compare(o1 interface{}, o2 interface{}) int {
PanicSanity("ValidatorCodec.Compare not implemented") cmn.PanicSanity("ValidatorCodec.Compare not implemented")
return 0 return 0
} }
@ -96,11 +97,11 @@ func (vc validatorCodec) Compare(o1 interface{}, o2 interface{}) int {
func RandValidator(randPower bool, minPower int64) (*Validator, *PrivValidator) { func RandValidator(randPower bool, minPower int64) (*Validator, *PrivValidator) {
privVal := GenPrivValidator() privVal := GenPrivValidator()
_, tempFilePath := Tempfile("priv_validator_") _, tempFilePath := cmn.Tempfile("priv_validator_")
privVal.SetFile(tempFilePath) privVal.SetFile(tempFilePath)
votePower := minPower votePower := minPower
if randPower { if randPower {
votePower += int64(RandUint32()) votePower += int64(cmn.RandUint32())
} }
val := NewValidator(privVal.PubKey, votePower) val := NewValidator(privVal.PubKey, votePower)
return val, privVal return val, privVal

View File

@ -5,9 +5,10 @@ import (
"fmt" "fmt"
"io" "io"
. "github.com/tendermint/tmlibs/common"
"github.com/tendermint/go-crypto" "github.com/tendermint/go-crypto"
"github.com/tendermint/go-wire" "github.com/tendermint/go-wire"
"github.com/tendermint/go-wire/data"
cmn "github.com/tendermint/tmlibs/common"
) )
var ( var (
@ -47,7 +48,7 @@ func IsVoteTypeValid(type_ byte) bool {
// Represents a prevote, precommit, or commit vote from validators for consensus. // Represents a prevote, precommit, or commit vote from validators for consensus.
type Vote struct { type Vote struct {
ValidatorAddress []byte `json:"validator_address"` ValidatorAddress data.Bytes `json:"validator_address"`
ValidatorIndex int `json:"validator_index"` ValidatorIndex int `json:"validator_index"`
Height int `json:"height"` Height int `json:"height"`
Round int `json:"round"` Round int `json:"round"`
@ -79,11 +80,11 @@ func (vote *Vote) String() string {
case VoteTypePrecommit: case VoteTypePrecommit:
typeString = "Precommit" typeString = "Precommit"
default: default:
PanicSanity("Unknown vote type") cmn.PanicSanity("Unknown vote type")
} }
return fmt.Sprintf("Vote{%v:%X %v/%02d/%v(%v) %X %v}", 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, vote.Height, vote.Round, vote.Type, typeString,
Fingerprint(vote.BlockID.Hash), vote.Signature) cmn.Fingerprint(vote.BlockID.Hash), vote.Signature)
} }