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

Check if generated transaction IDs are unique #800

Closed
thenswan opened this issue Sep 6, 2023 · 0 comments · Fixed by #807
Closed

Check if generated transaction IDs are unique #800

thenswan opened this issue Sep 6, 2023 · 0 comments · Fixed by #807
Assignees
Labels
bug Something isn't working
Milestone

Comments

@thenswan
Copy link

thenswan commented Sep 6, 2023

Description

Ref issue in the Java SDK, which is going to be fixed with this PR - please check if we have the same issue in the Go SDK.

Steps to reproduce

There is a sample code, which was used in the Java SDK for testing. testGenerateId uses TransactionId.generate() and fails after ~100K IDs. testGenerateId2 uses a monotonic clock and passes on 1M ids.

@Test
    void testGenerateId() {
        TransactionId[] ids = new TransactionId[1000000];
        AccountId accountId = AccountId.fromString("0.0.1000");
        for (int i = 0; i < ids.length; ++i) {
            ids[i] = TransactionId.generate(accountId);
        }
        HashSet<TransactionId> set = new HashSet<>(ids.length);
        for (int i = 0; i < ids.length; ++i) {
            assertThat(set.add(ids[i])).as("ids[%d] is not unique", i).isTrue();
        }
    }

    private final AtomicLong monotonicTime = new AtomicLong();

    TransactionId generate(AccountId accountId) {
        long currentTime;
        long lastTime;
        do {
            currentTime = System.currentTimeMillis() * 1_000_000L;
            lastTime = monotonicTime.get();
            if (currentTime <= lastTime) currentTime = lastTime + 1000L;
        } while (!monotonicTime.compareAndSet(lastTime, currentTime));
        return new TransactionId(accountId, Instant.ofEpochSecond(0, currentTime));
    }

    @Test
    void testGenerateId2() {
        TransactionId[] ids = new TransactionId[1000000];
        AccountId accountId = AccountId.fromString("0.0.1000");
        for (int i = 0; i < ids.length; ++i) {
            ids[i] = generate(accountId);
        }
        HashSet<TransactionId> set = new HashSet<>(ids.length);
        for (int i = 0; i < ids.length; ++i) {
            assertThat(set.add(ids[i])).as("ids[%d] is not unique", i).isTrue();
        }
    }

Additional context

No response

Hedera network

other

Version

v2.28.0

Operating system

None

@thenswan thenswan added the bug Something isn't working label Sep 6, 2023
@SimiHunjan SimiHunjan added this to the 2.30.0 milestone Sep 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants