Skip to content

Commit 909fdb0

Browse files
bullet-toothIlya Bogdanov
authored andcommitted
Pass instance status as bytes array [ECR-4119] (#1367)
Update the native to the newer revision (dfe9f7e2) of Core. Pass the instance status as byte array, as int constant no longer work [ECR-4119] Co-authored-by: Ilya Bogdanov <[email protected]>
1 parent c38ae03 commit 909fdb0

File tree

16 files changed

+231
-131
lines changed

16 files changed

+231
-131
lines changed

exonum-java-binding/core/rust/Cargo.lock

Lines changed: 102 additions & 82 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

exonum-java-binding/core/rust/Cargo.toml

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ exonum-cli = "0.13.0-rc.2"
2222
exonum-crypto = "0.13.0-rc.2"
2323
exonum-derive = "0.13.0-rc.2"
2424
exonum-proto = "0.13.0-rc.2"
25+
exonum-rust-runtime = "0.13.0-rc.2"
2526
exonum-supervisor = "0.13.0-rc.2"
2627
exonum-testkit = "0.13.0-rc.2"
2728
exonum-time = "0.13.0-rc.2"
@@ -52,14 +53,15 @@ rpath = true
5253

5354
# FIXME: using git dependency until Exonum 1.0 is released
5455
[patch.crates-io]
55-
exonum = { git = "https://github.com/exonum/exonum", rev = "b81d64ef" }
56-
exonum-testkit = { git = "https://github.com/exonum/exonum", rev = "b81d64ef" }
57-
exonum-time = { git = "https://github.com/exonum/exonum", rev = "b81d64ef" }
58-
exonum-build = { git = "https://github.com/exonum/exonum", rev = "b81d64ef" }
59-
exonum-derive = { git = "https://github.com/exonum/exonum", rev = "b81d64ef" }
60-
exonum-cli = { git = "https://github.com/exonum/exonum", rev = "b81d64ef" }
61-
exonum-explorer-service = { git = "https://github.com/exonum/exonum", rev = "b81d64ef" }
62-
exonum-proto = { git = "https://github.com/exonum/exonum", rev = "b81d64ef" }
63-
exonum-supervisor = { git = "https://github.com/exonum/exonum", rev = "b81d64ef" }
64-
exonum-crypto = { git = "https://github.com/exonum/exonum", rev = "b81d64ef" }
65-
exonum-merkledb = { git = "https://github.com/exonum/exonum", rev = "b81d64ef" }
56+
exonum = { git = "https://github.com/exonum/exonum", rev = "dfe9f7e2" }
57+
exonum-testkit = { git = "https://github.com/exonum/exonum", rev = "dfe9f7e2" }
58+
exonum-time = { git = "https://github.com/exonum/exonum", rev = "dfe9f7e2" }
59+
exonum-build = { git = "https://github.com/exonum/exonum", rev = "dfe9f7e2" }
60+
exonum-derive = { git = "https://github.com/exonum/exonum", rev = "dfe9f7e2" }
61+
exonum-cli = { git = "https://github.com/exonum/exonum", rev = "dfe9f7e2" }
62+
exonum-explorer-service = { git = "https://github.com/exonum/exonum", rev = "dfe9f7e2" }
63+
exonum-proto = { git = "https://github.com/exonum/exonum", rev = "dfe9f7e2" }
64+
exonum-rust-runtime = { git = "https://github.com/exonum/exonum", rev = "dfe9f7e2" }
65+
exonum-supervisor = { git = "https://github.com/exonum/exonum", rev = "dfe9f7e2" }
66+
exonum-crypto = { git = "https://github.com/exonum/exonum", rev = "dfe9f7e2" }
67+
exonum-merkledb = { git = "https://github.com/exonum/exonum", rev = "dfe9f7e2" }

exonum-java-binding/core/rust/exonum-java/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ authors = ["Exonum team <[email protected]>"]
66
[dependencies]
77
env_logger = "0.7"
88
exonum-explorer-service = "0.13.0-rc.2"
9+
exonum-rust-runtime = "0.13.0-rc.2"
910
exonum-supervisor = "0.13.0-rc.2"
1011
exonum-time = "0.13.0-rc.2"
1112
failure = "0.1"

exonum-java-binding/core/rust/exonum-java/src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
extern crate env_logger;
1818
extern crate exonum_explorer_service;
19+
extern crate exonum_rust_runtime;
1920
extern crate exonum_supervisor;
2021
extern crate exonum_time;
2122
extern crate java_bindings;

exonum-java-binding/core/rust/exonum-java/src/node.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616

1717
use exonum_explorer_service::ExplorerFactory;
18+
use exonum_rust_runtime::{DefaultInstance, RustRuntime, RustRuntimeBuilder, ServiceFactory};
1819
use exonum_supervisor::{mode::Mode as SupervisorMode, Supervisor};
1920
use exonum_time::TimeServiceFactory;
2021
use java_bindings::{
@@ -26,7 +27,6 @@ use java_bindings::{
2627
},
2728
exonum_merkledb::{Database, RocksDB},
2829
node::{ApiSender, Node, NodeChannel, NodeConfig as CoreNodeConfig},
29-
runtime::rust::{DefaultInstance, RustRuntime, RustRuntimeBuilder, ServiceFactory},
3030
},
3131
Command, Config, DefaultConfigManager, EjbCommand, EjbCommandResult, Executor, InternalConfig,
3232
JavaRuntimeProxy,
@@ -45,7 +45,8 @@ pub fn run_node(command: Command) -> Result<(), failure::Error> {
4545

4646
fn create_node(config: Config) -> Result<Node, failure::Error> {
4747
let node_config = config.run_config.node_config.clone();
48-
let events_pool_capacity = &node_config.private_config.mempool.events_pool_capacity;
48+
// TODO: rewrite with NodeBuilder API when it is available
49+
let events_pool_capacity = &Default::default();
4950
let channel = NodeChannel::new(events_pool_capacity);
5051
let blockchain = create_blockchain(&config, &channel)?;
5152

@@ -92,7 +93,7 @@ fn create_rust_runtime(channel: &NodeChannel) -> RustRuntime {
9293
.with_factory(TimeServiceFactory::default())
9394
.with_factory(Supervisor)
9495
.with_factory(ExplorerFactory)
95-
.build(channel.endpoints.0.clone())
96+
.build(channel.endpoints_sender())
9697
}
9798

9899
fn create_java_runtime(config: &Config) -> JavaRuntimeProxy {

exonum-java-binding/core/rust/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ extern crate exonum_cli;
2727
#[macro_use]
2828
extern crate exonum_derive;
2929
extern crate exonum_proto;
30+
extern crate exonum_rust_runtime;
3031
extern crate exonum_supervisor;
3132
extern crate failure;
3233
pub extern crate jni;

exonum-java-binding/core/rust/src/proxy/node.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use exonum::{
2222
};
2323
use exonum_merkledb::{ObjectHash, Snapshot};
2424
use failure;
25+
use futures::Future;
2526
use jni::objects::JClass;
2627
use jni::sys::{jbyteArray, jshort};
2728
use jni::JNIEnv;
@@ -66,7 +67,10 @@ impl Node {
6667
let verified = Verified::from_value(tx, pub_key.to_owned(), secret_key);
6768
let tx_hash = verified.object_hash();
6869
// TODO(ECR-3679): check Core behaviour/any errors on service inactivity
69-
self.blockchain.sender().broadcast_transaction(verified)?;
70+
self.blockchain
71+
.sender()
72+
.broadcast_transaction(verified)
73+
.wait()?;
7074
Ok(tx_hash)
7175
}
7276
}

exonum-java-binding/core/rust/src/proxy/runtime.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ use exonum::{
1919
crypto::{Hash, PublicKey},
2020
exonum_merkledb::{BinaryValue, Snapshot},
2121
runtime::{
22+
migrations::{InitMigrationError, MigrationScript},
23+
versioning::Version,
2224
ArtifactId, CallInfo, Caller, ExecutionContext, ExecutionError, InstanceId, InstanceSpec,
2325
InstanceStatus, Mailbox, Runtime, RuntimeIdentifier, SnapshotExt, WellKnownRuntime,
2426
},
@@ -165,13 +167,15 @@ impl Runtime for JavaRuntimeProxy {
165167
&mut self,
166168
_snapshot: &dyn Snapshot,
167169
instance_spec: &InstanceSpec,
168-
status: InstanceStatus,
170+
status: &InstanceStatus,
169171
) {
170172
let serialized_instance_spec: Vec<u8> = instance_spec.to_bytes();
173+
let serialized_instance_status: Vec<u8> = status.to_bytes();
171174
unwrap_jni(self.exec.with_attached(|env| {
172175
let instance_spec =
173176
JObject::from(env.byte_array_from_slice(&serialized_instance_spec)?);
174-
let instance_status = status as i32;
177+
let instance_status =
178+
JObject::from(env.byte_array_from_slice(&serialized_instance_status)?);
175179

176180
panic_on_exception(
177181
env,
@@ -186,6 +190,15 @@ impl Runtime for JavaRuntimeProxy {
186190
}));
187191
}
188192

193+
fn migrate(
194+
&self,
195+
_new_artifact: &ArtifactId,
196+
_data_version: &Version,
197+
) -> Result<Option<MigrationScript>, InitMigrationError> {
198+
// TODO (ECR-3787): implement
199+
Ok(None)
200+
}
201+
189202
fn execute(
190203
&self,
191204
context: ExecutionContext,

exonum-java-binding/core/rust/src/testkit/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ use exonum::{
2323
crypto::{PublicKey, SecretKey},
2424
exonum_merkledb::{self, BinaryValue},
2525
helpers::ValidatorId,
26-
runtime::rust::ServiceFactory,
2726
runtime::ArtifactSpec,
2827
};
2928
use exonum_proto::ProtobufConvert;
29+
use exonum_rust_runtime::ServiceFactory;
3030
use exonum_testkit::{TestKit, TestKitBuilder};
3131
use exonum_time::{time_provider::TimeProvider, TimeServiceFactory};
3232
use jni::{

exonum-java-binding/core/rust/src/utils/jni_cache.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ unsafe fn cache_methods(env: &JNIEnv) {
121121
&env,
122122
SERVICE_RUNTIME_ADAPTER_CLASS,
123123
"updateServiceStatus",
124-
"([BI)V",
124+
"([B[B)V",
125125
);
126126
RUNTIME_ADAPTER_EXECUTE_TX = get_method_id(
127127
&env,

exonum-java-binding/core/src/main/java/com/exonum/binding/core/runtime/ServiceRuntime.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@
2929
import com.exonum.binding.core.transaction.TransactionContext;
3030
import com.exonum.binding.core.transport.Server;
3131
import com.exonum.core.messages.Runtime.ErrorKind;
32-
import com.exonum.core.messages.Runtime.InstanceState;
32+
import com.exonum.core.messages.Runtime.InstanceStatus;
33+
import com.exonum.core.messages.Runtime.InstanceStatus.Simple;
3334
import com.google.common.annotations.VisibleForTesting;
3435
import com.google.inject.Inject;
3536
import com.google.inject.Singleton;
@@ -170,7 +171,7 @@ public boolean isArtifactDeployed(ServiceArtifactId id) {
170171
* Starts registration of a new service instance with the given specification.
171172
* It involves the initial configuration of the service instance with the given parameters.
172173
* The instance is not registered until
173-
* {@link #updateInstanceStatus(ServiceInstanceSpec, InstanceState.Status)}
174+
* {@link #updateInstanceStatus(ServiceInstanceSpec, InstanceStatus)}
174175
* is invoked with the {@code Status=Active}.
175176
*
176177
* @param fork a database access to apply configuration
@@ -253,9 +254,10 @@ public void initializeResumingService(Fork fork, ServiceInstanceSpec instanceSpe
253254
* is not deployed; or unrecognized service status received
254255
*/
255256
public void updateInstanceStatus(ServiceInstanceSpec instanceSpec,
256-
InstanceState.Status instanceStatus) {
257+
InstanceStatus instanceStatus) {
257258
synchronized (lock) {
258-
switch (instanceStatus) {
259+
Simple status = instanceStatus.getSimple();
260+
switch (status) {
259261
case ACTIVE:
260262
activateService(instanceSpec);
261263
break;
@@ -264,7 +266,7 @@ public void updateInstanceStatus(ServiceInstanceSpec instanceSpec,
264266
break;
265267
default:
266268
String msg = String.format("Unexpected status %s received for the service %s",
267-
instanceStatus.name(), instanceSpec.getName());
269+
status, instanceSpec.getName());
268270
logger.error(msg);
269271
throw new IllegalArgumentException(msg);
270272
}

exonum-java-binding/core/src/main/java/com/exonum/binding/core/runtime/ServiceRuntimeAdapter.java

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
import com.exonum.binding.core.transaction.ExecutionException;
3030
import com.exonum.core.messages.Runtime.ArtifactId;
3131
import com.exonum.core.messages.Runtime.InstanceSpec;
32-
import com.exonum.core.messages.Runtime.InstanceState;
32+
import com.exonum.core.messages.Runtime.InstanceStatus;
3333
import com.google.inject.Inject;
3434
import com.google.protobuf.InvalidProtocolBufferException;
3535
import java.util.OptionalInt;
@@ -176,15 +176,14 @@ void initializeResumingService(long forkHandle, byte[] instanceSpec, byte[] argu
176176
*
177177
* @param instanceSpec the service instance specification as a serialized {@link InstanceSpec}
178178
* protobuf message
179-
* @param numericInstanceStatus new status of the service instance as a numeric
180-
* representation of the {@link InstanceState.Status} enum.
181-
* @see ServiceRuntime#updateInstanceStatus(ServiceInstanceSpec, InstanceState.Status)
179+
* @param instanceStatus new status of the service instance as a serialized
180+
* representation of the {@link InstanceStatus} protobuf message
181+
* @see ServiceRuntime#updateInstanceStatus(ServiceInstanceSpec, InstanceStatus)
182182
*/
183-
void updateServiceStatus(byte[] instanceSpec, int numericInstanceStatus) {
183+
void updateServiceStatus(byte[] instanceSpec, byte[] instanceStatus) {
184184
ServiceInstanceSpec javaInstanceSpec = parseInstanceSpec(instanceSpec);
185-
InstanceState.Status instanceStatus =
186-
InstanceState.Status.forNumber(numericInstanceStatus);
187-
serviceRuntime.updateInstanceStatus(javaInstanceSpec, instanceStatus);
185+
InstanceStatus status = parseInstanceStatus(instanceStatus);
186+
serviceRuntime.updateInstanceStatus(javaInstanceSpec, status);
188187
}
189188

190189
private static ServiceInstanceSpec parseInstanceSpec(byte[] instanceSpec) {
@@ -199,6 +198,15 @@ private static ServiceInstanceSpec parseInstanceSpec(byte[] instanceSpec) {
199198
}
200199
}
201200

201+
private static InstanceStatus parseInstanceStatus(byte[] instanceStatus) {
202+
try {
203+
return InstanceStatus.parseFrom(instanceStatus);
204+
} catch (InvalidProtocolBufferException e) {
205+
logger.error(e);
206+
throw new IllegalArgumentException(e);
207+
}
208+
}
209+
202210
/**
203211
* Executes the service transaction.
204212
*

exonum-java-binding/core/src/test/java/com/exonum/binding/core/runtime/ServiceRuntimeAdapterTest.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
import com.exonum.binding.core.storage.database.Snapshot;
3434
import com.exonum.core.messages.Runtime.ArtifactId;
3535
import com.exonum.core.messages.Runtime.InstanceSpec;
36+
import com.exonum.core.messages.Runtime.InstanceStatus;
37+
import com.exonum.core.messages.Runtime.InstanceStatus.Simple;
3638
import org.junit.jupiter.api.BeforeEach;
3739
import org.junit.jupiter.api.Test;
3840
import org.junit.jupiter.api.extension.ExtendWith;
@@ -209,4 +211,25 @@ void afterCommit_AuditorNode() throws CloseFailuresException {
209211
assertThat(event.getValidatorId()).isEmpty();
210212
}
211213

214+
@Test
215+
void updateServiceStatus() {
216+
int serviceId = 1;
217+
String serviceName = "s1";
218+
ArtifactId artifact = ARTIFACT_ID;
219+
InstanceSpec instanceSpec = InstanceSpec.newBuilder()
220+
.setId(serviceId)
221+
.setName(serviceName)
222+
.setArtifact(artifact)
223+
.build();
224+
InstanceStatus status = InstanceStatus.newBuilder()
225+
.setSimple(Simple.ACTIVE)
226+
.build();
227+
228+
serviceRuntimeAdapter
229+
.updateServiceStatus(instanceSpec.toByteArray(), status.toByteArray());
230+
231+
ServiceInstanceSpec expectedSpec = ServiceInstanceSpec
232+
.newInstance(serviceName, serviceId, ServiceArtifactId.fromProto(artifact));
233+
verify(serviceRuntime).updateInstanceStatus(expectedSpec, status);
234+
}
212235
}

exonum-java-binding/core/src/test/java/com/exonum/binding/core/runtime/ServiceRuntimeConfigurationIntegrationTest.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@
2626
import com.exonum.binding.core.storage.database.TemporaryDb;
2727
import com.exonum.binding.test.RequiresNativeLibrary;
2828
import com.exonum.binding.test.runtime.ServiceArtifactBuilder;
29-
import com.exonum.core.messages.Runtime.InstanceState.Status;
29+
import com.exonum.core.messages.Runtime.InstanceStatus;
30+
import com.exonum.core.messages.Runtime.InstanceStatus.Simple;
3031
import com.google.inject.Guice;
3132
import com.google.inject.Injector;
3233
import com.google.inject.Stage;
@@ -81,9 +82,10 @@ void runtimeConfigurationTest(@TempDir Path tmpArtifactDir) throws Exception {
8182
// Initialize and register a service instance
8283
String name = "s1";
8384
ServiceInstanceSpec instanceSpec = ServiceInstanceSpec.newInstance(name, 1, ARTIFACT_ID);
85+
InstanceStatus instanceStatus = InstanceStatus.newBuilder().setSimple(Simple.ACTIVE).build();
8486
Fork fork = database.createFork(cleaner);
8587
runtime.initiateAddingService(fork, instanceSpec, new byte[0]);
86-
runtime.updateInstanceStatus(instanceSpec, Status.ACTIVE);
88+
runtime.updateInstanceStatus(instanceSpec, instanceStatus);
8789
assertThat(runtime.findService(name)).isNotEmpty();
8890
}
8991

0 commit comments

Comments
 (0)