-
Notifications
You must be signed in to change notification settings - Fork 863
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Code storage by hash v2 #6505
Merged
Merged
Code storage by hash v2 #6505
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
0f52f0a
Bonsai code storage by hash (#5889)
jframe 625a0d4
Fix codehash detection
jframe 7c62202
remove prefix on code hash storage
jframe 8f8ff25
Merge branch 'main' into code_storage_by_hash_v2
jframe 47fad26
add changelog entry
jframe 827b076
Merge remote-tracking branch 'upstream/main' into code_storage_by_has…
jframe bd12fd8
Merge branch 'main' into code_storage_by_hash_v2
jframe File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
54 changes: 54 additions & 0 deletions
54
...rg/hyperledger/besu/ethereum/trie/bonsai/storage/flat/AccountHashCodeStorageStrategy.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,54 @@ | ||
/* | ||
* 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.trie.bonsai.storage.flat; | ||
|
||
import static org.hyperledger.besu.ethereum.storage.keyvalue.KeyValueSegmentIdentifier.CODE_STORAGE; | ||
|
||
import org.hyperledger.besu.datatypes.Hash; | ||
import org.hyperledger.besu.plugin.services.storage.SegmentedKeyValueStorage; | ||
import org.hyperledger.besu.plugin.services.storage.SegmentedKeyValueStorageTransaction; | ||
|
||
import java.util.Optional; | ||
|
||
import org.apache.tuweni.bytes.Bytes; | ||
|
||
public class AccountHashCodeStorageStrategy implements CodeStorageStrategy { | ||
@Override | ||
public Optional<Bytes> getFlatCode( | ||
final Hash codeHash, final Hash accountHash, final SegmentedKeyValueStorage storage) { | ||
return storage | ||
.get(CODE_STORAGE, accountHash.toArrayUnsafe()) | ||
.map(Bytes::wrap) | ||
.filter(b -> Hash.hash(b).equals(codeHash)); | ||
} | ||
|
||
@Override | ||
public void putFlatCode( | ||
final SegmentedKeyValueStorageTransaction transaction, | ||
final Hash accountHash, | ||
final Hash codeHash, | ||
final Bytes code) { | ||
transaction.put(CODE_STORAGE, accountHash.toArrayUnsafe(), code.toArrayUnsafe()); | ||
} | ||
|
||
@Override | ||
public void removeFlatCode( | ||
final SegmentedKeyValueStorageTransaction transaction, | ||
final Hash accountHash, | ||
final Hash codeHash) { | ||
transaction.remove(CODE_STORAGE, accountHash.toArrayUnsafe()); | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
...a/org/hyperledger/besu/ethereum/trie/bonsai/storage/flat/CodeHashCodeStorageStrategy.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,55 @@ | ||
/* | ||
* 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.trie.bonsai.storage.flat; | ||
|
||
import static org.hyperledger.besu.ethereum.storage.keyvalue.KeyValueSegmentIdentifier.CODE_STORAGE; | ||
|
||
import org.hyperledger.besu.datatypes.Hash; | ||
import org.hyperledger.besu.plugin.services.storage.SegmentedKeyValueStorage; | ||
import org.hyperledger.besu.plugin.services.storage.SegmentedKeyValueStorageTransaction; | ||
|
||
import java.util.Optional; | ||
|
||
import org.apache.tuweni.bytes.Bytes; | ||
|
||
public class CodeHashCodeStorageStrategy implements CodeStorageStrategy { | ||
|
||
@Override | ||
public Optional<Bytes> getFlatCode( | ||
final Hash codeHash, final Hash accountHash, final SegmentedKeyValueStorage storage) { | ||
return storage.get(CODE_STORAGE, codeHash.toArrayUnsafe()).map(Bytes::wrap); | ||
} | ||
|
||
@Override | ||
public void putFlatCode( | ||
final SegmentedKeyValueStorageTransaction transaction, | ||
final Hash accountHash, | ||
final Hash codeHash, | ||
final Bytes code) { | ||
transaction.put(CODE_STORAGE, codeHash.toArrayUnsafe(), code.toArrayUnsafe()); | ||
} | ||
|
||
@Override | ||
public void removeFlatCode( | ||
final SegmentedKeyValueStorageTransaction transaction, | ||
final Hash accountHash, | ||
final Hash codeHash) {} | ||
|
||
public static boolean isCodeHashValue(final byte[] key, final byte[] value) { | ||
final Hash valueHash = Hash.hash(Bytes.wrap(value)); | ||
return Bytes.wrap(key).equals(valueHash); | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
...main/java/org/hyperledger/besu/ethereum/trie/bonsai/storage/flat/CodeStorageStrategy.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,41 @@ | ||
/* | ||
* 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.trie.bonsai.storage.flat; | ||
|
||
import org.hyperledger.besu.datatypes.Hash; | ||
import org.hyperledger.besu.plugin.services.storage.SegmentedKeyValueStorage; | ||
import org.hyperledger.besu.plugin.services.storage.SegmentedKeyValueStorageTransaction; | ||
|
||
import java.util.Optional; | ||
|
||
import org.apache.tuweni.bytes.Bytes; | ||
|
||
public interface CodeStorageStrategy { | ||
|
||
Optional<Bytes> getFlatCode( | ||
final Hash codeHash, final Hash accountHash, final SegmentedKeyValueStorage storage); | ||
|
||
void putFlatCode( | ||
final SegmentedKeyValueStorageTransaction transaction, | ||
final Hash accountHash, | ||
final Hash codeHash, | ||
final Bytes code); | ||
|
||
void removeFlatCode( | ||
final SegmentedKeyValueStorageTransaction transaction, | ||
final Hash accountHash, | ||
final Hash codeHash); | ||
} |
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
there is no risk to find a key value that match for codeByHash but it's codeByAddress in reality ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't think this can happen in reality. To do that would need to be able to predict the address and have that as the runtime code. I've gone through the scenarios for create and create2 and I can't see a case where this could happen. In the case of create it's not possible to predict the address since it hashes the nonce and sender address and applies the etherum address rule. In the case of the create2 it is more interesting since we can control the inputs, but I still don't think it's possible since you would need the initCode to create a contract with the runtime code of the address which is not possible to predict in advance although we can calculate it in the case of create2.
Also would prefer to replace this detection with the database metadata approach once that is available.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do we know when it will be available ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure. Think it's still a work in progress https://github.com/fab-10/besu/tree/database-metadata-refactor. So might be a little while off.