Skip to content
This repository has been archived by the owner on Oct 9, 2022. It is now read-only.

support nonblock & block dag api #148

Merged
merged 3 commits into from
Jan 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions api/src/ethereum/proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,14 @@ pub struct ProposalReq {
pub target: u64,
/// The last leaf of mmr
pub last_leaf: u64,
// Block or Unblock for generate ethash
// pub block: bool,
}

impl ProposalReq {
/// Get `EtHashProof`
fn ethash_proof(&self, api: &str) -> Result<Vec<EthashProofJson>> {
let proof = ffi::proof(api, self.target);
// let y = &mut bytes!(proof.as_str()).as_ref();
let proof = ffi::proof(api, self.target, false)?;
let proof_vec_u8 = bytes(proof.as_str())?;
let result = <Vec<EthashProof>>::decode(&mut proof_vec_u8.as_ref())?
.iter()
Expand Down Expand Up @@ -69,4 +70,4 @@ pub async fn handle(req: Json<ProposalReq>, app_data: Data<AppData>) -> impl Res
Ok(result) => Json(ProofResult::EthereumRelayProofs(result)),
Err(err) => Json(ProofResult::Error(err.to_json()))
}
}
}
3 changes: 2 additions & 1 deletion ffi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ include = [
]

[dependencies]
libc = "0.2.71"
libc = "0.2.71"
anyhow = "1.0.34"
10 changes: 5 additions & 5 deletions ffi/pkg/shadow/ffi/mod.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,20 +55,20 @@ func EpochWait(blockno uint64) bool {
}

//export Proof
func Proof(api string, number uint64) *C.char {
func Proof(api string, number uint64, wait bool) (*C.char, *C.char) {
header, err := eth.Header(api, number)
if err != nil {
log.Error("get ethashproof when get header failed %v", err)
return C.CString("")
return nil, C.CString(err.Error())
}

_, proof, err := ethproof.Proof(&header, true)
_, proof, err := ethproof.Proof(&header, wait)
if err != nil {
log.Error("get ethashproof when get proof failed %v", err)
return C.CString("")
return nil, C.CString(err.Error())
}

return C.CString(eth.EncodeProofArray(proof))
return C.CString(eth.EncodeProofArray(proof)), nil
}

//export Receipt
Expand Down
36 changes: 24 additions & 12 deletions ffi/src/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::{
slice,
};
use std::convert::TryInto;
use anyhow::{anyhow, Result};

#[repr(C)]
struct GoString {
Expand All @@ -20,6 +21,12 @@ struct GoTuple {
header_hash: *const c_char,
}

#[repr(C)]
struct EthashProof {
proof: *const c_char,
error: *const c_char,
}

extern "C" fn geth_handler(x: *const c_char, size: c_int, arg: *mut c_void) -> bool {
unsafe {
let receiver: &mut &mut dyn FnMut(Vec<[u8; 32]>) -> bool = &mut *(arg as *mut &mut dyn FnMut(Vec<[u8; 32]>) -> bool);
Expand All @@ -38,7 +45,7 @@ extern "C" fn geth_handler(x: *const c_char, size: c_int, arg: *mut c_void) -> b
#[link(name = "darwinia_shadow")]
extern "C" {
fn Import(path: GoString, genesis: GoString, from: c_ulonglong, to: c_ulonglong, batch: c_ulonglong, f: Option<extern "C" fn(x: *const c_char, len: c_int, arg: *mut c_void) -> bool>, arg: *mut c_void) -> bool;
fn Proof(api: GoString, number: libc::c_uint) -> *const c_char;
fn Proof(api: GoString, number: c_ulonglong, block: bool) -> EthashProof;
fn Receipt(api: GoString, tx: GoString) -> GoTuple;
fn Epoch(input: libc::c_uint) -> bool;
fn EpochWait(input: libc::c_uint) -> bool;
Expand Down Expand Up @@ -76,18 +83,22 @@ impl Drop for WrapperCString {
}

/// Proof eth header by number
pub fn proof(api: &str, block: u64) -> String {
pub fn proof(api: &str, number: u64, wait: bool) -> Result<String> {
let c_api = CString::new(api).expect("CString::new failed");
unsafe {
WrapperCString::new(
Proof(
GoString {
a: c_api.as_ptr(),
b: c_api.as_bytes().len() as i64,
},
block as u32,
)
).to_string()
let ethashproof = Proof(
GoString {
a: c_api.as_ptr(),
b: c_api.as_bytes().len() as i64,
},
number,
wait,
);
if ethashproof.error.is_null() {
Ok(WrapperCString::new(ethashproof.proof).to_string())
} else {
Err(anyhow!(WrapperCString::new(ethashproof.error).to_string()))
}
}
}

Expand Down Expand Up @@ -172,7 +183,8 @@ mod test {
super::proof(
"https://ropsten.infura.io/v3/0bfb9acbb13c426097aabb1d81a9d016",
1,
);
true,
).unwrap();
super::stop();
}

Expand Down