Skip to content

Commit 6d9e0b3

Browse files
author
Aurélien Richez
committed
simplify storage functions
1 parent 87730e2 commit 6d9e0b3

File tree

1 file changed

+21
-35
lines changed

1 file changed

+21
-35
lines changed

src/main/scala/io/iohk/ethereum/jsonrpc/TestService.scala

Lines changed: 21 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ import akka.util.{ByteString, Timeout}
55
import io.iohk.ethereum.blockchain.data.{GenesisAccount, GenesisData, GenesisDataLoader}
66
import io.iohk.ethereum.consensus.ConsensusConfig
77
import io.iohk.ethereum.consensus.blocks._
8-
import io.iohk.ethereum.{crypto, rlp}
8+
import io.iohk.ethereum.crypto.kec256
9+
import io.iohk.ethereum.{crypto, domain, rlp}
910
import io.iohk.ethereum.domain.Block._
10-
import io.iohk.ethereum.domain.{Address, Block, BlockchainImpl, UInt256}
11+
import io.iohk.ethereum.domain.{Account, Address, Block, BlockchainImpl, UInt256}
1112
import io.iohk.ethereum.ledger._
1213
import io.iohk.ethereum.testmode.{TestLedgerWrapper, TestmodeConsensus}
1314
import io.iohk.ethereum.transactions.PendingTransactionsManager
@@ -17,6 +18,7 @@ import monix.eval.Task
1718
import monix.execution.Scheduler
1819
import org.bouncycastle.util.encoders.Hex
1920
import io.iohk.ethereum.jsonrpc.JsonMethodsImplicits._
21+
import io.iohk.ethereum.mpt.MerklePatriciaTrie
2022
import io.iohk.ethereum.rlp.RLPList
2123

2224
import scala.concurrent.duration._
@@ -156,8 +158,8 @@ class TestService(
156158
genesisDataLoader.loadGenesisData(genesisData)
157159

158160
//save account codes to world state
159-
storeGenesisAccountCodes(newBlockchainConfig, genesisData.alloc)
160-
storeGenesisAccountStorageData(newBlockchainConfig, genesisData.alloc)
161+
storeGenesisAccountCodes(genesisData.alloc)
162+
storeGenesisAccountStorageData(genesisData.alloc)
161163
// update test ledger with new config
162164
testLedgerWrapper.blockchainConfig = newBlockchainConfig
163165

@@ -167,39 +169,23 @@ class TestService(
167169
SetChainParamsResponse().rightNow
168170
}
169171

170-
private def storeGenesisAccountCodes(config: BlockchainConfig, accounts: Map[String, GenesisAccount]): Unit = {
171-
val genesisBlock = blockchain.getBlockByNumber(0).get
172-
val world =
173-
blockchain.getWorldStateProxy(0, UInt256.Zero, genesisBlock.header.stateRoot, false, config.ethCompatibleStorage)
174-
175-
val accountsWithCodes = accounts.filter(pair => pair._2.code.isDefined)
176-
177-
val worldToPersist = accountsWithCodes.foldLeft(world)((world, addressAccountPair) => {
178-
world.saveCode(Address(addressAccountPair._1), addressAccountPair._2.code.get)
179-
})
180-
181-
InMemoryWorldStateProxy.persistState(worldToPersist)
172+
private def storeGenesisAccountCodes(accounts: Map[String, GenesisAccount]): Unit = {
173+
for {
174+
code <- accounts.map(pair => pair._2.code).collect { case Some(code) => code }
175+
} {
176+
blockchain.storeEvmCode(kec256(code), code).commit()
177+
}
182178
}
183179

184-
private def storeGenesisAccountStorageData(config: BlockchainConfig, accounts: Map[String, GenesisAccount]): Unit = {
185-
val genesisBlock = blockchain.getBlockByNumber(0).get
186-
val world =
187-
blockchain.getWorldStateProxy(0, UInt256.Zero, genesisBlock.header.stateRoot, false, config.ethCompatibleStorage)
188-
189-
val accountsWithStorageData = accounts.filter(pair => pair._2.storage.isDefined && pair._2.storage.get.nonEmpty)
190-
191-
val worldToPersist = accountsWithStorageData.foldLeft(world)((world, addressAccountPair) => {
192-
val address = Address(addressAccountPair._1)
193-
val emptyStorage = world.getStorage(address)
194-
val updatedStorage = addressAccountPair._2.storage.get.foldLeft(emptyStorage) { case (storage, (key, value)) =>
195-
storage.store(key, value)
196-
}
197-
val updatedWorld = world.saveStorage(Address(addressAccountPair._1), updatedStorage)
198-
updatedWorld.contractStorages.values.foreach(cs => cs.inner.nodeStorage.persist())
199-
updatedWorld
200-
})
201-
202-
InMemoryWorldStateProxy.persistState(worldToPersist)
180+
private def storeGenesisAccountStorageData(accounts: Map[String, GenesisAccount]): Unit = {
181+
val emptyStorage = domain.EthereumUInt256Mpt.storageMpt(
182+
Account.EmptyStorageRootHash,
183+
blockchain.getStateStorage.getBackingStorage(0)
184+
)
185+
val storagesToPersist = accounts.map(pair => pair._2.storage).collect { case Some(map) if map.nonEmpty => map }
186+
for { storage <- storagesToPersist } {
187+
storage.foldLeft(emptyStorage) { case (storage, (key, value)) => storage.put(key, value) }
188+
}
203189
}
204190

205191
def mineBlocks(request: MineBlocksRequest): ServiceResponse[MineBlocksResponse] = {

0 commit comments

Comments
 (0)