Browse Source

types: []byte -> data.Bytes

pull/467/head
Ethan Buchman 8 years ago
parent
commit
bdb34f9f4e
9 changed files with 60 additions and 49 deletions
  1. +12
    -11
      types/block.go
  2. +7
    -3
      types/canonical_json.go
  3. +5
    -4
      types/events.go
  4. +4
    -3
      types/genesis.go
  5. +14
    -13
      types/part_set.go
  6. +2
    -1
      types/tx.go
  7. +3
    -3
      types/tx_test.go
  8. +7
    -6
      types/validator.go
  9. +6
    -5
      types/vote.go

+ 12
- 11
types/block.go View File

@ -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"`
}


+ 7
- 3
types/canonical_json.go View File

@ -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 {


+ 5
- 4
types/events.go View File

@ -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


+ 4
- 3
types/genesis.go View File

@ -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)
}
//------------------------------------------------------------


+ 14
- 13
types/part_set.go View File

@ -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)
}


+ 2
- 1
types/tx.go View File

@ -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
}


+ 3
- 3
types/tx_test.go View File

@ -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)


+ 7
- 6
types/validator.go View File

@ -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


+ 6
- 5
types/vote.go View File

@ -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)
}

Loading…
Cancel
Save