-
Notifications
You must be signed in to change notification settings - Fork 859
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move hashing and preImage functions into a PreImageProxy class
[skip ci] Signed-off-by: garyschulte <garyschulte@gmail.com>
- Loading branch information
1 parent
1618f33
commit cab46aa
Showing
10 changed files
with
219 additions
and
122 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
86 changes: 85 additions & 1 deletion
86
.../core/src/main/java/org/hyperledger/besu/ethereum/bonsai/storage/BonsaiPreImageProxy.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 |
---|---|---|
@@ -1,2 +1,86 @@ | ||
package org.hyperledger.besu.ethereum.bonsai.storage;public interface BonsaiPreImageProxy { | ||
/* | ||
* 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.bonsai.storage; | ||
|
||
import org.hyperledger.besu.datatypes.Address; | ||
import org.hyperledger.besu.datatypes.Hash; | ||
import org.hyperledger.besu.ethereum.worldstate.WorldStatePreimageStorage; | ||
|
||
import java.util.Optional; | ||
|
||
import com.google.common.collect.BiMap; | ||
import com.google.common.collect.HashBiMap; | ||
import org.apache.tuweni.bytes.Bytes; | ||
import org.apache.tuweni.bytes.Bytes32; | ||
import org.apache.tuweni.units.bigints.UInt256; | ||
|
||
/** Acts as both a Hasher and PreImageStorage for Bonsai storage format. */ | ||
public interface BonsaiPreImageProxy extends WorldStatePreimageStorage { | ||
/** | ||
* If this value is not already present, save in preImage store and return the hash value. | ||
* | ||
* @param value value to hash | ||
* @return Hash of value | ||
*/ | ||
Hash hashAndSavePreImage(Bytes value); | ||
|
||
/** PreImageProxy which does not store or cache preImages and only implements hashing. */ | ||
class NoOpPreImageProxy implements BonsaiPreImageProxy { | ||
|
||
@Override | ||
public Hash hashAndSavePreImage(final Bytes value) { | ||
return Hash.hash(value); | ||
} | ||
|
||
@Override | ||
public Optional<UInt256> getStorageTrieKeyPreimage(final Bytes32 trieKey) { | ||
return Optional.empty(); | ||
} | ||
|
||
@Override | ||
public Optional<Address> getAccountTrieKeyPreimage(final Bytes32 trieKey) { | ||
return Optional.empty(); | ||
} | ||
|
||
@Override | ||
public Updater updater() { | ||
throw new UnsupportedOperationException("NoOpPreImageProxy does not implement an updater"); | ||
} | ||
} | ||
|
||
/** | ||
* A caching PreImageProxy suitable for ReferenceTestWorldState which saves hashes in an unbounded | ||
* BiMap. | ||
*/ | ||
class BonsaiReferenceTestPreImageProxy extends NoOpPreImageProxy { | ||
BiMap<Hash, Bytes> preImageCache = HashBiMap.create(); | ||
|
||
@Override | ||
public Hash hashAndSavePreImage(final Bytes value) { | ||
return preImageCache.inverse().computeIfAbsent(value, Hash::hash); | ||
} | ||
|
||
@Override | ||
public Optional<UInt256> getStorageTrieKeyPreimage(final Bytes32 trieKey) { | ||
return Optional.ofNullable(preImageCache.get(trieKey)).map(UInt256::fromBytes); | ||
} | ||
|
||
@Override | ||
public Optional<Address> getAccountTrieKeyPreimage(final Bytes32 trieKey) { | ||
return Optional.ofNullable(preImageCache.get(trieKey)).map(Address::wrap); | ||
} | ||
} | ||
} |
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
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
Oops, something went wrong.