Skip to content

Commit

Permalink
fix: added disposed flag to transport (#243)
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex-A4 authored Dec 5, 2023
1 parent dda886e commit b49beb8
Show file tree
Hide file tree
Showing 8 changed files with 126 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ class GenericContract extends RustToDartMirrorInterface
/// May throw error.
@override
Future<void> refresh() async {
if (_isRefreshing) return;
if (_isRefreshing || transport.disposed) return;

try {
_isRefreshing = true;
Expand Down Expand Up @@ -273,6 +273,8 @@ class GenericContract extends RustToDartMirrorInterface
/// This shitty repeated avoidCall needs to avoid `Use after free` when
/// method calls after dispose.
Future<void> _updateData() async {
if (transport.disposed) return;

if (avoidCall) return;
_contractState = await getContractState();
if (avoidCall) return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ class TokenWallet extends RustToDartMirrorInterface
/// May throw error.
@override
Future<void> refresh() async {
if (_isRefreshing) return;
if (_isRefreshing || transport.disposed) return;

try {
_isRefreshing = true;
Expand Down Expand Up @@ -316,6 +316,8 @@ class TokenWallet extends RustToDartMirrorInterface
/// This shitty repeated avoidCall needs to avoid `Use after free` when
/// method calls after dispose.
Future<void> _updateData() async {
if (transport.disposed) return;

if (avoidCall) return;
_contractState = await getContractState();
if (avoidCall) return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ class TonWallet extends RustToDartMirrorInterface
/// May throw error.
@override
Future<void> refresh() async {
if (_isRefreshing) return;
if (_isRefreshing || transport.disposed) return;

try {
_isRefreshing = true;
Expand Down Expand Up @@ -494,6 +494,8 @@ class TonWallet extends RustToDartMirrorInterface
/// This shitty repeated avoidCall needs to avoid `Use after free` when
/// method calls after dispose.
Future<void> _updateData() async {
if (transport.disposed) return;

if (avoidCall) return;
_contractState = await getContractState();
if (avoidCall) return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ class GqlTransport extends Transport {

GqlTransport._(this.gqlConnection);

@override
bool get disposed => _disposed;

bool _disposed = false;

static Future<GqlTransport> create({
required GqlConnection gqlConnection,
}) async {
Expand All @@ -31,6 +36,7 @@ class GqlTransport extends Transport {
void dispose() {
transport.innerTransport.dispose();
gqlConnection.dispose();
_disposed = true;
}

@override
Expand All @@ -39,6 +45,8 @@ class GqlTransport extends Transport {
required int limit,
String? continuation,
}) async {
if (_disposed) throw TransportCallAfterDisposeError();

final res = await transport.getAccountsByCodeHash(
codeHash: codeHash,
limit: limit,
Expand All @@ -49,12 +57,16 @@ class GqlTransport extends Transport {

@override
Future<RawContractState> getContractState(Address address) async {
if (_disposed) throw TransportCallAfterDisposeError();

final res = await transport.getContractState(address: address.address);
return RawContractState.fromJson(jsonDecode(res));
}

@override
Future<FullContractState?> getFullContractState(Address address) async {
if (_disposed) throw TransportCallAfterDisposeError();

final res = await transport.getFullContractState(address: address.address);
if (res == null) return null;

Expand All @@ -67,6 +79,8 @@ class GqlTransport extends Transport {
required String contractAbi,
FullContractState? cachedState,
}) async {
if (_disposed) throw TransportCallAfterDisposeError();

final state = cachedState ?? await getFullContractState(address);
if (state == null) {
return (null, null);
Expand All @@ -84,14 +98,22 @@ class GqlTransport extends Transport {

@override
Future<int?> getSignatureId() {
if (_disposed) throw TransportCallAfterDisposeError();

return transport.getSignatureId();
}

@override
Future<int> getNetworkId() => transport.getNetworkId();
Future<int> getNetworkId() {
if (_disposed) throw TransportCallAfterDisposeError();

return transport.getNetworkId();
}

@override
Future<Transaction?> getTransaction(String hash) async {
if (_disposed) throw TransportCallAfterDisposeError();

final res = await transport.getTransaction(hash: hash);
if (res == null) return null;
return Transaction.fromJson(jsonDecode(res));
Expand All @@ -103,6 +125,8 @@ class GqlTransport extends Transport {
required int count,
String? fromLt,
}) async {
if (_disposed) throw TransportCallAfterDisposeError();

final res = await transport.getTransactions(
address: address.address,
count: count,
Expand All @@ -113,32 +137,46 @@ class GqlTransport extends Transport {

@override
Future<RawTransaction?> getDstTransaction(String messageHash) async {
if (_disposed) throw TransportCallAfterDisposeError();

final res = await transport.getDstTransaction(messageHash: messageHash);

return res == null ? null : RawTransaction.fromJson(jsonDecode(res));
}

/// Get latest block by address and return it or throw error
Future<LatestBlock> getLatestBlock({required Address address}) =>
transport.getLatestBlock(address: address.address);
Future<LatestBlock> getLatestBlock({required Address address}) {
if (_disposed) throw TransportCallAfterDisposeError();

return transport.getLatestBlock(address: address.address);
}

/// Get transport block by id and return base64 encoded block or throw error
Future<String> getBlock({required String id}) => transport.getBlock(id: id);
Future<String> getBlock({required String id}) {
if (_disposed) throw TransportCallAfterDisposeError();

return transport.getBlock(id: id);
}

/// Wait until next block will come to blockchain and return its id or throw error
Future<String> waitForNextBlock({
required String currentBlockId,
required Address address,
required Duration timeout,
}) =>
transport.waitForNextBlock(
currentBlockId: currentBlockId,
address: address.address,
timeout: timeout.inMilliseconds,
);
}) {
if (_disposed) throw TransportCallAfterDisposeError();

return transport.waitForNextBlock(
currentBlockId: currentBlockId,
address: address.address,
timeout: timeout.inMilliseconds,
);
}

@override
Future<BlockchainConfig> getBlockchainConfig({bool force = true}) async {
if (_disposed) throw TransportCallAfterDisposeError();

final config = await transport.getBlockchainConfig(force: force);

return BlockchainConfig.fromJson(jsonDecode(config));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ class JrpcTransport extends Transport {

JrpcTransport._(this.jrpcConnection);

@override
bool get disposed => _disposed;

bool _disposed = false;

static Future<JrpcTransport> create({
required JrpcConnection jrpcConnection,
}) async {
Expand All @@ -31,6 +36,7 @@ class JrpcTransport extends Transport {
void dispose() {
transport.innerTransport.dispose();
jrpcConnection.dispose();
_disposed = true;
}

@override
Expand All @@ -39,6 +45,8 @@ class JrpcTransport extends Transport {
required int limit,
String? continuation,
}) async {
if (_disposed) throw TransportCallAfterDisposeError();

final res = await transport.getAccountsByCodeHash(
codeHash: codeHash,
limit: limit,
Expand All @@ -49,12 +57,16 @@ class JrpcTransport extends Transport {

@override
Future<RawContractState> getContractState(Address address) async {
if (_disposed) throw TransportCallAfterDisposeError();

final res = await transport.getContractState(address: address.address);
return RawContractState.fromJson(jsonDecode(res));
}

@override
Future<FullContractState?> getFullContractState(Address address) async {
if (_disposed) throw TransportCallAfterDisposeError();

final res = await transport.getFullContractState(address: address.address);
if (res == null) return null;

Expand All @@ -63,14 +75,22 @@ class JrpcTransport extends Transport {

@override
Future<int?> getSignatureId() {
if (_disposed) throw TransportCallAfterDisposeError();

return transport.getSignatureId();
}

@override
Future<int> getNetworkId() => transport.getNetworkId();
Future<int> getNetworkId() {
if (_disposed) throw TransportCallAfterDisposeError();

return transport.getNetworkId();
}

@override
Future<Transaction?> getTransaction(String hash) async {
if (_disposed) throw TransportCallAfterDisposeError();

final res = await transport.getTransaction(hash: hash);
if (res == null) return null;
return Transaction.fromJson(jsonDecode(res));
Expand All @@ -82,6 +102,8 @@ class JrpcTransport extends Transport {
required int count,
String? fromLt,
}) async {
if (_disposed) throw TransportCallAfterDisposeError();

final res = await transport.getTransactions(
address: address.address,
count: count,
Expand All @@ -92,6 +114,8 @@ class JrpcTransport extends Transport {

@override
Future<RawTransaction?> getDstTransaction(String messageHash) async {
if (_disposed) throw TransportCallAfterDisposeError();

final res = await transport.getDstTransaction(messageHash: messageHash);

return res == null ? null : RawTransaction.fromJson(jsonDecode(res));
Expand All @@ -103,6 +127,8 @@ class JrpcTransport extends Transport {
required String contractAbi,
FullContractState? cachedState,
}) async {
if (_disposed) throw TransportCallAfterDisposeError();

final state = cachedState ?? await getFullContractState(address);
if (state == null) {
return (null, null);
Expand All @@ -120,6 +146,8 @@ class JrpcTransport extends Transport {

@override
Future<BlockchainConfig> getBlockchainConfig({bool force = true}) async {
if (_disposed) throw TransportCallAfterDisposeError();

final config = await transport.getBlockchainConfig(force: force);

return BlockchainConfig.fromJson(jsonDecode(config));
Expand Down
Loading

0 comments on commit b49beb8

Please sign in to comment.