Skip to content

Commit

Permalink
Add methods nativeCreateBlockProof and nativeCreateIndexProof
Browse files Browse the repository at this point in the history
  • Loading branch information
Ilya Bogdanov committed Jan 10, 2020
1 parent 9213aa5 commit fab544b
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
69 changes: 69 additions & 0 deletions exonum-java-binding/core/rust/src/storage/blockchain.rs
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.

Copy link
@dmitry-timofeev

dmitry-timofeev Jan 10, 2020

Contributor

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)?

/// - 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.

}
});
utils::unwrap_exc_or(&env, res, ptr::null_mut())
}
2 changes: 2 additions & 0 deletions exonum-java-binding/core/rust/src/storage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

mod blockchain;
mod db;
mod entry;
mod fork;
Expand All @@ -27,6 +28,7 @@ mod raw_proof_map_index;
mod temporarydb;
mod value_set_index;

pub use self::blockchain::*;
pub use self::db::Java_com_exonum_binding_core_storage_database_Views_nativeFree;
pub(crate) use self::db::View;
pub use self::key_set_index::*;
Expand Down

0 comments on commit fab544b

Please sign in to comment.