Skip to content

Commit

Permalink
test: Add test for duplicate submission of subintent (#1015)
Browse files Browse the repository at this point in the history
## Summary

Adds a test for duplicate submission of subintent, with an assertion
commented out with a TODO:CUTTLEFISH which I'll fix in the next PR.

Also includes some other test-related refactors to commonise/unify test
helpers.
  • Loading branch information
dhedey authored Nov 7, 2024
2 parents f67e4b5 + befe08d commit b80edf2
Show file tree
Hide file tree
Showing 28 changed files with 888 additions and 526 deletions.
2 changes: 1 addition & 1 deletion cli-tools/src/main/java/com/radixdlt/shell/RadixShell.java
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ public Node build() throws Exception {
if (properties.get("network.genesis_data", "").isEmpty()) {
final var encodedGenesisData =
NodeSborCodecs.encode(
GenesisData.testingWithSingleValidator(),
GenesisData.withSingleValidatorForRadixShell(),
NodeSborCodecs.resolveCodec(new TypeToken<>() {}));
final var compressedGenesisData = Compress.compress(encodedGenesisData);
final var genesisDataBase64 = Base64.getEncoder().encodeToString(compressedGenesisData);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,9 @@ public static void registerCodec(CodecMap codecMap) {
GenesisData.class, codecs -> StructCodec.fromRecordComponents(GenesisData.class, codecs));
}

public static GenesisData testingWithSingleValidator() {
// NOTE: From tests, see also GenesisBuilder

public static GenesisData withSingleValidatorForRadixShell() {
final var validatorKey = ECKeyPair.fromSeed(new byte[] {0x02}).getPublicKey();
return new GenesisData(
UInt64.fromNonNegativeLong(1L),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,10 @@
import com.radixdlt.sbor.codec.CodecMap;
import com.radixdlt.sbor.codec.StructCodec;
import com.radixdlt.utils.UInt64;
import java.util.List;

public record PrepareTransactionIntentV2Request(
NetworkDefinition network, TransactionHeader header, UInt64 subintentCount) {
NetworkDefinition network, TransactionHeader header, List<UInt64> subintentCount) {
public static void registerCodec(CodecMap codecMap) {
codecMap.register(
PrepareTransactionIntentV2Request.class,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,11 @@ public static PreparedIntent prepareIntent(
Natives.builder(TransactionPreparer::prepareIntent).build(new TypeToken<>() {});

public static PreparedTransactionIntentV2 prepareTransactionIntentV2(
NetworkDefinition network, TransactionHeader header, long subintentCount) {
NetworkDefinition network, TransactionHeader header, List<Integer> subintentDiscriminators) {
var rustSubintentDiscriminators =
subintentDiscriminators.stream().map(UInt64::fromNonNegativeLong).toList();
return prepareTransactionIntentV2Func
.call(
new PrepareTransactionIntentV2Request(
network, header, UInt64.fromNonNegativeLong(subintentCount)))
.call(new PrepareTransactionIntentV2Request(network, header, rustSubintentDiscriminators))
.unwrap(TransactionPreparationException::new);
}

Expand All @@ -131,15 +131,18 @@ public static PreparedSignedIntent prepareSignedIntent(
Natives.builder(TransactionPreparer::prepareSignedIntent).build(new TypeToken<>() {});

public static PreparedSignedIntent prepareSignedIntentV2(
byte[] transactionIntent, List<SignatureWithPublicKey> signatures) {
byte[] transactionIntent,
List<SignatureWithPublicKey> signatures,
List<List<SignatureWithPublicKey>> subintentSignatures) {

return prepareSignedTransactionIntentV2Func
.call(tuple(transactionIntent, signatures))
.call(tuple(transactionIntent, signatures, subintentSignatures))
.unwrap(TransactionPreparationException::new);
}

private static final Natives.Call1<
Tuple.Tuple2<byte[], List<SignatureWithPublicKey>>, Result<PreparedSignedIntent, String>>
Tuple.Tuple3<byte[], List<SignatureWithPublicKey>, List<List<SignatureWithPublicKey>>>,
Result<PreparedSignedIntent, String>>
prepareSignedTransactionIntentV2Func =
Natives.builder(TransactionPreparer::prepareSignedTransactionIntentV2)
.build(new TypeToken<>() {});
Expand Down
34 changes: 23 additions & 11 deletions core-rust/state-manager/src/jni/transaction_preparer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ extern "system" fn Java_com_radixdlt_transaction_TransactionPreparer_prepareInte
struct PrepareTransactionIntentV2Request {
network_definition: NetworkDefinition,
header: TransactionHeaderJava,
subintent_count: usize,
subintent_discriminators: Vec<u64>,
}

#[derive(Debug, Clone, PartialEq, Eq, ScryptoSbor)]
Expand All @@ -144,7 +144,7 @@ extern "system" fn Java_com_radixdlt_transaction_TransactionPreparer_prepareTran
let PrepareTransactionIntentV2Request {
network_definition,
header,
subintent_count,
subintent_discriminators,
} = request;

let mut subintent_hashes = vec![];
Expand All @@ -165,24 +165,25 @@ extern "system" fn Java_com_radixdlt_transaction_TransactionPreparer_prepareTran
intent_discriminator: header.nonce as u64,
});

for i in 0..subintent_count {
for subintent_discriminator in subintent_discriminators {
let mut subintent_builder: PartialTransactionV2Builder = PartialTransactionV2Builder::new()
.intent_header(IntentHeaderV2 {
network_id: network_definition.id,
start_epoch_inclusive: Epoch::of(header.start_epoch_inclusive),
end_epoch_exclusive: Epoch::of(header.end_epoch_exclusive),
min_proposer_timestamp_inclusive: None,
max_proposer_timestamp_exclusive: None,
intent_discriminator: (header.nonce as u64) * 1000 + (i as u64),
intent_discriminator: subintent_discriminator,
})
.manifest_builder(|builder| {
builder
.yield_to_parent(())
});
let child_name = format!("child-{subintent_discriminator}");
subintent_hashes.push(subintent_builder.subintent_hash());
subintent_names.push(format!("child-{i}"));
subintent_names.push(child_name.clone());
transaction_builder = transaction_builder.add_signed_child(
format!("child-{i}"),
child_name,
subintent_builder.build(),
);
}
Expand Down Expand Up @@ -330,7 +331,7 @@ impl From<CurveDecryptorSetJava> for DecryptorsByCurve {
#[derive(Debug, Clone, PartialEq, Eq, ScryptoSbor)]
struct PrepareSignedIntentRequest {
intent_bytes: RawTransactionIntent,
signatures: Vec<SignatureWithPublicKeyV1>,
transaction_signatures: Vec<SignatureWithPublicKeyV1>,
}

#[derive(Debug, Clone, PartialEq, Eq, ScryptoSbor)]
Expand All @@ -354,7 +355,7 @@ extern "system" fn Java_com_radixdlt_transaction_TransactionPreparer_prepareSign
intent: IntentV1::from_raw(&request.intent_bytes)?,
intent_signatures: IntentSignaturesV1 {
signatures: request
.signatures
.transaction_signatures
.into_iter()
.map(IntentSignatureV1)
.collect(),
Expand All @@ -372,6 +373,13 @@ extern "system" fn Java_com_radixdlt_transaction_TransactionPreparer_prepareSign
)
}

#[derive(Debug, Clone, PartialEq, Eq, ScryptoSbor)]
struct PrepareSignedTransactionIntentV2Request {
intent_bytes: RawTransactionIntent,
transaction_signatures: Vec<SignatureWithPublicKeyV1>,
subintent_signatures: Vec<Vec<SignatureWithPublicKeyV1>>,
}

#[no_mangle]
extern "system" fn Java_com_radixdlt_transaction_TransactionPreparer_prepareSignedTransactionIntentV2(
env: JNIEnv,
Expand All @@ -381,18 +389,22 @@ extern "system" fn Java_com_radixdlt_transaction_TransactionPreparer_prepareSign
jni_sbor_coded_call(
&env,
request_payload,
|request: PrepareSignedIntentRequest| -> Result<PrepareSignedIntentResponse, StringError> {
|request: PrepareSignedTransactionIntentV2Request| -> Result<PrepareSignedIntentResponse, StringError> {
let signed_intent = SignedTransactionIntentV2 {
transaction_intent: TransactionIntentV2::from_raw(&request.intent_bytes)?,
transaction_intent_signatures: IntentSignaturesV2 {
signatures: request
.signatures
.transaction_signatures
.into_iter()
.map(IntentSignatureV1)
.collect(),
},
non_root_subintent_signatures: NonRootSubintentSignaturesV2 {
by_subintent: vec![],
by_subintent: request.subintent_signatures.into_iter().map(|signatures| {
IntentSignaturesV2 {
signatures: signatures.into_iter().map(IntentSignatureV1).collect(),
}
}).collect(),
},
};

Expand Down
Loading

0 comments on commit b80edf2

Please sign in to comment.