libs/common: Refactor libs/common 4 (#4237)

* libs/common: Refactor libs/common 4

- move byte function out of cmn to its own pkg
- move tempfile out of cmn to its own pkg
- move throttletimer to its own pkg

ref #4147

Signed-off-by: Marko Baricevic <marbar3778@yahoo.com>

* add changelog entry

* fix linting issues
This commit is contained in:
Marko
2019-12-11 23:16:35 +01:00
committed by GitHub
parent 15e80d2448
commit 89f0bbbd76
50 changed files with 181 additions and 251 deletions
+22 -21
View File
@@ -12,6 +12,7 @@ import (
"github.com/tendermint/tendermint/crypto"
"github.com/tendermint/tendermint/crypto/merkle"
"github.com/tendermint/tendermint/crypto/tmhash"
tmbytes "github.com/tendermint/tendermint/libs/bytes"
cmn "github.com/tendermint/tendermint/libs/common"
"github.com/tendermint/tendermint/version"
)
@@ -156,7 +157,7 @@ func (b *Block) fillHeader() {
// Hash computes and returns the block hash.
// If the block is incomplete, block hash is nil for safety.
func (b *Block) Hash() cmn.HexBytes {
func (b *Block) Hash() tmbytes.HexBytes {
if b == nil {
return nil
}
@@ -329,20 +330,20 @@ type Header struct {
LastBlockID BlockID `json:"last_block_id"`
// hashes of block data
LastCommitHash cmn.HexBytes `json:"last_commit_hash"` // commit from validators from the last block
DataHash cmn.HexBytes `json:"data_hash"` // transactions
LastCommitHash tmbytes.HexBytes `json:"last_commit_hash"` // commit from validators from the last block
DataHash tmbytes.HexBytes `json:"data_hash"` // transactions
// hashes from the app output from the prev block
ValidatorsHash cmn.HexBytes `json:"validators_hash"` // validators for the current block
NextValidatorsHash cmn.HexBytes `json:"next_validators_hash"` // validators for the next block
ConsensusHash cmn.HexBytes `json:"consensus_hash"` // consensus params for current block
AppHash cmn.HexBytes `json:"app_hash"` // state after txs from the previous block
ValidatorsHash tmbytes.HexBytes `json:"validators_hash"` // validators for the current block
NextValidatorsHash tmbytes.HexBytes `json:"next_validators_hash"` // validators for the next block
ConsensusHash tmbytes.HexBytes `json:"consensus_hash"` // consensus params for current block
AppHash tmbytes.HexBytes `json:"app_hash"` // state after txs from the previous block
// root hash of all results from the txs from the previous block
LastResultsHash cmn.HexBytes `json:"last_results_hash"`
LastResultsHash tmbytes.HexBytes `json:"last_results_hash"`
// consensus info
EvidenceHash cmn.HexBytes `json:"evidence_hash"` // evidence included in the block
ProposerAddress Address `json:"proposer_address"` // original proposer of the block
EvidenceHash tmbytes.HexBytes `json:"evidence_hash"` // evidence included in the block
ProposerAddress Address `json:"proposer_address"` // original proposer of the block
}
// Populate the Header with state-derived data.
@@ -372,7 +373,7 @@ func (h *Header) Populate(
// Returns nil if ValidatorHash is missing,
// since a Header is not valid unless there is
// a ValidatorsHash (corresponding to the validator set).
func (h *Header) Hash() cmn.HexBytes {
func (h *Header) Hash() tmbytes.HexBytes {
if h == nil || len(h.ValidatorsHash) == 0 {
return nil
}
@@ -479,8 +480,8 @@ func (cs CommitSig) Absent() bool {
func (cs CommitSig) String() string {
return fmt.Sprintf("CommitSig{%X by %X on %v @ %s}",
cmn.Fingerprint(cs.Signature),
cmn.Fingerprint(cs.ValidatorAddress),
tmbytes.Fingerprint(cs.Signature),
tmbytes.Fingerprint(cs.ValidatorAddress),
cs.BlockIDFlag,
CanonicalTime(cs.Timestamp))
}
@@ -559,7 +560,7 @@ type Commit struct {
// Memoized in first call to corresponding method.
// NOTE: can't memoize in constructor because constructor isn't used for
// unmarshaling.
hash cmn.HexBytes
hash tmbytes.HexBytes
bitArray *cmn.BitArray
}
@@ -696,7 +697,7 @@ func (commit *Commit) ValidateBasic() error {
}
// Hash returns the hash of the commit
func (commit *Commit) Hash() cmn.HexBytes {
func (commit *Commit) Hash() tmbytes.HexBytes {
if commit == nil {
return nil
}
@@ -809,11 +810,11 @@ type Data struct {
Txs Txs `json:"txs"`
// Volatile
hash cmn.HexBytes
hash tmbytes.HexBytes
}
// Hash returns the hash of the data
func (data *Data) Hash() cmn.HexBytes {
func (data *Data) Hash() tmbytes.HexBytes {
if data == nil {
return (Txs{}).Hash()
}
@@ -850,11 +851,11 @@ type EvidenceData struct {
Evidence EvidenceList `json:"evidence"`
// Volatile
hash cmn.HexBytes
hash tmbytes.HexBytes
}
// Hash returns the hash of the data.
func (data *EvidenceData) Hash() cmn.HexBytes {
func (data *EvidenceData) Hash() tmbytes.HexBytes {
if data.hash == nil {
data.hash = data.Evidence.Hash()
}
@@ -885,8 +886,8 @@ func (data *EvidenceData) StringIndented(indent string) string {
// BlockID defines the unique ID of a block as its Hash and its PartSetHeader
type BlockID struct {
Hash cmn.HexBytes `json:"hash"`
PartsHeader PartSetHeader `json:"parts"`
Hash tmbytes.HexBytes `json:"hash"`
PartsHeader PartSetHeader `json:"parts"`
}
// Equals returns true if the BlockID matches the given BlockID
+9 -8
View File
@@ -17,6 +17,7 @@ import (
"github.com/tendermint/tendermint/crypto"
"github.com/tendermint/tendermint/crypto/merkle"
"github.com/tendermint/tendermint/crypto/tmhash"
"github.com/tendermint/tendermint/libs/bytes"
cmn "github.com/tendermint/tendermint/libs/common"
tmrand "github.com/tendermint/tendermint/libs/rand"
tmtime "github.com/tendermint/tendermint/types/time"
@@ -251,7 +252,7 @@ func TestHeaderHash(t *testing.T) {
testCases := []struct {
desc string
header *Header
expectHash cmn.HexBytes
expectHash bytes.HexBytes
}{
{"Generates expected hash", &Header{
Version: version.Consensus{Block: 1, App: 2},
@@ -304,7 +305,7 @@ func TestHeaderHash(t *testing.T) {
byteSlices = append(byteSlices, cdcEncode(f.Interface()))
}
assert.Equal(t,
cmn.HexBytes(merkle.SimpleHashFromByteSlices(byteSlices)), tc.header.Hash())
bytes.HexBytes(merkle.SimpleHashFromByteSlices(byteSlices)), tc.header.Hash())
}
})
}
@@ -358,12 +359,12 @@ func randCommit() *Commit {
return commit
}
func hexBytesFromString(s string) cmn.HexBytes {
func hexBytesFromString(s string) bytes.HexBytes {
b, err := hex.DecodeString(s)
if err != nil {
panic(err)
}
return cmn.HexBytes(b)
return bytes.HexBytes(b)
}
func TestBlockMaxDataBytes(t *testing.T) {
@@ -559,10 +560,10 @@ func TestSignedHeaderValidateBasic(t *testing.T) {
func TestBlockIDValidateBasic(t *testing.T) {
validBlockID := BlockID{
Hash: cmn.HexBytes{},
Hash: bytes.HexBytes{},
PartsHeader: PartSetHeader{
Total: 1,
Hash: cmn.HexBytes{},
Hash: bytes.HexBytes{},
},
}
@@ -570,13 +571,13 @@ func TestBlockIDValidateBasic(t *testing.T) {
Hash: []byte{0},
PartsHeader: PartSetHeader{
Total: -1,
Hash: cmn.HexBytes{},
Hash: bytes.HexBytes{},
},
}
testCases := []struct {
testName string
blockIDHash cmn.HexBytes
blockIDHash bytes.HexBytes
blockIDPartsHeader PartSetHeader
expectErr bool
}{
+3 -3
View File
@@ -3,7 +3,7 @@ package types
import (
"time"
cmn "github.com/tendermint/tendermint/libs/common"
"github.com/tendermint/tendermint/libs/bytes"
tmtime "github.com/tendermint/tendermint/types/time"
)
@@ -13,12 +13,12 @@ import (
const TimeFormat = time.RFC3339Nano
type CanonicalBlockID struct {
Hash cmn.HexBytes
Hash bytes.HexBytes
PartsHeader CanonicalPartSetHeader
}
type CanonicalPartSetHeader struct {
Hash cmn.HexBytes
Hash bytes.HexBytes
Total int
}
+2 -1
View File
@@ -10,6 +10,7 @@ import (
"github.com/pkg/errors"
"github.com/tendermint/tendermint/crypto"
tmbytes "github.com/tendermint/tendermint/libs/bytes"
cmn "github.com/tendermint/tendermint/libs/common"
tmtime "github.com/tendermint/tendermint/types/time"
)
@@ -39,7 +40,7 @@ type GenesisDoc struct {
ChainID string `json:"chain_id"`
ConsensusParams *ConsensusParams `json:"consensus_params,omitempty"`
Validators []GenesisValidator `json:"validators,omitempty"`
AppHash cmn.HexBytes `json:"app_hash"`
AppHash tmbytes.HexBytes `json:"app_hash"`
AppState json.RawMessage `json:"app_state,omitempty"`
}
+6 -5
View File
@@ -9,6 +9,7 @@ import (
"github.com/pkg/errors"
"github.com/tendermint/tendermint/crypto/merkle"
tmbytes "github.com/tendermint/tendermint/libs/bytes"
cmn "github.com/tendermint/tendermint/libs/common"
)
@@ -19,7 +20,7 @@ var (
type Part struct {
Index int `json:"index"`
Bytes cmn.HexBytes `json:"bytes"`
Bytes tmbytes.HexBytes `json:"bytes"`
Proof merkle.SimpleProof `json:"proof"`
}
@@ -47,7 +48,7 @@ func (part *Part) StringIndented(indent string) string {
%s Proof: %v
%s}`,
part.Index,
indent, cmn.Fingerprint(part.Bytes),
indent, tmbytes.Fingerprint(part.Bytes),
indent, part.Proof.StringIndented(indent+" "),
indent)
}
@@ -55,12 +56,12 @@ func (part *Part) StringIndented(indent string) string {
//-------------------------------------
type PartSetHeader struct {
Total int `json:"total"`
Hash cmn.HexBytes `json:"hash"`
Total int `json:"total"`
Hash tmbytes.HexBytes `json:"hash"`
}
func (psh PartSetHeader) String() string {
return fmt.Sprintf("%v:%X", psh.Total, cmn.Fingerprint(psh.Hash))
return fmt.Sprintf("%v:%X", psh.Total, tmbytes.Fingerprint(psh.Hash))
}
func (psh PartSetHeader) IsZero() bool {
+2 -2
View File
@@ -5,7 +5,7 @@ import (
"fmt"
"time"
cmn "github.com/tendermint/tendermint/libs/common"
"github.com/tendermint/tendermint/libs/bytes"
tmtime "github.com/tendermint/tendermint/types/time"
)
@@ -83,7 +83,7 @@ func (p *Proposal) String() string {
p.Round,
p.BlockID,
p.POLRound,
cmn.Fingerprint(p.Signature),
bytes.Fingerprint(p.Signature),
CanonicalTime(p.Timestamp))
}
+3 -3
View File
@@ -3,7 +3,7 @@ package types
import (
abci "github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/crypto/merkle"
cmn "github.com/tendermint/tendermint/libs/common"
"github.com/tendermint/tendermint/libs/bytes"
)
//-----------------------------------------------------------------------------
@@ -12,8 +12,8 @@ import (
// TODO: add tags and other fields
// https://github.com/tendermint/tendermint/issues/1007
type ABCIResult struct {
Code uint32 `json:"code"`
Data cmn.HexBytes `json:"data"`
Code uint32 `json:"code"`
Data bytes.HexBytes `json:"data"`
}
// Bytes returns the amino encoded ABCIResult
+2 -2
View File
@@ -10,7 +10,7 @@ import (
abci "github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/crypto/merkle"
"github.com/tendermint/tendermint/crypto/tmhash"
cmn "github.com/tendermint/tendermint/libs/common"
tmbytes "github.com/tendermint/tendermint/libs/bytes"
)
// Tx is an arbitrary byte array.
@@ -83,7 +83,7 @@ func (txs Txs) Proof(i int) TxProof {
// TxProof represents a Merkle proof of the presence of a transaction in the Merkle tree.
type TxProof struct {
RootHash cmn.HexBytes `json:"root_hash"`
RootHash tmbytes.HexBytes `json:"root_hash"`
Data Tx `json:"data"`
Proof merkle.SimpleProof `json:"proof"`
}
+4 -4
View File
@@ -7,7 +7,7 @@ import (
"time"
"github.com/tendermint/tendermint/crypto"
cmn "github.com/tendermint/tendermint/libs/common"
tmbytes "github.com/tendermint/tendermint/libs/bytes"
)
const (
@@ -110,13 +110,13 @@ func (vote *Vote) String() string {
return fmt.Sprintf("Vote{%v:%X %v/%02d/%v(%v) %X %X @ %s}",
vote.ValidatorIndex,
cmn.Fingerprint(vote.ValidatorAddress),
tmbytes.Fingerprint(vote.ValidatorAddress),
vote.Height,
vote.Round,
vote.Type,
typeString,
cmn.Fingerprint(vote.BlockID.Hash),
cmn.Fingerprint(vote.Signature),
tmbytes.Fingerprint(vote.BlockID.Hash),
tmbytes.Fingerprint(vote.Signature),
CanonicalTime(vote.Timestamp),
)
}