Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fvm: return gasused when verifying consensus faults #235

Merged
merged 2 commits into from
Feb 18, 2022
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
15 changes: 8 additions & 7 deletions cgo/extern.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"unsafe"

"github.com/filecoin-project/go-address"

"github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/go-state-types/crypto"
)
Expand Down Expand Up @@ -69,6 +70,7 @@ func cgo_extern_verify_consensus_fault(
miner_id *C.uint64_t,
epoch *C.int64_t,
fault *C.int64_t,
gas_used *C.int64_t,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given that we're already using an "out" pointer here, what if we passed in a pointer to some gas_remaining variable? That would let us stop early without changing the parameters. I.e.:

  1. Read the current gas remaining from the gas_remaining parameter.
  2. Update it locally, stopping if/when we run out.
  3. Write back the final value.

Copy link
Member

@Stebalien Stebalien Feb 16, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although I guess this might complicate some other parts of the API a bit? We can always revisit this later.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that's a fair point...I'm gonna open an issue and kick the can down the road for now

) C.int32_t {
externs := Lookup(uint64(handle))
if externs == nil {
Expand All @@ -79,19 +81,18 @@ func cgo_extern_verify_consensus_fault(
h2Go := C.GoBytes(unsafe.Pointer(h2), h2_len)
extraGo := C.GoBytes(unsafe.Pointer(extra), extra_len)

res, err := externs.VerifyConsensusFault(context.TODO(), h1Go, h2Go, extraGo)
res, gas := externs.VerifyConsensusFault(context.TODO(), h1Go, h2Go, extraGo)
*gas_used = C.int64_t(gas)
*fault = C.int64_t(res.Type)

switch err {
case nil:
if res.Type != ConsensusFaultNone {
id, err := address.IDFromAddress(res.Target)
if err != nil {
return ErrIO
}
*epoch = C.int64_t(res.Epoch)
*fault = C.int64_t(res.Type)
*miner_id = C.uint64_t(id)
return 0
default:
return ErrIO
}

return 0
}
4 changes: 2 additions & 2 deletions cgo/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type ConsensusFault struct {
type ConsensusFaultType int64

const (
//ConsensusFaultNone ConsensusFaultType = 0
ConsensusFaultNone ConsensusFaultType = 0
ConsensusFaultDoubleForkMining ConsensusFaultType = 1
ConsensusFaultParentGrinding ConsensusFaultType = 2
ConsensusFaultTimeOffsetMining ConsensusFaultType = 3
Expand All @@ -30,7 +30,7 @@ const (
type Externs interface {
GetChainRandomness(ctx context.Context, personalization crypto.DomainSeparationTag, epoch abi.ChainEpoch, entropy []byte) ([]byte, error)
GetBeaconRandomness(ctx context.Context, personalization crypto.DomainSeparationTag, epoch abi.ChainEpoch, entropy []byte) ([]byte, error)
VerifyConsensusFault(ctx context.Context, h1, h2, extra []byte) (*ConsensusFault, error)
VerifyConsensusFault(ctx context.Context, h1, h2, extra []byte) (*ConsensusFault, int64)

blockstore.Blockstore
blockstore.Viewer
Expand Down
50 changes: 25 additions & 25 deletions rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions rust/src/fvm/cgo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ extern "C" {
miner_id: *mut u64,
epoch: *mut i64,
fault: *mut i64,
gas_used: *mut i64,
) -> i32;
}

Expand Down Expand Up @@ -128,6 +129,7 @@ mod mock {
miner_id: *mut u64,
epoch: *mut i64,
fault: *mut i64,
gas_used: *mut i64,
) -> i32 {
unimplemented!()
}
Expand Down
24 changes: 16 additions & 8 deletions rust/src/fvm/externs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,12 @@ impl Consensus for CgoExterns {
h1: &[u8],
h2: &[u8],
extra: &[u8],
) -> anyhow::Result<Option<ConsensusFault>> {
) -> anyhow::Result<(Option<ConsensusFault>, i64)> {
unsafe {
let mut miner_id: u64 = 0;
let mut epoch: i64 = 0;
let mut fault_type: i64 = 0;
let mut gas_used: i64 = 0;
match cgo_extern_verify_consensus_fault(
self.handle,
h1.as_ptr(),
Expand All @@ -99,17 +100,24 @@ impl Consensus for CgoExterns {
&mut miner_id,
&mut epoch,
&mut fault_type,
&mut gas_used,
) {
0 => Ok(Some(ConsensusFault {
target: Address::new_id(miner_id),
epoch,
fault_type: FromPrimitive::from_i64(fault_type)
.context("invalid fault type")?,
})),
0 => Ok((
match fault_type {
0 => None,
_ => Some(ConsensusFault {
target: Address::new_id(miner_id),
epoch,
fault_type: FromPrimitive::from_i64(fault_type)
.context("invalid fault type")?,
}),
},
gas_used,
)),
r @ 1.. => panic!("invalid return value from has: {}", r),
ERR_NO_EXTERN => panic!("extern {} not registered", self.handle),
e => Err(anyhow!(
"cgo extern 'get_beacon_randomness' failed with error code {}",
"cgo extern 'verify_consensus_fault' failed with error code {}",
e
)),
}
Expand Down