Skip to content

Commit a376f5c

Browse files
author
Aurélien Richez
committed
Add storage into genesis state
1 parent aafb637 commit a376f5c

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

src/main/scala/io/iohk/ethereum/blockchain/data/GenesisDataLoader.scala

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,14 +86,14 @@ class GenesisDataLoader(blockchain: Blockchain, blockchainConfig: BlockchainConf
8686
} yield ()
8787
}
8888

89-
def loadGenesisData(genesisData: GenesisData): Try[Unit] = {
89+
def loadGenesisData(genesisData: GenesisData, storageRootHashes: Map[Address, ByteString] = Map.empty): Try[Unit] = {
9090
import MerklePatriciaTrie.defaultByteArraySerializable
9191

9292
val stateStorage = blockchain.getStateStorage
9393
val storage = stateStorage.getReadOnlyStorage
9494
val initalRootHash = MerklePatriciaTrie.EmptyRootHash
9595

96-
val stateMptRootHash = getGenesisStateRoot(genesisData, initalRootHash, storage)
96+
val stateMptRootHash = getGenesisStateRoot(genesisData, initalRootHash, storage, storageRootHashes)
9797
val header: BlockHeader = prepareHeader(genesisData, stateMptRootHash)
9898

9999
log.debug(s"Prepared genesis header: $header")
@@ -122,20 +122,27 @@ class GenesisDataLoader(blockchain: Blockchain, blockchainConfig: BlockchainConf
122122
}
123123
}
124124

125-
private def getGenesisStateRoot(genesisData: GenesisData, initalRootHash: Array[Byte], storage: MptStorage) = {
125+
private def getGenesisStateRoot(
126+
genesisData: GenesisData,
127+
initalRootHash: Array[Byte],
128+
storage: MptStorage,
129+
storageRootHashes: Map[Address, ByteString]
130+
) = {
126131
import MerklePatriciaTrie.defaultByteArraySerializable
127132

128133
genesisData.alloc.zipWithIndex.foldLeft(initalRootHash) { case (rootHash, ((address, genesisAccount), _)) =>
129134
val mpt = MerklePatriciaTrie[Array[Byte], Account](rootHash, storage)
130135
val paddedAddress = address.reverse.padTo(addressLength, "0").reverse.mkString
136+
131137
val stateRoot = mpt
132138
.put(
133139
crypto.kec256(Hex.decode(paddedAddress)),
134140
Account(
135141
nonce = genesisAccount.nonce
136142
.getOrElse(blockchainConfig.accountStartNonce),
137143
balance = genesisAccount.balance,
138-
codeHash = genesisAccount.code.map(codeValue => crypto.kec256(codeValue)).getOrElse(Account.EmptyCodeHash)
144+
codeHash = genesisAccount.code.map(codeValue => crypto.kec256(codeValue)).getOrElse(Account.EmptyCodeHash),
145+
storageRoot = storageRootHashes.getOrElse(Address(Hex.decode(paddedAddress)), Account.EmptyStorageRootHash)
139146
)
140147
)
141148
.getRootHash

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,10 +157,14 @@ class TestService(
157157

158158
//save account codes to world state
159159
storeGenesisAccountCodes(newBlockchainConfig, genesisData.alloc)
160-
storeGenesisAccountStorageData(newBlockchainConfig, genesisData.alloc)
160+
val storesRootHash = storeGenesisAccountStorageData(newBlockchainConfig, genesisData.alloc)
161161
// update test ledger with new config
162162
testLedgerWrapper.blockchainConfig = newBlockchainConfig
163163

164+
// remove current genesis (Try because it may not exist)
165+
Try(blockchain.removeBlock(blockchain.genesisHeader.hash, withState = false))
166+
genesisDataLoader.loadGenesisData(genesisData, storesRootHash)
167+
164168
accountAddresses = genesisData.alloc.keys.toList
165169
accountRangeOffset = 0
166170

@@ -181,7 +185,7 @@ class TestService(
181185
InMemoryWorldStateProxy.persistState(worldToPersist)
182186
}
183187

184-
private def storeGenesisAccountStorageData(config: BlockchainConfig, accounts: Map[String, GenesisAccount]): Unit = {
188+
private def storeGenesisAccountStorageData(config: BlockchainConfig, accounts: Map[String, GenesisAccount]) = {
185189
val genesisBlock = blockchain.getBlockByNumber(0).get
186190
val world =
187191
blockchain.getWorldStateProxy(0, UInt256.Zero, genesisBlock.header.stateRoot, false, config.ethCompatibleStorage)
@@ -199,7 +203,9 @@ class TestService(
199203
updatedWorld
200204
})
201205

202-
InMemoryWorldStateProxy.persistState(worldToPersist)
206+
InMemoryWorldStateProxy.persistState(worldToPersist).contractStorages.map { case (address, values) =>
207+
address -> ByteString(values.inner.getRootHash)
208+
}
203209
}
204210

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

0 commit comments

Comments
 (0)