|
| 1 | +package org.unicitylabs.sdk.transaction.verification; |
| 2 | + |
| 3 | +import org.unicitylabs.sdk.api.StateId; |
| 4 | +import org.unicitylabs.sdk.api.bft.ShardId; |
| 5 | +import org.unicitylabs.sdk.api.bft.ShardTreeCertificate; |
| 6 | +import org.unicitylabs.sdk.util.verification.VerificationResult; |
| 7 | +import org.unicitylabs.sdk.util.verification.VerificationStatus; |
| 8 | + |
| 9 | +/** |
| 10 | + * Rule to verify that the shard id of the shard tree certificate is a prefix of the transaction |
| 11 | + * state id. An empty shard id matches any state id. |
| 12 | + */ |
| 13 | +public class ShardIdMatchesStateIdRule { |
| 14 | + |
| 15 | + private ShardIdMatchesStateIdRule() { |
| 16 | + } |
| 17 | + |
| 18 | + /** |
| 19 | + * Verify that the shard id is a prefix of the state id. |
| 20 | + * |
| 21 | + * @param stateId state id of the transaction being verified |
| 22 | + * @param shardTreeCertificate shard tree certificate carrying the shard id |
| 23 | + * |
| 24 | + * @return verification result with {@link VerificationStatus#OK} on match (or empty shard id), |
| 25 | + * otherwise {@link VerificationStatus#FAIL} |
| 26 | + */ |
| 27 | + public static VerificationResult<VerificationStatus> verify( |
| 28 | + StateId stateId, |
| 29 | + ShardTreeCertificate shardTreeCertificate |
| 30 | + ) { |
| 31 | + ShardId shardId = shardTreeCertificate.getShard(); |
| 32 | + if (shardId.getLength() == 0) { |
| 33 | + return new VerificationResult<>("ShardIdMatchesStateIdRule", VerificationStatus.OK); |
| 34 | + } |
| 35 | + |
| 36 | + if (!shardId.isPrefixOf(stateId.getData())) { |
| 37 | + return new VerificationResult<>("ShardIdMatchesStateIdRule", VerificationStatus.FAIL); |
| 38 | + } |
| 39 | + |
| 40 | + return new VerificationResult<>("ShardIdMatchesStateIdRule", VerificationStatus.OK); |
| 41 | + } |
| 42 | +} |
0 commit comments