<feed xmlns='http://www.w3.org/2005/Atom'>
<title>guess-market/server, branch 3.0</title>
<subtitle>definitely not polymarket
</subtitle>
<id>https://git.sartin.in/guess-market/atom?h=3.0</id>
<link rel='self' href='https://git.sartin.in/guess-market/atom?h=3.0'/>
<link rel='alternate' type='text/html' href='https://git.sartin.in/guess-market/'/>
<updated>2026-10-15T08:40:30Z</updated>
<entry>
<title>server: add a chat shared by everyone logged in</title>
<updated>2026-10-15T08:40:30Z</updated>
<author>
<name>Kostya</name>
<email>mail@sartin.in</email>
</author>
<published>2026-10-15T08:40:05Z</published>
<link rel='alternate' type='text/html' href='https://git.sartin.in/guess-market/commit/?id=4563c9d6d6c1576b6fb45899c2b6d6073966d8b8'/>
<id>urn:sha1:4563c9d6d6c1576b6fb45899c2b6d6073966d8b8</id>
<content type='text'>

Ex3 bonus. POST /api/chat says a line as the logged-in user and
GET /api/chat?since=N returns the lines after id N, so a polling client
only ever downloads what it has not seen. The last 500 lines are kept
in memory.

A chat line does not change the market, so the lock filter does not
bump the market version for it; otherwise every line would make every
client refetch all events.

Also read form fields as UTF-8 in the filter rather than relying on
Tomcat's default, since names and chat text may not be ASCII.


</content>
</entry>
<entry>
<title>api: share the user directory row with clients</title>
<updated>2026-10-12T08:51:00Z</updated>
<author>
<name>Kostya</name>
<email>mail@sartin.in</email>
</author>
<published>2026-10-12T08:50:31Z</published>
<link rel='alternate' type='text/html' href='https://git.sartin.in/guess-market/commit/?id=da1683eccff72b5d4745fe7f5eaac42d700e7989'/>
<id>urn:sha1:da1683eccff72b5d4745fe7f5eaac42d700e7989</id>
<content type='text'>

The row GET /api/users and /api/sync send for each user was a record
private to UsersServlet, so a client had nothing to deserialize it
into. Move it to the api module as UserSummaryDTO.

Make TestServer public as well, so client modules can test against a
real embedded server.


</content>
</entry>
<entry>
<title>server: add versioned sync endpoint for polling clients</title>
<updated>2026-10-11T14:16:05Z</updated>
<author>
<name>Kostya</name>
<email>mail@sartin.in</email>
</author>
<published>2026-10-11T14:15:39Z</published>
<link rel='alternate' type='text/html' href='https://git.sartin.in/guess-market/commit/?id=dc616e0f340f28b320253bf9a70778a07b885ffd'/>
<id>urn:sha1:dc616e0f340f28b320253bf9a70778a07b885ffd</id>
<content type='text'>

The spec has clients pull updates every half second or so, by
fetching everything, only changes, or something in between. Take the
middle road: the engine keeps a version that moves on every change,
and GET /api/sync?since={version} answers {"changed": false} when
nothing has happened, which is almost every poll. Otherwise it returns
a snapshot of the event summaries, the user directory and the caller's
account, with the version to send next time. A client looking at one
event refetches its details from /api/events/{name} only when the
version moves.

The lock filter bumps the version after every state-changing request,
still inside the write lock, so a reader can never pick up new data
under an old version. CORS preflights now take the read lock, since
they change nothing. A session that times out also bumps the version,
so other users see that user go offline. The user directory rows move
into a helper that /api/users and the snapshot share.


</content>
</entry>
<entry>
<title>server: serialize engine access with one read/write lock</title>
<updated>2026-10-11T11:48:39Z</updated>
<author>
<name>Kostya</name>
<email>mail@sartin.in</email>
</author>
<published>2026-10-11T11:48:12Z</published>
<link rel='alternate' type='text/html' href='https://git.sartin.in/guess-market/commit/?id=241fd5f0a46aff89a3302d2753a01e217a562e79'/>
<id>urn:sha1:241fd5f0a46aff89a3302d2753a01e217a562e79</id>
<content type='text'>

The engine was written for a single-threaded desktop app: users,
events and ledgers live in plain lists, and a balance update is a
read followed by a write. Tomcat runs each request on its own thread,
so once clients poll while others trade, a read can iterate a list
mid-insert and fail with a 500, and two deposits can both read the
same old balance and lose one update.

Every server-side call into the engine arrives through /api/*, so put
one ReentrantReadWriteLock in a filter there. GETs, which is all the
polling, share the read lock and still run in parallel. Everything else
takes the write lock and changes the market alone, which also makes
check-then-act steps like opening an event or claiming a username
atomic. Writes are short in-memory operations, so one lock is enough.
Per-event locks are the next step if trading volume ever needs them.

The test runs writers depositing into one account while others poll
three endpoints and log new users in. Without the filter it fails with
500s; with it, every request succeeds and the balance and ledger count
match the number of deposits.


</content>
</entry>
<entry>
<title>server: add account, deposit and user directory endpoints</title>
<updated>2026-10-11T09:21:10Z</updated>
<author>
<name>Kostya</name>
<email>mail@sartin.in</email>
</author>
<published>2026-10-11T09:20:47Z</published>
<link rel='alternate' type='text/html' href='https://git.sartin.in/guess-market/commit/?id=cf13d4608acfb2d81ec0056717e561bb40f991a2'/>
<id>urn:sha1:cf13d4608acfb2d81ec0056717e561bb40f991a2</id>
<content type='text'>

Give each logged-in user their own account view and a view of
everyone else, as the spec asks:

- GET /api/user/account returns the caller's balance, blocked flag and
  every ledger row (trades, fees, payouts, deposits).
- POST /api/user/deposit (form field "amount") tops the account up
  through AccountContext.deposit, so its validation and DEPOSIT ledger
  entry are the same as in the engine. Bad amounts get 400.
- GET /api/users lists every account with its name, balance and
  whether it makes any market, the three facts the spec requires about
  other users, plus whether it is logged in right now, which the login
  registry already knows. Accounts outlive their sessions, so the list
  includes users who are currently logged out.

A test drives two clients through a whole market: one deposits,
uploads and opens an event, the other deposits and buys, the first
resolves it, and the winner's balance rises by exactly one per winning
share.


</content>
</entry>
<entry>
<title>server: add event and order endpoints</title>
<updated>2026-10-11T07:35:48Z</updated>
<author>
<name>Kostya</name>
<email>mail@sartin.in</email>
</author>
<published>2026-10-11T07:35:20Z</published>
<link rel='alternate' type='text/html' href='https://git.sartin.in/guess-market/commit/?id=1cb67cec8951553fdb5969d947e49f2288f3847f'/>
<id>urn:sha1:1cb67cec8951553fdb5969d947e49f2288f3847f</id>
<content type='text'>

Expose the market to logged-in clients:

- GET /api/events lists every event, GET /api/events/{name} returns
  one event's details, order books, trades and participants.
- POST /api/events/{name}/open and /resolve (form field "option")
  open and settle an event. The engine's openEvent does not know who
  is asking, so the servlet checks the caller is the event's Market
  Maker and answers 403 otherwise, for both actions.
- POST /api/orders places a buy or sell order as the logged-in user,
  never as a name the client sends.

Events are addressed by their URL-encoded name, which is their
identity in v3. Request input is checked before it reaches the engine.
The engine treats any side other than SELL as a buy, takes any
quantity, and Event.getOption throws on an unknown key, so all of
these would have turned client mistakes into wrong trades or 500
errors. Above all, settling on an unknown winning option would close
the event and pay nobody. The servlets reject these as 400s.

Login is checked inside each servlet through Sessions.requireUser
rather than in a second filter: the order of annotation-registered
filters is unspecified, and an auth filter running before CorsFilter
would send 401s without CORS headers, which browsers report as
network errors.


</content>
</entry>
<entry>
<title>server: accept event file uploads held entirely in memory</title>
<updated>2026-10-10T11:13:20Z</updated>
<author>
<name>Kostya</name>
<email>mail@sartin.in</email>
</author>
<published>2026-10-10T11:12:55Z</published>
<link rel='alternate' type='text/html' href='https://git.sartin.in/guess-market/commit/?id=6f0ab9c9a20b96890ed60bc168b28d29f5db39e5'/>
<id>urn:sha1:6f0ab9c9a20b96890ed60bc168b28d29f5db39e5</id>
<content type='text'>

Add POST /api/upload: a logged-in user sends a Schema v3 file as a
multipart part, its events join the market for everyone, and the
uploader becomes their Market Maker. It uses the Servlet API's own
multipart support, as the spec asks, and hands the part's stream to
the v3 loader, so validation errors (bad XML, broken Exercise 1 rules,
a name already in the market) come back as 400 with their reason.
As in Exercise 1, a file whose name does not end in .xml is refused
before it is read, since a request need not come from our client.

The spec warns that the grading server cannot write files, and Tomcat
writes any multipart part larger than @MultipartConfig's
fileSizeThreshold to a temp file. That threshold defaults to 0, so a
plain upload servlet always touches the disk. Cap uploads at 1 MB and
put the threshold above the whole request limit, so every upload stays
in memory. Bigger files get 413, requests without a session 401, and
non-multipart bodies 400.

Tomcat deletes spooled parts once the request ends, so comparing the
files before and after an upload cannot catch spooling. The test
instead watches the webapp's temp directory for any file being
created while a near-limit upload runs. It fails if the threshold is
set back to 0.


</content>
</entry>
<entry>
<title>server: add username login and logout endpoints</title>
<updated>2026-10-09T15:45:01Z</updated>
<author>
<name>Kostya</name>
<email>mail@sartin.in</email>
</author>
<published>2026-10-09T15:44:33Z</published>
<link rel='alternate' type='text/html' href='https://git.sartin.in/guess-market/commit/?id=33d313e17b841db3736c465447913df0cc73d5df'/>
<id>urn:sha1:33d313e17b841db3736c465447913df0cc73d5df</id>
<content type='text'>

The spec asks for a login by unique name only, with no passwords or
sign-up, and an error that lets the user try again if the name exists.
Add POST /api/login (form field "username") and POST /api/logout.

Sessions maps each name, ignoring case, to the one HTTP session
holding it. A second session asking for a held name gets 409
NAME_TAKEN, and a blank name gets 400. When a session ends, by logout
or by Tomcat timing it out after a client quits without logging out,
an HttpSessionListener frees the name. The account itself (ledger,
Market Maker roles) stays, and logging in with that name again resumes
it rather than locking the user out for the life of the server. New
accounts start with a zero balance and are funded through deposits.

Json gives every endpoint the same response shape, with errors as
{"error": CODE, "message": text} built from a failed Result. Tomcat
setup for the tests moves into a shared TestServer helper, whose
clients each keep their own cookie jar and so act as separate users.


</content>
</entry>
<entry>
<title>catalog: make Loader a stream-based strategy</title>
<updated>2026-10-09T12:32:33Z</updated>
<author>
<name>Kostya</name>
<email>mail@sartin.in</email>
</author>
<published>2026-10-09T12:32:09Z</published>
<link rel='alternate' type='text/html' href='https://git.sartin.in/guess-market/commit/?id=9c1a10712a724b448de9e066ff76f97059c86b40'/>
<id>urn:sha1:9c1a10712a724b448de9e066ff76f97059c86b40</id>
<content type='text'>

Loader was tied to the disk: load(Path) did its own file checks and
opening, so the v3 loader, which reads HTTP uploads, could not
implement it, and the server had to hand its catalog a stub loader
that refused everything.

Reduce Loader to a single strategy method, load(InputStream, source,
owner). The v1, v2 and v3 loaders are now interchangeable
implementations, picked by wiring: v1 for the console, v2 for the
desktop, v3 for the server. The owner is the uploading user; v1 and v2
ignore it because their files name their own Market Makers.

Opening a file by path is now LocalCatalogContext.loadEvents' job,
with the same INVALID_PATH, FILE_NOT_FOUND and NOT_XML errors as
before. The server catalog uses the v3 loader directly. A path load
there passes no uploader, which v3 refuses, so nothing reaches the
server's market except an upload.

Drop Loader.seed, which every loader implemented by throwing and
nothing called.


</content>
</entry>
<entry>
<title>server: add Tomcat webapp module with shared engine and CORS</title>
<updated>2026-10-09T09:55:40Z</updated>
<author>
<name>Kostya</name>
<email>mail@sartin.in</email>
</author>
<published>2026-10-09T09:55:16Z</published>
<link rel='alternate' type='text/html' href='https://git.sartin.in/guess-market/commit/?id=9a62e94261e6531c6b60d296322e0cef2a5b21e1'/>
<id>urn:sha1:9a62e94261e6531c6b60d296322e0cef2a5b21e1</id>
<content type='text'>

Exercise 3 moves the market behind a Tomcat server that the desktop
and browser clients share. Add a server module, registered in the
build scripts, holding the pieces every later endpoint builds on:

- ServerEngine wires one in-memory market (repositories, ledger,
  trading pipeline, v3 upload loader) and lives in the servlet context
  for the webapp's lifetime. Its catalog refuses path-based loading,
  so events can only arrive through uploaded streams and the server
  never reads or writes files.
- EngineBootstrap builds that engine when the webapp deploys.
- CorsFilter answers preflights and echoes the caller's Origin with
  Allow-Credentials, which the browser client needs to send its
  session cookie from another origin. With usernames as the only
  login, echoing any origin exposes nothing a page couldn't get by
  logging in itself.
- /api/health reports that the engine is up.

Vendor tomcat-juli 10.1.26 next to the other Tomcat jars. It ships in
Tomcat's bin/ rather than lib/, and embedded Tomcat cannot start
without it. A test deploys the module into embedded Tomcat, so
annotation scanning, the health check and CORS are checked against a
real container.


</content>
</entry>
</feed>
