Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add core API server in Rust #91

Merged
merged 18 commits into from
Aug 26, 2022
Merged

Add core API server in Rust #91

merged 18 commits into from
Aug 26, 2022

Conversation

LukasGasior1
Copy link
Contributor

Initial work towards core API server in Rust.

@LukasGasior1 LukasGasior1 marked this pull request as draft August 18, 2022 19:04
@LukasGasior1 LukasGasior1 marked this pull request as ready for review August 23, 2022 11:20
Copy link
Contributor

@dhedey dhedey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really nice work

Have a few misc comments - a few neatenings here or there, and slight questions about our need to us dyn, but basically this is almost ready to merge I think

@@ -96,6 +96,9 @@ protected void configure() {
binder.addBinding(HandlerRoute.get("/system/version")).to(VersionHandler.class);
binder.addBinding(HandlerRoute.get("/system/peers")).to(PeersHandler.class);
binder.addBinding(HandlerRoute.get("/system/addressbook")).to(AddressBookHandler.class);
binder
.addBinding(HandlerRoute.get("/system/network-sync-status"))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved from Core API? Seems sensible

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yup, moved

@@ -73,37 +66,6 @@ public void setSyncStatus(SyncStatus syncStatus) {
}


public NetworkSyncStatusResponse peers(List<Peer> peers) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess peers is returned through some other call?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's already in /system/peers

@@ -141,8 +140,6 @@ public PrometheusService(
InMemorySystemInfo inMemorySystemInfo,
@Self BFTNode self,
Addressing addressing) {
boolean enableTransactions = properties.get("api.transactions.enable", false);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DevOps currently have this on the dashboard - I imagine we'll still have something like this in the new Core API?

Do we need to remove this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess it's something to discuss. Does it still make sense to put this API behind a flag in babylon?

assertThat(bech32.getNodeHrp()).isEqualTo(addressing.forNodes().getHrp());
assertThat(bech32.getValidatorHrp()).isEqualTo(addressing.forValidators().getHrp());
assertThat(bech32.getResourceHrpSuffix()).isEqualTo(addressing.forResources().getHrp());
private static int mkNetworkConfigurationRequest(int port) throws Exception {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does mk mean? Is it short for make?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, not a particularly char-saving abbreviation :D but quite commonly used across Linux

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you be up for making it make to avoid any potential confusion? :D

// Act & Assert #1: start the server and verify it's up
server.start();
final var statusCode = mkNetworkConfigurationRequest(port);
assertEquals(200, statusCode);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the assert fails, will our server be shut down properly, or do we leak unmanaged memory, and possibly threads?

Do you think it might be worth creating some easy-to-use with try-with-resources Core Api runner?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TBH I'm not a big fan of using auto-closeable for long living objects. Seems a bit off to me. I've added a regular try-finally block, if that's okay?


jni = "0.19.0"

# Deps for generated models and the Hyper server
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So many dependencies and transient dependencies :( - are there any options on the generator to remove some of these?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unfortunately there aren't any config options for the generator; I've removed mime and percent-encoding deps which weren't used (all others are)

@@ -0,0 +1,16 @@
/// A workaround for including the symbols defined in state_manager / core_api_server
/// in the output library file. See: https://github.com/rust-lang/rfcs/issues/2771
/// I truly have no idea why this works, but it does.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤣 love it

request_payload: jbyteArray,
) -> StateManagerResult<bool> {
let state_manager = JNIStateManager::get_state_manager(env, sm_instance);
let state_manager = JNIStateManager::get_state_manager(env, j_state_manager);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Side note - Ohh looks like I somehow missed changing these to use the new-fangled method for native functions 🤷

let jni_state_manager = JNIStateManager { state_manager };
let jni_state_manager = JNIStateManager {
state_manager,
// tokio_runtime,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this can be removed now :D

&self,
_context: &C,
) -> Result<StatusNetworkConfigurationPostResponse, ApiError> {
let network = &self
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Heads up - this will need changing after my network merge

pub struct JNIStateManager {
pub state_manager: ActualStateManager,
pub state_manager: Arc<Mutex<dyn StateManager + 'static + Send + Sync>>,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we stick to the jni rw lock for now? Feels too early to start implementing multithreaded locks around state manager?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Main reason is that the server gets it's own copy of the StateManager arc and needs to be able to lock on it outside of the jni context.

}
}

pub struct StateManagerImpl<M: Mempool, S> {
Copy link
Member

@talekhinezh talekhinezh Aug 24, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

StateManagerImpl vs StateManager trait feels like a broken use of dyn and traits. Let's go over this sometime tomorrow.

@sonarqubecloud
Copy link

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 10 Code Smells

52.0% 52.0% Coverage
0.0% 0.0% Duplication

Copy link
Contributor

@dhedey dhedey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! I'd be happy to get this merged. I think Josh's objections have been dealt with, but if not, we can fix in a follow up PR? :)

@@ -98,7 +99,8 @@ public enum Network {
LOCALSIMULATOR(242 /* 0xF1 */, "simulator", "sim", GenesisSource.fromConfiguration);

// For the Radix Shell to provide a default
public static final String DefaultHexGenesisTransaction = "01";
public static final String DefaultHexGenesisTransaction =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a bit confused - why does this say Transaction if it's generating a public key?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've changed the genesis from an integer (num of validators), to a list of validators pub keys a while back

.type(NetworkNotSupportedError.class.getSimpleName()));
}
/**
* Stores a pointer to the rust state manager across JNI calls. In the JNI model, this is
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this comment still needs updating - it says "rust state manager" but the pointer says "Core API"

]

[[package]]
name = "android_system_properties"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:'( yay open source. I just don't know how we somehow end up pulling in an android_system_properties library via transitive dependencies...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

chrono (provides DateTime) -> iana-time-zone -> android_system_properties 🤷‍♂️

api_version: API_VERSION.to_string(),
},
network_identifier: NetworkIdentifier {
network: format!("{:?}", network),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you want network.logical_name here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 will do in the follow up PR (#95)

.to_string(),
validator_hrp: "TODO".to_string(),
node_hrp: "TODO".to_string(),
resource_hrp_suffix: hrp_set
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is all a bit outdated hmm. We should probably change this to account, resource, system, component. Or maybe just return the network_hrp_suffix.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 will do in the follow up PR (#95)

@LukasGasior1 LukasGasior1 merged commit a2291ea into main Aug 26, 2022
@LukasGasior1 LukasGasior1 deleted the feature/openapi-rust branch August 26, 2022 12:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants