-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add methods nativeCreateBlockProof and nativeCreateIndexProof
- Loading branch information
Ilya Bogdanov
committed
Jan 10, 2020
1 parent
9213aa5
commit fab544b
Showing
2 changed files
with
71 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
use exonum::{blockchain::Schema, helpers::Height, runtime::SnapshotExt}; | ||
use jni::{ | ||
objects::JObject, | ||
sys::{jbyteArray, jlong, jstring}, | ||
JNIEnv, | ||
}; | ||
|
||
use std::{panic, ptr}; | ||
|
||
use { | ||
handle, | ||
storage::db::{View, ViewRef}, | ||
utils::{self, convert_to_string, proto_to_java_bytes}, | ||
}; | ||
|
||
/// Returns IndexProof (serialized to protobuf) for specified index. | ||
/// | ||
/// Throws exception and returns null if | ||
/// - index is not initialized (index have not been used before calling the method) | ||
This comment has been minimized.
Sorry, something went wrong. |
||
/// - index is not Merkelized | ||
/// - passed `snapshot_handle` is Fork handle | ||
#[no_mangle] | ||
pub extern "system" fn Java_com_exonum_binding_core_blockchain_Blockchain_nativeCreateIndexProof( | ||
env: JNIEnv, | ||
_: JObject, | ||
snapshot_handle: jlong, | ||
full_index_name: jstring, | ||
) -> jbyteArray { | ||
let res = panic::catch_unwind(|| { | ||
let name = convert_to_string(&env, full_index_name)?; | ||
let db = handle::cast_handle::<View>(snapshot_handle); | ||
match db.get() { | ||
ViewRef::Snapshot(snapshot) => { | ||
let proof = snapshot.proof_for_index(&name).unwrap(); | ||
proto_to_java_bytes(&env, proof) | ||
} | ||
ViewRef::Fork(_) => panic!("nativeCreateIndexProof called with Fork"), | ||
} | ||
}); | ||
utils::unwrap_exc_or(&env, res, ptr::null_mut()) | ||
} | ||
|
||
/// Returns BlockProof (serialized to protobuf) for specified block. | ||
/// | ||
/// Throws exception and returns null if | ||
/// - there is no such block | ||
/// - passed `snapshot_handle` is Fork handle | ||
#[no_mangle] | ||
pub extern "system" fn Java_com_exonum_binding_core_blockchain_Blockchain_nativeCreateBlockProof( | ||
env: JNIEnv, | ||
_: JObject, | ||
snapshot_handle: jlong, | ||
block_height: jlong, | ||
) -> jbyteArray { | ||
let res = panic::catch_unwind(|| { | ||
let db = handle::cast_handle::<View>(snapshot_handle); | ||
match db.get() { | ||
ViewRef::Snapshot(snapshot) => { | ||
let schema = Schema::new(snapshot); | ||
let proof = schema | ||
.block_and_precommits(Height(block_height as u64)) | ||
.unwrap(); | ||
proto_to_java_bytes(&env, proof) | ||
} | ||
ViewRef::Fork(_) => panic!("nativeCreateBlockProof called with Fork"), | ||
This comment has been minimized.
Sorry, something went wrong. |
||
} | ||
}); | ||
utils::unwrap_exc_or(&env, res, ptr::null_mut()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Why doesn't it return a proof of absence (no such index)? Especially as an index is not "initialized" unless (?) something is recorded in it? Doesn't that place extra burden on the service developers to "initialize" an index? Shall they now write an extra "initialize" method in a schema of each service (and we — document that)?