forked from hyperledger/besu
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SNAPSYNC add request task (hyperledger#3601)
PR that adds the different request tasks necessary for the snapsync as well as a utility to manage the ranges of requests Signed-off-by: Karim TAAM <karim.t2am@gmail.com>
- Loading branch information
1 parent
79587a0
commit 84e9286
Showing
34 changed files
with
1,714 additions
and
229 deletions.
There are no files selected for viewing
95 changes: 95 additions & 0 deletions
95
ethereum/core/src/test-support/java/org/hyperledger/besu/ethereum/core/TrieGenerator.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,95 @@ | ||
/* | ||
* Copyright contributors to Hyperledger Besu | ||
* | ||
* 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.core; | ||
|
||
import org.hyperledger.besu.datatypes.Hash; | ||
import org.hyperledger.besu.datatypes.Wei; | ||
import org.hyperledger.besu.ethereum.rlp.RLP; | ||
import org.hyperledger.besu.ethereum.trie.MerklePatriciaTrie; | ||
import org.hyperledger.besu.ethereum.trie.StoredMerklePatriciaTrie; | ||
import org.hyperledger.besu.ethereum.worldstate.StateTrieAccountValue; | ||
import org.hyperledger.besu.ethereum.worldstate.WorldStateStorage; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import org.apache.tuweni.bytes.Bytes; | ||
import org.apache.tuweni.bytes.Bytes32; | ||
import org.apache.tuweni.units.bigints.UInt256; | ||
|
||
public class TrieGenerator { | ||
|
||
public static MerklePatriciaTrie<Bytes32, Bytes> generateTrie( | ||
final WorldStateStorage worldStateStorage, final int nbAccounts) { | ||
final List<Hash> accountHash = new ArrayList<>(); | ||
final MerklePatriciaTrie<Bytes32, Bytes> accountStateTrie = | ||
emptyAccountStateTrie(worldStateStorage); | ||
// Add some storage values | ||
for (int i = 0; i < nbAccounts; i++) { | ||
final WorldStateStorage.Updater updater = worldStateStorage.updater(); | ||
|
||
accountHash.add(Hash.wrap(Bytes32.leftPad(Bytes.of(i + 1)))); | ||
final MerklePatriciaTrie<Bytes32, Bytes> storageTrie = | ||
emptyStorageTrie(worldStateStorage, accountHash.get(i)); | ||
writeStorageValue(storageTrie, UInt256.ONE, UInt256.valueOf(2L)); | ||
writeStorageValue(storageTrie, UInt256.valueOf(2L), UInt256.valueOf(4L)); | ||
writeStorageValue(storageTrie, UInt256.valueOf(3L), UInt256.valueOf(6L)); | ||
int accountIndex = i; | ||
storageTrie.commit( | ||
(location, hash, value) -> | ||
updater.putAccountStorageTrieNode( | ||
accountHash.get(accountIndex), location, hash, value)); | ||
final Hash codeHash = Hash.hash(Bytes32.leftPad(Bytes.of(i + 10))); | ||
final StateTrieAccountValue accountValue = | ||
new StateTrieAccountValue(1L, Wei.of(2L), Hash.wrap(storageTrie.getRootHash()), codeHash); | ||
accountStateTrie.put(accountHash.get(i), RLP.encode(accountValue::writeTo)); | ||
accountStateTrie.commit(updater::putAccountStateTrieNode); | ||
|
||
// Persist updates | ||
updater.commit(); | ||
} | ||
return accountStateTrie; | ||
} | ||
|
||
private static void writeStorageValue( | ||
final MerklePatriciaTrie<Bytes32, Bytes> storageTrie, | ||
final UInt256 key, | ||
final UInt256 value) { | ||
storageTrie.put(storageKeyHash(key), encodeStorageValue(value)); | ||
} | ||
|
||
private static Bytes32 storageKeyHash(final UInt256 storageKey) { | ||
return Hash.hash(storageKey); | ||
} | ||
|
||
private static Bytes encodeStorageValue(final UInt256 storageValue) { | ||
return RLP.encode(out -> out.writeBytes(storageValue.toMinimalBytes())); | ||
} | ||
|
||
public static MerklePatriciaTrie<Bytes32, Bytes> emptyStorageTrie( | ||
final WorldStateStorage worldStateStorage, final Hash accountHash) { | ||
return new StoredMerklePatriciaTrie<>( | ||
(location, hash) -> | ||
worldStateStorage.getAccountStorageTrieNode(accountHash, location, hash), | ||
b -> b, | ||
b -> b); | ||
} | ||
|
||
public static MerklePatriciaTrie<Bytes32, Bytes> emptyAccountStateTrie( | ||
final WorldStateStorage worldStateStorage) { | ||
return new StoredMerklePatriciaTrie<>( | ||
worldStateStorage::getAccountStateTrieNode, b -> b, b -> b); | ||
} | ||
} |
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
93 changes: 93 additions & 0 deletions
93
...main/java/org/hyperledger/besu/ethereum/eth/manager/snap/GetAccountRangeFromPeerTask.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,93 @@ | ||
/* | ||
* Copyright contributors to Hyperledger Besu | ||
* | ||
* 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.eth.manager.snap; | ||
|
||
import org.hyperledger.besu.ethereum.core.BlockHeader; | ||
import org.hyperledger.besu.ethereum.eth.manager.EthContext; | ||
import org.hyperledger.besu.ethereum.eth.manager.EthPeer; | ||
import org.hyperledger.besu.ethereum.eth.manager.PendingPeerRequest; | ||
import org.hyperledger.besu.ethereum.eth.manager.task.AbstractPeerRequestTask; | ||
import org.hyperledger.besu.ethereum.eth.messages.snap.AccountRangeMessage; | ||
import org.hyperledger.besu.ethereum.eth.messages.snap.SnapV1; | ||
import org.hyperledger.besu.ethereum.p2p.rlpx.wire.MessageData; | ||
import org.hyperledger.besu.plugin.services.MetricsSystem; | ||
|
||
import java.util.Optional; | ||
|
||
import org.apache.tuweni.bytes.Bytes32; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class GetAccountRangeFromPeerTask | ||
extends AbstractPeerRequestTask<AccountRangeMessage.AccountRangeData> { | ||
|
||
private static final Logger LOG = LoggerFactory.getLogger(GetAccountRangeFromPeerTask.class); | ||
|
||
private final Bytes32 startKeyHash; | ||
private final Bytes32 endKeyHash; | ||
private final BlockHeader blockHeader; | ||
|
||
private GetAccountRangeFromPeerTask( | ||
final EthContext ethContext, | ||
final Bytes32 startKeyHash, | ||
final Bytes32 endKeyHash, | ||
final BlockHeader blockHeader, | ||
final MetricsSystem metricsSystem) { | ||
super(ethContext, SnapV1.ACCOUNT_RANGE, metricsSystem); | ||
this.startKeyHash = startKeyHash; | ||
this.endKeyHash = endKeyHash; | ||
this.blockHeader = blockHeader; | ||
} | ||
|
||
public static GetAccountRangeFromPeerTask forAccountRange( | ||
final EthContext ethContext, | ||
final Bytes32 startKeyHash, | ||
final Bytes32 endKeyHash, | ||
final BlockHeader blockHeader, | ||
final MetricsSystem metricsSystem) { | ||
return new GetAccountRangeFromPeerTask( | ||
ethContext, startKeyHash, endKeyHash, blockHeader, metricsSystem); | ||
} | ||
|
||
@Override | ||
protected PendingPeerRequest sendRequest() { | ||
return sendRequestToPeer( | ||
peer -> { | ||
LOG.trace( | ||
"Requesting account range [{} ,{}] for state root {} from peer {} .", | ||
startKeyHash, | ||
endKeyHash, | ||
blockHeader.getStateRoot(), | ||
peer); | ||
return peer.getSnapAccountRange(blockHeader.getStateRoot(), startKeyHash, endKeyHash); | ||
}, | ||
blockHeader.getNumber()); | ||
} | ||
|
||
@Override | ||
protected Optional<AccountRangeMessage.AccountRangeData> processResponse( | ||
final boolean streamClosed, final MessageData message, final EthPeer peer) { | ||
|
||
if (streamClosed) { | ||
// We don't record this as a useless response because it's impossible to know if a peer has | ||
// the data we're requesting. | ||
return Optional.empty(); | ||
} | ||
final AccountRangeMessage accountRangeMessage = AccountRangeMessage.readFrom(message); | ||
final AccountRangeMessage.AccountRangeData accountRangeData = | ||
accountRangeMessage.accountData(true); | ||
return Optional.of(accountRangeData); | ||
} | ||
} |
Oops, something went wrong.