make rpc functional

master
fuwa 5 years ago
parent 8ab975361d
commit 1363bf59a4

@ -37,3 +37,5 @@ const emuHost = '192.168.10.100';
const host = isEmu ? emuHost : '127.0.0.1';
const int hashLength = 12;

@ -30,7 +30,7 @@ import '../../helper.dart';
int rpcID = 0;
Future<http.Response> rpc(String method) async {
Future<http.Response> rpcHTTP(String method) async {
final url = 'http://${host}:${config.port}/json_rpc';
rpcID += 1;
@ -58,8 +58,8 @@ Future<http.Response> rpc(String method) async {
return response;
}
Future<dynamic> rpcDynamic(String method, {String field}) async {
final response = await rpc(method);
Future<dynamic> rpc(String method, {String field}) async {
final response = await rpcHTTP(method);
if (response == null) return null;
@ -74,76 +74,52 @@ Future<dynamic> rpcDynamic(String method, {String field}) async {
}
Future<String> rpcString(String method, {String field}) async {
final _field = await rpcDynamic(method, field: field);
final _field = await rpc(method, field: field);
return pretty(_field);
}
Future<http.Response> syncInfo() async => rpc('sync_info');
Future<String> syncInfoString() async => rpcString('sync_info');
Future<http.Response> syncInfo() => rpc('sync_info');
Future<String> syncInfoString() => rpcString('sync_info');
Future<int> targetHeight() => rpcDynamic('sync_info', field: 'target_height');
Future<dynamic> targetHeight() => rpc('sync_info', field: 'target_height');
Future<dynamic> height() => rpc('sync_info', field: 'height');
Future<int> height() async {
final response = await syncInfo();
if (response == null) return -1;
Future<http.Response> getInfo() => rpc('get_info');
Future<String> getInfoString() => rpcString('get_info');
// print('Response status: ${response.statusCode}');
if (response.statusCode != 200) {
return -1;
} else {
final responseBody = json.decode(response.body)['result'];
return responseBody["height"];
}
}
Future<http.Response> getInfo() async => rpc('get_info');
Future<String> getInfoString() async => rpcString('get_info');
Future<bool> offline() async {
final response = await getInfo();
if (response == null) return true;
// print('Response status: ${response.statusCode}');
if (response.statusCode != 200) {
return true;
} else {
final responseBody = json.decode(response.body)['result'];
return responseBody["offline"];
}
}
Future<dynamic> offline() => rpc('get_info', field: 'offline');
Future<int> outgoingConnectionsCount() async {
final response = await getInfo();
Future<dynamic> outgoingConnectionsCount() => rpc('get_info', field: 'outgoing_connections_count');
Future<dynamic> incomingConnectionsCount() => rpc('get_info', field: 'incoming_connections_count');
if (response == null) return -1;
// Future<http.Response>> getConnections() async => rpcHTTP('get_connections', field: 'connections');
Future<List<dynamic>> getConnectionsSimple() async {
final List<dynamic> _connections = await rpc('get_connections', field: 'connections');
// print('Response status: ${response.statusCode}');
if (response.statusCode != 200) {
return -1;
} else {
final responseBody = json.decode(response.body)['result'];
return responseBody["outgoing_connections_count"];
}
}
Future<int> incomingConnectionsCount() async {
final response = await getInfo();
if (response == null) return -1;
// print('Response status: ${response.statusCode}');
if (response.statusCode != 200) {
return -1;
} else {
final responseBody = json.decode(response.body)['result'];
return responseBody["incoming_connections_count"];
}
return _connections.map
(
(x) {
const _remove =
[
// 'tx_blob',
// 'tx_json',
// 'last_failed_id_hash',
// 'max_used_block_id_hash',
];
return x.map
(
(k, v) {
if (k == 'connection_id') {
return MapEntry(k, v.substring(0, 12) + '...');
} else {
return MapEntry(k, v);
}
}
);
}
).toList();
}
// Future<http.Response>> getConnections() async => rpc('get_connections', field: 'connections');
Future<String> getConnectionsString() async => rpcString('get_connections', field: 'connections');

@ -38,7 +38,7 @@ Future<http.Response> rpc2(String method) async {
);
}
catch (e) {
// print(e);
log.warning(e);
}
return response;
@ -73,21 +73,42 @@ Future<List<dynamic>> getTransactionPoolSimple() async {
return [];
} else {
final responseBody = json.decode(response.body);
var result = responseBody['transactions'];
final result = responseBody['transactions'];
if (result == null) {
return [];
}
else {
result.forEach
return result.map
(
(tx) {
tx.remove('tx_blob');
tx.remove('tx_json');
tx.remove('last_failed_id_hash');
tx.remove('max_used_block_id_hash');
(x) {
const _remove =
[
'tx_blob',
'tx_json',
'last_failed_id_hash',
'max_used_block_id_hash',
];
return Map.fromIterable
(
x.keys.where
(
(k) => !_remove.contains(k)
),
value: (k) => x[k],
).map
(
(k, v) {
if (k == 'id_hash') {
return MapEntry(k, v.substring(0, 12) + '...');
} else {
return MapEntry(k, v);
}
}
)
;
}
);
return result;
).toList();
}
}
}

@ -200,7 +200,7 @@ class SyncedState extends HookedState {
bool connected = true;
String getInfo = 'getInfo';
String syncInfo = 'syncInfo';
String getConnections = 'getConnections';
List<dynamic> getConnections = [];
List<dynamic> getTransactionPool = [];
SyncedState(f, s, this.stdout, this.processOutput) : super (f, s);
@ -230,7 +230,7 @@ class SyncedState extends HookedState {
connected = await daemon.isConnected();
getInfo = await rpc.getInfoString();
// syncInfo = await rpc.syncInfoString();
getConnections = await rpc.getConnectionsString();
getConnections = await rpc.getConnectionsSimple();
// getTransactionPool = await rpc.getTransactionPoolString();
getTransactionPool = await rpc.getTransactionPoolSimple();

@ -156,7 +156,8 @@ Widget rpcView(String title, String body) {
}
Widget getInfo(SyncedState state) => rpcView('info', state.getInfo);
Widget getConnections(SyncedState state) => rpcView('connections', state.getConnections);
Widget getConnections(SyncedState state) =>
rpcView('connections', pretty(state.getConnections));
Widget syncInfo(SyncedState state) => rpcView('sync info', state.syncInfo);
Widget getTransactionPool(SyncedState state) =>
rpcView('tx pool', pretty(state.getTransactionPool));

Loading…
Cancel
Save