From 213f78b36cd84d6b3635ec5b1ea26a1b3b47309d Mon Sep 17 00:00:00 2001 From: julian Date: Wed, 8 May 2024 16:26:04 -0600 Subject: [PATCH] quick vSize calc --- lib/utilities/extensions/extensions.dart | 4 ++ .../extensions/impl/cl_transaction.dart | 53 +++++++++++++++++++ .../electrumx_interface.dart | 2 +- .../paynym_interface.dart | 2 +- 4 files changed, 59 insertions(+), 2 deletions(-) create mode 100644 lib/utilities/extensions/impl/cl_transaction.dart diff --git a/lib/utilities/extensions/extensions.dart b/lib/utilities/extensions/extensions.dart index 4a9aac287..a798c002a 100644 --- a/lib/utilities/extensions/extensions.dart +++ b/lib/utilities/extensions/extensions.dart @@ -9,5 +9,9 @@ */ export 'impl/big_int.dart'; +export 'impl/box_shadow.dart'; +export 'impl/cl_transaction.dart'; +export 'impl/contract_abi.dart'; +export 'impl/gradient.dart'; export 'impl/string.dart'; export 'impl/uint8_list.dart'; diff --git a/lib/utilities/extensions/impl/cl_transaction.dart b/lib/utilities/extensions/impl/cl_transaction.dart new file mode 100644 index 000000000..1c117cf8c --- /dev/null +++ b/lib/utilities/extensions/impl/cl_transaction.dart @@ -0,0 +1,53 @@ +import 'dart:typed_data'; + +import 'package:coinlib_flutter/coinlib_flutter.dart'; + +extension CLTransactionExt on Transaction { + int weight() { + final base = _byteLength(false); + final total = _byteLength(true); + return base * 3 + total; + } + + int vSize() => (weight() / 4).ceil(); + + int _byteLength(final bool allowWitness) { + final hasWitness = allowWitness && isWitness; + return (hasWitness ? 10 : 8) + + _encodingLength(inputs.length) + + _encodingLength(outputs.length) + + inputs.fold(0, (sum, input) => sum + input.size) + + outputs.fold(0, (sum, output) => sum + output.size) + + (hasWitness + ? inputs.fold(0, (sum, input) { + if (input is! WitnessInput) { + return sum; + } else { + return sum + _vectorSize(input.witness); + } + }) + : 0); + } + + int _varSliceSize(Uint8List someScript) { + final length = someScript.length; + return _encodingLength(length) + length; + } + + int _vectorSize(List someVector) { + final length = someVector.length; + return _encodingLength(length) + + someVector.fold( + 0, + (sum, witness) => sum + _varSliceSize(witness), + ); + } + + int _encodingLength(int number) => number < 0xfd + ? 1 + : number <= 0xffff + ? 3 + : number <= 0xffffffff + ? 5 + : 9; +} diff --git a/lib/wallets/wallet/wallet_mixin_interfaces/electrumx_interface.dart b/lib/wallets/wallet/wallet_mixin_interfaces/electrumx_interface.dart index 2d84eb420..b4fdb7a9d 100644 --- a/lib/wallets/wallet/wallet_mixin_interfaces/electrumx_interface.dart +++ b/lib/wallets/wallet/wallet_mixin_interfaces/electrumx_interface.dart @@ -768,7 +768,7 @@ mixin ElectrumXInterface on Bip39HDWallet { return txData.copyWith( raw: clTx.toHex(), - vSize: clTx.size, + vSize: clTx.vSize(), tempTx: TransactionV2( walletId: walletId, blockHash: null, diff --git a/lib/wallets/wallet/wallet_mixin_interfaces/paynym_interface.dart b/lib/wallets/wallet/wallet_mixin_interfaces/paynym_interface.dart index c17965584..202b22943 100644 --- a/lib/wallets/wallet/wallet_mixin_interfaces/paynym_interface.dart +++ b/lib/wallets/wallet/wallet_mixin_interfaces/paynym_interface.dart @@ -866,7 +866,7 @@ mixin PaynymInterface ); } - return Tuple2(clTx.toHex(), clTx.size); + return Tuple2(clTx.toHex(), clTx.vSize()); } catch (e, s) { Logging.instance.log( "_createNotificationTx(): $e\n$s",