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.
Signed-off-by: Adrian Sutton <adrian.sutton@consensys.net>
pull/2/head
Adrian Sutton 6 years ago committed by GitHub
parent 94ce7c3a4a
commit ee8daad35f
  1. 6
      ethereum/jsonrpc/src/main/java/tech/pegasys/pantheon/ethereum/jsonrpc/internal/filter/FilterIdGenerator.java
  2. 6
      ethereum/jsonrpc/src/test/java/tech/pegasys/pantheon/ethereum/jsonrpc/internal/filter/FilterIdGeneratorTest.java

@ -15,11 +15,15 @@ package tech.pegasys.pantheon.ethereum.jsonrpc.internal.filter;
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);
}
}

@ -14,7 +14,7 @@ package tech.pegasys.pantheon.ethereum.jsonrpc.internal.filter;
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());
}
}

Loading…
Cancel
Save