Skip to content

Commit 8279e55

Browse files
authored
Implement pool refresh (#24)
Signed-off-by: conanoc <[email protected]>
1 parent f335e39 commit 8279e55

File tree

3 files changed

+77
-4
lines changed

3 files changed

+77
-4
lines changed

indy-vdr/src/uffi/pool.rs

+50
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,58 @@ fn handle_request_result(
6161
}
6262
}
6363

64+
async fn handle_pool_refresh(
65+
old_txns: Vec<String>,
66+
new_txns: Vec<String>,
67+
) -> Result<Option<PoolRunner>, ErrorCode> {
68+
let mut txns = PoolTransactions::from_json_transactions(old_txns)?;
69+
txns.extend_from_json(&new_txns)?;
70+
let builder = {
71+
let gcfg = read_lock!(POOL_CONFIG)?;
72+
PoolBuilder::from(gcfg.clone())
73+
};
74+
let runner = builder.transactions(txns)?.into_runner()?;
75+
Ok(Some(runner))
76+
}
77+
6478
#[uniffi::export(async_runtime = "tokio")]
6579
impl Pool {
80+
pub async fn refresh(&self) -> Result<(), ErrorCode> {
81+
let rt = tokio::runtime::Builder::new_current_thread()
82+
.enable_all()
83+
.build()
84+
.unwrap();
85+
let (tx, rx) = oneshot::channel();
86+
read_pool!(self.pool)?.refresh(Box::new(move |result| {
87+
match result {
88+
Ok((old_txns, new_txns, _timing)) => {
89+
if let Some(new_txns) = new_txns {
90+
let result = rt.block_on(handle_pool_refresh(old_txns, new_txns));
91+
let _ = tx.send(result);
92+
} else {
93+
let _ = tx.send(Ok(None));
94+
}
95+
}
96+
Err(err) => {
97+
let code = ErrorCode::from(err);
98+
let _ = tx.send(Err(code));
99+
}
100+
};
101+
}))?;
102+
let result = rx.await.map_err(|err| ErrorCode::Unexpected {
103+
message: format!("Channel error: {}", err),
104+
})?;
105+
match result {
106+
Ok(runner) => {
107+
if let Some(runner) = runner {
108+
*self.pool.write().await = Some(runner);
109+
}
110+
Ok(())
111+
}
112+
Err(err) => Err(err),
113+
}
114+
}
115+
66116
pub async fn get_status(&self) -> Result<String, ErrorCode> {
67117
let (tx, rx) = oneshot::channel();
68118
read_pool!(self.pool)?.get_status(Box::new(move |result| {

swift/Sources/IndyVdr/indy_vdr_uniffi.swift

+19
Original file line numberDiff line numberDiff line change
@@ -785,6 +785,7 @@ public protocol PoolProtocol {
785785
func close() async throws
786786
func getStatus() async throws -> String
787787
func getTransactions() async throws -> String
788+
func refresh() async throws
788789
func submitAction(request: Request, nodeAliases: [String]?, timeout: Int64?) async throws -> String
789790
func submitRequest(request: Request) async throws -> String
790791
}
@@ -848,6 +849,21 @@ public class Pool: PoolProtocol {
848849
)
849850
}
850851

852+
public func refresh() async throws {
853+
return try await uniffiRustCallAsync(
854+
rustFutureFunc: {
855+
uniffi_indy_vdr_uniffi_fn_method_pool_refresh(
856+
self.pointer
857+
)
858+
},
859+
pollFunc: ffi_indy_vdr_uniffi_rust_future_poll_void,
860+
completeFunc: ffi_indy_vdr_uniffi_rust_future_complete_void,
861+
freeFunc: ffi_indy_vdr_uniffi_rust_future_free_void,
862+
liftFunc: { $0 },
863+
errorHandler: FfiConverterTypeErrorCode.lift
864+
)
865+
}
866+
851867
public func submitAction(request: Request, nodeAliases: [String]?, timeout: Int64?) async throws -> String {
852868
return try await uniffiRustCallAsync(
853869
rustFutureFunc: {
@@ -1585,6 +1601,9 @@ private var initializationResult: InitializationResult {
15851601
if uniffi_indy_vdr_uniffi_checksum_method_pool_get_transactions() != 23565 {
15861602
return InitializationResult.apiChecksumMismatch
15871603
}
1604+
if uniffi_indy_vdr_uniffi_checksum_method_pool_refresh() != 11901 {
1605+
return InitializationResult.apiChecksumMismatch
1606+
}
15881607
if uniffi_indy_vdr_uniffi_checksum_method_pool_submit_action() != 35559 {
15891608
return InitializationResult.apiChecksumMismatch
15901609
}

swift/Tests/IndyVdrTests/BasicTests.swift

+8-4
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,18 @@ import XCTest
22
@testable import IndyVdr
33

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

1112
let pool = try openPool(transactionsPath: genesisUrl.path, transactions: nil, nodeWeights: nil)
12-
print("Status: \(try await pool.getStatus())")
13+
print("Status:", try await pool.getStatus())
14+
15+
try await pool.refresh()
16+
print("Status after refresh:", try await pool.getStatus())
1317

1418
let ledger = Ledger()
1519

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

3034
req = try ledger.buildGetTxnAuthorAgreementRequest(submitterDid: nil, data: nil)
31-
print(try await pool.submitRequest(request: req))
35+
print("buildGetTxnAuthorAgreementRequest:", try await pool.submitRequest(request: req).prefix(printLength))
3236

3337
req = try ledger.buildGetAcceptanceMechanismsRequest(submitterDid: nil, timestamp: nil, version: nil)
34-
print(try await pool.submitRequest(request: req))
38+
print("buildGetAcceptanceMechanismsRequest:", try await pool.submitRequest(request: req).prefix(printLength))
3539

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

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

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

0 commit comments

Comments
 (0)