-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add another test and port internal test factory
- Loading branch information
Showing
9 changed files
with
330 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package factory | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/tendermint/tendermint/crypto" | ||
"github.com/tendermint/tendermint/crypto/tmhash" | ||
"github.com/tendermint/tendermint/types" | ||
"github.com/tendermint/tendermint/version" | ||
) | ||
|
||
const ( | ||
DefaultTestChainID = "test-chain" | ||
) | ||
|
||
var ( | ||
DefaultTestTime = time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC) | ||
) | ||
|
||
func RandomAddress() []byte { | ||
return crypto.CRandBytes(crypto.AddressSize) | ||
} | ||
|
||
func RandomHash() []byte { | ||
return crypto.CRandBytes(tmhash.Size) | ||
} | ||
|
||
func MakeBlockID() types.BlockID { | ||
return MakeBlockIDWithHash(RandomHash()) | ||
} | ||
|
||
func MakeBlockIDWithHash(hash []byte) types.BlockID { | ||
return types.BlockID{ | ||
Hash: hash, | ||
PartSetHeader: types.PartSetHeader{ | ||
Total: 100, | ||
Hash: RandomHash(), | ||
}, | ||
} | ||
} | ||
|
||
// MakeHeader fills the rest of the contents of the header such that it passes | ||
// validate basic | ||
func MakeHeader(t *testing.T, h *types.Header) *types.Header { | ||
t.Helper() | ||
if h.Version.Block == 0 { | ||
h.Version.Block = version.BlockProtocol | ||
} | ||
if h.Height == 0 { | ||
h.Height = 1 | ||
} | ||
if h.LastBlockID.IsZero() { | ||
h.LastBlockID = MakeBlockID() | ||
} | ||
if h.ChainID == "" { | ||
h.ChainID = DefaultTestChainID | ||
} | ||
if len(h.LastCommitHash) == 0 { | ||
h.LastCommitHash = RandomHash() | ||
} | ||
if len(h.DataHash) == 0 { | ||
h.DataHash = RandomHash() | ||
} | ||
if len(h.ValidatorsHash) == 0 { | ||
h.ValidatorsHash = RandomHash() | ||
} | ||
if len(h.NextValidatorsHash) == 0 { | ||
h.NextValidatorsHash = RandomHash() | ||
} | ||
if len(h.ConsensusHash) == 0 { | ||
h.ConsensusHash = RandomHash() | ||
} | ||
if len(h.AppHash) == 0 { | ||
h.AppHash = RandomHash() | ||
} | ||
if len(h.LastResultsHash) == 0 { | ||
h.LastResultsHash = RandomHash() | ||
} | ||
if len(h.EvidenceHash) == 0 { | ||
h.EvidenceHash = RandomHash() | ||
} | ||
if len(h.ProposerAddress) == 0 { | ||
h.ProposerAddress = RandomAddress() | ||
} | ||
|
||
require.NoError(t, h.ValidateBasic()) | ||
|
||
return h | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
/* | ||
Package factory provides generation code for common structs in Tendermint. | ||
It is used primarily for the testing of internal components such as statesync, | ||
consensus, blocksync etc.. | ||
*/ | ||
package factory |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package factory | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/tendermint/tendermint/types" | ||
) | ||
|
||
func TestMakeHeader(t *testing.T) { | ||
MakeHeader(t, &types.Header{}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package factory | ||
|
||
import ( | ||
"time" | ||
|
||
cfg "github.com/tendermint/tendermint/config" | ||
tmproto "github.com/tendermint/tendermint/proto/tendermint/types" | ||
"github.com/tendermint/tendermint/types" | ||
) | ||
|
||
func GenesisDoc( | ||
config *cfg.Config, | ||
time time.Time, | ||
validators []*types.Validator, | ||
consensusParams *tmproto.ConsensusParams, | ||
) *types.GenesisDoc { | ||
|
||
genesisValidators := make([]types.GenesisValidator, len(validators)) | ||
|
||
for i := range validators { | ||
genesisValidators[i] = types.GenesisValidator{ | ||
Power: validators[i].VotingPower, | ||
PubKey: validators[i].PubKey, | ||
} | ||
} | ||
|
||
return &types.GenesisDoc{ | ||
GenesisTime: time, | ||
InitialHeight: 1, | ||
ChainID: config.ChainID(), | ||
Validators: genesisValidators, | ||
ConsensusParams: consensusParams, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package factory | ||
|
||
import "github.com/tendermint/tendermint/types" | ||
|
||
func MakeNTxs(height, n int64) []types.Tx { | ||
txs := make([]types.Tx, n) | ||
for i := range txs { | ||
txs[i] = types.Tx([]byte{byte(height), byte(i / 256), byte(i % 256)}) | ||
} | ||
return txs | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package factory | ||
|
||
import ( | ||
"context" | ||
"sort" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/tendermint/tendermint/types" | ||
) | ||
|
||
func Validator(ctx context.Context, votingPower int64) (*types.Validator, types.PrivValidator, error) { | ||
privVal := types.NewMockPV() | ||
pubKey, err := privVal.GetPubKey() | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
val := types.NewValidator(pubKey, votingPower) | ||
return val, privVal, nil | ||
} | ||
|
||
func ValidatorSet(ctx context.Context, t *testing.T, numValidators int, votingPower int64) (*types.ValidatorSet, []types.PrivValidator) { | ||
var ( | ||
valz = make([]*types.Validator, numValidators) | ||
privValidators = make([]types.PrivValidator, numValidators) | ||
) | ||
t.Helper() | ||
|
||
for i := 0; i < numValidators; i++ { | ||
val, privValidator, err := Validator(ctx, votingPower) | ||
require.NoError(t, err) | ||
valz[i] = val | ||
privValidators[i] = privValidator | ||
} | ||
|
||
sort.Sort(types.PrivValidatorsByAddress(privValidators)) | ||
|
||
return types.NewValidatorSet(valz), privValidators | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package factory | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
tmproto "github.com/tendermint/tendermint/proto/tendermint/types" | ||
"github.com/tendermint/tendermint/types" | ||
) | ||
|
||
func MakeVote( | ||
ctx context.Context, | ||
val types.PrivValidator, | ||
chainID string, | ||
valIndex int32, | ||
height int64, | ||
round int32, | ||
step int, | ||
blockID types.BlockID, | ||
time time.Time, | ||
) (*types.Vote, error) { | ||
pubKey, err := val.GetPubKey() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
v := &types.Vote{ | ||
ValidatorAddress: pubKey.Address(), | ||
ValidatorIndex: valIndex, | ||
Height: height, | ||
Round: round, | ||
Type: tmproto.SignedMsgType(step), | ||
BlockID: blockID, | ||
Timestamp: time, | ||
} | ||
|
||
vpb := v.ToProto() | ||
if err := val.SignVote(chainID, vpb); err != nil { | ||
return nil, err | ||
} | ||
|
||
v.Signature = vpb.Signature | ||
return v, nil | ||
} |