Skip to content

Commit

Permalink
Implement pool refresh (#24)
Browse files Browse the repository at this point in the history
Signed-off-by: conanoc <[email protected]>
  • Loading branch information
conanoc authored Mar 5, 2024
1 parent f335e39 commit 8279e55
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 4 deletions.
50 changes: 50 additions & 0 deletions indy-vdr/src/uffi/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,58 @@ fn handle_request_result(
}
}

async fn handle_pool_refresh(
old_txns: Vec<String>,
new_txns: Vec<String>,
) -> Result<Option<PoolRunner>, ErrorCode> {
let mut txns = PoolTransactions::from_json_transactions(old_txns)?;
txns.extend_from_json(&new_txns)?;
let builder = {
let gcfg = read_lock!(POOL_CONFIG)?;
PoolBuilder::from(gcfg.clone())
};
let runner = builder.transactions(txns)?.into_runner()?;
Ok(Some(runner))
}

#[uniffi::export(async_runtime = "tokio")]
impl Pool {
pub async fn refresh(&self) -> Result<(), ErrorCode> {
let rt = tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.unwrap();
let (tx, rx) = oneshot::channel();
read_pool!(self.pool)?.refresh(Box::new(move |result| {
match result {
Ok((old_txns, new_txns, _timing)) => {
if let Some(new_txns) = new_txns {
let result = rt.block_on(handle_pool_refresh(old_txns, new_txns));
let _ = tx.send(result);
} else {
let _ = tx.send(Ok(None));
}
}
Err(err) => {
let code = ErrorCode::from(err);
let _ = tx.send(Err(code));
}
};
}))?;
let result = rx.await.map_err(|err| ErrorCode::Unexpected {
message: format!("Channel error: {}", err),
})?;
match result {
Ok(runner) => {
if let Some(runner) = runner {
*self.pool.write().await = Some(runner);
}
Ok(())
}
Err(err) => Err(err),
}
}

pub async fn get_status(&self) -> Result<String, ErrorCode> {
let (tx, rx) = oneshot::channel();
read_pool!(self.pool)?.get_status(Box::new(move |result| {
Expand Down
19 changes: 19 additions & 0 deletions swift/Sources/IndyVdr/indy_vdr_uniffi.swift
Original file line number Diff line number Diff line change
Expand Up @@ -785,6 +785,7 @@ public protocol PoolProtocol {
func close() async throws
func getStatus() async throws -> String
func getTransactions() async throws -> String
func refresh() async throws
func submitAction(request: Request, nodeAliases: [String]?, timeout: Int64?) async throws -> String
func submitRequest(request: Request) async throws -> String
}
Expand Down Expand Up @@ -848,6 +849,21 @@ public class Pool: PoolProtocol {
)
}

public func refresh() async throws {
return try await uniffiRustCallAsync(
rustFutureFunc: {
uniffi_indy_vdr_uniffi_fn_method_pool_refresh(
self.pointer
)
},
pollFunc: ffi_indy_vdr_uniffi_rust_future_poll_void,
completeFunc: ffi_indy_vdr_uniffi_rust_future_complete_void,
freeFunc: ffi_indy_vdr_uniffi_rust_future_free_void,
liftFunc: { $0 },
errorHandler: FfiConverterTypeErrorCode.lift
)
}

public func submitAction(request: Request, nodeAliases: [String]?, timeout: Int64?) async throws -> String {
return try await uniffiRustCallAsync(
rustFutureFunc: {
Expand Down Expand Up @@ -1585,6 +1601,9 @@ private var initializationResult: InitializationResult {
if uniffi_indy_vdr_uniffi_checksum_method_pool_get_transactions() != 23565 {
return InitializationResult.apiChecksumMismatch
}
if uniffi_indy_vdr_uniffi_checksum_method_pool_refresh() != 11901 {
return InitializationResult.apiChecksumMismatch
}
if uniffi_indy_vdr_uniffi_checksum_method_pool_submit_action() != 35559 {
return InitializationResult.apiChecksumMismatch
}
Expand Down
12 changes: 8 additions & 4 deletions swift/Tests/IndyVdrTests/BasicTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@ import XCTest
@testable import IndyVdr

final class BasicTests: XCTestCase {
let printLength = 200
func testFeatures() async throws {
guard let genesisUrl = Bundle.module.url(forResource: "genesis_sov_buildernet", withExtension: "txn") else {
XCTFail("Genesis file not found")
return
}

let pool = try openPool(transactionsPath: genesisUrl.path, transactions: nil, nodeWeights: nil)
print("Status: \(try await pool.getStatus())")
print("Status:", try await pool.getStatus())

try await pool.refresh()
print("Status after refresh:", try await pool.getStatus())

let ledger = Ledger()

Expand All @@ -28,10 +32,10 @@ final class BasicTests: XCTestCase {
print("Custom request signature input:", sigIn)

req = try ledger.buildGetTxnAuthorAgreementRequest(submitterDid: nil, data: nil)
print(try await pool.submitRequest(request: req))
print("buildGetTxnAuthorAgreementRequest:", try await pool.submitRequest(request: req).prefix(printLength))

req = try ledger.buildGetAcceptanceMechanismsRequest(submitterDid: nil, timestamp: nil, version: nil)
print(try await pool.submitRequest(request: req))
print("buildGetAcceptanceMechanismsRequest:", try await pool.submitRequest(request: req).prefix(printLength))

let acceptance = try ledger.prepareTxnAuthorAgreementAcceptance(
text: "acceptance text",
Expand All @@ -46,7 +50,7 @@ final class BasicTests: XCTestCase {
print("Request with TAA acceptance and endorser:", try req.body())

req = try ledger.buildGetTxnRequest(submitterDid: nil, ledgerType: .domain, seqNo: 1)
print(try await pool.submitRequest(request: req))
print("buildGetTxnRequest:", try await pool.submitRequest(request: req).prefix(printLength))

req = try ledger.buildGetSchemaRequest(submitterDid: nil, schemaId: "6qnvgJtqwK44D8LFYnV5Yf:2:relationship.dflow:1.0.0")
print("Get schema request:", try req.body())
Expand Down

0 comments on commit 8279e55

Please sign in to comment.