Skip to content

Commit

Permalink
clean refactor
Browse files Browse the repository at this point in the history
Signed-off-by: Karim TAAM <[email protected]>
  • Loading branch information
matkt committed Nov 28, 2023
1 parent e4e3ab1 commit caa73c3
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,16 @@
*/
package org.hyperledger.besu.ethereum.privacy.storage;

import org.hyperledger.besu.ethereum.WorldStateKeyValueStorage;
import org.hyperledger.besu.ethereum.worldstate.WorldStatePreimageStorage;
import org.hyperledger.besu.ethereum.worldstate.WorldStateStorageCoordinator;

import java.io.Closeable;

public interface PrivacyStorageProvider extends Closeable {

WorldStateKeyValueStorage createWorldStateStorage();

WorldStateStorageCoordinator createWorldStateStorageCoordinator();

WorldStatePreimageStorage createWorldStatePreimageStorage();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
*/
package org.hyperledger.besu.ethereum.privacy.storage.keyvalue;

import org.hyperledger.besu.ethereum.WorldStateKeyValueStorage;
import org.hyperledger.besu.ethereum.forest.storage.ForestWorldStateKeyValueStorage;
import org.hyperledger.besu.ethereum.privacy.storage.LegacyPrivateStateKeyValueStorage;
import org.hyperledger.besu.ethereum.privacy.storage.LegacyPrivateStateStorage;
Expand Down Expand Up @@ -46,10 +47,14 @@ public PrivacyKeyValueStorageProvider(
this.factoryVersion = factoryVersion;
}

@Override
public WorldStateKeyValueStorage createWorldStateStorage() {
return new ForestWorldStateKeyValueStorage(privateWorldStateKeyValueStorage);
}

@Override
public WorldStateStorageCoordinator createWorldStateStorageCoordinator() {
return new WorldStateStorageCoordinator(
new ForestWorldStateKeyValueStorage(privateWorldStateKeyValueStorage));
return new WorldStateStorageCoordinator(createWorldStateStorage());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
*/
package org.hyperledger.besu.ethereum.core;

import org.hyperledger.besu.ethereum.WorldStateKeyValueStorage;
import org.hyperledger.besu.ethereum.forest.ForestWorldStateArchive;
import org.hyperledger.besu.ethereum.forest.storage.ForestWorldStateKeyValueStorage;
import org.hyperledger.besu.ethereum.forest.worldview.ForestMutableWorldState;
Expand Down Expand Up @@ -42,15 +43,19 @@ public static WorldStateArchive createInMemoryWorldStateArchive() {
public static MutableWorldState createInMemoryWorldState() {
final InMemoryPrivacyStorageProvider provider = new InMemoryPrivacyStorageProvider();
return new ForestMutableWorldState(
provider.createWorldStateStorageCoordinator().worldStateKeyValueStorage(),
provider.createWorldStateStorage(),
provider.createWorldStatePreimageStorage(),
EvmConfiguration.DEFAULT);
}

@Override
public WorldStateKeyValueStorage createWorldStateStorage() {
return new ForestWorldStateKeyValueStorage(new InMemoryKeyValueStorage());
}

@Override
public WorldStateStorageCoordinator createWorldStateStorageCoordinator() {
return new WorldStateStorageCoordinator(
new ForestWorldStateKeyValueStorage(new InMemoryKeyValueStorage()));
return new WorldStateStorageCoordinator(createWorldStateStorage());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,13 @@ public static MerkleTrie<Bytes, Bytes> generateTrie(
final StateTrieAccountValue accountValue =
new StateTrieAccountValue(1L, Wei.of(2L), Hash.wrap(storageTrie.getRootHash()), codeHash);
accountStateTrie.put(accounts.get(i), RLP.encode(accountValue::writeTo));
int finalI = i;
applyForStrategy(
updater,
onBonsai -> {
onBonsai.putAccountInfoState(accounts.get(finalI), RLP.encode(accountValue::writeTo));
onBonsai.putAccountInfoState(
accounts.get(accountIndex), RLP.encode(accountValue::writeTo));
accountStateTrie.commit(onBonsai::putAccountStateTrieNode);
onBonsai.putCode(accounts.get(finalI), codeHash, code);
onBonsai.putCode(accounts.get(accountIndex), codeHash, code);
},
onForest -> {
accountStateTrie.commit(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,17 +136,15 @@ protected int doPersist(
final AtomicReference<StackTrie.FlatDatabaseUpdater> flatDatabaseUpdater =
new AtomicReference<>(noop());

if (!snapSyncState.isHealFlatDatabaseInProgress()) {
// we have a flat DB only with Bonsai
worldStateStorageCoordinator.applyOnMatchingFlatMode(
FlatDbMode.FULL,
bonsaiWorldStateStorageStrategy -> {
flatDatabaseUpdater.set(
(key, value) ->
((BonsaiWorldStateKeyValueStorage.Updater) updater)
.putAccountInfoState(Hash.wrap(key), value));
});
}
// we have a flat DB only with Bonsai
worldStateStorageCoordinator.applyOnMatchingFlatMode(
FlatDbMode.FULL,
bonsaiWorldStateStorageStrategy -> {
flatDatabaseUpdater.set(
(key, value) ->
((BonsaiWorldStateKeyValueStorage.Updater) updater)
.putAccountInfoState(Hash.wrap(key), value));
});

stackTrie.commit(flatDatabaseUpdater.get(), nodeUpdater);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ protected int doPersist(
onBonsai.putCode(Hash.wrap(accountHash), codeHash, code);
},
onForest -> {
onForest.putCode(Hash.wrap(accountHash), code);
onForest.putCode(codeHash, code);
});
downloadState.getMetricsManager().notifyCodeDownloaded();
return possibleParent
Expand Down

0 comments on commit caa73c3

Please sign in to comment.