You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tuweni.apache.org by to...@apache.org on 2020/05/30 22:24:27 UTC

[incubator-tuweni] branch master updated: Use UInt64 instead of bytes for the block header nonce

This is an automated email from the ASF dual-hosted git repository.

toulmean pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-tuweni.git


The following commit(s) were added to refs/heads/master by this push:
     new a939154  Use UInt64 instead of bytes for the block header nonce
a939154 is described below

commit a93915406c58861e0b2a0a396690fa50bff1f844
Author: Antoine Toulme <an...@lunar-ocean.com>
AuthorDate: Sat May 30 15:24:12 2020 -0700

    Use UInt64 instead of bytes for the block header nonce
---
 .../tuweni/eth/reference/BlockRLPTestSuite.java    |  3 +-
 .../tuweni/eth/repository/BlockchainIndexTest.kt   | 11 ++++---
 .../eth/repository/BlockchainRepositoryTest.kt     | 37 +++++++++++-----------
 .../apache/tuweni/les/LESSubProtocolHandlerTest.kt |  5 +--
 .../kotlin/org/apache/tuweni/les/MessagesTest.kt   |  3 +-
 .../org/apache/tuweni/units/bigints/UInt64.java    |  9 ++++++
 6 files changed, 41 insertions(+), 27 deletions(-)

diff --git a/eth-reference-tests/src/test/java/org/apache/tuweni/eth/reference/BlockRLPTestSuite.java b/eth-reference-tests/src/test/java/org/apache/tuweni/eth/reference/BlockRLPTestSuite.java
index a69a2e6..9e53dd8 100644
--- a/eth-reference-tests/src/test/java/org/apache/tuweni/eth/reference/BlockRLPTestSuite.java
+++ b/eth-reference-tests/src/test/java/org/apache/tuweni/eth/reference/BlockRLPTestSuite.java
@@ -26,6 +26,7 @@ import org.apache.tuweni.eth.Transaction;
 import org.apache.tuweni.io.Resources;
 import org.apache.tuweni.junit.BouncyCastleExtension;
 import org.apache.tuweni.units.bigints.UInt256;
+import org.apache.tuweni.units.bigints.UInt64;
 import org.apache.tuweni.units.ethereum.Gas;
 import org.apache.tuweni.units.ethereum.Wei;
 
@@ -106,7 +107,7 @@ class BlockRLPTestSuite {
         Instant.ofEpochSecond(Bytes.fromHexString((String) headerData.get("timestamp")).toLong()),
         Bytes.fromHexString((String) headerData.get("extraData")),
         Hash.fromHexString((String) headerData.get("mixHash")),
-        Bytes.fromHexString((String) headerData.get("nonce")));
+        UInt64.fromHexString((String) headerData.get("nonce")));
   }
 
   @SuppressWarnings({"unchecked", "rawtypes"})
diff --git a/eth-repository/src/test/kotlin/org/apache/tuweni/eth/repository/BlockchainIndexTest.kt b/eth-repository/src/test/kotlin/org/apache/tuweni/eth/repository/BlockchainIndexTest.kt
index 92c6ce4..ecafc16 100644
--- a/eth-repository/src/test/kotlin/org/apache/tuweni/eth/repository/BlockchainIndexTest.kt
+++ b/eth-repository/src/test/kotlin/org/apache/tuweni/eth/repository/BlockchainIndexTest.kt
@@ -39,6 +39,7 @@ import org.apache.lucene.search.TermQuery
 import org.apache.lucene.search.TopScoreDocCollector
 import org.apache.lucene.store.Directory
 import org.apache.lucene.util.BytesRef
+import org.apache.tuweni.units.bigints.UInt64
 import org.junit.jupiter.api.Assertions.assertEquals
 import org.junit.jupiter.api.Test
 import org.junit.jupiter.api.extension.ExtendWith
@@ -69,7 +70,7 @@ internal class BlockchainIndexTest {
       Instant.now().truncatedTo(ChronoUnit.SECONDS),
       Bytes.of(2, 3, 4),
       Hash.fromBytes(Bytes32.random()),
-      Bytes32.random()
+      UInt64.random()
     )
     blockchainIndex.index { it.indexBlockHeader(header) }
 
@@ -101,7 +102,7 @@ internal class BlockchainIndexTest {
       Instant.now().truncatedTo(ChronoUnit.SECONDS),
       Bytes.of(2, 3, 4),
       Hash.fromBytes(Bytes32.random()),
-      Bytes32.random()
+      UInt64.random()
     )
     blockchainIndex.index { w -> w.indexBlockHeader(header) }
 
@@ -132,7 +133,7 @@ internal class BlockchainIndexTest {
       Instant.now().plusSeconds(30).truncatedTo(ChronoUnit.SECONDS),
       Bytes.of(2, 3, 4, 5, 6, 7, 8, 9, 10),
       Hash.fromBytes(Bytes32.random()),
-      Bytes32.random()
+      UInt64.random()
     )
     blockchainIndex.index { it.indexBlockHeader(header) }
 
@@ -230,7 +231,7 @@ internal class BlockchainIndexTest {
       Instant.now().truncatedTo(ChronoUnit.SECONDS),
       Bytes.of(2, 3, 4),
       Hash.fromBytes(Bytes32.random()),
-      Bytes32.random()
+      UInt64.random()
     )
     blockchainIndex.index { w -> w.indexBlockHeader(header) }
     assertEquals(UInt256.valueOf(1), blockchainIndex.totalDifficulty(header.hash))
@@ -250,7 +251,7 @@ internal class BlockchainIndexTest {
       Instant.now().truncatedTo(ChronoUnit.SECONDS),
       Bytes.of(2, 3, 4),
       Hash.fromBytes(Bytes32.random()),
-      Bytes32.random()
+      UInt64.random()
     )
 
     blockchainIndex.index { w -> w.indexBlockHeader(childHeader) }
diff --git a/eth-repository/src/test/kotlin/org/apache/tuweni/eth/repository/BlockchainRepositoryTest.kt b/eth-repository/src/test/kotlin/org/apache/tuweni/eth/repository/BlockchainRepositoryTest.kt
index 98ada59..86355f6 100644
--- a/eth-repository/src/test/kotlin/org/apache/tuweni/eth/repository/BlockchainRepositoryTest.kt
+++ b/eth-repository/src/test/kotlin/org/apache/tuweni/eth/repository/BlockchainRepositoryTest.kt
@@ -37,6 +37,7 @@ import org.apache.tuweni.units.bigints.UInt256
 import org.apache.tuweni.units.ethereum.Gas
 import org.apache.tuweni.units.ethereum.Wei
 import org.apache.lucene.index.IndexWriter
+import org.apache.tuweni.units.bigints.UInt64
 import org.junit.jupiter.api.Assertions.assertEquals
 import org.junit.jupiter.api.Test
 import org.junit.jupiter.api.extension.ExtendWith
@@ -64,7 +65,7 @@ internal class BlockchainRepositoryTest {
       Instant.now().plusSeconds(30).truncatedTo(ChronoUnit.SECONDS),
       Bytes.of(2, 3, 4, 5, 6, 7, 8, 9, 10),
       Hash.fromBytes(Bytes32.random()),
-      Bytes32.random()
+      UInt64.random()
     )
     val genesisBlock = Block(genesisHeader, BlockBody(emptyList(), emptyList()))
     val repo = BlockchainRepository
@@ -91,7 +92,7 @@ internal class BlockchainRepositoryTest {
       Instant.now().truncatedTo(ChronoUnit.SECONDS),
       Bytes.of(2, 3, 4),
       Hash.fromBytes(Bytes32.random()),
-      Bytes32.random()
+      UInt64.random()
     )
     val body = BlockBody(
       listOf(
@@ -132,7 +133,7 @@ internal class BlockchainRepositoryTest {
       Instant.now().plusSeconds(30).truncatedTo(ChronoUnit.SECONDS),
       Bytes.of(2, 3, 4, 5, 6, 7, 8, 9, 10),
       Hash.fromBytes(Bytes32.random()),
-      Bytes32.random()
+      UInt64.random()
     )
     val genesisBlock = Block(genesisHeader, BlockBody(emptyList(), emptyList()))
     val repo = BlockchainRepository
@@ -160,7 +161,7 @@ internal class BlockchainRepositoryTest {
       Instant.now().truncatedTo(ChronoUnit.SECONDS),
       Bytes.of(2, 3, 4),
       Hash.fromBytes(Bytes32.random()),
-      Bytes32.random()
+      UInt64.random()
     )
     val biggerNumber = BlockHeader(
       header.getHash(),
@@ -177,7 +178,7 @@ internal class BlockchainRepositoryTest {
       Instant.now().truncatedTo(ChronoUnit.SECONDS),
       Bytes.of(2, 3, 4),
       Hash.fromBytes(Bytes32.random()),
-      Bytes32.random()
+      UInt64.random()
     )
     val biggerNumber2 = BlockHeader(
       biggerNumber.getHash(),
@@ -194,7 +195,7 @@ internal class BlockchainRepositoryTest {
       Instant.now().truncatedTo(ChronoUnit.SECONDS),
       Bytes.of(2, 3, 4),
       Hash.fromBytes(Bytes32.random()),
-      Bytes32.random()
+      UInt64.random()
     )
     val biggerNumber3 = BlockHeader(
       biggerNumber2.getHash(),
@@ -211,7 +212,7 @@ internal class BlockchainRepositoryTest {
       Instant.now().truncatedTo(ChronoUnit.SECONDS),
       Bytes.of(2, 3, 4),
       Hash.fromBytes(Bytes32.random()),
-      Bytes32.random()
+      UInt64.random()
     )
 
     repo.storeBlockHeader(header)
@@ -240,7 +241,7 @@ internal class BlockchainRepositoryTest {
       Instant.now().plusSeconds(30).truncatedTo(ChronoUnit.SECONDS),
       Bytes.of(2, 3, 4, 5, 6, 7, 8, 9, 10),
       Hash.fromBytes(Bytes32.random()),
-      Bytes32.random()
+      UInt64.random()
     )
     val genesisBlock = Block(genesisHeader, BlockBody(emptyList(), emptyList()))
     val repo = BlockchainRepository.init(
@@ -267,7 +268,7 @@ internal class BlockchainRepositoryTest {
       Instant.now().truncatedTo(ChronoUnit.SECONDS),
       Bytes.of(2, 3, 4),
       Hash.fromBytes(Bytes32.random()),
-      Bytes32.random()
+      UInt64.random()
     )
     val biggerNumber = BlockHeader(
       header.getHash(),
@@ -284,7 +285,7 @@ internal class BlockchainRepositoryTest {
       Instant.now().truncatedTo(ChronoUnit.SECONDS),
       Bytes.of(2, 3, 4),
       Hash.fromBytes(Bytes32.random()),
-      Bytes32.random()
+      UInt64.random()
     )
     val biggerNumber2 = BlockHeader(
       biggerNumber.getHash(),
@@ -301,7 +302,7 @@ internal class BlockchainRepositoryTest {
       Instant.now().truncatedTo(ChronoUnit.SECONDS),
       Bytes.of(2, 3, 4),
       Hash.fromBytes(Bytes32.random()),
-      Bytes32.random()
+      UInt64.random()
     )
     val biggerNumber3 = BlockHeader(
       biggerNumber2.getHash(),
@@ -318,7 +319,7 @@ internal class BlockchainRepositoryTest {
       Instant.now().truncatedTo(ChronoUnit.SECONDS),
       Bytes.of(2, 3, 4),
       Hash.fromBytes(Bytes32.random()),
-      Bytes32.random()
+      UInt64.random()
     )
 
     repo.storeBlock(Block(header, BlockBody(emptyList(), emptyList())))
@@ -346,7 +347,7 @@ internal class BlockchainRepositoryTest {
       Instant.now().plusSeconds(30).truncatedTo(ChronoUnit.SECONDS),
       Bytes.of(2, 3, 4, 5, 6, 7, 8, 9, 10),
       Hash.fromBytes(Bytes32.random()),
-      Bytes32.random()
+      UInt64.random()
     )
     val genesisBlock = Block(genesisHeader, BlockBody(emptyList(), emptyList()))
     val repo = BlockchainRepository.init(
@@ -373,7 +374,7 @@ internal class BlockchainRepositoryTest {
       Instant.now().truncatedTo(ChronoUnit.SECONDS),
       Bytes.of(2, 3, 4),
       Hash.fromBytes(Bytes32.random()),
-      Bytes32.random()
+      UInt64.random()
     )
     val biggerNumber = BlockHeader(
       header.getHash(),
@@ -390,7 +391,7 @@ internal class BlockchainRepositoryTest {
       Instant.now().truncatedTo(ChronoUnit.SECONDS),
       Bytes.of(2, 3, 4),
       Hash.fromBytes(Bytes32.random()),
-      Bytes32.random()
+      UInt64.random()
     )
     val biggerNumber2 = BlockHeader(
       biggerNumber.getHash(),
@@ -407,7 +408,7 @@ internal class BlockchainRepositoryTest {
       Instant.now().truncatedTo(ChronoUnit.SECONDS),
       Bytes.of(2, 3, 4),
       Hash.fromBytes(Bytes32.random()),
-      Bytes32.random()
+      UInt64.random()
     )
     val biggerNumber3 = BlockHeader(
       biggerNumber2.getHash(),
@@ -424,7 +425,7 @@ internal class BlockchainRepositoryTest {
       Instant.now().truncatedTo(ChronoUnit.SECONDS),
       Bytes.of(2, 3, 4),
       Hash.fromBytes(Bytes32.random()),
-      Bytes32.random()
+      UInt64.random()
     )
 
     repo.storeBlock(Block(biggerNumber3, BlockBody(emptyList(), emptyList())))
@@ -452,7 +453,7 @@ internal class BlockchainRepositoryTest {
       Instant.now().plusSeconds(30).truncatedTo(ChronoUnit.SECONDS),
       Bytes.of(2, 3, 4, 5, 6, 7, 8, 9, 10),
       Hash.fromBytes(Bytes32.random()),
-      Bytes32.random()
+      UInt64.random()
     )
     val genesisBlock = Block(genesisHeader, BlockBody(emptyList(), emptyList()))
     val repo = BlockchainRepository.init(
diff --git a/les/src/test/kotlin/org/apache/tuweni/les/LESSubProtocolHandlerTest.kt b/les/src/test/kotlin/org/apache/tuweni/les/LESSubProtocolHandlerTest.kt
index 89a1f02..05b4d81 100644
--- a/les/src/test/kotlin/org/apache/tuweni/les/LESSubProtocolHandlerTest.kt
+++ b/les/src/test/kotlin/org/apache/tuweni/les/LESSubProtocolHandlerTest.kt
@@ -45,6 +45,7 @@ import org.apache.tuweni.units.ethereum.Gas
 import org.apache.tuweni.units.ethereum.Wei
 import org.apache.lucene.index.IndexWriter
 import org.apache.tuweni.concurrent.AsyncResult
+import org.apache.tuweni.units.bigints.UInt64
 import org.junit.jupiter.api.Assertions.assertEquals
 import org.junit.jupiter.api.Assertions.assertNotNull
 import org.junit.jupiter.api.Assertions.assertThrows
@@ -75,7 +76,7 @@ constructor() {
     Instant.now().truncatedTo(ChronoUnit.SECONDS),
     Bytes.of(2, 3, 4),
     Hash.fromBytes(Bytes32.random()),
-    Bytes32.random()
+    UInt64.random()
   )
   private val body = BlockBody(
     listOf(
@@ -369,7 +370,7 @@ constructor() {
         Instant.now().truncatedTo(ChronoUnit.SECONDS),
         Bytes.of(2, 3, 4),
         Hash.fromBytes(Bytes32.random()),
-        Bytes32.random()
+        UInt64.random()
       )
 
       handler.handleNewPeerConnection("abc").await()
diff --git a/les/src/test/kotlin/org/apache/tuweni/les/MessagesTest.kt b/les/src/test/kotlin/org/apache/tuweni/les/MessagesTest.kt
index 27e68fb..eb8c98c 100644
--- a/les/src/test/kotlin/org/apache/tuweni/les/MessagesTest.kt
+++ b/les/src/test/kotlin/org/apache/tuweni/les/MessagesTest.kt
@@ -29,6 +29,7 @@ import org.apache.tuweni.eth.Transaction
 import org.apache.tuweni.eth.TransactionReceipt
 import org.apache.tuweni.junit.BouncyCastleExtension
 import org.apache.tuweni.units.bigints.UInt256
+import org.apache.tuweni.units.bigints.UInt64
 import org.apache.tuweni.units.ethereum.Gas
 import org.apache.tuweni.units.ethereum.Wei
 import org.junit.jupiter.api.Assertions.assertEquals
@@ -125,7 +126,7 @@ internal class BlockHeadersMessageTest {
       Instant.now().truncatedTo(ChronoUnit.SECONDS),
       Bytes.of(2, 3, 4),
       Hash.fromBytes(Bytes32.random()),
-      Bytes32.random()
+      UInt64.random()
     )
     val message = BlockHeadersMessage(3L, 2L, listOf(header))
     val bytes = message.toBytes()
diff --git a/units/src/main/java/org/apache/tuweni/units/bigints/UInt64.java b/units/src/main/java/org/apache/tuweni/units/bigints/UInt64.java
index 0d2dda4..4e0fd01 100644
--- a/units/src/main/java/org/apache/tuweni/units/bigints/UInt64.java
+++ b/units/src/main/java/org/apache/tuweni/units/bigints/UInt64.java
@@ -60,6 +60,14 @@ public final class UInt64 implements UInt64Value<UInt64> {
   }
 
   /**
+   * Return a {@link UInt64} containing a random value.
+   * @return a {@link UInt64} containing a random value
+   */
+  public static UInt64 random() {
+    return UInt64.fromBytes(Bytes.random(8));
+  }
+
+  /**
    * Return a {@link UInt64} containing the specified value.
    *
    * @param value the value to create a {@link UInt64} for
@@ -556,4 +564,5 @@ public final class UInt64 implements UInt64Value<UInt64> {
     assert v > 0;
     return 63 - Long.numberOfLeadingZeros(v);
   }
+
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@tuweni.apache.org
For additional commands, e-mail: commits-help@tuweni.apache.org