From 23813723d10cccc4f910a2aa7f18a9c9e173c140 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Olav?= Date: Fri, 10 Nov 2023 00:23:25 +0100 Subject: [PATCH] rpc: introduce SidechainSubRPC for quick changing of sidechains --- lib/config/dependencies.dart | 32 ++--------- .../tabs/sidechain_explorer_tab_page.dart | 8 +-- lib/rpc/rpc_ethereum.dart | 2 +- lib/rpc/rpc_sidechain.dart | 55 +++++++++++++++++-- lib/rpc/rpc_testchain.dart | 2 +- test/dashboard_test.dart | 2 +- test/mocks/rpc_mock_sidechain.dart | 36 +++++++++++- 7 files changed, 96 insertions(+), 41 deletions(-) diff --git a/lib/config/dependencies.dart b/lib/config/dependencies.dart index 7282cf42..9081422c 100644 --- a/lib/config/dependencies.dart +++ b/lib/config/dependencies.dart @@ -56,42 +56,18 @@ Future _initSidechainRPC(Sidechain chain) async { () => ethRPC, ); - SidechainRPC sidechainRPC; + SidechainSubRPC sidechainSubRPC; switch (chain.type) { case SidechainType.testChain: - sidechainRPC = testRPC; + sidechainSubRPC = testRPC; break; case SidechainType.ethereum: - sidechainRPC = ethRPC; + sidechainSubRPC = ethRPC; break; } GetIt.I.registerLazySingleton( - () => sidechainRPC, - ); -} - -// swap out the SidechainRPC type in GetIt with the appropriate -// rpc. All of them are already registered, so this is pretty quick -void swapSidechainRPC(Sidechain chain) async { - SidechainRPC sidechainRPC; - switch (chain.type) { - case SidechainType.testChain: - final testchainRPC = GetIt.I.get(); - sidechainRPC = testchainRPC; - break; - - case SidechainType.ethereum: - final ethRPC = GetIt.I.get(); - sidechainRPC = ethRPC; - break; - } - - if (GetIt.I.isRegistered()) { - GetIt.I.unregister(); - } - GetIt.I.registerLazySingleton( - () => sidechainRPC, + () => SidechainRPC(subRPC: sidechainSubRPC), ); } diff --git a/lib/pages/tabs/sidechain_explorer_tab_page.dart b/lib/pages/tabs/sidechain_explorer_tab_page.dart index 9272eb51..e67ee2e7 100644 --- a/lib/pages/tabs/sidechain_explorer_tab_page.dart +++ b/lib/pages/tabs/sidechain_explorer_tab_page.dart @@ -41,7 +41,7 @@ class SidechainExplorerTabPage extends StatelessWidget { unconfirmedBalance: viewModel.pendingBalance, highlighted: false, currentChain: viewModel.chain.type == SidechainType.testChain, - onPressed: () => viewModel.setSidechainRPC(TestSidechain()), + onPressed: () => viewModel.setSidechainRPC(test), ), ChainOverviewCard( chain: EthereumSidechain(), @@ -49,7 +49,7 @@ class SidechainExplorerTabPage extends StatelessWidget { unconfirmedBalance: viewModel.pendingBalance, highlighted: false, currentChain: viewModel.chain.type == SidechainType.ethereum, - onPressed: () => viewModel.setSidechainRPC(EthereumSidechain()), + onPressed: () => viewModel.setSidechainRPC(eth), ), ], ), @@ -82,8 +82,8 @@ class SidechainExplorerTabViewModel extends BaseViewModel { _sideRPC.addListener(notifyListeners); } - void setSidechainRPC(Sidechain chain) { - _sideRPC.setChain(chain); + void setSidechainRPC(SidechainSubRPC sideSubRPC) { + _sideRPC.setSubRPC(sideSubRPC); notifyListeners(); } diff --git a/lib/rpc/rpc_ethereum.dart b/lib/rpc/rpc_ethereum.dart index 75670bca..b89270e9 100644 --- a/lib/rpc/rpc_ethereum.dart +++ b/lib/rpc/rpc_ethereum.dart @@ -5,7 +5,7 @@ import 'package:sidesail/rpc/models/core_transaction.dart'; import 'package:sidesail/rpc/rpc_sidechain.dart'; import 'package:web3dart/web3dart.dart'; -abstract class EthereumRPC extends SidechainRPC {} +abstract class EthereumRPC extends SidechainSubRPC {} class EthereumRPCLive extends EthereumRPC { final sgweiPerSat = 1000000000; diff --git a/lib/rpc/rpc_sidechain.dart b/lib/rpc/rpc_sidechain.dart index 6ed15db2..bbb3fa09 100644 --- a/lib/rpc/rpc_sidechain.dart +++ b/lib/rpc/rpc_sidechain.dart @@ -1,23 +1,68 @@ import 'dart:async'; -import 'package:sidesail/config/dependencies.dart'; import 'package:sidesail/config/sidechains.dart'; +import 'package:sidesail/pages/tabs/settings/node_settings_tab.dart'; import 'package:sidesail/rpc/models/core_transaction.dart'; import 'package:sidesail/rpc/rpc.dart'; /// RPC connection for all sidechain nodes -abstract class SidechainRPC extends RPCConnection { +abstract class SidechainSubRPC extends RPCConnection { Future callRAW(String method, [List? params]); Future<(double, double)> getBalance(); Future> listTransactions(); late Sidechain chain; - void setChain(Sidechain newChain) { - chain = newChain; - swapSidechainRPC(newChain); +} + +/// RPC connection for all sidechain nodes +class SidechainRPC extends SidechainSubRPC { + SidechainSubRPC subRPC; + + SidechainRPC({ + required this.subRPC, + }) { + chain = subRPC.chain; + } + + // values for tracking connection state, and error (if any) + @override + SingleNodeConnectionSettings get connectionSettings => subRPC.connectionSettings; + @override + bool get connected => subRPC.connected; + @override + String? get connectionError => subRPC.connectionError; + + void setSubRPC(SidechainSubRPC newSubRPC) { + subRPC = newSubRPC; + chain = subRPC.chain; notifyListeners(); } + + @override + Future callRAW(String method, [List? params]) async { + return await subRPC.callRAW(method, params); + } + + @override + Future<(double, double)> getBalance() async { + return subRPC.getBalance(); + } + + @override + Future> listTransactions() { + return subRPC.listTransactions(); + } + + @override + Future createClient() async { + return subRPC.createClient(); + } + + @override + Future ping() async { + return subRPC.ping(); + } } class RPCError { diff --git a/lib/rpc/rpc_testchain.dart b/lib/rpc/rpc_testchain.dart index 0afc11af..de84aeae 100644 --- a/lib/rpc/rpc_testchain.dart +++ b/lib/rpc/rpc_testchain.dart @@ -15,7 +15,7 @@ import 'package:sidesail/rpc/rpc_sidechain.dart'; import 'package:sidesail/rpc/rpc_withdrawal_bundle.dart'; /// RPC connection the sidechain node. -abstract class TestchainRPC extends SidechainRPC { +abstract class TestchainRPC extends SidechainSubRPC { Future mainSend( String address, double amount, diff --git a/test/dashboard_test.dart b/test/dashboard_test.dart index 741da6ee..46582c29 100644 --- a/test/dashboard_test.dart +++ b/test/dashboard_test.dart @@ -21,7 +21,7 @@ final txProvider = TransactionsProvider(); void main() { setUpAll(() async { - GetIt.I.registerLazySingleton(() => MockSidechainRPC()); + GetIt.I.registerLazySingleton(() => MockSidechainRPC(subRPC: MockSidechainSubRPC())); GetIt.I.registerLazySingleton(() => MockMainchainRPC()); GetIt.I.registerLazySingleton(() => txProvider); diff --git a/test/mocks/rpc_mock_sidechain.dart b/test/mocks/rpc_mock_sidechain.dart index 98828556..f137cbab 100644 --- a/test/mocks/rpc_mock_sidechain.dart +++ b/test/mocks/rpc_mock_sidechain.dart @@ -3,7 +3,7 @@ import 'package:sidesail/rpc/models/core_transaction.dart'; import 'package:sidesail/rpc/rpc_sidechain.dart'; class MockSidechainRPC extends SidechainRPC { - MockSidechainRPC() { + MockSidechainRPC({required super.subRPC}) { chain = TestSidechain(); } @@ -37,3 +37,37 @@ class MockSidechainRPC extends SidechainRPC { return List.empty(); } } + +class MockSidechainSubRPC extends SidechainSubRPC { + MockSidechainSubRPC(); + + @override + Future<(double, double)> getBalance() async { + return (1.12345678, 2.24680); + } + + @override + Future callRAW(String method, [dynamic params]) async { + return; + } + + @override + Future<(bool, String?)> testConnection() async { + return (true, null); + } + + @override + Future createClient() async { + return; + } + + @override + Future ping() async { + return; + } + + @override + Future> listTransactions() async { + return List.empty(); + } +}