Skip to content

Commit

Permalink
rpc: introduce SidechainSubRPC for quick changing of sidechains
Browse files Browse the repository at this point in the history
  • Loading branch information
octobocto committed Nov 9, 2023
1 parent 90b6f1b commit 6d6ec2f
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 41 deletions.
32 changes: 4 additions & 28 deletions lib/config/dependencies.dart
Original file line number Diff line number Diff line change
Expand Up @@ -56,42 +56,18 @@ Future<void> _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>(
() => 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<TestchainRPC>();
sidechainRPC = testchainRPC;
break;

case SidechainType.ethereum:
final ethRPC = GetIt.I.get<EthereumRPC>();
sidechainRPC = ethRPC;
break;
}

if (GetIt.I.isRegistered<SidechainRPC>()) {
GetIt.I.unregister<SidechainRPC>();
}
GetIt.I.registerLazySingleton<SidechainRPC>(
() => sidechainRPC,
() => SidechainRPC(subRPC: sidechainSubRPC),
);
}
8 changes: 4 additions & 4 deletions lib/pages/tabs/sidechain_explorer_tab_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,15 @@ 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(),
confirmedBalance: viewModel.balance,
unconfirmedBalance: viewModel.pendingBalance,
highlighted: false,
currentChain: viewModel.chain.type == SidechainType.ethereum,
onPressed: () => viewModel.setSidechainRPC(EthereumSidechain()),
onPressed: () => viewModel.setSidechainRPC(eth),
),
],
),
Expand Down Expand Up @@ -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();
}

Expand Down
2 changes: 1 addition & 1 deletion lib/rpc/rpc_ethereum.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
55 changes: 50 additions & 5 deletions lib/rpc/rpc_sidechain.dart
Original file line number Diff line number Diff line change
@@ -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<dynamic> callRAW(String method, [List<dynamic>? params]);

Future<(double, double)> getBalance();
Future<List<CoreTransaction>> 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<dynamic> callRAW(String method, [List<dynamic>? params]) async {
return await subRPC.callRAW(method, params);
}

@override
Future<(double, double)> getBalance() async {
return subRPC.getBalance();
}

@override
Future<List<CoreTransaction>> listTransactions() {
return subRPC.listTransactions();
}

@override
Future<void> createClient() async {
return subRPC.createClient();
}

@override
Future<void> ping() async {
return subRPC.ping();
}
}

class RPCError {
Expand Down
2 changes: 1 addition & 1 deletion lib/rpc/rpc_testchain.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> mainSend(
String address,
double amount,
Expand Down
2 changes: 1 addition & 1 deletion test/dashboard_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ final txProvider = TransactionsProvider();

void main() {
setUpAll(() async {
GetIt.I.registerLazySingleton<SidechainRPC>(() => MockSidechainRPC());
GetIt.I.registerLazySingleton<SidechainRPC>(() => MockSidechainRPC(subRPC: MockSidechainSubRPC()));
GetIt.I.registerLazySingleton<MainchainRPC>(() => MockMainchainRPC());

GetIt.I.registerLazySingleton<TransactionsProvider>(() => txProvider);
Expand Down
38 changes: 37 additions & 1 deletion test/mocks/rpc_mock_sidechain.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,43 @@ 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();
}

@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<void> createClient() async {
return;
}

@override
Future<void> ping() async {
return;
}

@override
Future<List<CoreTransaction>> listTransactions() async {
return List.empty();
}
}

class MockSidechainSubRPC extends SidechainSubRPC {
MockSidechainSubRPC() {
chain = TestSidechain();
}

Expand Down

0 comments on commit 6d6ec2f

Please sign in to comment.