use of org.hyperledger.besu.ethereum.privacy.storage.PrivateBlockMetadata in project besu by hyperledger.
the class PrivateStateRootResolverTest method resolveCorrectRootHashWhenMultipleCommitmentsExistForPrivacyGroup.
@Test
public void resolveCorrectRootHashWhenMultipleCommitmentsExistForPrivacyGroup() {
final PrivateStateStorage.Updater updater = privateStateStorage.updater();
updater.putPrivateBlockMetadata(BLOCKCHAIN.getBlockByNumber(16).get().getHash(), Bytes32.wrap(privacyGroupId), new PrivateBlockMetadata(Collections.singletonList(new PrivateTransactionMetadata(BLOCK_GENERATOR.transaction().getHash(), pmt1StateHash))));
updater.putPrivateBlockMetadata(BLOCKCHAIN.getBlockByNumber(16).get().getHash(), Bytes32.wrap(failingPrivacyGroupId), new PrivateBlockMetadata(Collections.singletonList(new PrivateTransactionMetadata(BLOCK_GENERATOR.transaction().getHash(), pmt2StateHash))));
updater.putPrivacyGroupHeadBlockMap(BLOCKCHAIN.getChainHeadHash(), new PrivacyGroupHeadBlockMap(Collections.singletonMap(Bytes32.wrap(privacyGroupId), BLOCKCHAIN.getBlockByNumber(16).get().getHash())));
updater.commit();
final PrivateStateRootResolver privateStateRootResolver = new PrivateStateRootResolver(privateStateStorage);
assertThat(privateStateRootResolver.resolveLastStateRoot(privacyGroupId, BLOCKCHAIN.getChainHeadHash())).isEqualTo(pmt1StateHash);
}
use of org.hyperledger.besu.ethereum.privacy.storage.PrivateBlockMetadata in project besu by hyperledger.
the class PrivateMetadataUpdaterTest method addingMetadataSuccessfull.
@Test
public void addingMetadataSuccessfull() {
when(blockHeader.getHash()).thenReturn(hashBlockOne);
pmtHash = Hash.ZERO;
final PrivateTransactionMetadata expected = new PrivateTransactionMetadata(pmtHash, stateRoot);
updater.addPrivateTransactionMetadata(privacyGroupId, expected);
updater.commit();
final Optional<PrivateBlockMetadata> privateBlockMetadata = privateStateStorage.getPrivateBlockMetadata(hashBlockOne, privacyGroupId);
assertThat(privateBlockMetadata.get().getLatestStateRoot().get()).isEqualTo(stateRoot);
}
use of org.hyperledger.besu.ethereum.privacy.storage.PrivateBlockMetadata in project besu by hyperledger.
the class PrivacyBlockProcessor method transactionsInGroupThatNeedToBeApplied.
private List<PrivateTransactionWithMetadata> transactionsInGroupThatNeedToBeApplied(final BlockHeader blockHeader, final List<PrivateTransactionWithMetadata> privateTransactionWithMetadataList, final Bytes32 privacyGroupId) {
// if we are the member adding another member we do not have to rehydrate
// if we have been removed from the group at some point we only need to rehydrate from where we
// were removed
// if we are a new member we need to rehydrate the complete state
List<PrivateTransactionWithMetadata> actualList = privateTransactionWithMetadataList;
final Optional<PrivacyGroupHeadBlockMap> maybePrivacyGroupHeadBlockMap = privateStateStorage.getPrivacyGroupHeadBlockMap(blockHeader.getParentHash());
if (maybePrivacyGroupHeadBlockMap.isPresent()) {
final PrivacyGroupHeadBlockMap privacyGroupHeadBlockMap = maybePrivacyGroupHeadBlockMap.get();
final Hash lastBlockWithTx = privacyGroupHeadBlockMap.get(privacyGroupId);
if (lastBlockWithTx != null) {
// we are or have been a member of the privacy group
final PrivateBlockMetadata nodeLatestBlockMetadata = privateStateStorage.getPrivateBlockMetadata(lastBlockWithTx, privacyGroupId).orElseThrow();
final List<PrivateTransactionMetadata> nodeLatestPrivateTxMetadataList = nodeLatestBlockMetadata.getPrivateTransactionMetadataList();
final Hash nodeLatestStateRoot = nodeLatestPrivateTxMetadataList.get(nodeLatestPrivateTxMetadataList.size() - 1).getStateRoot();
final Hash latestStateRootFromRehydrationList = privateTransactionWithMetadataList.get(privateTransactionWithMetadataList.size() - 1).getPrivateTransactionMetadata().getStateRoot();
if (nodeLatestStateRoot.equals(latestStateRootFromRehydrationList)) {
// we are already on the latest state root, which means that we are the member adding a
// new member
actualList = Collections.emptyList();
} else {
// we are being added, but do not have to rehydrate all private transactions
final Hash nodeLatestPrivateMarkerTransactionHash = nodeLatestPrivateTxMetadataList.get(nodeLatestPrivateTxMetadataList.size() - 1).getPrivateMarkerTransactionHash();
for (int i = 0; i < privateTransactionWithMetadataList.size(); i++) {
if (!privateTransactionWithMetadataList.get(i).getPrivateTransactionMetadata().getPrivateMarkerTransactionHash().equals(nodeLatestPrivateMarkerTransactionHash)) {
continue;
}
if (privateTransactionWithMetadataList.size() - 1 == i) {
// nothing needs to be re-hydrated
actualList = Collections.emptyList();
} else {
actualList = privateTransactionWithMetadataList.subList(i + 1, privateTransactionWithMetadataList.size());
}
break;
}
}
}
}
return actualList;
}
use of org.hyperledger.besu.ethereum.privacy.storage.PrivateBlockMetadata in project besu by hyperledger.
the class PrivateStateRootResolverTest method resolveLatestRootHashWhenMultipleCommitmentsForTheSamePrivacyGroupExist.
@Test
public void resolveLatestRootHashWhenMultipleCommitmentsForTheSamePrivacyGroupExist() {
final PrivateStateStorage.Updater updater = privateStateStorage.updater();
updater.putPrivateBlockMetadata(BLOCKCHAIN.getBlockByNumber(16).get().getHash(), Bytes32.wrap(privacyGroupId), new PrivateBlockMetadata(Arrays.asList(new PrivateTransactionMetadata(BLOCK_GENERATOR.transaction().getHash(), pmt1StateHash), new PrivateTransactionMetadata(BLOCK_GENERATOR.transaction().getHash(), pmt2StateHash))));
updater.putPrivacyGroupHeadBlockMap(BLOCKCHAIN.getChainHeadHash(), new PrivacyGroupHeadBlockMap(Collections.singletonMap(Bytes32.wrap(privacyGroupId), BLOCKCHAIN.getBlockByNumber(16).get().getHash())));
updater.commit();
final PrivateStateRootResolver privateStateRootResolver = new PrivateStateRootResolver(privateStateStorage);
assertThat(privateStateRootResolver.resolveLastStateRoot(privacyGroupId, BLOCKCHAIN.getChainHeadHash())).isEqualTo(pmt2StateHash);
}
use of org.hyperledger.besu.ethereum.privacy.storage.PrivateBlockMetadata in project besu by hyperledger.
the class PrivateStateRootResolverTest method resolveExpectedRootHashWhenCommitmentForPrivacyGroupExists.
@Test
public void resolveExpectedRootHashWhenCommitmentForPrivacyGroupExists() {
final PrivateStateStorage.Updater updater = privateStateStorage.updater();
updater.putPrivateBlockMetadata(BLOCKCHAIN.getBlockByNumber(16).get().getHash(), Bytes32.wrap(privacyGroupId), new PrivateBlockMetadata(Collections.singletonList(new PrivateTransactionMetadata(BLOCK_GENERATOR.transaction().getHash(), pmt1StateHash))));
updater.putPrivacyGroupHeadBlockMap(BLOCKCHAIN.getChainHeadHash(), new PrivacyGroupHeadBlockMap(Collections.singletonMap(Bytes32.wrap(privacyGroupId), BLOCKCHAIN.getBlockByNumber(16).get().getHash())));
updater.commit();
final PrivateStateRootResolver privateStateRootResolver = new PrivateStateRootResolver(privateStateStorage);
assertThat(privateStateRootResolver.resolveLastStateRoot(privacyGroupId, BLOCKCHAIN.getChainHeadHash())).isEqualTo(pmt1StateHash);
}
Aggregations