From e054095fc059fd56ac474405dcb8c30272fa8cac Mon Sep 17 00:00:00 2001 From: Adrian Sutton Date: Mon, 10 Jun 2019 14:31:31 +1000 Subject: [PATCH] FilterIdGenerator fixes (#1544) Don't create a new SecureRandom instance every time a filter ID is generated. Fix intermittency in FilterIdGeneratorTest - quantity values omit leading 0s. --- .../ethereum/jsonrpc/internal/filter/FilterIdGenerator.java | 6 +++++- .../jsonrpc/internal/filter/FilterIdGeneratorTest.java | 6 +++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/ethereum/jsonrpc/src/main/java/tech/pegasys/pantheon/ethereum/jsonrpc/internal/filter/FilterIdGenerator.java b/ethereum/jsonrpc/src/main/java/tech/pegasys/pantheon/ethereum/jsonrpc/internal/filter/FilterIdGenerator.java index 116a81ab2d..a8618cf45f 100644 --- a/ethereum/jsonrpc/src/main/java/tech/pegasys/pantheon/ethereum/jsonrpc/internal/filter/FilterIdGenerator.java +++ b/ethereum/jsonrpc/src/main/java/tech/pegasys/pantheon/ethereum/jsonrpc/internal/filter/FilterIdGenerator.java @@ -15,11 +15,15 @@ import tech.pegasys.pantheon.crypto.SecureRandomProvider; import tech.pegasys.pantheon.ethereum.jsonrpc.internal.results.Quantity; +import java.security.SecureRandom; + public class FilterIdGenerator { + private final SecureRandom secureRandom = SecureRandomProvider.createSecureRandom(); + public String nextId() { final byte[] randomBytes = new byte[16]; - SecureRandomProvider.createSecureRandom().nextBytes(randomBytes); + secureRandom.nextBytes(randomBytes); return Quantity.create(randomBytes); } } diff --git a/ethereum/jsonrpc/src/test/java/tech/pegasys/pantheon/ethereum/jsonrpc/internal/filter/FilterIdGeneratorTest.java b/ethereum/jsonrpc/src/test/java/tech/pegasys/pantheon/ethereum/jsonrpc/internal/filter/FilterIdGeneratorTest.java index 9097d026ef..c6e4327fe3 100644 --- a/ethereum/jsonrpc/src/test/java/tech/pegasys/pantheon/ethereum/jsonrpc/internal/filter/FilterIdGeneratorTest.java +++ b/ethereum/jsonrpc/src/test/java/tech/pegasys/pantheon/ethereum/jsonrpc/internal/filter/FilterIdGeneratorTest.java @@ -14,7 +14,7 @@ import static org.junit.Assert.assertEquals; -import tech.pegasys.pantheon.util.bytes.BytesValue; +import tech.pegasys.pantheon.util.uint.UInt256; import org.junit.Test; @@ -24,7 +24,7 @@ public class FilterIdGeneratorTest { public void idIsAHexString() { final FilterIdGenerator generator = new FilterIdGenerator(); final String s = generator.nextId(); - final BytesValue bytesValue = BytesValue.fromHexString(s); - assertEquals(s, bytesValue.toString()); + final UInt256 bytesValue = UInt256.fromHexString(s); + assertEquals(s, bytesValue.toShortHexString()); } }