diff --git a/cyberwow/lib/config.dart b/cyberwow/lib/config.dart index 480758e..1910855 100644 --- a/cyberwow/lib/config.dart +++ b/cyberwow/lib/config.dart @@ -38,3 +38,5 @@ const host = isEmu ? emuHost : '127.0.0.1'; const stdoutLineBufferSize = 200; const bannerShownKey = 'banner-shown'; +const int maxPoolTxSize = 5000; + diff --git a/cyberwow/lib/logic/sensor/rpc/rpc2.dart b/cyberwow/lib/logic/sensor/rpc/rpc2.dart index 59cfd23..5f21320 100644 --- a/cyberwow/lib/logic/sensor/rpc/rpc2.dart +++ b/cyberwow/lib/logic/sensor/rpc/rpc2.dart @@ -27,11 +27,14 @@ import 'package:http/http.dart' as http; import 'package:flutter/foundation.dart'; import '../../../helper.dart'; +import '../../../config.dart' as config; import '../../../logging.dart'; import '../../interface/rpc/rpc2.dart' as rpc2; Future getTransactionPool() async => rpc2.rpc2('get_transaction_pool'); +Map txInOutCache = {}; + Future>> getTransactionPoolSimple() async { final response = await getTransactionPool(); @@ -57,12 +60,31 @@ Future>> getTransactionPoolSimple() async { final _decodedPool = await Stream.fromIterable(_sortedPool).asyncMap ( (x) async { - final String _tx_json = x['tx_json']; - final _tx_json_decoded = await compute(jsonDecode, _tx_json); + if (txInOutCache.length > config.maxPoolTxSize) { + txInOutCache = {}; + } + + final _txid = x['id_hash']; + + if (txInOutCache[_txid] == null) { + final String _tx_json = x['tx_json']; + final _tx_json_decoded = await compute(jsonDecode, _tx_json); + + final _inOut = + { + 'vin': _tx_json_decoded['vin'].length, + 'vout': _tx_json_decoded['vout'].length, + }; + + final _inOutString = _inOut['vin'].toString() + '/' + _inOut['vout'].toString(); + + txInOutCache[_txid] = _inOutString; + log.fine('cached tx_json in pool for: ${_txid}'); + } return { ...x, - ...{'tx_decoded': _tx_json_decoded}, + ...{'i/o': txInOutCache[_txid]}, }; } ); diff --git a/cyberwow/lib/logic/view/rpc/rpc2.dart b/cyberwow/lib/logic/view/rpc/rpc2.dart index 4d8d7af..34c2a06 100644 --- a/cyberwow/lib/logic/view/rpc/rpc2.dart +++ b/cyberwow/lib/logic/view/rpc/rpc2.dart @@ -53,45 +53,33 @@ Map txView(Map x) { final _formattedTx = _filteredTx.map ( (k, v) { - if (k == 'id_hash') { - return MapEntry('id', trimHash(v)); + switch (k) { + case 'id_hash': + return MapEntry('id', trimHash(v)); + case 'blob_size': + return MapEntry('size', (v / 1024).toStringAsFixed(2) + ' kB'); + case 'fee': { + final formatter = NumberFormat.currency + ( + symbol: '', + decimalDigits: 2, + ); + return MapEntry(k, formatter.format(v / pow(10, 11)) + ' ⍵'); + } + case 'receive_time': { + final _receive_time = DateTime.fromMillisecondsSinceEpoch(v * 1000); + final _diff = DateTime.now().difference(_receive_time); + + format(Duration d) => d.toString().split('.').first.padLeft(8, "0"); + return MapEntry('age', format(_diff)); + } + default: + return MapEntry(k, v); } + } - else if (k == 'blob_size') { - return MapEntry('size', (v / 1024).toStringAsFixed(2) + ' kB'); - } - - else if (k == 'fee') { - final formatter = NumberFormat.currency - ( - symbol: '', - decimalDigits: 2, - ); - return MapEntry(k, formatter.format(v / pow(10, 11)) + ' ⍵'); - } - - else if (k == 'receive_time') { - final _receive_time = DateTime.fromMillisecondsSinceEpoch(v * 1000); - final _diff = DateTime.now().difference(_receive_time); - - format(Duration d) => d.toString().split('.').first.padLeft(8, "0"); - return MapEntry('age', format(_diff)); - } - else if (k == 'tx_decoded') { - final _out = - { - 'vin': v['vin'].length, - 'vout': v['vout'].length, - }; - final _outString = _out['vin'].toString() + '/' + _out['vout'].toString(); - return MapEntry('i/o', _outString); - } - else { - return MapEntry(k, v); - } - } ); final List keys =