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 #499

Open
thenswan opened this issue Sep 6, 2023 · 1 comment
Open

Check if generated transaction IDs are unique #499

thenswan opened this issue Sep 6, 2023 · 1 comment
Labels
Bug A error that causes the feature to behave differently than what was expected based on design docs

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 C++ 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

v0.15.0

Operating system

None

@thenswan thenswan added the Bug A error that causes the feature to behave differently than what was expected based on design docs label Sep 6, 2023
@SimiHunjan
Copy link
Contributor

Research issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A error that causes the feature to behave differently than what was expected based on design docs
Projects
None yet
Development

No branches or pull requests

2 participants