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

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


The following commit(s) were added to refs/heads/main by this push:
     new fe55786  make the processor use a transient state
     new 55f2b9b  Merge pull request #385 from 
atoulme/use_transient_state_block_processor
fe55786 is described below

commit fe55786e75eadbfee85e90d87222d283440fefa9
Author: Antoine Toulme <anto...@lunar-ocean.com>
AuthorDate: Thu Mar 17 00:02:35 2022 -0700

    make the processor use a transient state
---
 .../apache/tuweni/blockprocessor/BlockProcessor.kt | 25 ++++++++++++----------
 .../org/apache/tuweni/blockprocessor/ProtoBlock.kt |  8 ++++++-
 .../tuweni/eth/repository/BlockchainRepository.kt  |  2 ++
 .../tuweni/eth/repository/StateRepository.kt       |  7 ++++++
 .../eth/repository/TransientStateRepository.kt     |  2 ++
 5 files changed, 32 insertions(+), 12 deletions(-)

diff --git 
a/eth-blockprocessor/src/main/kotlin/org/apache/tuweni/blockprocessor/BlockProcessor.kt
 
b/eth-blockprocessor/src/main/kotlin/org/apache/tuweni/blockprocessor/BlockProcessor.kt
index baf537a..2846fdb 100644
--- 
a/eth-blockprocessor/src/main/kotlin/org/apache/tuweni/blockprocessor/BlockProcessor.kt
+++ 
b/eth-blockprocessor/src/main/kotlin/org/apache/tuweni/blockprocessor/BlockProcessor.kt
@@ -24,6 +24,7 @@ import org.apache.tuweni.eth.LogsBloomFilter
 import org.apache.tuweni.eth.Transaction
 import org.apache.tuweni.eth.TransactionReceipt
 import org.apache.tuweni.eth.repository.BlockchainRepository
+import org.apache.tuweni.eth.repository.TransientStateRepository
 import org.apache.tuweni.evm.EVMExecutionStatusCode
 import org.apache.tuweni.evm.EthereumVirtualMachine
 import org.apache.tuweni.evm.impl.EvmVmImpl
@@ -42,6 +43,7 @@ import java.time.Instant
 class BlockProcessor {
 
   suspend fun execute(parentBlock: Block, transactions: List<Transaction>, 
repository: BlockchainRepository): ProtoBlock {
+    val stateChanges = TransientStateRepository(repository)
     val vm = EthereumVirtualMachine(repository, EvmVmImpl::create)
     vm.start()
     var index = 0L
@@ -70,10 +72,10 @@ class BlockProcessor {
           UInt256.ONE,
           Wei.valueOf(0),
           Hash.fromBytes(MerkleTrie.EMPTY_TRIE_ROOT_HASH),
-          org.apache.tuweni.eth.Hash.hash(tx.payload)
+          Hash.hash(tx.payload)
         )
-        repository.storeAccount(contractAddress, state)
-        repository.storeCode(tx.payload)
+        stateChanges.storeAccount(contractAddress, state)
+        stateChanges.storeCode(tx.payload)
         val receipt = TransactionReceipt(
           1,
           0, // TODO
@@ -84,7 +86,7 @@ class BlockProcessor {
         receiptsTrie.put(indexKey, receipt.toBytes())
         counter++
       } else {
-        val code = repository.getAccountCode(tx.to!!)
+        val code = stateChanges.getAccountCode(tx.to!!)
         val result = vm.execute(
           tx.sender!!,
           tx.to!!,
@@ -103,20 +105,20 @@ class BlockProcessor {
           throw Exception("invalid transaction result")
         }
         for (balanceChange in result.changes.getBalanceChanges()) {
-          val state = repository.getAccount(balanceChange.key)?.let {
+          val state = stateChanges.getAccount(balanceChange.key)?.let {
             AccountState(it.nonce, balanceChange.value, it.storageRoot, 
it.codeHash)
-          } ?: repository.newAccountState()
-          repository.storeAccount(balanceChange.key, state)
+          } ?: stateChanges.newAccountState()
+          stateChanges.storeAccount(balanceChange.key, state)
         }
 
         for (storageChange in result.changes.getAccountChanges()) {
           for (oneStorageChange in storageChange.value) {
-            repository.storeAccountValue(storageChange.key, 
oneStorageChange.key, oneStorageChange.value)
+            stateChanges.storeAccountValue(storageChange.key, 
oneStorageChange.key, oneStorageChange.value)
           }
         }
 
         for (accountToDestroy in result.changes.accountsToDestroy()) {
-          repository.destroyAccount(accountToDestroy)
+          stateChanges.destroyAccount(accountToDestroy)
         }
         for (log in result.changes.getLogs()) {
           bloomFilter.insertLog(log)
@@ -143,7 +145,7 @@ class BlockProcessor {
     val block = ProtoBlock(
       SealableHeader(
         parentBlock.header.hash,
-        Hash.fromBytes(repository.worldState!!.rootHash()),
+        Hash.fromBytes(stateChanges.stateRootHash()),
         Hash.fromBytes(transactionsTrie.rootHash()),
         Hash.fromBytes(receiptsTrie.rootHash()),
         bloomFilter.toBytes(),
@@ -152,7 +154,8 @@ class BlockProcessor {
         allGasUsed,
       ),
       ProtoBlockBody(transactions),
-      allReceipts
+      allReceipts,
+      stateChanges
     )
     return block
   }
diff --git 
a/eth-blockprocessor/src/main/kotlin/org/apache/tuweni/blockprocessor/ProtoBlock.kt
 
b/eth-blockprocessor/src/main/kotlin/org/apache/tuweni/blockprocessor/ProtoBlock.kt
index c747840..39984ab 100644
--- 
a/eth-blockprocessor/src/main/kotlin/org/apache/tuweni/blockprocessor/ProtoBlock.kt
+++ 
b/eth-blockprocessor/src/main/kotlin/org/apache/tuweni/blockprocessor/ProtoBlock.kt
@@ -24,6 +24,7 @@ import org.apache.tuweni.eth.BlockHeader
 import org.apache.tuweni.eth.Hash
 import org.apache.tuweni.eth.Transaction
 import org.apache.tuweni.eth.TransactionReceipt
+import org.apache.tuweni.eth.repository.TransientStateRepository
 import org.apache.tuweni.rlp.RLP
 import org.apache.tuweni.units.bigints.UInt256
 import org.apache.tuweni.units.bigints.UInt64
@@ -94,7 +95,12 @@ data class ProtoBlockBody(val transactions: 
List<Transaction>) {
  *
  * Proto-blocks are produced when transactions are executed, and can be turned 
into full valid blocks.
  */
-class ProtoBlock(val header: SealableHeader, val body: ProtoBlockBody, val 
transactionReceipts: List<TransactionReceipt>) {
+class ProtoBlock(
+  val header: SealableHeader,
+  val body: ProtoBlockBody,
+  val transactionReceipts: List<TransactionReceipt>,
+  val stateChanges: TransientStateRepository
+) {
 
   fun toBlock(
     ommers: List<BlockHeader>,
diff --git 
a/eth-repository/src/main/kotlin/org/apache/tuweni/eth/repository/BlockchainRepository.kt
 
b/eth-repository/src/main/kotlin/org/apache/tuweni/eth/repository/BlockchainRepository.kt
index a0df58d..066cbd8 100644
--- 
a/eth-repository/src/main/kotlin/org/apache/tuweni/eth/repository/BlockchainRepository.kt
+++ 
b/eth-repository/src/main/kotlin/org/apache/tuweni/eth/repository/BlockchainRepository.kt
@@ -580,4 +580,6 @@ class BlockchainRepository(
   override suspend fun storeCode(code: Bytes) {
     worldState!!.put(Hash.hash(code), code)
   }
+
+  override fun stateRootHash(): Bytes32 = worldState!!.rootHash()
 }
diff --git 
a/eth-repository/src/main/kotlin/org/apache/tuweni/eth/repository/StateRepository.kt
 
b/eth-repository/src/main/kotlin/org/apache/tuweni/eth/repository/StateRepository.kt
index ee89ced..d70f6a9 100644
--- 
a/eth-repository/src/main/kotlin/org/apache/tuweni/eth/repository/StateRepository.kt
+++ 
b/eth-repository/src/main/kotlin/org/apache/tuweni/eth/repository/StateRepository.kt
@@ -102,4 +102,11 @@ interface StateRepository {
    * @param code the code to store
    */
   suspend fun storeCode(code: Bytes)
+
+  /**
+   * Computes the root hash of the state
+   *
+   * @return the root hash of the state
+   */
+  fun stateRootHash(): Bytes32
 }
diff --git 
a/eth-repository/src/main/kotlin/org/apache/tuweni/eth/repository/TransientStateRepository.kt
 
b/eth-repository/src/main/kotlin/org/apache/tuweni/eth/repository/TransientStateRepository.kt
index 61bc4b5..3fac879 100644
--- 
a/eth-repository/src/main/kotlin/org/apache/tuweni/eth/repository/TransientStateRepository.kt
+++ 
b/eth-repository/src/main/kotlin/org/apache/tuweni/eth/repository/TransientStateRepository.kt
@@ -120,6 +120,8 @@ class TransientStateRepository(val repository: 
BlockchainRepository) : StateRepo
     transientWorldState.put(Hash.hash(code), code)
   }
 
+  override fun stateRootHash(): Bytes32 = transientWorldState.rootHash()
+
   /**
    * Apply changes of this repository to the blockchain repository.
    */

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

Reply via email to