Search in sources :

Example 6 with ArrayDeque

use of kotlin.collections.ArrayDeque in project besu by hyperledger.

the class GetStorageRangeMessage method range.

public StorageRange range(final boolean withRequestId) {
    final ArrayDeque<Bytes32> hashes = new ArrayDeque<>();
    final RLPInput input = new BytesValueRLPInput(data, false);
    input.enterList();
    if (withRequestId)
        input.skipNext();
    final Hash worldStateRootHash = Hash.wrap(Bytes32.wrap(input.readBytes32()));
    final ImmutableStorageRange.Builder range = ImmutableStorageRange.builder().worldStateRootHash(getRootHash().orElse(worldStateRootHash));
    input.enterList();
    while (!input.isEndOfCurrentList()) {
        hashes.add(input.readBytes32());
    }
    range.hashes(hashes);
    input.leaveList();
    if (input.nextIsNull()) {
        input.skipNext();
        range.startKeyHash(Hash.ZERO);
    } else {
        range.startKeyHash(Hash.wrap(Bytes32.wrap(input.readBytes32())));
    }
    if (input.nextIsNull()) {
        input.skipNext();
        range.endKeyHash(Hash.ZERO);
    } else {
        range.endKeyHash(Hash.wrap(Bytes32.wrap(input.readBytes32())));
    }
    range.responseBytes(input.readBigIntegerScalar());
    input.leaveList();
    return range.build();
}
Also used : BytesValueRLPInput(org.hyperledger.besu.ethereum.rlp.BytesValueRLPInput) RLPInput(org.hyperledger.besu.ethereum.rlp.RLPInput) Hash(org.hyperledger.besu.datatypes.Hash) Bytes32(org.apache.tuweni.bytes.Bytes32) BytesValueRLPInput(org.hyperledger.besu.ethereum.rlp.BytesValueRLPInput) ArrayDeque(kotlin.collections.ArrayDeque)

Example 7 with ArrayDeque

use of kotlin.collections.ArrayDeque in project besu by hyperledger.

the class StorageRangeMessage method create.

public static StorageRangeMessage create(final Optional<BigInteger> requestId, final ArrayDeque<TreeMap<Bytes32, Bytes>> slots, final List<Bytes> proof) {
    final BytesValueRLPOutput tmp = new BytesValueRLPOutput();
    tmp.startList();
    requestId.ifPresent(tmp::writeBigIntegerScalar);
    tmp.writeList(slots, (accountList, accountRlpOutput) -> accountRlpOutput.writeList(accountList.entrySet(), (entry, slotRlpOutput) -> {
        slotRlpOutput.startList();
        slotRlpOutput.writeBytes(entry.getKey());
        slotRlpOutput.writeBytes(entry.getValue());
        slotRlpOutput.endList();
    }));
    tmp.writeList(proof, (bytes, rlpOutput) -> rlpOutput.writeBytes(bytes));
    tmp.endList();
    return new StorageRangeMessage(tmp.encoded());
}
Also used : BytesValueRLPInput(org.hyperledger.besu.ethereum.rlp.BytesValueRLPInput) Bytes(org.apache.tuweni.bytes.Bytes) ArrayDeque(kotlin.collections.ArrayDeque) BytesValueRLPOutput(org.hyperledger.besu.ethereum.rlp.BytesValueRLPOutput) List(java.util.List) MessageData(org.hyperledger.besu.ethereum.p2p.rlpx.wire.MessageData) TreeMap(java.util.TreeMap) RLPInput(org.hyperledger.besu.ethereum.rlp.RLPInput) Value(org.immutables.value.Value) AbstractSnapMessageData(org.hyperledger.besu.ethereum.p2p.rlpx.wire.AbstractSnapMessageData) Optional(java.util.Optional) BigInteger(java.math.BigInteger) Bytes32(org.apache.tuweni.bytes.Bytes32) BytesValueRLPOutput(org.hyperledger.besu.ethereum.rlp.BytesValueRLPOutput)

Example 8 with ArrayDeque

use of kotlin.collections.ArrayDeque in project besu by hyperledger.

the class TaskGenerator method createStorageRangeDataRequest.

private static StorageRangeDataRequest createStorageRangeDataRequest(final WorldStateProofProvider worldStateProofProvider, final WorldStateStorage worldStateStorage, final Hash rootHash, final Hash accountHash, final Bytes32 storageRoot, final boolean withData) {
    final RangeStorageEntriesCollector collector = RangeStorageEntriesCollector.createCollector(Bytes32.ZERO, RangeManager.MAX_RANGE, 100, Integer.MAX_VALUE);
    final StoredMerklePatriciaTrie<Bytes, Bytes> storageTrie = new StoredMerklePatriciaTrie<>((location, hash) -> worldStateStorage.getAccountStorageTrieNode(accountHash, location, hash), storageRoot, b -> b, b -> b);
    final TrieIterator<Bytes> visitor = RangeStorageEntriesCollector.createVisitor(collector);
    final TreeMap<Bytes32, Bytes> slots = (TreeMap<Bytes32, Bytes>) storageTrie.entriesFrom(root -> RangeStorageEntriesCollector.collectEntries(collector, visitor, root, Bytes32.ZERO));
    final StorageRangeDataRequest request = SnapDataRequest.createStorageRangeDataRequest(rootHash, accountHash, storageRoot, RangeManager.MIN_RANGE, RangeManager.MAX_RANGE);
    if (withData) {
        request.setProofValid(true);
        request.addResponse(null, worldStateProofProvider, slots, new ArrayDeque<>());
    }
    return request;
}
Also used : TrieGenerator(org.hyperledger.besu.ethereum.core.TrieGenerator) StoredMerklePatriciaTrie(org.hyperledger.besu.ethereum.trie.StoredMerklePatriciaTrie) InMemoryKeyValueStorageProvider(org.hyperledger.besu.ethereum.core.InMemoryKeyValueStorageProvider) RLP(org.hyperledger.besu.ethereum.rlp.RLP) Bytes(org.apache.tuweni.bytes.Bytes) MerklePatriciaTrie(org.hyperledger.besu.ethereum.trie.MerklePatriciaTrie) BytecodeRequest(org.hyperledger.besu.ethereum.eth.sync.snapsync.request.BytecodeRequest) ArrayDeque(kotlin.collections.ArrayDeque) SnapDataRequest(org.hyperledger.besu.ethereum.eth.sync.snapsync.request.SnapDataRequest) List(java.util.List) RangeStorageEntriesCollector(org.hyperledger.besu.ethereum.trie.RangeStorageEntriesCollector) DataStorageFormat(org.hyperledger.besu.ethereum.worldstate.DataStorageFormat) StateTrieAccountValue(org.hyperledger.besu.ethereum.worldstate.StateTrieAccountValue) TreeMap(java.util.TreeMap) WorldStateProofProvider(org.hyperledger.besu.ethereum.proof.WorldStateProofProvider) AccountRangeDataRequest(org.hyperledger.besu.ethereum.eth.sync.snapsync.request.AccountRangeDataRequest) TrieIterator(org.hyperledger.besu.ethereum.trie.TrieIterator) StorageRangeDataRequest(org.hyperledger.besu.ethereum.eth.sync.snapsync.request.StorageRangeDataRequest) WorldStateStorage(org.hyperledger.besu.ethereum.worldstate.WorldStateStorage) Task(org.hyperledger.besu.services.tasks.Task) Bytes32(org.apache.tuweni.bytes.Bytes32) Hash(org.hyperledger.besu.datatypes.Hash) Bytes(org.apache.tuweni.bytes.Bytes) StoredMerklePatriciaTrie(org.hyperledger.besu.ethereum.trie.StoredMerklePatriciaTrie) RangeStorageEntriesCollector(org.hyperledger.besu.ethereum.trie.RangeStorageEntriesCollector) TreeMap(java.util.TreeMap) Bytes32(org.apache.tuweni.bytes.Bytes32) StorageRangeDataRequest(org.hyperledger.besu.ethereum.eth.sync.snapsync.request.StorageRangeDataRequest)

Example 9 with ArrayDeque

use of kotlin.collections.ArrayDeque in project besu by hyperledger.

the class TaskGenerator method createAccountRequest.

public static List<Task<SnapDataRequest>> createAccountRequest(final boolean withData) {
    final WorldStateStorage worldStateStorage = new InMemoryKeyValueStorageProvider().createWorldStateStorage(DataStorageFormat.FOREST);
    final WorldStateProofProvider worldStateProofProvider = new WorldStateProofProvider(worldStateStorage);
    final MerklePatriciaTrie<Bytes32, Bytes> trie = TrieGenerator.generateTrie(worldStateStorage, 1);
    final RangeStorageEntriesCollector collector = RangeStorageEntriesCollector.createCollector(Bytes32.ZERO, RangeManager.MAX_RANGE, 1, Integer.MAX_VALUE);
    final TrieIterator<Bytes> visitor = RangeStorageEntriesCollector.createVisitor(collector);
    final TreeMap<Bytes32, Bytes> accounts = (TreeMap<Bytes32, Bytes>) trie.entriesFrom(root -> RangeStorageEntriesCollector.collectEntries(collector, visitor, root, Bytes32.ZERO));
    final Hash rootHash = Hash.wrap(trie.getRootHash());
    final AccountRangeDataRequest accountRangeDataRequest = SnapDataRequest.createAccountRangeDataRequest(rootHash, RangeManager.MIN_RANGE, RangeManager.MAX_RANGE);
    if (withData) {
        accountRangeDataRequest.addResponse(worldStateProofProvider, accounts, new ArrayDeque<>());
    }
    final StateTrieAccountValue stateTrieAccountValue = StateTrieAccountValue.readFrom(RLP.input(accounts.firstEntry().getValue()));
    final Hash accountHash = Hash.wrap(accounts.firstKey());
    final StorageRangeDataRequest storageRangeDataRequest = createStorageRangeDataRequest(worldStateProofProvider, worldStateStorage, rootHash, accountHash, stateTrieAccountValue.getStorageRoot(), withData);
    final BytecodeRequest bytecodeRequest = createBytecodeDataRequest(worldStateStorage, rootHash, accountHash, stateTrieAccountValue.getCodeHash(), withData);
    return List.of(new StubTask(accountRangeDataRequest), new StubTask(storageRangeDataRequest), new StubTask(bytecodeRequest));
}
Also used : TrieGenerator(org.hyperledger.besu.ethereum.core.TrieGenerator) StoredMerklePatriciaTrie(org.hyperledger.besu.ethereum.trie.StoredMerklePatriciaTrie) InMemoryKeyValueStorageProvider(org.hyperledger.besu.ethereum.core.InMemoryKeyValueStorageProvider) RLP(org.hyperledger.besu.ethereum.rlp.RLP) Bytes(org.apache.tuweni.bytes.Bytes) MerklePatriciaTrie(org.hyperledger.besu.ethereum.trie.MerklePatriciaTrie) BytecodeRequest(org.hyperledger.besu.ethereum.eth.sync.snapsync.request.BytecodeRequest) ArrayDeque(kotlin.collections.ArrayDeque) SnapDataRequest(org.hyperledger.besu.ethereum.eth.sync.snapsync.request.SnapDataRequest) List(java.util.List) RangeStorageEntriesCollector(org.hyperledger.besu.ethereum.trie.RangeStorageEntriesCollector) DataStorageFormat(org.hyperledger.besu.ethereum.worldstate.DataStorageFormat) StateTrieAccountValue(org.hyperledger.besu.ethereum.worldstate.StateTrieAccountValue) TreeMap(java.util.TreeMap) WorldStateProofProvider(org.hyperledger.besu.ethereum.proof.WorldStateProofProvider) AccountRangeDataRequest(org.hyperledger.besu.ethereum.eth.sync.snapsync.request.AccountRangeDataRequest) TrieIterator(org.hyperledger.besu.ethereum.trie.TrieIterator) StorageRangeDataRequest(org.hyperledger.besu.ethereum.eth.sync.snapsync.request.StorageRangeDataRequest) WorldStateStorage(org.hyperledger.besu.ethereum.worldstate.WorldStateStorage) Task(org.hyperledger.besu.services.tasks.Task) Bytes32(org.apache.tuweni.bytes.Bytes32) Hash(org.hyperledger.besu.datatypes.Hash) WorldStateStorage(org.hyperledger.besu.ethereum.worldstate.WorldStateStorage) WorldStateProofProvider(org.hyperledger.besu.ethereum.proof.WorldStateProofProvider) BytecodeRequest(org.hyperledger.besu.ethereum.eth.sync.snapsync.request.BytecodeRequest) StateTrieAccountValue(org.hyperledger.besu.ethereum.worldstate.StateTrieAccountValue) TreeMap(java.util.TreeMap) Hash(org.hyperledger.besu.datatypes.Hash) Bytes32(org.apache.tuweni.bytes.Bytes32) Bytes(org.apache.tuweni.bytes.Bytes) RangeStorageEntriesCollector(org.hyperledger.besu.ethereum.trie.RangeStorageEntriesCollector) InMemoryKeyValueStorageProvider(org.hyperledger.besu.ethereum.core.InMemoryKeyValueStorageProvider) AccountRangeDataRequest(org.hyperledger.besu.ethereum.eth.sync.snapsync.request.AccountRangeDataRequest) StorageRangeDataRequest(org.hyperledger.besu.ethereum.eth.sync.snapsync.request.StorageRangeDataRequest)

Example 10 with ArrayDeque

use of kotlin.collections.ArrayDeque in project besu by hyperledger.

the class StorageRangeMessageTest method roundTripTest.

@Test
public void roundTripTest() {
    final ArrayDeque<TreeMap<Bytes32, Bytes>> keys = new ArrayDeque<>();
    final TreeMap<Bytes32, Bytes> storage = new TreeMap<>();
    storage.put(Hash.wrap(Bytes32.leftPad(Bytes.of(1))), Bytes32.random());
    keys.add(storage);
    final List<Bytes> proofs = new ArrayList<>();
    proofs.add(Bytes32.random());
    // Perform round-trip transformation
    final MessageData initialMessage = StorageRangeMessage.create(keys, proofs);
    final MessageData raw = new RawMessage(SnapV1.STORAGE_RANGE, initialMessage.getData());
    final StorageRangeMessage message = StorageRangeMessage.readFrom(raw);
    // check match originals.
    final StorageRangeMessage.SlotRangeData range = message.slotsData(false);
    Assertions.assertThat(range.slots()).isEqualTo(keys);
    Assertions.assertThat(range.proofs()).isEqualTo(proofs);
}
Also used : Bytes(org.apache.tuweni.bytes.Bytes) MessageData(org.hyperledger.besu.ethereum.p2p.rlpx.wire.MessageData) ArrayList(java.util.ArrayList) TreeMap(java.util.TreeMap) RawMessage(org.hyperledger.besu.ethereum.p2p.rlpx.wire.RawMessage) Bytes32(org.apache.tuweni.bytes.Bytes32) ArrayDeque(kotlin.collections.ArrayDeque) Test(org.junit.Test)

Aggregations

ArrayDeque (kotlin.collections.ArrayDeque)11 Bytes (org.apache.tuweni.bytes.Bytes)9 Bytes32 (org.apache.tuweni.bytes.Bytes32)7 BytesValueRLPInput (org.hyperledger.besu.ethereum.rlp.BytesValueRLPInput)7 RLPInput (org.hyperledger.besu.ethereum.rlp.RLPInput)7 TreeMap (java.util.TreeMap)6 List (java.util.List)4 Hash (org.hyperledger.besu.datatypes.Hash)3 StorageRangeDataRequest (org.hyperledger.besu.ethereum.eth.sync.snapsync.request.StorageRangeDataRequest)3 MessageData (org.hyperledger.besu.ethereum.p2p.rlpx.wire.MessageData)3 StateTrieAccountValue (org.hyperledger.besu.ethereum.worldstate.StateTrieAccountValue)3 BigInteger (java.math.BigInteger)2 Optional (java.util.Optional)2 InMemoryKeyValueStorageProvider (org.hyperledger.besu.ethereum.core.InMemoryKeyValueStorageProvider)2 TrieGenerator (org.hyperledger.besu.ethereum.core.TrieGenerator)2 AccountRangeDataRequest (org.hyperledger.besu.ethereum.eth.sync.snapsync.request.AccountRangeDataRequest)2 BytecodeRequest (org.hyperledger.besu.ethereum.eth.sync.snapsync.request.BytecodeRequest)2 SnapDataRequest (org.hyperledger.besu.ethereum.eth.sync.snapsync.request.SnapDataRequest)2 AbstractSnapMessageData (org.hyperledger.besu.ethereum.p2p.rlpx.wire.AbstractSnapMessageData)2 WorldStateProofProvider (org.hyperledger.besu.ethereum.proof.WorldStateProofProvider)2