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/31 06:40:41 UTC

[incubator-tuweni] branch master updated: use completion handle

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 eef33c3  use completion handle
eef33c3 is described below

commit eef33c3abdafe9c20c503dade41f0cc15ddf98fa
Author: Antoine Toulme <an...@lunar-ocean.com>
AuthorDate: Sat May 30 23:40:25 2020 -0700

    use completion handle
---
 .../java/org/apache/tuweni/eth/genesis/GenesisFile.java | 17 +++++++++++------
 1 file changed, 11 insertions(+), 6 deletions(-)

diff --git a/eth/src/main/java/org/apache/tuweni/eth/genesis/GenesisFile.java b/eth/src/main/java/org/apache/tuweni/eth/genesis/GenesisFile.java
index ff05260..daf8b09 100644
--- a/eth/src/main/java/org/apache/tuweni/eth/genesis/GenesisFile.java
+++ b/eth/src/main/java/org/apache/tuweni/eth/genesis/GenesisFile.java
@@ -13,6 +13,7 @@
 package org.apache.tuweni.eth.genesis;
 
 import org.apache.tuweni.bytes.Bytes;
+import org.apache.tuweni.concurrent.AsyncCompletion;
 import org.apache.tuweni.eth.AccountState;
 import org.apache.tuweni.eth.Address;
 import org.apache.tuweni.eth.Block;
@@ -33,6 +34,8 @@ import java.util.Collections;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
 
 import com.fasterxml.jackson.core.JsonFactory;
 import com.fasterxml.jackson.core.JsonParser;
@@ -205,13 +208,15 @@ public class GenesisFile {
     Hash empty = Hash.hash(Bytes.EMPTY);
     MerklePatriciaTrie<AccountState> stateTree = new MerklePatriciaTrie<>(AccountState::toBytes);
 
+    List<AsyncCompletion> futures = new ArrayList<>();
+    for (Map.Entry<Address, Wei> entry : allocs.entrySet()) {
+      AccountState accountState = new AccountState(UInt256.ZERO, entry.getValue(), emptyHash, empty);
+      futures.add(stateTree.putAsync(Hash.hash(entry.getKey()), accountState));
+    }
     try {
-      for (Map.Entry<Address, Wei> entry : allocs.entrySet()) {
-        AccountState accountState = new AccountState(UInt256.ZERO, entry.getValue(), emptyHash, empty);
-        stateTree.putAsync(Hash.hash(entry.getKey()), accountState).join();
-      }
-    } catch (InterruptedException e) {
-      throw new RuntimeException(e);
+      AsyncCompletion.allOf(futures).join(10, TimeUnit.SECONDS);
+    } catch (InterruptedException | TimeoutException e) {
+      throw new RuntimeException("Creating the account states took more than 10 seconds.");
     }
     return new Block(
         new BlockHeader(


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