forked from hyperledger/besu
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
first stab at bonsai reference test worldstate
Signed-off-by: garyschulte <garyschulte@gmail.com>
- Loading branch information
1 parent
6ac03af
commit b3f5267
Showing
4 changed files
with
177 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
151 changes: 151 additions & 0 deletions
151
...main/java/org/hyperledger/besu/ethereum/referencetests/BonsaiReferenceTestWorldState.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
/* | ||
* Copyright Hyperledger Besu Contributors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
package org.hyperledger.besu.ethereum.referencetests; | ||
|
||
import org.hyperledger.besu.datatypes.Address; | ||
import org.hyperledger.besu.datatypes.Hash; | ||
import org.hyperledger.besu.ethereum.bonsai.cache.CachedMerkleTrieLoader; | ||
import org.hyperledger.besu.ethereum.bonsai.storage.BonsaiWorldStateKeyValueStorage; | ||
import org.hyperledger.besu.ethereum.bonsai.trielog.TrieLogAddedEvent; | ||
import org.hyperledger.besu.ethereum.bonsai.trielog.TrieLogFactoryImpl; | ||
import org.hyperledger.besu.ethereum.bonsai.trielog.TrieLogManager; | ||
import org.hyperledger.besu.ethereum.bonsai.worldview.BonsaiWorldState; | ||
import org.hyperledger.besu.ethereum.bonsai.worldview.BonsaiWorldStateUpdateAccumulator; | ||
import org.hyperledger.besu.ethereum.core.BlockHeader; | ||
import org.hyperledger.besu.ethereum.core.InMemoryKeyValueStorageProvider; | ||
import org.hyperledger.besu.evm.account.MutableAccount; | ||
import org.hyperledger.besu.evm.worldstate.WorldUpdater; | ||
import org.hyperledger.besu.metrics.ObservableMetricsSystem; | ||
import org.hyperledger.besu.metrics.noop.NoOpMetricsSystem; | ||
import org.hyperledger.besu.plugin.services.trielogs.TrieLog; | ||
import org.hyperledger.besu.plugin.services.trielogs.TrieLogEvent; | ||
import org.hyperledger.besu.plugin.services.trielogs.TrieLogFactory; | ||
import org.hyperledger.besu.util.Subscribers; | ||
|
||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.function.Function; | ||
|
||
import com.fasterxml.jackson.annotation.JsonCreator; | ||
import org.apache.tuweni.units.bigints.UInt256; | ||
|
||
public class BonsaiReferenceTestWorldState extends BonsaiWorldState { | ||
|
||
protected BonsaiReferenceTestWorldState( | ||
final BonsaiWorldStateKeyValueStorage worldStateStorage, | ||
final CachedMerkleTrieLoader cachedMerkleTrieLoader, | ||
final TrieLogManager trieLogManager) { | ||
super(worldStateStorage, cachedMerkleTrieLoader, trieLogManager); | ||
} | ||
|
||
@JsonCreator | ||
public static BonsaiReferenceTestWorldState create( | ||
final Map<String, ReferenceTestWorldState.AccountMock> accounts) { | ||
final ObservableMetricsSystem metricsSystem = new NoOpMetricsSystem(); | ||
final CachedMerkleTrieLoader cachedMerkleTrieLoader = new CachedMerkleTrieLoader(metricsSystem); | ||
final TrieLogManager trieLogManager = new NoOpTrieLogManager(); | ||
final BonsaiWorldStateKeyValueStorage worldStateStorage = | ||
new BonsaiWorldStateKeyValueStorage(new InMemoryKeyValueStorageProvider(), metricsSystem); | ||
final BonsaiReferenceTestWorldState worldState = | ||
new BonsaiReferenceTestWorldState( | ||
worldStateStorage, cachedMerkleTrieLoader, trieLogManager); | ||
|
||
final WorldUpdater updater = worldState.updater(); | ||
for (final Map.Entry<String, ReferenceTestWorldState.AccountMock> entry : accounts.entrySet()) { | ||
insertAccount(updater, Address.fromHexString(entry.getKey()), entry.getValue()); | ||
} | ||
updater.commit(); | ||
return worldState; | ||
} | ||
|
||
static void insertAccount( | ||
final WorldUpdater updater, | ||
final Address address, | ||
final ReferenceTestWorldState.AccountMock toCopy) { | ||
final MutableAccount account = updater.getOrCreate(address).getMutable(); | ||
account.setNonce(toCopy.getNonce()); | ||
account.setBalance(toCopy.getBalance()); | ||
account.setCode(toCopy.getCode()); | ||
for (final Map.Entry<UInt256, UInt256> entry : toCopy.getStorage().entrySet()) { | ||
account.setStorageValue(entry.getKey(), entry.getValue()); | ||
} | ||
} | ||
|
||
static class NoOpTrieLogManager implements TrieLogManager { | ||
private final Subscribers<TrieLogEvent.TrieLogObserver> trieLogObservers = Subscribers.create(); | ||
private final TrieLogFactory trieLogFactory = new TrieLogFactoryImpl(); | ||
|
||
@Override | ||
public void saveTrieLog( | ||
final BonsaiWorldStateUpdateAccumulator localUpdater, | ||
final Hash forWorldStateRootHash, | ||
final BlockHeader forBlockHeader, | ||
final BonsaiWorldState forWorldState) { | ||
// notify trie log added observers, synchronously | ||
TrieLog trieLog = trieLogFactory.create(localUpdater, forBlockHeader); | ||
trieLogObservers.forEach(o -> o.onTrieLogAdded(new TrieLogAddedEvent(trieLog))); | ||
} | ||
|
||
@Override | ||
public void addCachedLayer( | ||
final BlockHeader blockHeader, | ||
final Hash worldStateRootHash, | ||
final BonsaiWorldState forWorldState) {} | ||
|
||
@Override | ||
public boolean containWorldStateStorage(final Hash blockHash) { | ||
return false; | ||
} | ||
|
||
@Override | ||
public Optional<BonsaiWorldState> getWorldState(final Hash blockHash) { | ||
return Optional.empty(); | ||
} | ||
|
||
@Override | ||
public Optional<BonsaiWorldState> getNearestWorldState(final BlockHeader blockHeader) { | ||
return Optional.empty(); | ||
} | ||
|
||
@Override | ||
public Optional<BonsaiWorldState> getHeadWorldState( | ||
final Function<Hash, Optional<BlockHeader>> hashBlockHeaderFunction) { | ||
return Optional.empty(); | ||
} | ||
|
||
@Override | ||
public long getMaxLayersToLoad() { | ||
return 0; | ||
} | ||
|
||
@Override | ||
public void reset() {} | ||
|
||
@Override | ||
public Optional<? extends TrieLog> getTrieLogLayer(final Hash blockHash) { | ||
return Optional.empty(); | ||
} | ||
|
||
@Override | ||
public synchronized long subscribe(final TrieLogEvent.TrieLogObserver sub) { | ||
return trieLogObservers.subscribe(sub); | ||
} | ||
|
||
@Override | ||
public synchronized void unsubscribe(final long id) { | ||
trieLogObservers.unsubscribe(id); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters