Skip to content

Commit

Permalink
fix ffi code
Browse files Browse the repository at this point in the history
  • Loading branch information
nkysg committed Oct 30, 2023
1 parent 26f6038 commit 56519d2
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 13 deletions.
13 changes: 7 additions & 6 deletions consensus/cryptonight-rs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use libc::{c_char, c_int, c_void, size_t};
extern "C" {
fn cryptonight_hash(
data: *const c_char,
hash: *const c_char,
hash: *mut c_char,
length: size_t,
variant: c_int,
height: u64,
Expand All @@ -13,16 +13,17 @@ extern "C" {

#[link(name = "cryptonight", kind = "static")]
extern "C" {
fn hash_extra_jh(data: *const c_char, length: size_t, hash: *const c_char) -> c_void;
fn hash_extra_jh(data: *const c_char, length: size_t, hash: *mut c_char) -> c_void;
}
const VARIANT: i32 = 4;
const HEIGHT: u64 = 0;

// https://github.com/paritytech/rust-snappy/blob/master/snappy-sys/src/lib.rs#L19
#[allow(clippy::unsound_collection_transmute)]
pub fn cryptonight_r(data: &[u8], size: usize) -> Vec<u8> {
let hash: Vec<i8> = vec![0i8; 32];
let mut hash: Vec<i8> = vec![0i8; 32];
let data_ptr: *const c_char = data.as_ptr() as *const c_char;
let hash_ptr: *const c_char = hash.as_ptr() as *const c_char;
let hash_ptr: *mut c_char = hash.as_mut_ptr() as *mut c_char;
let mut hash = unsafe {
cryptonight_hash(data_ptr, hash_ptr, size, VARIANT, HEIGHT);
std::mem::transmute::<Vec<i8>, Vec<u8>>(hash)
Expand All @@ -33,9 +34,9 @@ pub fn cryptonight_r(data: &[u8], size: usize) -> Vec<u8> {

#[allow(clippy::unsound_collection_transmute)]
pub fn hash_extra_jh_r(data: &[u8], size: usize) -> Vec<u8> {
let hash: Vec<i8> = vec![0i8; 32];
let mut hash: Vec<i8> = vec![0i8; 32];
let data_ptr: *const c_char = data.as_ptr() as *const c_char;
let hash_ptr: *const c_char = hash.as_ptr() as *const c_char;
let hash_ptr: *mut c_char = hash.as_mut_ptr() as *mut c_char;
let mut hash = unsafe {
hash_extra_jh(data_ptr, size, hash_ptr);
std::mem::transmute::<Vec<i8>, Vec<u8>>(hash)
Expand Down
11 changes: 4 additions & 7 deletions scripts/verify_header.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,13 @@ function download() {
}

function usage() {
echo -e "usage: verify_header.sh net to_dir"
echo -e "usage: verify_header.sh net"
echo -e "net is main, barnard, proxima, halley"
echo -e "to_dir like ~/.starcoin/main,~/.starcoin/barnard"
}

function import_block() {
function verify_header() {
net=$1
block_list=$2
to_dir=$3

download "$net" "$block_list"

Expand All @@ -62,15 +60,14 @@ function import_block() {
echo -e "$net verify-header succ"
}

if [ $# != 2 ]; then
if [ $# != 1 ]; then
usage
exit 1
fi
net=$1
to_dir=$2
case $net in
"main" | "barnard" | "proxima" |"halley")
import_block "$net" block_list.csv "$to_dir"
verify_header "$net" block_list.csv
;;
*)
echo "$net not supported"
Expand Down

0 comments on commit 56519d2

Please sign in to comment.