Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add connection hash #211

Merged
merged 4 commits into from
Sep 18, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class GqlConnection extends RustToDartMirrorInterface {

final String _name;
final String _group;
final GqlNetworkSettings _settings;
final GqlNetworkSettings settings;

final type = TransportType.gql;

Expand All @@ -33,7 +33,7 @@ class GqlConnection extends RustToDartMirrorInterface {
GqlConnection._(
this._post,
this._get,
this._settings,
this.settings,
this._name,
this._group,
);
Expand Down Expand Up @@ -65,8 +65,8 @@ class GqlConnection extends RustToDartMirrorInterface {
try {
String endpoint;

if (_settings.endpoints.length == 1) {
endpoint = _settings.endpoints.first;
if (settings.endpoints.length == 1) {
endpoint = settings.endpoints.first;
} else {
endpoint = await _getEndpoint();
}
Expand All @@ -87,24 +87,24 @@ class GqlConnection extends RustToDartMirrorInterface {
if (_cachedEndpoint != null) return _cachedEndpoint!;

_cachedEndpoint = await _selectQueryingEndpoint().timeout(
Duration(milliseconds: _settings.latencyDetectionInterval),
Duration(milliseconds: settings.latencyDetectionInterval),
onTimeout: () => throw ErrorCode.Network,
);
return _cachedEndpoint!;
}

Future<String> _selectQueryingEndpoint() async {
final maxLatency = _settings.maxLatency;
final retryCount = _settings.endpointSelectionRetryCount;
final endpointsCount = _settings.endpoints.length;
final maxLatency = settings.maxLatency;
final retryCount = settings.endpointSelectionRetryCount;
final endpointsCount = settings.endpoints.length;

for (var i = 0; i < retryCount; i++) {
try {
final completer = Completer<String>();

var checkedEndpoints = 0;

for (final e in _settings.endpoints) {
for (final e in settings.endpoints) {
_checkLatency(e).whenComplete(() {
checkedEndpoints++;
}).then((v) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ class JrpcConnection extends RustToDartMirrorInterface {

final String _name;
final String _group;
final JrpcNetworkSettings _settings;
final JrpcNetworkSettings settings;

final type = TransportType.gql;

JrpcConnection._(
this._post,
this._settings,
this.settings,
this._name,
this._group,
);
Expand Down Expand Up @@ -53,7 +53,7 @@ class JrpcConnection extends RustToDartMirrorInterface {
Future<String> post(String requestData) async {
try {
return await _post(
endpoint: _settings.endpoint,
endpoint: settings.endpoint,
headers: {
'Content-Type': 'application/json',
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ class ProtoConnection extends RustToDartMirrorInterface {

final String _name;
final String _group;
final ProtoNetworkSettings _settings;
final ProtoNetworkSettings settings;

final type = TransportType.proto;

ProtoConnection._(
this._post,
this._settings,
this.settings,
this._name,
this._group,
);
Expand Down Expand Up @@ -56,7 +56,7 @@ class ProtoConnection extends RustToDartMirrorInterface {
Future<Uint8List> post(Uint8List requestData) async {
try {
return await _post(
endpoint: _settings.endpoint,
endpoint: settings.endpoint,
headers: {
'Content-Type': 'x-protobuf',
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ class GqlTransport extends Transport {

instance.networkId = await instance.getNetworkId();

instance.connectionParamsHash =
getHash('gql:${gqlConnection.settings.endpoints.join(',')}');

return instance;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ class JrpcTransport extends Transport {

instance.networkId = await instance.getNetworkId();

instance.connectionParamsHash =
getHash('jrpc:${jrpcConnection.settings.endpoint}');

return instance;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ class ProtoTransport extends Transport {

instance.networkId = await instance.getNetworkId();

instance.connectionParamsHash =
getHash('proto:${protoConnection.settings.endpoint}');

return instance;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ abstract class Transport {

TransportType get type;

/// Get uniquer identifier of transport based on type and endpoints
late final String connectionParamsHash;

/// Get contract state of address and return RawContractState or throw error
Future<RawContractState> getContractState(Address address);

Expand Down
4 changes: 4 additions & 0 deletions packages/flutter_nekoton_bridge/lib/nekoton/utils.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:crypto/crypto.dart';
import 'package:flutter_nekoton_bridge/flutter_nekoton_bridge.dart';
import 'package:freezed_annotation/freezed_annotation.dart';

Expand Down Expand Up @@ -56,3 +57,6 @@ extension KeySignerName on KeySigner {
);
}
}

/// Get hash of String
String getHash(String string) => sha256.convert(string.codeUnits).toString();
1 change: 1 addition & 0 deletions packages/flutter_nekoton_bridge/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ dependencies:
# for generating mirror hash
uuid: ^3.0.7
tuple: ^2.0.1
crypto: ^3.0.3
dev_dependencies:
ffi: ^2.0.2
ffigen: ^8.0.2
Expand Down
Loading