Skip to content

Commit

Permalink
Feature/mesh api mempool (#1019)
Browse files Browse the repository at this point in the history
- Implement `mempool` endpoints
- Implement `mempool` tests in Java
  • Loading branch information
lrubasze authored Nov 13, 2024
2 parents 547957a + 86767bd commit add63fe
Show file tree
Hide file tree
Showing 36 changed files with 1,457 additions and 528 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
4 changes: 4 additions & 0 deletions core-rust/mesh-api-server/src/mesh_api/conversions/common.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
use crate::prelude::*;

pub fn from_hex<T: AsRef<[u8]>>(v: T) -> Result<Vec<u8>, ExtractionError> {
hex::decode(v).map_err(|_| ExtractionError::InvalidHex)
}

pub fn to_mesh_api_operation(
mapping_context: &MappingContext,
database: &StateManagerDatabase<impl ReadableRocks>,
Expand Down
17 changes: 17 additions & 0 deletions core-rust/mesh-api-server/src/mesh_api/conversions/hashes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,20 @@ pub fn to_api_transaction_hash_bech32m<T: IsTransactionHash>(
.encode(hash)
.map_err(|err| MappingError::InvalidTransactionHash { encode_error: err })
}

pub fn extract_transaction_intent_hash(
context: &ExtractionContext,
hash_str: String,
) -> Result<TransactionIntentHash, ExtractionError> {
from_hex(&hash_str)
.ok()
.and_then(|bytes| Hash::try_from(bytes.as_slice()).ok())
.map(TransactionIntentHash::from_hash)
.or_else(|| {
context
.transaction_hash_decoder
.validate_and_decode(&hash_str)
.ok()
})
.ok_or(ExtractionError::InvalidHash)
}
62 changes: 62 additions & 0 deletions core-rust/mesh-api-server/src/mesh_api/handlers/mempool.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
use crate::prelude::*;

pub(crate) async fn handle_mempool(
state: State<MeshApiState>,
Json(request): Json<models::NetworkRequest>,
) -> Result<Json<models::MempoolResponse>, ResponseError> {
assert_matching_network(&request.network_identifier, &state.network)?;

let mapping_context = MappingContext::new(&state.network);
let mempool = state.state_manager.mempool.read();

Ok(Json(models::MempoolResponse::new(
mempool
.all_hashes_iter()
.map(|(intent_hash, _)| {
Ok(models::TransactionIdentifier {
hash: to_api_transaction_hash_bech32m(&mapping_context, intent_hash)?,
})
})
.collect::<Result<Vec<_>, MappingError>>()?,
)))
}

pub(crate) async fn handle_mempool_transaction(
state: State<MeshApiState>,
Json(request): Json<models::MempoolTransactionRequest>,
) -> Result<Json<models::MempoolTransactionResponse>, ResponseError> {
assert_matching_network(&request.network_identifier, &state.network)?;

let extraction_context = ExtractionContext::new(&state.network);
let mapping_context = MappingContext::new(&state.network);
let mempool = state.state_manager.mempool.read();

let intent_hash = extract_transaction_intent_hash(
&extraction_context,
request.transaction_identifier.hash.clone(),
)
.map_err(|err| err.into_response_error("intent_hash"))?;

if mempool
.get_notarized_transaction_hashes_for_intent(&intent_hash)
.is_empty()
{
return Err(
ResponseError::from(ApiError::TransactionNotFound).with_details(format!(
"transaction {} not found in mempool transactions",
&request.transaction_identifier.hash
)),
);
}

// TODO:MESH prepare transaction estimates
let transaction = models::Transaction::new(
models::TransactionIdentifier::new(to_api_transaction_hash_bech32m(
&mapping_context,
&intent_hash,
)?),
vec![],
);

Ok(Json(models::MempoolTransactionResponse::new(transaction)))
}
2 changes: 2 additions & 0 deletions core-rust/mesh-api-server/src/mesh_api/handlers/mod.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
mod account_balance;
mod block;
mod mempool;
mod network_list;
mod network_options;
mod network_status;

pub(crate) use account_balance::*;
pub(crate) use block::*;
pub(crate) use mempool::*;
pub(crate) use network_list::*;
pub(crate) use network_options::*;
pub(crate) use network_status::*;
4 changes: 2 additions & 2 deletions core-rust/mesh-api-server/src/mesh_api/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ pub async fn create_server<F>(
.route("/block", post(handle_block))
.route("/block/transaction", post(handle_block_transaction))
// TODO:MESH mempool
.route("/mempool", post(handle_endpoint_todo))
.route("/mempool/transaction", post(handle_endpoint_todo))
.route("/mempool", post(handle_mempool))
.route("/mempool/transaction", post(handle_mempool_transaction))
.route("/construction/derive", post(handle_endpoint_todo))
.route("/construction/preprocess", post(handle_endpoint_todo))
.route("/construction/metadata", post(handle_endpoint_todo))
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 add63fe

Please sign in to comment.