Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import org.dashfoundation.dashsdk.ffi.DecisionProposalsNative
import org.dashfoundation.dashsdk.persistence.toBase58String
import org.dashfoundation.dashsdk.wallet.TeardownGate
import org.dashfoundation.dashsdk.wallet.op
import org.dashfoundation.dashsdk.wallet.opReturningCompletedResult

/**
* Decision proposals (protocol version 15): yes/no questions any identity puts to the
Expand Down Expand Up @@ -157,6 +158,14 @@ class DecisionProposals internal constructor(private val sdk: Sdk) {
* Each call is one native call (`platform_wallet_decision_proposal_create` / `_edit`): the
* wallet builds the proposal (or reads the stored one and edits it), picks the signing key,
* broadcasts and waits for Platform to confirm. Nothing is decided or checked here.
*
* Cancellation: native code cannot see a coroutine's cancellation, so a write can complete
* after its caller was cancelled. The coroutine that made the call then still receives
* what Platform confirmed (see [opReturningCompletedResult]) instead of an exception, so it
* can record a paid create rather than pay for it again. That coroutine stays
* cancelled, so handle the result before its next suspension point. Code outside the
* coroutine never gets the result: awaiting its `Deferred` throws a
* `CancellationException`, and joining its `Job` only returns.
*/
class DecisionProposalTransactions internal constructor(private val gate: TeardownGate?) {

Expand All @@ -175,7 +184,7 @@ class DecisionProposalTransactions internal constructor(private val gate: Teardo
ownerId: ByteArray,
params: DecisionProposalParams,
signerHandle: Long,
): CreatedDecisionProposal = gate.op {
): CreatedDecisionProposal = gate.opReturningCompletedResult {
val proposalId = ByteArray(PROPOSAL_ID_SIZE)
val documentJson = mapNativeErrors {
DecisionProposalsNative.create(
Expand Down Expand Up @@ -214,7 +223,7 @@ class DecisionProposalTransactions internal constructor(private val gate: Teardo
proposalId: ByteArray,
edit: DecisionProposalEdit,
signerHandle: Long,
): String = gate.op {
): String = gate.opReturningCompletedResult {
val url = edit.url.toNative()
mapNativeErrors {
DecisionProposalsNative.edit(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,12 @@ class MultipleChoiceProposals internal constructor(private val sdk: Sdk) {
* decided or checked here.
*
* Cancellation: native code cannot see a coroutine's cancellation, so a write can complete
* after its caller was cancelled. Each call then still returns what Platform confirmed
* (see [opReturningCompletedResult]) instead of throwing, so a paid create or choice is
* never hidden from a caller that would otherwise pay for it again. The coroutine stays
* cancelled: handle the result before the next suspension point.
* after its caller was cancelled. The coroutine that made the call then still receives
* what Platform confirmed (see [opReturningCompletedResult]) instead of an exception, so it
* can record a paid create or choice rather than pay for it again. That coroutine stays
* cancelled, so handle the result before its next suspension point. Code outside the
* coroutine never gets the result: awaiting its `Deferred` throws a
* `CancellationException`, and joining its `Job` only returns.
*/
class MultipleChoiceProposalTransactions internal constructor(private val gate: TeardownGate?) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ extension ManagedPlatformWallet {
///
/// Lifetime contract: as `createDocument`, the `signer` is pinned with
/// `withExtendedLifetime` across the synchronous FFI call (Rust holds a
/// `passUnretained` ctx pointer to it).
/// `passUnretained` ctx pointer to it), and so is the wallet, whose
/// deinit destroys the handle the call uses.
///
/// - Parameters:
/// - ownerIdentityId: The proposer's 32-byte identity id.
Expand All @@ -51,18 +52,20 @@ extension ManagedPlatformWallet {
let handle = self.handle
let signerHandle = signer.handle
let ownerBytes = [UInt8](ownerIdentityId)
return try await Task.detached(priority: .userInitiated) {
return try await Task.detached(priority: .userInitiated) { [self] in
var proposalIdBytes = [UInt8](repeating: 0, count: 32)
// Receives an owned canonical-document JSON C string on success;
// freed with `platform_wallet_string_free` below.
var documentJsonPtr: UnsafeMutablePointer<CChar>? = nil
defer { if let p = documentJsonPtr { platform_wallet_string_free(p) } }

// Pin `signer` for the whole FFI call (see `createDocument` for
// why a bare `_ = signer` is unreliable under -O). Rust reads the
// C strings synchronously inside the call, so the
// `withDecisionProposalCString` scopes are long enough.
let result = try withExtendedLifetime(signer) {
// Pin the wallet and `signer` for the whole FFI call (see
// `createDocument` for why a bare `_ = signer` is unreliable
// under -O): a wallet deinit mid-call would destroy the handle
// the FFI is still using. Rust reads the C strings synchronously
// inside the call, so the `withDecisionProposalCString` scopes
// are long enough.
let result = try withExtendedLifetime((self, signer)) {
try ownerBytes.withUnsafeBufferPointer { ownerBp -> PlatformWalletFFIResult in
try withDecisionProposalCString(
params.title, field: "title", invalid: PlatformWalletError.invalidParameter
Expand Down Expand Up @@ -128,8 +131,9 @@ extension ManagedPlatformWallet {
/// refuses an edit more than a day after the proposal was created (or a
/// fifth of the time it runs, if that is shorter).
///
/// Lifetime contract: as `createDocument`, the `signer` is pinned with
/// `withExtendedLifetime` across the synchronous FFI call.
/// Lifetime contract: the wallet and the `signer` are pinned with
/// `withExtendedLifetime` across the synchronous FFI call, as the create
/// pins them.
///
/// - Parameters:
/// - ownerIdentityId: The proposer's 32-byte identity id.
Expand Down Expand Up @@ -173,12 +177,13 @@ extension ManagedPlatformWallet {
url = nil
removeURL = true
}
return try await Task.detached(priority: .userInitiated) {
return try await Task.detached(priority: .userInitiated) { [self] in
var documentJsonPtr: UnsafeMutablePointer<CChar>? = nil
defer { if let p = documentJsonPtr { platform_wallet_string_free(p) } }

// Pin `signer` for the whole FFI call (see `createDocument`).
let result = try withExtendedLifetime(signer) {
// Pin the wallet and `signer` for the whole FFI call, as the
// create does.
let result = try withExtendedLifetime((self, signer)) {
try ownerBytes.withUnsafeBufferPointer { ownerBp -> PlatformWalletFFIResult in
try proposalBytes.withUnsafeBufferPointer { proposalBp -> PlatformWalletFFIResult in
try withDecisionProposalCString(
Expand Down
Loading