Skip to content

Commit

Permalink
Signature & Marshalling fix
Browse files Browse the repository at this point in the history
  • Loading branch information
jimmyjose-dev committed Jun 13, 2022
1 parent 63436eb commit 6493336
Show file tree
Hide file tree
Showing 31 changed files with 871 additions and 321 deletions.
6 changes: 3 additions & 3 deletions example/accumulate_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ void main() {
print("faucet $res");
String txId = res["result"]["txid"];
print("txId $txId");
await client.waitOnTx(txId);
await client.waitOnTx(DateTime.now().millisecondsSinceEpoch,txId);
print("waiting done");

res = await client.queryUrl(lid.url);
Expand Down Expand Up @@ -92,7 +92,7 @@ void main() {
res = await client.createIdentity(lid.url, createIdentity, lid);
txId = res["result"]["txid"];
print("createIdentity txId $txId");
await client.waitOnTx(txId);
await client.waitOnTx(DateTime.now().millisecondsSinceEpoch,txId);
print("waiting done");

//res = await client.queryUrl(identityUrl);
Expand Down Expand Up @@ -125,7 +125,7 @@ void main() {
res = await client.sendTokens(lid.acmeTokenAccount, sendTokensArg, lid);

txId = res["result"]["txid"];
await client.waitOnTx(txId);
await client.waitOnTx(DateTime.now().millisecondsSinceEpoch,txId);

res = await client.queryTx(txId);
print(res);
Expand Down
6 changes: 3 additions & 3 deletions example/example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ void testFeatures() async {
print("faucet $res");
String txId = res["result"]["txid"];
print("txId $txId");
await client.waitOnTx(txId);
await client.waitOnTx(DateTime.now().millisecondsSinceEpoch,txId);
print("waiting done");

res = await client.queryUrl(lid.url);
Expand Down Expand Up @@ -89,7 +89,7 @@ void testFeatures() async {
res = await client.createIdentity(lid.url, createIdentity, lid);
txId = res["result"]["txid"];
print("createIdentity txId $txId");
await client.waitOnTx(txId);
await client.waitOnTx(DateTime.now().millisecondsSinceEpoch,txId);
print("waiting done");

//res = await client.queryUrl(identityUrl);
Expand Down Expand Up @@ -122,7 +122,7 @@ void testFeatures() async {
res = await client.sendTokens(lid.acmeTokenAccount, sendTokensArg, lid);

txId = res["result"]["txid"];
await client.waitOnTx(txId);
await client.waitOnTx(DateTime.now().millisecondsSinceEpoch,txId);

res = await client.queryTx(txId);
print(res);
Expand Down
16 changes: 7 additions & 9 deletions lib/src/api_types.dart
Original file line number Diff line number Diff line change
Expand Up @@ -97,32 +97,30 @@ class MinorBlocksQueryOptions {
class WaitTxOptions {
/// Timeout after which status polling is aborted. Duration in ms.
/// Default: 30000ms (30s)
int? timeout;
int timeout = 30000;

/// Interval between each tx status poll. Duration in ms.
/// Default: 500ms.
int? pollInterval;
int pollInterval = 500;

/// If set to true, only the user tx status is checked.
/// If set to false, will also wait on the associated synthetic txs to be delivered.
/// Default: false
bool? ignoreSyntheticTxs;
bool ignoreSyntheticTxs = false;


Map<String, dynamic> get toMap {
Map <String, dynamic> value = {};

if(timeout != null){

value.addAll({"timeout":timeout!});
}

if(pollInterval != null){

value.addAll({"pollInterval":pollInterval!});
}

if(ignoreSyntheticTxs != null){

value.addAll({"ignoreSyntheticTxs":ignoreSyntheticTxs!});
}


return value;

Expand Down
158 changes: 134 additions & 24 deletions lib/src/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'dart:convert';
import 'dart:developer';
import 'dart:io';
import 'dart:typed_data';
import 'model/query_transaction_response_model.dart' as query_trx_res_model;
import 'package:hex/hex.dart';

import "acc_url.dart" show AccURL;
Expand Down Expand Up @@ -63,8 +64,9 @@ class Client {
}

Future<Map<String, dynamic>> execute(Transaction tx) {
return call("create-adi", tx.toTxRequest().toMap);
//return call("execute", tx.toTxRequest().toMap);
//return call("send-tokens", tx.toTxRequest().toMap);
//return call("create-adi", tx.toTxRequest().toMap);
return call("execute", tx.toTxRequest().toMap);
}

Future<Map<String, dynamic>> queryAcmeOracle() {
Expand Down Expand Up @@ -171,18 +173,110 @@ class Client {
return call("query-directory", params);
}

/**
* Wait for a transaction (and its associated synthetic tx ids) to be delivered.
* Throw an error if the transaction has failed or the timeout is exhausted.
* @param txId
* @param options
* @returns void
*/
Future<void> waitOnTx(String txId, [WaitTxOptions? options]) async {

waitOnTx(int startTime,String txId, [WaitTxOptions? options]) async {
Completer completer = Completer();
// Options
int timeout = options?.timeout ?? 30000;
final pollInterval = options?.pollInterval ?? 1000;
final ignoreSyntheticTxs = options?.ignoreSyntheticTxs ?? false;

try {
print(txId);
final resp = await queryTx(txId);
query_trx_res_model
.QueryTransactionResponseModel queryTransactionResponseModel = query_trx_res_model
.QueryTransactionResponseModel.fromJson(resp);

if (queryTransactionResponseModel.result != null) {
query_trx_res_model
.QueryTransactionResponseModelResult result = queryTransactionResponseModel
.result!;
if (result.status != null) {
if (result.status!.delivered!) {
log("${result.syntheticTxids}");
if(ignoreSyntheticTxs){
completer.complete(true);
}else{

if(result.syntheticTxids!.isNotEmpty){
int nowTime = DateTime
.now()
.millisecondsSinceEpoch;
if (nowTime - startTime < timeout) {
sleep(Duration(milliseconds: pollInterval));
completer.complete(await waitOnTx(startTime, result.syntheticTxids!.first));
} else {
completer.complete(false);
}
}else{
completer.complete(true);
}
}

} else {
int nowTime = DateTime
.now()
.millisecondsSinceEpoch;
if (nowTime - startTime < timeout) {
sleep(Duration(milliseconds: pollInterval));
completer.complete(await waitOnTx(startTime, txId));
} else {
completer.complete(false);
}
}
} else {
int nowTime = DateTime
.now()
.millisecondsSinceEpoch;
if (nowTime - startTime < timeout) {
sleep(Duration(milliseconds: pollInterval));
completer.complete(await waitOnTx(startTime, txId));
} else {
completer.complete(false);
}
}
} else {
int nowTime = DateTime
.now()
.millisecondsSinceEpoch;
if (nowTime - startTime < timeout) {
sleep(Duration(milliseconds: pollInterval));
completer.complete(await waitOnTx(startTime, txId));
} else {
completer.complete(false);
}
}
}catch (e) {
// Do not retry on definitive transaction errors
if (e is TxError) {
//rethrow;
completer.completeError(false);
}

int nowTime = DateTime
.now()
.millisecondsSinceEpoch;
if (nowTime - startTime < timeout) {
sleep(Duration(milliseconds: pollInterval));
completer.complete(await waitOnTx(startTime, txId));
} else {
completer.complete(false);
}

// lastError = e;
print("going to sleep");

}

return completer.future;

}
waitOnTx1(String txId, [WaitTxOptions? options]) async {
Completer completer = Completer();
// Options
final to = options?.timeout ?? 30000;
final pollInterval = options?.pollInterval ?? 500;
final pollInterval = options?.pollInterval ?? 1000;
final ignoreSyntheticTxs = options?.ignoreSyntheticTxs ?? false;


Expand All @@ -192,9 +286,20 @@ class Client {
do {
try {
final resp = await queryTx(txId);
query_trx_res_model.QueryTransactionResponseModel queryTransactionResponseModel = query_trx_res_model.QueryTransactionResponseModel.fromJson(resp);
if(queryTransactionResponseModel.result != null){
print("has error");
}else{
print("has NO error");
print(queryTransactionResponseModel.result!.toJson());
}
Map<String,dynamic> result = resp["result"];

log("wait on tx resp ${jsonEncode(resp["result"]["syntheticTxids"])}");




//log("wait on tx resp ${jsonEncode(resp["result"]["syntheticTxids"])}");
List<String> syntheticTxids = [];//resp["result"]["syntheticTxids"];
Map<String, dynamic> status = {};
if(result.containsKey("syntheticTxids")){
Expand All @@ -208,26 +313,28 @@ class Client {
}




if (!status.containsKey("delivered")) {
throw Exception("Transaction not delivered");
completer.completeError(false);
//throw Exception("Transaction not delivered");
}else{
bool delivered = status["delivered"] as bool;
print(delivered);
log("wait on tx $delivered");
if(delivered){
return completer.complete();
completer.complete(true);
}
}

if (status.containsKey("code")) {
throw TxError(txId, status);
//throw TxError(txId, status);
completer.completeError(false);
}

if (ignoreSyntheticTxs) {
return;
completer.complete(true);
}

log("going to add stxIds");

// Also verify the associated synthetic txs
final timeoutLeft = to - DateTime.now().millisecondsSinceEpoch + start;
final List<String> stxIds = [];
Expand All @@ -237,11 +344,13 @@ class Client {

WaitTxOptions waitTxOptions = WaitTxOptions();
waitTxOptions.timeout = timeoutLeft;
waitTxOptions.pollInterval = options?.pollInterval;
waitTxOptions.ignoreSyntheticTxs = options?.ignoreSyntheticTxs;
waitTxOptions.pollInterval = options!.pollInterval;
waitTxOptions.ignoreSyntheticTxs = options!.ignoreSyntheticTxs;

for(String stxId in stxIds) {
await waitOnTx(stxId, waitTxOptions);
log("execute loop $stxId");
waitOnTx1(stxId, waitTxOptions);
log("done loop wait $stxId");
}

/*
Expand All @@ -252,7 +361,8 @@ class Client {
} catch (e) {
// Do not retry on definitive transaction errors
if (e is TxError) {
rethrow;
//rethrow;
completer.completeError(false);
}

lastError = e;
Expand All @@ -261,7 +371,7 @@ class Client {
// Poll while timeout is not reached
} while (DateTime.now().millisecondsSinceEpoch - start < to);

return completer.complete();
completer.completeError(false);
throw Exception(
'Transaction $txId was not confirmed within ${to / 1000}s. Cause: $lastError');
}
Expand Down
Loading

0 comments on commit 6493336

Please sign in to comment.