@@ -5,9 +5,10 @@ import akka.util.{ByteString, Timeout}
5
5
import io .iohk .ethereum .blockchain .data .{GenesisAccount , GenesisData , GenesisDataLoader }
6
6
import io .iohk .ethereum .consensus .ConsensusConfig
7
7
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 }
9
10
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 }
11
12
import io .iohk .ethereum .ledger ._
12
13
import io .iohk .ethereum .testmode .{TestLedgerWrapper , TestmodeConsensus }
13
14
import io .iohk .ethereum .transactions .PendingTransactionsManager
@@ -17,6 +18,7 @@ import monix.eval.Task
17
18
import monix .execution .Scheduler
18
19
import org .bouncycastle .util .encoders .Hex
19
20
import io .iohk .ethereum .jsonrpc .JsonMethodsImplicits ._
21
+ import io .iohk .ethereum .mpt .MerklePatriciaTrie
20
22
import io .iohk .ethereum .rlp .RLPList
21
23
22
24
import scala .concurrent .duration ._
@@ -156,8 +158,8 @@ class TestService(
156
158
genesisDataLoader.loadGenesisData(genesisData)
157
159
158
160
// save account codes to world state
159
- storeGenesisAccountCodes(newBlockchainConfig, genesisData.alloc)
160
- storeGenesisAccountStorageData(newBlockchainConfig, genesisData.alloc)
161
+ storeGenesisAccountCodes(genesisData.alloc)
162
+ storeGenesisAccountStorageData(genesisData.alloc)
161
163
// update test ledger with new config
162
164
testLedgerWrapper.blockchainConfig = newBlockchainConfig
163
165
@@ -167,39 +169,23 @@ class TestService(
167
169
SetChainParamsResponse ().rightNow
168
170
}
169
171
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
+ }
182
178
}
183
179
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
+ }
203
189
}
204
190
205
191
def mineBlocks (request : MineBlocksRequest ): ServiceResponse [MineBlocksResponse ] = {
0 commit comments