Skip to content

Increase Thread Safety - #18

Merged
aurelienshz merged 8 commits into
mainfrom
feature/a2u-concurrency
Feb 2, 2024
Merged

aurelienshz merged 8 commits into
mainfrom
feature/a2u-concurrency

Conversation

@hklee93

@hklee93 hklee93 commented Nov 17, 2023

Copy link
Copy Markdown
Contributor

Base automatically changed from fix/raise-submit-error to main February 2, 2024 18:48
@aurelienshz
aurelienshz force-pushed the feature/a2u-concurrency branch from 51334a8 to 087aafd Compare February 2, 2024 18:49
Comment thread pinetwork.gemspec
Comment on lines +8 to +13
s.files = [
"lib/pinetwork.rb",
"lib/errors.rb",
"test/a2u_concurrency_test.rb",
"Rakefile"
]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to bundle the test and the Rakefile when bundling the gem?

@aurelienshz
aurelienshz merged commit 61a2d44 into main Feb 2, 2024
@hklee93
hklee93 deleted the feature/a2u-concurrency branch February 5, 2024 09:41
jin-Pi pushed a commit to jin-Pi/pi-ruby that referenced this pull request Nov 17, 2026
The deployed Pi Testnet v1 contract (CCUF75B6...) has no shutdown_service and no
ServiceShutdown error; they came with pi-smartcontracts#2 commit c1c2fc2, which is not
deployed. Error names now match v1 (1-17), and the README example uses ServiceNotFound.
Matches the subscriptions docs (pi-platform-docs!37).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
aurelienshz pushed a commit that referenced this pull request Nov 17, 2026
…(pinetwork 0.2.0.pre.1) (#42)

* Add server-side PiNetwork::SmartContract::Subscription.process (0.2.0.pre.1)

PiNetwork::SmartContract::Subscription.process(merchant_secret, service_id, offset, limit,
**options) builds the Pi Subscription contract's process() call
(PiCoreTeam/pi-smartcontracts#2), signs it with the merchant's secret seed on the server
and sends it straight to the Pi Soroban RPC server, so renewals can be collected from a
scheduled job without the Pi Browser passphrase screen. Same path and argument order as
the client SDK's Pi.SmartContract.Subscription.process and pi-backend's
SmartContract.Subscription.process.

- mode :simulate returns { charged:, failed:, skipped:, total: } without signing or
  sending; :submit (default) signs, sends, waits and returns { tx_hash:, ledger:, result: }
- Pi Testnet defaults (RPC URL, passphrase, contract), each overridable
- Errors::SubscriptionError codes, contract errors decoded by name; an unknown outcome
  keeps the tx_hash so callers don't resend blindly
- The stellar-sdk gem predates Soroban, so the call is encoded by hand (lib/smart_contract/
  xdr.rb) and RPC results are read as JSON; no new dependencies

Non-breaking, released as a prerelease (0.2.0.pre.1) so 0.1.x users are untouched:
Bundler/RubyGems install it only when asked for by exact version or --pre.
- Ruby 2.5+ again, like 0.1.5 (main's unreleased time-bounds change used 3.1 syntax)
- README: Versions, SDK structure, subscription reference; fix constructor keyword in
  the example; CHANGELOG

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Validate options and RPC data types; document the sequence race with A2U

- Validate rpc_url and network_passphrase like contract_address (:invalid_rpc_url,
  :invalid_network_passphrase).
- RPC data that isn't the expected type is :unexpected_response instead of a raw
  ArgumentError/TypeError: a simulation result that isn't a ProcessResult map, counts that
  aren't Integers, an unreadable account sequence or simulation fee data.
- Document that process and A2U payments from the same wallet can race on the sequence
  number; on Pi Testnet the losing process ends in :transaction_not_confirmed (NOT_FOUND).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* README: correct the process fee guidance

A process call on the default Pi Testnet contract costs about 0.01 to 0.02 Pi, not
"several Pi" (that figure came from a different contract).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Test: avoid an endless range so the tests parse on Ruby 2.5

`bytes[-64..]` is Ruby 2.6+ syntax; `bytes[-64..-1]` is the same slice. With this, the
full suite (18 runs) passes on Ruby 2.5.9.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Address review: keep the txid on post-send errors, match Node on a missing return value

- A successful transaction whose result can't be read no longer raises a raw NoMethodError
  (resultMetaJson missing) or :unexpected_response without the txid. As in the Node.js
  package, a success with no return value is :transaction_not_confirmed, and an unreadable
  one is :unexpected_response; both keep the txid. A FAILED result code that can't be read
  leaves detail nil instead of raising.
- rpc_url is parsed with URI: any http(s) URL with a host is accepted, and it is sent
  with the scheme lower-cased.
- account_sequence: an entry that isn't a Hash is :unexpected_response.
- Race note: a losing process is :transaction_rejected (txBadSeq) when the other
  transaction already landed before it was sent, or :transaction_not_confirmed
  (NOT_FOUND) when the RPC server dropped it after accepting it.
- README: an :unexpected_response or :rpc_error with a txid means the transaction was sent.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Harden RPC response handling; nil options use defaults; bound timeout_seconds

Found in a full review by feeding process() malformed RPC responses:

- A JSON-RPC response without a result object (or a body that isn't one) raised a raw
  NoMethodError/TypeError, including after the transaction was signed and sent, which
  lost the txid. It is now :rpc_error: retried while polling, :transaction_not_confirmed
  with the txid after sending.
- A refused send whose errorResultXdr can't be read raised a raw ArgumentError; it is now
  :transaction_rejected with the status as detail.
- Only ERROR and TRY_AGAIN_LATER from sendTransaction count as refused. Any other status
  is polled for, as in the Node.js package, instead of being reported as rejected while
  the transaction may still land.
- :transaction_failed detail uses the same names as everywhere else ("txFailed", not
  "tx_failed").
- A simulation whose results isn't a list is :simulation_failed instead of a TypeError.
- timeout_seconds must be in 1..4294967295, so the u64 time bounds can't wrap around.
- An option passed as nil uses its default, as null does in the Node.js package.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Validate every number read from the RPC server; scope the test helpers

From a second, independent review:

- A FAILED result code made only of underscores raised a raw NoMethodError after the
  transaction was sent (txid lost). It now leaves detail nil.
- Numbers from the RPC server are range-checked before they are packed into XDR, where
  Ruby's pack would silently wrap them: minResourceFee must be a non-negative integer (a
  negative one signed a ~429 Pi fee), the account sequence an int64 below the maximum,
  and transactionData longer than its 8-byte resourceFee. All are :unexpected_response
  before anything is signed.
- After sending, the counts must be u32s and the ledger an integer, else
  :unexpected_response with the txid.
- A Faraday connection with JSON response middleware (Hash body) works.
- Test helpers live in a SubscriptionProcessTests module instead of top-level constants.
- README example: the rescue shows what to do with :transaction_not_confirmed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Request timeouts, invalid-encoding input, clearer code for an unreadable success

From a third independent review:

- The default Faraday connection gets a 30 s request and open timeout, so one RPC request
  that never answers can't hold process far past its deadline.
- Integer arguments and mode given as Strings with invalid bytes are invalid arguments
  instead of raw ArgumentError/EncodingError.
- A successful transaction whose return value is missing or unreadable is now
  :unexpected_response with the txid. It was :transaction_not_confirmed ("outcome
  unknown"), but the outcome is known: it succeeded. Same change in pi-nodejs#36.
- process_result raises from one place.
- README: only :unexpected_response can carry a txid (an :rpc_error after sending becomes
  :transaction_not_confirmed).
- Tests for archived state, fee_too_high, TRY_AGAIN_LATER, DUPLICATE, an HTTP error and the
  default timeout (33 runs, also on Ruby 2.5.9).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Drop ServiceShutdown (#18) from the contract error names

The deployed Pi Testnet v1 contract (CCUF75B6...) has no shutdown_service and no
ServiceShutdown error; they came with pi-smartcontracts#2 commit c1c2fc2, which is not
deployed. Error names now match v1 (1-17), and the README example uses ServiceNotFound.
Matches the subscriptions docs (pi-platform-docs!37).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Base64 fallback for getTransaction; note how to re-verify the error names

Review on #42:
- getTransaction is read as JSON. If Pi's RPC server can't JSON-encode the answer (as it
  can't for some sendTransaction answers), the status is read from the base64 answer:
  FAILED is :transaction_failed with the result code, SUCCESS is :unexpected_response with
  the txid, instead of being retried as a failed request until the deadline.
  Verified on Pi Testnet that a failed Soroban transaction (8697d170..., instruction budget
  too small) reads fine as JSON today: status FAILED, resultJson resource_limit_exceeded,
  and process reports :transaction_failed / txFailed with the txid.
- CONTRACT_ERRORS matches the deployed wasm's enum ContractError (1-17, compared with
  stellar contract info interface); the comment says how to re-check after an upgrade.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Jin Seok Ahn <jinseokahn@Jinseoks-MacBook-Pro.local>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants