From 45a447aa2ff978e10d7ef6e43e208f45b2970597 Mon Sep 17 00:00:00 2001 From: Anton Kaliaev Date: Tue, 9 Jul 2019 17:41:06 +0400 Subject: [PATCH] move PrivKeys to internal folder read https://golang.org/cmd/go/#hdr-Internal_Directories if you want to know how internal directories work --- lite/base_verifier_test.go | 5 ++- .../privkeys/privkeys.go} | 43 +++++++------------ lite/provider_test.go | 12 ++++-- lite/verifying/provider.go | 3 ++ lite/verifying/provider_test.go | 14 +++--- 5 files changed, 38 insertions(+), 39 deletions(-) rename lite/{helpers.go => internal/privkeys/privkeys.go} (86%) diff --git a/lite/base_verifier_test.go b/lite/base_verifier_test.go index f0421eb78..714219bb5 100644 --- a/lite/base_verifier_test.go +++ b/lite/base_verifier_test.go @@ -6,13 +6,14 @@ import ( "github.com/stretchr/testify/assert" lerr "github.com/tendermint/tendermint/lite/errors" + pks "github.com/tendermint/tendermint/lite/internal/privkeys" "github.com/tendermint/tendermint/types" ) func TestBaseVerifier(t *testing.T) { assert := assert.New(t) - keys := GenPrivKeys(4) + keys := pks.GenPrivKeys(4) // 20, 30, 40, 50 - the first 3 don't have 2/3, the last 3 do! vals := keys.ToValidators(20, 10) // and a Verifier based on our known set @@ -20,7 +21,7 @@ func TestBaseVerifier(t *testing.T) { cert := NewBaseVerifier(chainID, 2, vals) cases := []struct { - keys PrivKeys + keys pks.PrivKeys vals *types.ValidatorSet height int64 first, last int // who actually signs diff --git a/lite/helpers.go b/lite/internal/privkeys/privkeys.go similarity index 86% rename from lite/helpers.go rename to lite/internal/privkeys/privkeys.go index 61bd7dc8c..a90f8a969 100644 --- a/lite/helpers.go +++ b/lite/internal/privkeys/privkeys.go @@ -1,15 +1,14 @@ -package lite +package privkeys import ( "github.com/tendermint/tendermint/crypto" "github.com/tendermint/tendermint/crypto/ed25519" "github.com/tendermint/tendermint/crypto/secp256k1" - "github.com/tendermint/tendermint/types" tmtime "github.com/tendermint/tendermint/types/time" ) -// privKeys is a helper type for testing. +// PrivKeys is a helper type for testing. // // It lets us simulate signing with many keys. The main use case is to create // a set, and call GenSignedHeader to get properly signed header for testing. @@ -18,7 +17,7 @@ import ( // and can optionally extend the validator set later with Extend. type PrivKeys []crypto.PrivKey -// genPrivKeys produces an array of private keys to generate commits. +// GenPrivKeys produces an array of private keys to generate commits. func GenPrivKeys(n int) PrivKeys { res := make(PrivKeys, n) for i := range res { @@ -84,6 +83,18 @@ func (pkz PrivKeys) signHeader(header *types.Header, first, last int) *types.Com return types.NewCommit(blockID, commitSigs) } +// GenSignedHeader calls genHeader and signHeader and combines them into a SignedHeader. +func (pkz PrivKeys) GenSignedHeader(chainID string, height int64, txs types.Txs, + valset, nextValset *types.ValidatorSet, appHash, consHash, resHash []byte, first, last int) types.SignedHeader { + + header := genHeader(chainID, height, txs, valset, nextValset, appHash, consHash, resHash) + check := types.SignedHeader{ + Header: header, + Commit: pkz.signHeader(header, first, last), + } + return check +} + func makeVote(header *types.Header, valset *types.ValidatorSet, key crypto.PrivKey) *types.Vote { addr := key.PubKey().Address() idx, _ := valset.GetByAddress(addr) @@ -127,27 +138,3 @@ func genHeader(chainID string, height int64, txs types.Txs, LastResultsHash: resHash, } } - -// GenSignedHeader calls genHeader and signHeader and combines them into a SignedHeader. -func (pkz PrivKeys) GenSignedHeader(chainID string, height int64, txs types.Txs, - valset, nextValset *types.ValidatorSet, appHash, consHash, resHash []byte, first, last int) types.SignedHeader { - - header := genHeader(chainID, height, txs, valset, nextValset, appHash, consHash, resHash) - check := types.SignedHeader{ - Header: header, - Commit: pkz.signHeader(header, first, last), - } - return check -} - -// GenFullCommit calls genHeader and signHeader and combines them into a FullCommit. -func (pkz PrivKeys) GenFullCommit(chainID string, height int64, txs types.Txs, - valset, nextValset *types.ValidatorSet, appHash, consHash, resHash []byte, first, last int) FullCommit { - - header := genHeader(chainID, height, txs, valset, nextValset, appHash, consHash, resHash) - commit := types.SignedHeader{ - Header: header, - Commit: pkz.signHeader(header, first, last), - } - return NewFullCommit(commit, valset, nextValset) -} diff --git a/lite/provider_test.go b/lite/provider_test.go index 56c3968dd..d578b5c4a 100644 --- a/lite/provider_test.go +++ b/lite/provider_test.go @@ -10,6 +10,7 @@ import ( dbm "github.com/tendermint/tendermint/libs/db" log "github.com/tendermint/tendermint/libs/log" lerr "github.com/tendermint/tendermint/lite/errors" + pks "github.com/tendermint/tendermint/lite/internal/privkeys" "github.com/tendermint/tendermint/types" ) @@ -48,7 +49,7 @@ func TestMultiProvider(t *testing.T) { func checkProvider(t *testing.T, p PersistentProvider, chainID, app string) { assert, require := assert.New(t), require.New(t) appHash := []byte(app) - keys := GenPrivKeys(5) + keys := pks.GenPrivKeys(5) count := 10 // Make a bunch of full commits. @@ -56,7 +57,9 @@ func checkProvider(t *testing.T, p PersistentProvider, chainID, app string) { for i := 0; i < count; i++ { vals := keys.ToValidators(10, int64(count/2)) h := int64(20 + 10*i) - fcz[i] = keys.GenFullCommit(chainID, h, nil, vals, vals, appHash, []byte("params"), []byte("results"), 0, 5) + signedHeader := keys.GenSignedHeader(chainID, h, nil, vals, vals, appHash, + []byte("params"), []byte("results"), 0, 5) + fcz[i] = NewFullCommit(signedHeader, vals, vals) } // Check that provider is initially empty. @@ -113,14 +116,15 @@ func TestMultiLatestFullCommit(t *testing.T) { chainID := "cache-best-height" appHash := []byte("01234567") - keys := GenPrivKeys(5) + keys := pks.GenPrivKeys(5) count := 10 // Set a bunch of full commits. for i := 0; i < count; i++ { vals := keys.ToValidators(10, int64(count/2)) h := int64(10 * (i + 1)) - fc := keys.GenFullCommit(chainID, h, nil, vals, vals, appHash, []byte("params"), []byte("results"), 0, 5) + signedHeader := keys.GenSignedHeader(chainID, h, nil, vals, vals, appHash, []byte("params"), []byte("results"), 0, 5) + fc := NewFullCommit(signedHeader, vals, vals) err := p2.SaveFullCommit(fc) require.NoError(err) } diff --git a/lite/verifying/provider.go b/lite/verifying/provider.go index ccd38cae4..7c297543b 100644 --- a/lite/verifying/provider.go +++ b/lite/verifying/provider.go @@ -1,3 +1,6 @@ +/* +Package verifying +*/ package verifying import ( diff --git a/lite/verifying/provider_test.go b/lite/verifying/provider_test.go index 4d462903f..dd881ce11 100644 --- a/lite/verifying/provider_test.go +++ b/lite/verifying/provider_test.go @@ -35,10 +35,11 @@ func TestProviderValidPath(t *testing.T) { nextVals := nkeys.ToValidators(vote, 0) h := int64(1 + i) appHash := []byte(fmt.Sprintf("h=%d", h)) - fcz[i] = keys.GenFullCommit( + signedHeader := keys.GenSignedHeader( chainID, h, nil, vals, nextVals, appHash, consHash, resHash, 0, len(keys)) + fcz[i] = lite.NewFullCommit(signedHeader, vals, nextVals) // Extend the keys by 1 each time. keys = nkeys nkeys = nkeys.Extend(1) @@ -129,14 +130,15 @@ func TestProviderDynamicVerification(t *testing.T) { } func makeFullCommit(height int64, keys lite.PrivKeys, vals, nextVals *types.ValidatorSet, chainID string) lite.FullCommit { - height += 1 + height++ consHash := []byte("special-params") appHash := []byte(fmt.Sprintf("h=%d", height)) resHash := []byte(fmt.Sprintf("res=%d", height)) - return keys.GenFullCommit( + signedHeader := keys.GenSignedHeader( chainID, height, nil, vals, nextVals, appHash, consHash, resHash, 0, len(keys)) + return lite.NewFullCommit(signedHeader, vals, nextVals) } func TestVerifingProviderHistorical(t *testing.T) { @@ -160,10 +162,11 @@ func TestVerifingProviderHistorical(t *testing.T) { h := int64(1 + i) appHash := []byte(fmt.Sprintf("h=%d", h)) resHash := []byte(fmt.Sprintf("res=%d", h)) - fcz[i] = keys.GenFullCommit( + signedHeader := keys.GenSignedHeader( chainID, h, nil, vals, nextVals, appHash, consHash, resHash, 0, len(keys)) + fcz[i] = lite.NewFullCommit(signedHeader, vals, nextVals) // Extend the keys by 1 each time. keys = nkeys nkeys = nkeys.Extend(1) @@ -241,10 +244,11 @@ func TestConcurrentProvider(t *testing.T) { h := int64(1 + i) appHash := []byte(fmt.Sprintf("h=%d", h)) resHash := []byte(fmt.Sprintf("res=%d", h)) - fcz[i] = keys.GenFullCommit( + signedHeader := keys.GenSignedHeader( chainID, h, nil, vals, nextVals, appHash, consHash, resHash, 0, len(keys)) + fcz[i] = lite.NewFullCommit(signedHeader, vals, nextVals) // Extend the keys by 1 each time. keys = nkeys nkeys = nkeys.Extend(1)