From 1dd693480cbfea6cffb1d1fa947fb1ccbb9d3acb Mon Sep 17 00:00:00 2001 From: Conner Fromknecht Date: Tue, 2 Feb 2021 12:42:28 -0800 Subject: [PATCH 1/2] txscript/hashcache_test: always add inputs during getTxn TestHashCacheAddContainsHashes flakes fairly regularly when rebasing PR #1684 with: txid wasn't inserted into cache but was found. With probabilty 1/10^2 there will be no inputs on the transaction. This reduces the entropy in the txid, and I belive is the primary cause of the flake. --- txscript/hashcache_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/txscript/hashcache_test.go b/txscript/hashcache_test.go index 389918e2f2..09b71fafa6 100644 --- a/txscript/hashcache_test.go +++ b/txscript/hashcache_test.go @@ -18,7 +18,7 @@ func genTestTx() (*wire.MsgTx, error) { tx := wire.NewMsgTx(2) tx.Version = rand.Int31() - numTxins := rand.Intn(11) + numTxins := 1 + rand.Intn(11) for i := 0; i < numTxins; i++ { randTxIn := wire.TxIn{ PreviousOutPoint: wire.OutPoint{ @@ -34,7 +34,7 @@ func genTestTx() (*wire.MsgTx, error) { tx.TxIn = append(tx.TxIn, &randTxIn) } - numTxouts := rand.Intn(11) + numTxouts := 1 + rand.Intn(11) for i := 0; i < numTxouts; i++ { randTxOut := wire.TxOut{ Value: rand.Int63(), From 5300a19d06d29171637fcb067dd5d3af6895d900 Mon Sep 17 00:00:00 2001 From: Conner Fromknecht Date: Tue, 2 Feb 2021 13:24:27 -0800 Subject: [PATCH 2/2] txscript/hashcache_test: call rand.Seed once in init This resolves the more fundamental flake in the unit tests noted in the prior commit. Because multiple unit tests call rand.Seed in parallel, it's possible they can be executed with the same unix timestamp (in seconds). If the second call happens between generating the hash cache and checking that the cache doesn't contain a random txn, the random transaction is in fact a duplicate of one generated earlier since the RNG state was reset. To remedy, we initialize rand.Seed once in the init function. --- txscript/hashcache_test.go | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/txscript/hashcache_test.go b/txscript/hashcache_test.go index 09b71fafa6..cee59b9956 100644 --- a/txscript/hashcache_test.go +++ b/txscript/hashcache_test.go @@ -13,6 +13,10 @@ import ( "github.com/davecgh/go-spew/spew" ) +func init() { + rand.Seed(time.Now().Unix()) +} + // genTestTx creates a random transaction for uses within test cases. func genTestTx() (*wire.MsgTx, error) { tx := wire.NewMsgTx(2) @@ -56,8 +60,6 @@ func genTestTx() (*wire.MsgTx, error) { func TestHashCacheAddContainsHashes(t *testing.T) { t.Parallel() - rand.Seed(time.Now().Unix()) - cache := NewHashCache(10) var err error @@ -109,8 +111,6 @@ func TestHashCacheAddContainsHashes(t *testing.T) { func TestHashCacheAddGet(t *testing.T) { t.Parallel() - rand.Seed(time.Now().Unix()) - cache := NewHashCache(10) // To start, we'll generate a random transaction and compute the set of @@ -144,8 +144,6 @@ func TestHashCacheAddGet(t *testing.T) { func TestHashCachePurge(t *testing.T) { t.Parallel() - rand.Seed(time.Now().Unix()) - cache := NewHashCache(10) var err error