DEV Community

Merging two grants shouldn't move the goalposts - TTSwap's _addShare metric bug

One more from TTSwap's fully QA-verified nine-fix bounty sweep (PR #337, bounty #322): a two-line merge rule quietly raised the unlock requirements on shares users had already earned - and the failure only shows up at shareMint time, long after the bad merge.

The bug class

_addShare merges a second share grant into an existing position. The merge rule took the max of almost every field - which is reasonable for chips (more unlocks allowed) but wrong for metric, the threshold that gates each unlock.

// base: merge took the max of everything
newpart.metric = newpart.metric >= _share.metric ? newpart.metric : _share.metric;

Scenario from the regression test: a user holds a grant at metric = 0. A second grant arrives at metric = 120. The merge stores metric = 120 - retroactively raising the unlock bar for the first grant's unminted remainder. shareMint calls that were valid under the original terms now fail the gate.

The reproduced figure is exact: existing metric kept: 120 != 0. Independent QA reran it on clean worktrees under pinned solc 0.8.29 - base fails with that assertion, fix branch passes 3/3.

The fix pattern

Don't merge what shouldn't merge. chips still takes max (more unlocks is fine); metric keeps the existing value - merging must not raise the bar for what's already granted:

// Keep the existing metric - merging must not raise the unlock
// bar for the previously granted remainder (#322, RT-14b).
shares[owner] = newpart;

The first-grant path is untouched, so nothing changes for new positions - only the merge semantics.

Why skeptical devs respect this

QA was precise about scope: the "60/60 focused tests" claim was not fully rerun (UNVERIFIED subset), and the claimed parity with upstream v2.1.0 "RT-14b" was not independently diffed - also UNVERIFIED. What is verified: the exact failure figure reproduced on base, 3/3 dedicated merge tests pass on the fix, pinned compiler throughout.

The test itself is the interesting artifact - it mocks the market gate, grants metric 0 then metric 120, merges, and asserts shareMint still succeeds under the original gate.

The meta-lesson

"Take the max" feels safe in a merge - it never loses data. But for fields that encode terms (thresholds, rates, deadlines), max can silently rewrite a contract the user already agreed to. When merging state, ask per-field: is this a quantity to combine, or a term to preserve? If it's a term, the correct merge is often "keep the existing one," and the bug only fires when the second grant is worse for the holder.

Same discipline on our end: every number on a product page is verified against the delivered archive before it ships. Free tools and the auto-updating store link are on the nexusfoundry dev.to profile.

Read on DEV Community ↗ ← Back to News

Comments

No comments yet. Start the discussion.