use of org.hyperledger.besu.ethereum.rlp.BytesValueRLPInput in project besu by hyperledger.
the class RestoreState method restoreBlocks.
private void restoreBlocks() throws IOException {
try (final RollingFileReader headerReader = new RollingFileReader(this::headerFileName, compressed);
final RollingFileReader bodyReader = new RollingFileReader(this::bodyFileName, compressed);
final RollingFileReader receiptReader = new RollingFileReader(this::receiptFileName, compressed)) {
final MutableBlockchain blockchain = besuController.getProtocolContext().getBlockchain();
// target block is "including" the target block, so LE test not LT.
for (long i = 0; i <= targetBlock; i++) {
if (i % 100000 == 0) {
LOG.info("Loading chain data {} / {}", i, targetBlock);
}
final byte[] headerEntry = headerReader.readBytes();
final byte[] bodyEntry = bodyReader.readBytes();
final byte[] receiptEntry = receiptReader.readBytes();
final BlockHeaderFunctions functions = new MainnetBlockHeaderFunctions();
final BlockHeader header = BlockHeader.readFrom(new BytesValueRLPInput(Bytes.wrap(headerEntry), false, true), functions);
final BlockBody body = BlockBody.readFrom(new BytesValueRLPInput(Bytes.wrap(bodyEntry), false, true), functions);
final RLPInput receiptsRlp = new BytesValueRLPInput(Bytes.wrap(receiptEntry), false, true);
final int receiptsCount = receiptsRlp.enterList();
final List<TransactionReceipt> receipts = new ArrayList<>(receiptsCount);
for (int j = 0; j < receiptsCount; j++) {
receipts.add(TransactionReceipt.readFrom(receiptsRlp, true));
}
receiptsRlp.leaveList();
blockchain.appendBlock(new Block(header, body), receipts);
}
}
LOG.info("Chain data loaded");
}
use of org.hyperledger.besu.ethereum.rlp.BytesValueRLPInput in project besu by hyperledger.
the class PkiQbftExtraDataCodec method encode.
private Bytes encode(final BftExtraData bftExtraData, final EncodingType encodingType, final boolean includeCms) {
final Bytes encoded = super.encode(bftExtraData, encodingType);
if (!(bftExtraData instanceof PkiQbftExtraData) || !includeCms) {
return encoded;
}
final BytesValueRLPOutput rlpOutput = new BytesValueRLPOutput();
rlpOutput.startList();
// Read through extraData RLP list elements and write them to the new RLP output
new BytesValueRLPInput(encoded, false).readList(RLPInput::readAsRlp).stream().map(RLPInput::raw).forEach(rlpOutput::writeRLPBytes);
rlpOutput.writeBytes(((PkiQbftExtraData) bftExtraData).getCms());
rlpOutput.endList();
return rlpOutput.encoded();
}
use of org.hyperledger.besu.ethereum.rlp.BytesValueRLPInput in project besu by hyperledger.
the class StateBackupService method visitAccount.
private TrieIterator.State visitAccount(final Bytes32 nodeKey, final Node<Bytes> node) {
if (node.getValue().isEmpty()) {
return State.CONTINUE;
}
backupStatus.currentAccount = nodeKey;
final Bytes nodeValue = node.getValue().orElse(Hash.EMPTY);
final StateTrieAccountValue account = StateTrieAccountValue.readFrom(new BytesValueRLPInput(nodeValue, false));
final Bytes code = worldStateStorage.getCode(account.getCodeHash(), null).orElse(Bytes.EMPTY);
backupStatus.codeSize.addAndGet(code.size());
final BytesValueRLPOutput accountOutput = new BytesValueRLPOutput();
accountOutput.startList();
// trie hash
accountOutput.writeBytes(nodeKey);
// account rlp
accountOutput.writeBytes(nodeValue);
// code
accountOutput.writeBytes(code);
accountOutput.endList();
try {
accountFileWriter.writeBytes(accountOutput.encoded().toArrayUnsafe());
} catch (final IOException ioe) {
LOG.error("Failure writing backup", ioe);
return State.STOP;
}
// storage is written for each leaf, otherwise the whole trie would have to fit in memory
final StoredMerklePatriciaTrie<Bytes32, Bytes> storageTrie = new StoredMerklePatriciaTrie<>(worldStateStorage::getAccountStateTrieNode, account.getStorageRoot(), Function.identity(), Function.identity());
storageTrie.visitLeafs((storageKey, storageValue) -> visitAccountStorage(storageKey, storageValue, accountFileWriter));
try {
accountFileWriter.writeBytes(ACCOUNT_END_MARKER.toArrayUnsafe());
} catch (final IOException ioe) {
LOG.error("Failure writing backup", ioe);
return State.STOP;
}
backupStatus.accountCount.incrementAndGet();
return State.CONTINUE;
}
use of org.hyperledger.besu.ethereum.rlp.BytesValueRLPInput in project besu by hyperledger.
the class PrivateTransactionTest method testReadFromInvalid.
@Test
public void testReadFromInvalid() {
final BytesValueRLPInput input = new BytesValueRLPInput(Bytes.fromHexString(INVALID_RLP), false);
assertThatThrownBy(() -> PrivateTransaction.readFrom(input)).isInstanceOf(RLPException.class);
}
use of org.hyperledger.besu.ethereum.rlp.BytesValueRLPInput in project besu by hyperledger.
the class PrivateTransactionTest method testReadFromWithLargeChainId.
@Test
public void testReadFromWithLargeChainId() {
final PrivateTransaction p = PrivateTransaction.readFrom(new BytesValueRLPInput(Bytes.fromHexString(VALID_SIGNED_PRIVATE_TRANSACTION_LARGE_CHAINID_RLP), false));
assertThat(p).isEqualTo(VALID_SIGNED_PRIVATE_TRANSACTION_LARGE_CHAINID);
}
Aggregations