-
Notifications
You must be signed in to change notification settings - Fork 40
feat: prove Steam trade offer state via notary for buyer-side trades #418
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Step7750
wants to merge
11
commits into
master
Choose a base branch
from
feat/csf-1979-notary-offer-state
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
7c95dbc
feat: prove Steam offer state via notary in background
Step7750 1afdce3
feat: share one trade offers fetch across ping, offer state proof, an…
Step7750 5672e86
feat: add updated_at to SteamOffer for buyer min offer age
Step7750 1385864
refactor: skip offer pings and proofs when the shared trade offers fe…
Step7750 e524e74
refactor: prove offer states last so a slow or failed proof never del…
Step7750 196486d
refactor: run offer state proof then cancel ping at the end of the alarm
Step7750 dae93f4
refactor: prove offer states for buyer-side trades only, seller pings…
Step7750 4f9f5c5
refactor: an offer the buyer cannot see is not a divergence, cancel-p…
Step7750 3d79fe4
docs: proving is a few seconds, not slow
Step7750 1e114f8
refactor: drop per-trade offer state proof throttle, rely on shared f…
Step7750 b5c76ef
refactor: inline single-use allOffers helper
Step7750 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| import {describe, expect, it} from 'vitest'; | ||
| import {OfferStatus} from '../bridge/handlers/trade_offer_status'; | ||
| import {ProofType} from '../notary/types'; | ||
| import {SlimTrade, TradeState} from '../types/float_market'; | ||
| import {TradeOfferState} from '../types/steam_constants'; | ||
| import {BUYER_MIN_OFFER_AGE_MS, buildOfferStateProveRequest, findBuyerOfferStateCandidates} from './offer_state'; | ||
|
|
||
| const sellerID = '76561198000000000'; | ||
| const buyerID = '76561198111111111'; | ||
| const offerID = '9340135296'; | ||
| const now = 1_800_000_000_000; | ||
| // Old enough that the minimum offer age never applies unless a test sets a fresh time_updated | ||
| const sentAt = new Date(now - 2 * BUYER_MIN_OFFER_AGE_MS).toISOString(); | ||
|
|
||
| function trade(state: TradeOfferState, waitForCancelPing = false, id = 'csfloat-trade-id'): SlimTrade { | ||
| return { | ||
| id, | ||
| state: TradeState.PENDING, | ||
| seller_id: sellerID, | ||
| buyer_id: buyerID, | ||
| contract: {item: {asset_id: '3899876543210123456', market_hash_name: 'AK-47 | Redline'}}, | ||
| steam_offer: {id: offerID, state, sent_at: sentAt}, | ||
| wait_for_cancel_ping: waitForCancelPing, | ||
| } as SlimTrade; | ||
| } | ||
|
|
||
| function offer(state: TradeOfferState, ageMs = 2 * BUYER_MIN_OFFER_AGE_MS): OfferStatus { | ||
| return {offer_id: offerID, state, time_updated: Math.floor((now - ageMs) / 1000)}; | ||
| } | ||
|
|
||
| describe('buyer offer state notary candidates', () => { | ||
| it('proves when the visible offer state differs from CSFloat', () => { | ||
| const t = trade(TradeOfferState.CreatedNeedsConfirmation); | ||
| expect(findBuyerOfferStateCandidates([t], [offer(TradeOfferState.Active)], now)).toEqual([ | ||
| {csfloatTrade: t, localState: TradeOfferState.Active}, | ||
| ]); | ||
| }); | ||
|
|
||
| it('gives seller telemetry first crack: skips offers that changed under 5 minutes ago', () => { | ||
| const t = trade(TradeOfferState.CreatedNeedsConfirmation); | ||
| const fresh = offer(TradeOfferState.Active, BUYER_MIN_OFFER_AGE_MS - 1000); | ||
| expect(findBuyerOfferStateCandidates([t], [fresh], now)).toEqual([]); | ||
| }); | ||
|
|
||
| it('does not treat an offer the buyer cannot see as a divergence until waiting on a cancel ping', () => { | ||
| expect(findBuyerOfferStateCandidates([trade(TradeOfferState.CreatedNeedsConfirmation)], [], now)).toEqual([]); | ||
| expect(findBuyerOfferStateCandidates([trade(TradeOfferState.Active)], [], now)).toEqual([]); | ||
| }); | ||
|
|
||
| it('proves when waiting on a cancel ping and the offer is gone', () => { | ||
| const t = trade(TradeOfferState.CreatedNeedsConfirmation, true); | ||
| expect(findBuyerOfferStateCandidates([t], [], now)).toEqual([{csfloatTrade: t, localState: undefined}]); | ||
| }); | ||
|
|
||
| it('does not prove when waiting on a cancel ping but the offer is still active', () => { | ||
| const t = trade(TradeOfferState.Active, true); | ||
| expect(findBuyerOfferStateCandidates([t], [offer(TradeOfferState.Active)], now)).toEqual([]); | ||
| }); | ||
|
|
||
| it('skips accepted offers since trade history owns that transition', () => { | ||
| const t = trade(TradeOfferState.Active, true); | ||
| expect(findBuyerOfferStateCandidates([t], [offer(TradeOfferState.Accepted)], now)).toEqual([]); | ||
| }); | ||
|
|
||
| it('ignores trades without an annotated offer', () => { | ||
| const t = {...trade(TradeOfferState.Active), steam_offer: {}} as SlimTrade; | ||
| expect(findBuyerOfferStateCandidates([t], [], now)).toEqual([]); | ||
| }); | ||
| }); | ||
|
|
||
| describe('offer state prove request', () => { | ||
| it('proves a single offer directly', () => { | ||
| expect(buildOfferStateProveRequest([{csfloatTrade: trade(TradeOfferState.Active)}])).toEqual({ | ||
| type: ProofType.TRADE_OFFER, | ||
| tradeofferid: offerID, | ||
| }); | ||
| }); | ||
|
|
||
| it('batches multiple offers into one unfiltered GetTradeOffers proof of received offers', () => { | ||
| expect( | ||
| buildOfferStateProveRequest([ | ||
| {csfloatTrade: trade(TradeOfferState.Active, false, 'a')}, | ||
| {csfloatTrade: trade(TradeOfferState.Active, true, 'b')}, | ||
| ]) | ||
| ).toEqual({type: ProofType.TRADE_OFFERS, get_sent_offers: false, get_received_offers: true}); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| import {OfferStatus} from '../bridge/handlers/trade_offer_status'; | ||
| import {NotaryProveRequest, ProofType} from '../notary/types'; | ||
| import {StorageKey} from '../storage/keys'; | ||
| import {gStore} from '../storage/store'; | ||
| import {SlimTrade, TradeState} from '../types/float_market'; | ||
| import {TradeOfferState} from '../types/steam_constants'; | ||
| import {reportTradeError} from './error_report'; | ||
| import {isBackgroundNotaryOfferStateEnabled, submitNotaryProof} from './notary'; | ||
|
|
||
| // Only prove offers that changed at least this long ago, giving the seller's telemetry first crack | ||
| export const BUYER_MIN_OFFER_AGE_MS = 5 * 60 * 1000; | ||
|
|
||
| export interface OfferStateCandidate { | ||
| csfloatTrade: SlimTrade; | ||
| // State of the offer as seen by the buyer on Steam, undefined if not visible to them | ||
| localState?: TradeOfferState; | ||
| } | ||
|
|
||
| /** | ||
| * Picks the buyer's pending trades worth proving: the offer state Steam shows them differs from what | ||
| * CSFloat has (seller telemetry offline), or the trade is waiting on a cancel ping and the offer is not active. | ||
| * An offer the buyer cannot see is not a divergence on its own: the proof of a hidden offer is empty, and | ||
| * "offer is gone" is already covered once the trade is waiting on a cancel ping. | ||
| * Offers that changed less than BUYER_MIN_OFFER_AGE_MS ago are skipped so the seller's own ping can resolve it. | ||
| * Accepted offers are skipped since the trade history proof owns that transition. | ||
| * | ||
| * @param buyerTrades Pending trades where this user is the buyer | ||
| * @param receivedOffers Trade offers received by this user on Steam | ||
| */ | ||
| export function findBuyerOfferStateCandidates( | ||
| buyerTrades: SlimTrade[], | ||
| receivedOffers: OfferStatus[], | ||
| now = Date.now() | ||
| ): OfferStateCandidate[] { | ||
| const candidates: OfferStateCandidate[] = []; | ||
|
|
||
| for (const trade of buyerTrades) { | ||
| if (trade.state !== TradeState.PENDING || !trade.steam_offer?.id) { | ||
| continue; | ||
| } | ||
|
|
||
| const serverState = trade.steam_offer.state; | ||
| const localOffer = receivedOffers.find((e) => e.offer_id === trade.steam_offer.id); | ||
| const localState = localOffer?.state; | ||
| if (serverState === TradeOfferState.Accepted || localState === TradeOfferState.Accepted) { | ||
| continue; | ||
| } | ||
|
|
||
| const diverged = localState !== undefined && localState !== serverState; | ||
| const waitingOnCancel = !!trade.wait_for_cancel_ping && localState !== TradeOfferState.Active; | ||
| if (!diverged && !waitingOnCancel) { | ||
| continue; | ||
| } | ||
|
|
||
| if (now - offerLastChangedMs(trade, localOffer) < BUYER_MIN_OFFER_AGE_MS) { | ||
| continue; | ||
| } | ||
|
|
||
| candidates.push({csfloatTrade: trade, localState}); | ||
| } | ||
|
|
||
| return candidates; | ||
| } | ||
|
|
||
| // Steam's time_updated if the offer is visible, otherwise the last change CSFloat knows about | ||
| function offerLastChangedMs(trade: SlimTrade, localOffer?: OfferStatus): number { | ||
| if (localOffer?.time_updated) { | ||
| return localOffer.time_updated * 1000; | ||
| } | ||
|
|
||
| return new Date(trade.steam_offer.updated_at || trade.steam_offer.sent_at || trade.created_at).getTime(); | ||
| } | ||
|
|
||
| /** | ||
| * One GetTradeOffer proof for a single candidate, otherwise a single GetTradeOffers proof of the buyer's | ||
| * received offers. Same unfiltered dump the untrusted ping reads, so the server sees the offers we evaluated. | ||
| */ | ||
| export function buildOfferStateProveRequest(candidates: OfferStateCandidate[]): NotaryProveRequest { | ||
| if (candidates.length === 1) { | ||
| return {type: ProofType.TRADE_OFFER, tradeofferid: candidates[0].csfloatTrade.steam_offer.id}; | ||
| } | ||
|
|
||
| return {type: ProofType.TRADE_OFFERS, get_sent_offers: false, get_received_offers: true}; | ||
| } | ||
|
|
||
| /** | ||
| * Submits notarized trade offer proofs for trades where this user is the buyer, so CSFloat can update the | ||
| * Steam offer state from trusted data when the seller's telemetry is offline. | ||
| * Seller-side proofs are deliberately not sent: pingSentTradeOffers already reports the same state and the | ||
| * server does not yet prefer notarized state over it, so proving would be redundant work. | ||
| * | ||
| * @param buyerTrades Pending trades where this user is the buyer | ||
| * @param receivedOffers Trade offers received by this user on Steam, already fetched for this alarm run | ||
| */ | ||
| export async function proveBuyerOfferStates(buyerTrades: SlimTrade[], receivedOffers: OfferStatus[]) { | ||
| if (!buyerTrades.some((e) => e.state === TradeState.PENDING && e.steam_offer?.id)) { | ||
| return; | ||
| } | ||
|
|
||
| if (!(await isBackgroundNotaryOfferStateEnabled())) { | ||
| return; | ||
| } | ||
|
|
||
| const lastFailure = await gStore.getWithStorage<number>( | ||
| chrome.storage.local, | ||
| StorageKey.LAST_NOTARY_BG_PROOF_FAILURE | ||
| ); | ||
| if (lastFailure && lastFailure > Date.now() - 60 * 60 * 1000) { | ||
| console.log('skipping offer state notary proof, last failure was less than 60 minutes ago'); | ||
| return; | ||
| } | ||
|
|
||
| const candidates = findBuyerOfferStateCandidates(buyerTrades, receivedOffers); | ||
| if (candidates.length === 0) { | ||
| return; | ||
| } | ||
|
|
||
| try { | ||
| await submitNotaryProof(buildOfferStateProveRequest(candidates)); | ||
| console.log(`proved offer state for ${candidates.length} trade(s) via notary`); | ||
| } catch (e) { | ||
| console.error('offer state notary proving failed', e); | ||
| await gStore.setWithStorage(chrome.storage.local, StorageKey.LAST_NOTARY_BG_PROOF_FAILURE, Date.now()); | ||
| reportTradeError(candidates[0].csfloatTrade.id, `background extension offer state notary failed: ${e}`); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seller offer ping lost HTML fallback
Medium Severity
The alarm now drives
pingSentTradeOffersfromgetSentAndReceivedTradeOffersFromAPIonly. That path treats an empty Steam body as success and never clears a bad token or falls back to HTML, so seller offer telemetry can stop silently whenGetTradeOffersreturns nothing.Additional Locations (1)
src/lib/alarms/trade_offer.ts#L22-L29Reviewed by Cursor Bugbot for commit b5c76ef. Configure here.