Skip to content

Commit

Permalink
Merge branch 'main' into protobuf
Browse files Browse the repository at this point in the history
  • Loading branch information
parodyBit authored Oct 6, 2022
2 parents de8e5ee + 0ac92a9 commit 4f3817b
Showing 1 changed file with 17 additions and 16 deletions.
33 changes: 17 additions & 16 deletions lib/src/network/node/node_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ class NodeClient {
late Socket _socket;
Uint8List _buffer = Uint8List.fromList([]);

NodeClient(
{required this.address, required this.port, this.keepAlive = false});

/// Get node status
Expand Down Expand Up @@ -62,7 +60,7 @@ class NodeClient {
Future<dynamic> inventory(Map<String, dynamic> inventoryItem) async {
try {
var response = await sendMessage(
formatRequest(method: 'inventory', params: inventoryItem))
formatRequest(method: 'inventory', params: inventoryItem))
.then((Map<String, dynamic> data) {
if (data.containsKey('error')) {
throw NodeException.fromJson(data['error']);
Expand All @@ -80,8 +78,8 @@ class NodeClient {
{required int epoch, required int limit}) async {
try {
var response = await sendMessage(formatRequest(
method: 'getBlockChain',
params: {'epoch': epoch, 'limit': limit}))
method: 'getBlockChain',
params: {'epoch': epoch, 'limit': limit}))
.then((Map<String, dynamic> data) {
if (data.containsKey('error')) {
throw NodeException.fromJson(data['error']);
Expand All @@ -101,7 +99,7 @@ class NodeClient {
Future<Block> getBlock({required String blockHash}) async {
try {
var response = await sendMessage(
formatRequest(method: 'getBlock', params: [blockHash]))
formatRequest(method: 'getBlock', params: [blockHash]))
.then((Map<String, dynamic> data) {
if (data.containsKey('result')) {
return data['result'];
Expand Down Expand Up @@ -159,7 +157,7 @@ class NodeClient {
Future<Map<String, dynamic>> getBalance({required String address}) async {
try {
var response = await sendMessage(
formatRequest(method: 'getBalance', params: [address]))
formatRequest(method: 'getBalance', params: [address]))
.then((Map<String, dynamic> data) {
if (data.containsKey('result')) {
return data['result'];
Expand All @@ -178,7 +176,7 @@ class NodeClient {
Future<Map<String, dynamic>> getReputation({required String address}) async {
try {
var response = await sendMessage(
formatRequest(method: 'getReputation', params: [address]))
formatRequest(method: 'getReputation', params: [address]))
.then((Map<String, dynamic> data) {
if (data.containsKey('result')) {
return data['result'];
Expand All @@ -196,8 +194,8 @@ class NodeClient {
Future<Map<String, dynamic>> getReputationAll() async {
try {
var response =
await sendMessage(formatRequest(method: 'getReputationAll'))
.then((Map<String, dynamic> data) {
await sendMessage(formatRequest(method: 'getReputationAll'))
.then((Map<String, dynamic> data) {
if (data.containsKey('result')) {
return data['result'];
} else if (data.containsKey('error')) {
Expand Down Expand Up @@ -268,8 +266,8 @@ class NodeClient {
Future<Map<String, dynamic>> getConsensusConstants() async {
try {
var response =
await sendMessage(formatRequest(method: 'getConsensusConstants'))
.then((Map<String, dynamic> data) {
await sendMessage(formatRequest(method: 'getConsensusConstants'))
.then((Map<String, dynamic> data) {
if (data.containsKey('result')) {
return data['result'];
} else if (data.containsKey('error')) {
Expand Down Expand Up @@ -307,7 +305,7 @@ class NodeClient {
Future<UtxoInfo> getUtxoInfo({required String address}) async {
try {
var response = await sendMessage(
formatRequest(method: 'getUtxoInfo', params: [address]))
formatRequest(method: 'getUtxoInfo', params: [address]))
.then((Map<String, dynamic> data) {
if (data.containsKey('result')) {
return data['result'];
Expand Down Expand Up @@ -363,7 +361,8 @@ class NodeClient {
/// Handle the Socket connection for the Node
Future<Map<String, dynamic>> _handle(String _request) async {
Completer<Map<String, dynamic>> _completer =
new Completer<Map<String, dynamic>>();

new Completer<Map<String, dynamic>>();

String _secureResponse = '';

Expand All @@ -372,14 +371,15 @@ class NodeClient {
// =============================================================
_socket = await Socket.connect(address, port);
_socket.listen(
(Uint8List data) {
(Uint8List data) {
// add the current chunk to the end of the buffer
_buffer = concatBytes([_buffer, data]);
// try to decode the buffer
try {
// this will throw an error if the _buffer is not complete
Map<String, dynamic> response =
json.decode(new String.fromCharCodes(_buffer).trim());
json.decode(new String.fromCharCodes(_buffer).trim());


// clear the buffer
_buffer = Uint8List.fromList([]);
Expand All @@ -397,6 +397,7 @@ class NodeClient {
_socket.add(utf8.encode(_request));
}
} on SocketException catch (e) {

throw NodeException(code: e.osError!.errorCode, message: e.message);
} catch (e) {
throw NodeException(code: -1, message: e.toString());
Expand Down

0 comments on commit 4f3817b

Please sign in to comment.