@@ -5,14 +5,15 @@ 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
14
15
import io .iohk .ethereum .transactions .PendingTransactionsManager .PendingTransactionsResponse
15
- import io .iohk .ethereum .utils .{BlockchainConfig , ByteStringUtils , Logger }
16
+ import io .iohk .ethereum .utils .{ByteStringUtils , Logger }
16
17
import monix .eval .Task
17
18
import monix .execution .Scheduler
18
19
import org .bouncycastle .util .encoders .Hex
@@ -154,10 +155,10 @@ class TestService(
154
155
// load the new genesis
155
156
val genesisDataLoader = new GenesisDataLoader (blockchain, newBlockchainConfig)
156
157
genesisDataLoader.loadGenesisData(genesisData)
157
-
158
158
// save account codes to world state
159
- storeGenesisAccountCodes(newBlockchainConfig, genesisData.alloc)
160
- storeGenesisAccountStorageData(newBlockchainConfig, genesisData.alloc)
159
+ storeGenesisAccountCodes(genesisData.alloc)
160
+ storeGenesisAccountStorageData(genesisData.alloc)
161
+
161
162
// update test ledger with new config
162
163
testLedgerWrapper.blockchainConfig = newBlockchainConfig
163
164
@@ -167,39 +168,23 @@ class TestService(
167
168
SetChainParamsResponse ().rightNow
168
169
}
169
170
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)
182
- }
183
-
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)
171
+ private def storeGenesisAccountCodes (accounts : Map [String , GenesisAccount ]): Unit =
172
+ accounts
173
+ .collect { case (_, GenesisAccount (_, _, Some (code), _, _)) => code }
174
+ .foreach { code => blockchain.storeEvmCode(kec256(code), code).commit() }
190
175
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
- })
176
+ private def storeGenesisAccountStorageData (accounts : Map [String , GenesisAccount ]): Unit = {
177
+ val emptyStorage = domain.EthereumUInt256Mpt .storageMpt(
178
+ Account .EmptyStorageRootHash ,
179
+ blockchain.getStateStorage.getBackingStorage(0 )
180
+ )
181
+ val storagesToPersist = accounts
182
+ .flatMap(pair => pair._2.storage)
183
+ .map(accountStorage => accountStorage.filterNot { case (_, v) => v.isZero })
184
+ .filter(_.nonEmpty)
201
185
202
- InMemoryWorldStateProxy .persistState(worldToPersist)
186
+ val toBigInts : ((UInt256 , UInt256 )) => (BigInt , BigInt ) = { case (a, b) => (a, b) }
187
+ storagesToPersist.foreach(storage => emptyStorage.update(Nil , storage.toSeq.map(toBigInts)))
203
188
}
204
189
205
190
def mineBlocks (request : MineBlocksRequest ): ServiceResponse [MineBlocksResponse ] = {
@@ -259,7 +244,7 @@ class TestService(
259
244
}
260
245
261
246
private def getBlockForMining (parentBlock : Block ): Task [PendingBlock ] = {
262
- implicit val timeout : Timeout = Timeout (5 .seconds)
247
+ implicit val timeout : Timeout = Timeout (20 .seconds)
263
248
pendingTransactionsManager
264
249
.askFor[PendingTransactionsResponse ](PendingTransactionsManager .GetPendingTransactions )
265
250
.timeout(timeout.duration)
0 commit comments