Update go-crypto, move testutils into types

This commit is contained in:
Ethan Frey 2017-02-24 22:24:01 +01:00
parent 4ad645f318
commit 1fd2d17cd9
9 changed files with 57 additions and 74 deletions

View File

@ -11,7 +11,7 @@ install:
go install github.com/tendermint/basecoin/cmd/...
test:
go test --race `${NOVENDOR}`
go test `${NOVENDOR}`
#go run tests/tendermint/*.go
get_deps:

2
glide.lock generated
View File

@ -65,7 +65,7 @@ imports:
- name: github.com/tendermint/go-config
version: e64b424499acd0eb9856b88e10c0dff41628c0d6
- name: github.com/tendermint/go-crypto
version: 8c9b889ccfe1f891ce8ab36c843f15794ce8f30f
version: 562b4cc9ef0d20217f6e95679f9e83cb7bc98b17
- name: github.com/tendermint/go-data
version: f199ef165cd5a50d569b179201702c5ec8899013
- name: github.com/tendermint/go-db

View File

@ -6,7 +6,6 @@ import (
"github.com/stretchr/testify/assert"
abci "github.com/tendermint/abci/types"
"github.com/tendermint/basecoin/app"
"github.com/tendermint/basecoin/testutils"
"github.com/tendermint/basecoin/types"
crypto "github.com/tendermint/go-crypto"
"github.com/tendermint/go-wire"
@ -27,7 +26,7 @@ func TestCounterPlugin(t *testing.T) {
bcApp.RegisterPlugin(counterPlugin)
// Account initialization
test1PrivAcc := testutils.PrivAccountFromSecret("test1")
test1PrivAcc := types.PrivAccountFromSecret("test1")
// Seed Basecoin with account
test1Acc := test1PrivAcc.Account

View File

@ -8,7 +8,6 @@ import (
"github.com/stretchr/testify/assert"
abci "github.com/tendermint/abci/types"
"github.com/tendermint/basecoin/testutils"
"github.com/tendermint/basecoin/types"
cmn "github.com/tendermint/go-common"
crypto "github.com/tendermint/go-crypto"
@ -29,7 +28,7 @@ func genGenesisDoc(chainID string, numVals int) (*tm.GenesisDoc, []types.PrivAcc
for i := 0; i < numVals; i++ {
name := cmn.Fmt("%v_val_%v", chainID, i)
privAcc := testutils.PrivAccountFromSecret(name)
privAcc := types.PrivAccountFromSecret(name)
genDoc.Validators = append(genDoc.Validators, tm.GenesisValidator{
PubKey: privAcc.PubKey.PubKey,
Amount: 1,

View File

@ -5,7 +5,6 @@ import (
"time"
"github.com/gorilla/websocket"
"github.com/tendermint/basecoin/testutils"
"github.com/tendermint/basecoin/types"
cmn "github.com/tendermint/go-common"
crypto "github.com/tendermint/go-crypto"
@ -38,10 +37,10 @@ func main() {
}()
// Get the root account
root := testutils.PrivAccountFromSecret("test")
root := types.PrivAccountFromSecret("test")
sequence := int(0)
// Make a bunch of PrivAccounts
privAccounts := testutils.RandAccounts(1000, 1000000, 0)
privAccounts := types.RandAccounts(1000, 1000000, 0)
privAccountSequences := make(map[string]int)
// Send coins to each account

View File

@ -4,7 +4,6 @@ import (
"testing"
"github.com/tendermint/basecoin/app"
"github.com/tendermint/basecoin/testutils"
"github.com/tendermint/basecoin/types"
cmn "github.com/tendermint/go-common"
crypto "github.com/tendermint/go-crypto"
@ -19,8 +18,8 @@ func TestSendTx(t *testing.T) {
bcApp.SetOption("base/chainID", chainID)
t.Log(bcApp.Info())
test1PrivAcc := testutils.PrivAccountFromSecret("test1")
test2PrivAcc := testutils.PrivAccountFromSecret("test2")
test1PrivAcc := types.PrivAccountFromSecret("test1")
test2PrivAcc := types.PrivAccountFromSecret("test2")
// Seed Basecoin with account
test1Acc := test1PrivAcc.Account
@ -66,14 +65,14 @@ func TestSequence(t *testing.T) {
t.Log(bcApp.Info())
// Get the test account
test1PrivAcc := testutils.PrivAccountFromSecret("test1")
test1PrivAcc := types.PrivAccountFromSecret("test1")
test1Acc := test1PrivAcc.Account
test1Acc.Balance = types.Coins{{"", 1 << 53}}
t.Log(bcApp.SetOption("base/account", string(wire.JSONBytes(test1Acc))))
sequence := int(1)
// Make a bunch of PrivAccounts
privAccounts := testutils.RandAccounts(1000, 1000000, 0)
privAccounts := types.RandAccounts(1000, 1000000, 0)
privAccountSequences := make(map[string]int)
// Send coins to each account

View File

@ -1,47 +0,0 @@
// Functions used in testing throughout
package testutils
import (
"github.com/tendermint/basecoin/types"
. "github.com/tendermint/go-common"
"github.com/tendermint/go-crypto"
)
// Creates a PrivAccount from secret.
// The amount is not set.
func PrivAccountFromSecret(secret string) types.PrivAccount {
privKey := crypto.GenPrivKeyEd25519FromSecret([]byte(secret))
privAccount := types.PrivAccount{
PrivKeyS: crypto.PrivKeyS{privKey},
Account: types.Account{
PubKey: crypto.PubKeyS{privKey.PubKey()},
Sequence: 0,
},
}
return privAccount
}
// Make `num` random accounts
func RandAccounts(num int, minAmount int64, maxAmount int64) []types.PrivAccount {
privAccs := make([]types.PrivAccount, num)
for i := 0; i < num; i++ {
balance := minAmount
if maxAmount > minAmount {
balance += RandInt64() % (maxAmount - minAmount)
}
privKey := crypto.GenPrivKeyEd25519()
pubKey := crypto.PubKeyS{privKey.PubKey()}
privAccs[i] = types.PrivAccount{
PrivKeyS: crypto.PrivKeyS{privKey},
Account: types.Account{
PubKey: pubKey,
Sequence: 0,
Balance: types.Coins{types.Coin{"", balance}},
},
}
}
return privAccs
}

45
types/test_helpers.go Normal file
View File

@ -0,0 +1,45 @@
package types
// Helper functions for testing
import (
cmn "github.com/tendermint/go-common"
"github.com/tendermint/go-crypto"
)
// Creates a PrivAccount from secret.
// The amount is not set.
func PrivAccountFromSecret(secret string) PrivAccount {
privKey := crypto.GenPrivKeyEd25519FromSecret([]byte(secret))
privAccount := PrivAccount{
PrivKeyS: crypto.PrivKeyS{privKey},
Account: Account{
PubKey: crypto.PubKeyS{privKey.PubKey()},
},
}
return privAccount
}
// Make `num` random accounts
func RandAccounts(num int, minAmount int64, maxAmount int64) []PrivAccount {
privAccs := make([]PrivAccount, num)
for i := 0; i < num; i++ {
balance := minAmount
if maxAmount > minAmount {
balance += cmn.RandInt64() % (maxAmount - minAmount)
}
privKey := crypto.GenPrivKeyEd25519()
pubKey := crypto.PubKeyS{privKey.PubKey()}
privAccs[i] = PrivAccount{
PrivKeyS: crypto.PrivKeyS{privKey},
Account: Account{
PubKey: pubKey,
Balance: Coins{Coin{"", balance}},
},
}
}
return privAccs
}

View File

@ -67,21 +67,10 @@ func TestAppTxSignable(t *testing.T) {
cmn.Fmt("Got unexpected sign string for SendTx. Expected:\n%v\nGot:\n%v", expected, signBytesHex))
}
// d'oh, can't use the version in testutils due to circular imports :(
func makePrivAcct() PrivAccount {
privKey := crypto.PrivKeyS{crypto.GenPrivKeyEd25519()}
return PrivAccount{
PrivKeyS: privKey,
Account: Account{
PubKey: crypto.PubKeyS{privKey.PubKey()},
},
}
}
func TestSendTxJSON(t *testing.T) {
chainID := "test_chain_id"
test1PrivAcc := makePrivAcct()
test2PrivAcc := makePrivAcct()
test1PrivAcc := PrivAccountFromSecret("sendtx1")
test2PrivAcc := PrivAccountFromSecret("sendtx2")
// Construct a SendTx signature
tx := &SendTx{