hiero-ledger / hiero-ledger/hiero-enterprise-java

Issue 4 — ContractSubscription: AutoCloseable subscription handle

Open
#310 1 comment 0 reactions 2 assignees Claimed by @manishdait View on GitHub
API-Design enhancement Mentorship pending-review smart-contracts
Dominant language
Java
Stars
6
Forks
21
Avg merge
10h 27m
Merged PRs (30d)
37

Description

### Context

The design doc adds ContractSubscription, the object returned by every subscribeEvent(...) overload, so a caller can explicitly cancel an active subscription and release the underlying networking resources without the public API ever exposing Web3j types directly:

```java
public class ContractSubscription extends AutoCloseable {

/**
* Cancels the event subscription and releases underlying networking resources.
*/
void unsubscribe() {
...
}

/**
* Checks if the subscription has been cancelled.
*
* @return true if unsubscribed/disposed, false otherwise
*/
boolean isUnsubscribed() {
...
};

/**
* Alias for {@link #unsubscribe()} to support {@link AutoCloseable}.
*/
@Override
default void close() {
unsubscribe();
}
}
```

### Conceptual flow:

```txt
subscribeEvent(...)

ContractSubscription

Web3j Disposable

unsubscribe()

Disposable.dispose()
```

### Scope
- Implement the `ContractSubscription` class exactly as specified (unsubscribe(), isUnsubscribed(), default close()).
- Implement a concrete implementation that wraps a Web3j `Disposable` internally, so Disposable/Web3j never leaks into the public org.hiero.base API surface this is explicitly called out in the doc ("Web3j remains an implementation detail of the SDK").
- `unsubscribe()` must be idempotent calling it more than once should not throw, and `isUnsubscribed()` should return true afterward.
- Decide and document thread-safety expectations (e.g. can `unsubscribe()` be called concurrently with event delivery / from a different thread than the one that created the subscription).

### Acceptance Criteria
- [ ] Interface implemented with the exact method set shown above.
- [ ] Concrete implementation correctly disposes the wrapped Web3j Disposable on unsubscribe()/close().
- [ ] Unit tests: `isUnsubscribed()` reflects state before/after unsubscribe(); calling unsubscribe() twice does not throw; try-with-resources usage via `AutoCloseable` works as expected.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.