!5314huginn-rsi/test-speed-20261102 → develop5d5041353c

Tests wait for what they assert: no-fsync fixtures, one shared wasmvm

AI-generatedEverything on this page was written by an AI agent from the diff: the summaries, the narration, the diagrams, the flags and the glossary. It can be wrong or incomplete, so check it against the code before you rely on it.

15commits
21files
440lines changed
33lines to read
1consensus-path edit
3prod package vars

Where the diff is

3-minute explainer

What changed

Wait, don't sleep

Tests poll for the signal they assert on instead of fixed sleeps.

go run()wait for signalassert

Cheaper fixtures

No-fsync LevelDB, in-memory monero DBs, BestSpeed lzma in tests.

observer −20.7%monero −23.6%tss −29.8%

Shared setup

One wasmvm per test binary; one gocheck run, shorter p2p waits.

keeper 1keeper 2keeper 3one wasmvm

Production seams

Three package vars default to the literals they replace.

lzmaLevel = BestCompressionopenObserverLevelDB = db.NewLevelDBsubscribeRetryBackoff = 1s

Fee voter signer decode

GetSigners decodes through the shared memoized bech32 lookup.

AccAddressFromBech32SignerAddressString

Look here first

Consensus path now uses signer memoOnly consensus-side edit. Memo keys on prefix+string, caches successes; AccAddress(addr) copies the unsafe cache-backed string. Check signers, order, skips.type_observed_network_fee_voter.go:26 ↗verify
1919 return fmt.Sprintf("%s-%d-%d-%d", m.Chain.String(), m.ReportBlockHeight, m.FeeRate, m.TransactionSize)
2020 }
2121
2222 func (m *ObservedNetworkFeeVoter) GetSigners() []cosmos.AccAddress {
2323 addrs := make([]cosmos.AccAddress, 0)
2424 for _, a := range m.Signers {
25− addr, err := cosmos.AccAddressFromBech32(a)
26− if err != nil {
25+ // memoized cosmos.AccAddressFromBech32; skips the same strings
26+ addr, ok := common.SignerAddressString(a)
27+ if !ok {
2728 continue
2829 }
29− addrs = append(addrs, addr)
30+ addrs = append(addrs, cosmos.AccAddress(addr))
3031 }
3132 return addrs
3233 }
3334
3435 // HasSigned - check if given address has signed
3536 func (m *ObservedNetworkFeeVoter) HasSigned(signer cosmos.AccAddress) bool {
3637 return signersInclude(m.Signers, signer)
3738 }
Global wasmvm shared across testsUnsynchronised package-level VM; isolation relies on per-test stores and serial gocheck runs. A parallel test would race it.handler_test.go:102 ↗careful
9393 c.Assert(err, IsNil)
9494 }
9595
9696 var keyThorchain = storetypes.NewKVStoreKey(StoreKey)
9797
98+// sharedWasmVM is the wasmvm every setupManagerForTest keeper runs on: a new VM
99+// costs milliseconds of cgo cache setup per test. Its code cache is keyed by
100+// checksum and each keeper finds code through its own store, so tests sharing
101+// it still see only the code they stored. TestMain removes its directory.
102+var sharedWasmVM struct {
103+ dir string
104+ vm wasmtypes.WasmEngine
105+}
106+
107+// sharedWasmVMOption returns the wasm home dir and keeper option for
108+// setupManagerForTest: the first call lets NewKeeper build the VM with its
109+// default config and keeps it, later calls hand that VM to the keeper.
110+func sharedWasmVMOption(c testFatal) (string, wasmkeeper.Option) {
111+ if sharedWasmVM.vm != nil {
112+ return sharedWasmVM.dir, wasmkeeper.WithWasmEngine(sharedWasmVM.vm)
113+ }
114+ dir, err := os.MkdirTemp("", "thorchain-wasm-")
115+ if err != nil {
Mutable lzma level in production packageDefault equals BestCompression, so backups are byte-identical; any non-test write would silently change keyshare output.encrypt_keyshares.go:50 ↗careful
3841 // ageWorkFactor is the scrypt work factor EncryptKeyshares and
3942 // EncryptRawKeyshares use: AgeWorkFactor, except in this package's tests,
4043 // which lower it because every encryption also decrypts to verify the
4144 // round trip.
4245 var ageWorkFactor = AgeWorkFactor
4346
47+// lzmaLevel is the compression level EncryptKeyshares and EncryptRawKeyshares
48+// use: LzmaLevel, except in this package's tests, which compress their small
49+// fixtures with a smaller dictionary.
50+var lzmaLevel = LzmaLevel
51+
4452 // EncryptKeyshares encrypts the keyshares at the provided path using the passphrase.
4553 func EncryptKeyshares(path, passphrase string) ([]byte, error) {
4654 // verify the passphrase is provided and a mnemonic
4755 if passphrase == "" {
4856 return nil, errors.New("failed keyshare encrypt: signer seed phrase is not set")
4957 }
5058 if len(strings.Split(passphrase, " ")) != 24 {
5159 return nil, errors.New("failed keyshare encrypt: signer seed phrase is not 24 words")
5260 }
5361 if !bip39.IsMnemonicValid(passphrase) {
5462 return nil, errors.New("failed keyshare encrypt: signer seed phrase is not valid bip39 mnemonic")
5563 }
Timing bounds replace fixed sleepFirst tick asserted within [blockTime, 2×blockTime); a loaded CI box could breach the upper bound.solvency_test.go:323 ↗careful
295319
296− calls := int(s.sp.callCount.Load())
320+ c.Assert(s.sp.callCount.Load() > 0, Equals, true, Commentf("no tick within %s", limit))
297321 c.Assert(s.sp.ShouldReportSolvencyRan(), Equals, true)
298− // With ThorchainBlockTime=1s (mocknet), expect 2-4 calls in 3.5s.
299− // A wrong sub-second default would produce far more calls.
300− c.Assert(calls >= 2, Equals, true, Commentf("expected at least 2 calls, got %d", calls))
301− c.Assert(calls <= 5, Equals, true, Commentf("expected at most 5 calls, got %d — interval likely shorter than ThorchainBlockTime", calls))
322+ c.Assert(elapsed >= constants.ThorchainBlockTime, Equals, true, Commentf("first tick after %s — interval shorter than ThorchainBlockTime", elapsed))
323+ c.Assert(elapsed < 2*constants.ThorchainBlockTime, Equals, true, Commentf("first tick after %s — interval longer than ThorchainBlockTime", elapsed))
302324 }
303325
304326 func (s *SolvencyTestSuite) TestSolvencyCheckRunnerStopChannel(c *C) {
305327 stopchan := make(chan struct{})
306328 wg := &sync.WaitGroup{}
307329 wg.Add(1)
308330 go SolvencyCheckRunner(common.ETHChain, s.sp, nil, stopchan, wg, time.Hour)
309331 close(stopchan)
310332 wg.Wait() // should return promptly via stopper
311333 }
312334
313335 func (s *SolvencyTestSuite) TestSolvencyCheckRunnerGetMimirError(c *C) {
314336 // Server that returns errors for mimir requests
Timeout test margin shrinks to 200msAt 2s the leader answers at 0.9×timeout, 200ms before the member deadline; a heavily loaded CI box has less slack.party_coordinator_test_with_leader_test.go:147 ↗careful
138139 }
139140 assert.Equal(t, pcs[0].host.ID().String(), leader)
140141 // now we test the leader appears firstly and the the members
141142 leaderAppersFirstTest(t, msgID, peers, pcs)
142143 leaderAppearsLastTest(t, msgID, peers, pcs)
143144 }
144145
145146 func TestNewPartyCoordinatorTimeOut(t *testing.T) {
146− timeout := time.Second * 3
147+ timeout := time.Second * 2
147148 hosts := setupHosts(t, 4)
148149 var pcs []*PartyCoordinator
149150 var peers []string
150151 for _, el := range hosts {
151152 pcs = append(pcs, NewPartyCoordinator(el, timeout))
152153 }
153154 sort.Slice(pcs, func(i, j int) bool {
154155 return pcs[i].host.ID().String() > pcs[j].host.ID().String()
155156 })
156157 for _, el := range pcs {
157158 peers = append(peers, el.host.ID().String())
158159 }
159160
Observer DB opener is a varDefaults to db.NewLevelDB; only TestMain swaps in a NoSync opener.observer_storage.go:171 ↗look
162162 }
163163
164164 type errataDeckMutation struct {
165165 key txInKey
166166 txIn *types.TxIn // nil deletes the exact original deck key
167167 }
168168
169+// openObserverLevelDB opens the observer database. Tests replace it with an
170+// opener that skips fsync, which no in-process test can observe.
171+var openObserverLevelDB = db.NewLevelDB
172+
169173 // NewObserverStorage create a new instance of LevelDBScannerStorage
170174 func NewObserverStorage(path string, opts config.LevelDBOptions) (*ObserverStorage, error) {
171− ldb, err := db.NewLevelDB(path, opts)
175+ ldb, err := openObserverLevelDB(path, opts)
172176 if err != nil {
173177 return nil, fmt.Errorf("failed to create observer storage: %w", err)
174178 }
175179
176180 return &ObserverStorage{db: ldb}, nil
177181 }
178182
179183 // createTxKey creates a unique key for a TxIn based on chain, finalization height, and future-observation mode.
Resubscribe backoff is a vartime.Second default used for both initial and reset backoff, as before.event_client.go:16 ↗look
77 "time"
88
99 "github.com/rs/zerolog"
1010 "github.com/rs/zerolog/log"
1111 "gitlab.com/thorchain/thornode/v3/x/thorchain/ebifrost"
1212 )
1313
14+// subscribeRetryBackoff is the first resubscribe backoff after an error, and the
15+// value the backoff resets to after a clean exit; a var so tests can shorten it.
16+var subscribeRetryBackoff = time.Second
17+
1418 // EventClient manages a single subscription to events
1519 type EventClient struct {
1620 logger zerolog.Logger
1721 client ebifrost.LocalhostBifrostClient
1822 eventTypes []string
1923 handlers map[string]func(*ebifrost.EventNotification)
2024
2125 // Subscription state
2226 mu sync.RWMutex
2327 isActive bool
2428 ctx context.Context
2529 cancelFunc context.CancelFunc
Hand-rolled gocheck run forwards three flagsPasses check.f, v and vv only; check.list, check.b and check.work don't apply, so -check.list runs this suite.type_dynamic_fee_test.go:16 ↗look
78 . "gopkg.in/check.v1"
89 )
910
10−func TestDynamicFeeTypes(t *testing.T) { TestingT(t) }
11+// TestDynamicFeeTypes runs only DynamicFeeTypesSuite. The suite is not
12+// registered with Suite: TestingT runs every registered suite, which would run
13+// the whole package a second time next to TestPackage.
14+func TestDynamicFeeTypes(t *testing.T) {
15+ flagValue := func(name string) string { return flag.Lookup(name).Value.String() }
16+ result := Run(&DynamicFeeTypesSuite{}, &RunConf{
17+ Filter: flagValue("check.f"),
18+ Verbose: flagValue("check.v") == "true",
19+ Stream: flagValue("check.vv") == "true",
20+ })
21+ println(result.String())
22+ if !result.Passed() {
23+ t.Fail()
24+ }
25+}
1126
1227 type DynamicFeeTypesSuite struct{}
1328
14−var _ = Suite(&DynamicFeeTypesSuite{})

Ask an AI about this change

Chat assistantClaude, ChatGPT, … (reads a link)

Coding agentClaude Code, Cursor, … (reads the repo)

The context file is context.md. Use Copy with context for an AI that can't open links; it pastes the whole file in.

Glossary

Bifrost
THORNode's chain-observer and signer daemon; most changed tests live under bifrost/.
fsync
Flushing writes to disk; LevelDB's NoSync option skips it, which only matters if the process or machine crashes.
gocheck
The gopkg.in/check.v1 suite framework; one go test entry point runs every registered suite unless filtered.
lzma level
Compression preset for keyshare backups; BestCompression uses a 2^27-byte dictionary, BestSpeed a small one. bifrost/tss/encrypt_keyshares.go ↗
SignerAddressString
Memoized bech32 decode of a signer, keyed on account prefix plus string; returns the address bytes as a string. common/type_observed_tx.go ↗
Solvency runner
Bifrost loop that periodically reports chain solvency to THORChain; its tests now wait for the asserted tick.
wasmvm
CosmWasm's Go wrapper around the Rust VM; it keeps a code cache keyed by contract checksum on disk.