Skip to content
Open
Show file tree
Hide file tree
Changes from 9 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
1 change: 1 addition & 0 deletions contracts/binding/src/lib_quorum.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Copyright 2022-2024 Protocol Labs
pub use lib_quorum::*;
/// This module was auto-generated with ethers-rs Abigen.
/// More information at: <https://github.com/gakonst/ethers-rs>
Expand Down
68 changes: 61 additions & 7 deletions fendermint/actors/blobs/src/actor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// SPDX-License-Identifier: Apache-2.0, MIT

use std::collections::HashSet;
use std::ops::Mul;

use fendermint_actor_blobs_shared::params::{
AddBlobParams, ApproveCreditParams, BuyCreditParams, DeleteBlobParams, FinalizeBlobParams,
Expand All @@ -15,14 +16,15 @@ use fendermint_actor_blobs_shared::state::{
use fendermint_actor_blobs_shared::Method;
use fil_actors_runtime::runtime::builtins::Type;
use fil_actors_runtime::{
actor_dispatch, actor_error, deserialize_block,
actor_dispatch, actor_error, deserialize_block, extract_send_result,
runtime::{ActorCode, Runtime},
ActorError, AsActorError, FIRST_EXPORTED_METHOD_NUMBER, SYSTEM_ACTOR_ADDR,
ActorError, AsActorError, BURNT_FUNDS_ACTOR_ADDR, FIRST_EXPORTED_METHOD_NUMBER,
SYSTEM_ACTOR_ADDR,
};
use fvm_ipld_encoding::ipld_block::IpldBlock;
use fvm_shared::address::Address;
use fvm_shared::sys::SendFlags;
use fvm_shared::{error::ExitCode, MethodNum};
use fvm_shared::{error::ExitCode, MethodNum, METHOD_SEND};
use num_traits::Zero;

use crate::{ext, ConstructorParams, State, BLOBS_ACTOR_NAME};
Expand All @@ -34,6 +36,9 @@ pub struct BlobsActor;

type BlobTuple = (Hash, HashSet<(Address, PublicKey)>);

/// Proportion of tokens to be burned, in bps.
const BURNED_PROPORTION_BPS: u16 = 5000;

impl BlobsActor {
fn constructor(rt: &impl Runtime, params: ConstructorParams) -> Result<(), ActorError> {
rt.validate_immediate_caller_is(std::iter::once(&SYSTEM_ACTOR_ADDR))?;
Expand All @@ -57,9 +62,20 @@ impl BlobsActor {
recipient
)));
}
rt.transaction(|st: &mut State, rt| {
st.buy_credit(recipient, rt.message().value_received(), rt.curr_epoch())
})
let amount_received = rt.message().value_received();
let amount_to_burn = amount_received.div_floor(10000).mul(BURNED_PROPORTION_BPS);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

maybe good to add a comment stating the unit of "value received" so it is easy to understand this expression.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Ok.

let account = rt.transaction(|st: &mut State, rt| {
st.buy_credit(recipient, amount_received, rt.curr_epoch())
});

// TODO When virtual gas is in place, the gas actor should be set as a destination
extract_send_result(rt.send_simple(
&BURNT_FUNDS_ACTOR_ADDR,
METHOD_SEND,
None,
amount_to_burn,
))?;
account
}

fn approve_credit(
Expand Down Expand Up @@ -498,8 +514,18 @@ mod tests {
rt.set_origin(id_addr);

let mut expected_credits = BigInt::from(1000000000000000000u64);
rt.set_received(TokenAmount::from_whole(1));
let received_0 = TokenAmount::from_whole(1);
rt.set_received(received_0.clone());
rt.expect_validate_caller_any();
rt.set_balance(received_0.clone());
rt.expect_send_simple(
BURNT_FUNDS_ACTOR_ADDR,
METHOD_SEND,
None,
TokenAmount::from_whole(1).div_floor(2), // 0.5 gets burned
None,
ExitCode::OK,
);
let fund_params = BuyCreditParams(f4_eth_addr);
let result = rt
.call::<BlobsActor>(
Expand All @@ -516,6 +542,15 @@ mod tests {
expected_credits += BigInt::from(1000000000u64);
rt.set_received(TokenAmount::from_nano(1));
rt.expect_validate_caller_any();
rt.set_balance(TokenAmount::from_nano(1) + TokenAmount::from_whole(42));
rt.expect_send_simple(
BURNT_FUNDS_ACTOR_ADDR,
METHOD_SEND,
None,
TokenAmount::from_nano(1).div_floor(2), // 0.5 gets burned
None,
ExitCode::OK,
);
let fund_params = BuyCreditParams(f4_eth_addr);
let result = rt
.call::<BlobsActor>(
Expand All @@ -533,6 +568,15 @@ mod tests {
rt.set_received(TokenAmount::from_atto(1));
rt.expect_validate_caller_any();
let fund_params = BuyCreditParams(f4_eth_addr);
rt.set_balance(TokenAmount::from_atto(1) + TokenAmount::from_whole(42));
rt.expect_send_simple(
BURNT_FUNDS_ACTOR_ADDR,
METHOD_SEND,
None,
TokenAmount::from_atto(1).div_floor(2),
None,
ExitCode::OK,
);
let result = rt
.call::<BlobsActor>(
Method::BuyCredit as u64,
Expand Down Expand Up @@ -771,12 +815,22 @@ mod tests {

// Fund an account
rt.set_received(TokenAmount::from_whole(1));
rt.set_balance(TokenAmount::from_whole(1));
rt.expect_validate_caller_any();
rt.expect_send_simple(
BURNT_FUNDS_ACTOR_ADDR,
METHOD_SEND,
None,
TokenAmount::from_whole(1).div_ceil(2), // 0.5 gets burned
None,
ExitCode::OK,
);
let fund_params = BuyCreditParams(f4_eth_addr);
let result = rt.call::<BlobsActor>(
Method::BuyCredit as u64,
IpldBlock::serialize_cbor(&fund_params).unwrap(),
);
println!("r.0 {:?}", result);
assert!(result.is_ok());
rt.verify();

Expand Down
1 change: 1 addition & 0 deletions fendermint/actors/blobs/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ impl State {
account.last_debit_epoch = current_epoch;
debug!("debited {} credits from {}", debit, address);
}

Ok(delete_from_disc)
}

Expand Down
1 change: 1 addition & 0 deletions fendermint/rpc/src/response.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Copyright 2022-2024 Protocol Labs
// Copyright 2022-2024 Protocol Labs

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

double copyright

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Right. Removed.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

twice as nice

// SPDX-License-Identifier: Apache-2.0, MIT
use anyhow::{anyhow, Context};
use base64::Engine;
Expand Down