From f6c39126ed355aa60490f4884c2a32d76076689d Mon Sep 17 00:00:00 2001 From: William Banfield <4561443+williambanfield@users.noreply.github.com> Date: Wed, 1 Dec 2021 18:26:36 -0500 Subject: [PATCH] tools/tm-signer-harness: switch to not use hardcoded bytes for configs in test (#7362) The current testing for this tool relies on hardcoding a set of configs into the tests. This means that when the config structure changes, the tests break. I'm not sure that this makes sense since we are separately testing our ability to read and validate the configuration file format. Hardcoding bytes into a different file duplicates this. Using the structs themselves should therefore be preferred. --- .../internal/test_harness_test.go | 88 +++++++------------ 1 file changed, 32 insertions(+), 56 deletions(-) diff --git a/tools/tm-signer-harness/internal/test_harness_test.go b/tools/tm-signer-harness/internal/test_harness_test.go index 2ef630555..ca54e5473 100644 --- a/tools/tm-signer-harness/internal/test_harness_test.go +++ b/tools/tm-signer-harness/internal/test_harness_test.go @@ -12,64 +12,14 @@ import ( "github.com/tendermint/tendermint/crypto" "github.com/tendermint/tendermint/crypto/ed25519" + tmjson "github.com/tendermint/tendermint/libs/json" "github.com/tendermint/tendermint/libs/log" + tmrand "github.com/tendermint/tendermint/libs/rand" "github.com/tendermint/tendermint/privval" "github.com/tendermint/tendermint/types" ) const ( - keyFileContents = `{ - "address": "D08FCA3BA74CF17CBFC15E64F9505302BB0E2748", - "pub_key": { - "type": "tendermint/PubKeyEd25519", - "value": "ZCsuTjaczEyon70nmKxwvwu+jqrbq5OH3yQjcK0SFxc=" - }, - "priv_key": { - "type": "tendermint/PrivKeyEd25519", - "value": "8O39AkQsoe1sBQwud/Kdul8lg8K9SFsql9aZvwXQSt1kKy5ONpzMTKifvSeYrHC/C76Oqturk4ffJCNwrRIXFw==" - } -}` - - stateFileContents = `{ - "height": "0", - "round": 0, - "step": 0 -}` - - genesisFileContents = `{ - "genesis_time": "2019-01-15T11:56:34.8963Z", - "chain_id": "test-chain-0XwP5E", - "consensus_params": { - "block": { - "max_bytes": "22020096", - "max_gas": "-1", - "time_iota_ms": "1000" - }, - "evidence": { - "max_age_num_blocks": "100000", - "max_age_duration": "172800000000000", - "max_num": 50 - }, - "validator": { - "pub_key_types": [ - "ed25519" - ] - } - }, - "validators": [ - { - "address": "D08FCA3BA74CF17CBFC15E64F9505302BB0E2748", - "pub_key": { - "type": "tendermint/PubKeyEd25519", - "value": "ZCsuTjaczEyon70nmKxwvwu+jqrbq5OH3yQjcK0SFxc=" - }, - "power": "10", - "name": "" - } - ], - "app_hash": "" -}` - defaultConnDeadline = 100 ) @@ -191,10 +141,36 @@ func harnessTest( } func makeConfig(t *testing.T, acceptDeadline, acceptRetries int) TestHarnessConfig { + t.Helper() + const keyFilename = "tm-testharness-keyfile" + const stateFilename = "tm-testharness-statefile" + pvFile, err := privval.GenFilePV(keyFilename, stateFilename, types.ABCIPubKeyTypeEd25519) + if err != nil { + panic(err) + } + pvGenDoc := types.GenesisDoc{ + ChainID: fmt.Sprintf("test-chain-%v", tmrand.Str(6)), + GenesisTime: time.Now(), + ConsensusParams: types.DefaultConsensusParams(), + Validators: []types.GenesisValidator{ + { + Address: pvFile.Key.Address, + PubKey: pvFile.Key.PubKey, + Power: 10, + }, + }, + } + + keyFileContents, err := tmjson.Marshal(pvFile.Key) + require.NoError(t, err) + stateFileContents, err := tmjson.Marshal(pvFile.LastSignState) + require.NoError(t, err) + genesisFileContents, err := tmjson.Marshal(pvGenDoc) + require.NoError(t, err) return TestHarnessConfig{ BindAddr: privval.GetFreeLocalhostAddrPort(), - KeyFile: makeTempFile("tm-testharness-keyfile", keyFileContents), - StateFile: makeTempFile("tm-testharness-statefile", stateFileContents), + KeyFile: makeTempFile(keyFilename, keyFileContents), + StateFile: makeTempFile(stateFilename, stateFileContents), GenesisFile: makeTempFile("tm-testharness-genesisfile", genesisFileContents), AcceptDeadline: time.Duration(acceptDeadline) * time.Millisecond, ConnDeadline: time.Duration(defaultConnDeadline) * time.Millisecond, @@ -210,12 +186,12 @@ func cleanup(cfg TestHarnessConfig) { os.Remove(cfg.GenesisFile) } -func makeTempFile(name, content string) string { +func makeTempFile(name string, content []byte) string { tempFile, err := os.CreateTemp("", fmt.Sprintf("%s-*", name)) if err != nil { panic(err) } - if _, err := tempFile.Write([]byte(content)); err != nil { + if _, err := tempFile.Write(content); err != nil { tempFile.Close() panic(err) }