method2Code = `
/// Method num 2.
pub fn say_hello() -> Option<RawBytes> {
let mut state = State::load();
state.count += 1;
let state_cid = state.save();
let ret = to_vec(format!("Hello world #{}! CID: {}", &state.count, &state_cid).as_str());
match ret {
Ok(ret) => Some(RawBytes::new(ret)),
Err(err) => {
abort!(
USR_ILLEGAL_STATE,
"failed to serialize return value: {:?}",
err
);
}
}
}
/// Method num 3.
pub fn get_state_cid() -> Option<RawBytes> {
let state_cid = sdk::sself::root().unwrap();
Some(RawBytes::new(state_cid.to_bytes()))
}
/// Method num 4.
pub fn echo_raw_bytes(params: u32) -> Option<RawBytes> {
let params = sdk::message::params_raw(params).unwrap().1;
let params = RawBytes::new(params);
let ret = to_vec(format!("Params {:?}",
params).as_str());
match ret {
Ok(ret) => Some(RawBytes::new(ret)),
Err(err) => {
abort!(
USR_ILLEGAL_STATE,
"failed to serialize return value: {:?}",
err
);
}
}
}
#[derive(Debug, Serialize_tuple, Deserialize_tuple)]
pub struct CidParams {
pub cid: Cid,
}
/// Method num 5.
pub fn get_state_cid_cbor() -> Option<RawBytes> {
let state_cid = sdk::sself::root().unwrap();
let cid_for_cbor = CidParams {
cid: state_cid
};
Some(RawBytes::serialize(cid_for_cbor).unwrap())
}
/// Method num 6.
pub fn echo_cid_params(params: u32) -> Option<RawBytes> {
let params = sdk::message::params_raw(params).unwrap().1;
let params = RawBytes::new(params);
let params: CidParams = params.deserialize().unwrap();
let ret = to_vec(format!("Params {:?}",
params).as_str());
match ret {
Ok(ret) => Some(RawBytes::new(ret)),
Err(err) => {
abort!(
USR_ILLEGAL_STATE,
"failed to serialize return value: {:?}",
err
);
}
}
}
/// Method num 7.
pub fn get_old_state(params: u32) -> Option<RawBytes> {
let params = sdk::message::params_raw(params).unwrap().1;
let params = RawBytes::new(params);
let params: CidParams = params.deserialize().unwrap();
let old_state_cid = params.cid;
let old_state = Blockstore.get_cbor::<State>(&old_state_cid).unwrap();
Some(RawBytes::serialize(&old_state).unwrap())
}
/// Method num 8.
pub fn get_state_as_bytes(params: u32) -> Option<RawBytes> {
let params = sdk::message::params_raw(params).unwrap().1;
let params = RawBytes::new(params);
let params: CidParams = params.deserialize().unwrap();
let old_state_cid = params.cid;
let old_state_vec = sdk::ipld::get(&old_state_cid).unwrap();
Some(RawBytes::new(old_state_vec))
}
/// Storage power actor state
#[derive(Default, Serialize_tuple, Deserialize_tuple)]
pub struct PowerActorState {
#[serde(with = "bigint_ser")]
pub total_raw_byte_power: StoragePower,
#[serde(with = "bigint_ser")]
pub total_bytes_committed: StoragePower,
#[serde(with = "bigint_ser")]
pub total_quality_adj_power: StoragePower,
#[serde(with = "bigint_ser")]
pub total_qa_bytes_committed: StoragePower,
#[serde(with = "bigint_ser")]
pub total_pledge_collateral: TokenAmount,
#[serde(with = "bigint_ser")]
pub this_epoch_raw_byte_power: StoragePower,
#[serde(with = "bigint_ser")]
pub this_epoch_quality_adj_power: StoragePower,
#[serde(with = "bigint_ser")]
pub this_epoch_pledge_collateral: TokenAmount,
pub this_epoch_qa_power_smoothed: FilterEstimate,
pub miner_count: i64,
/// Number of miners having proven the minimum consensus power.
pub miner_above_min_power_count: i64,
/// A queue of events to be triggered by cron, indexed by epoch.
pub cron_event_queue: Cid, // Multimap, (HAMT[ChainEpoch]AMT[CronEvent]
/// First epoch in which a cron task may be stored. Cron will iterate every epoch between this
/// and the current epoch inclusively to find tasks to execute.
pub first_cron_epoch: ChainEpoch,
/// Claimed power for each miner.
pub claims: Cid, // Map, HAMT[address]Claim
pub proof_validation_batch: Option<Cid>,
}
/// Method num 9.
pub fn get_power_actor_state(params: u32) -> Option<RawBytes> {
let params = sdk::message::params_raw(params).unwrap().1;
let params = RawBytes::new(params);
let params: CidParams = params.deserialize().unwrap();
let state_cid = params.cid;
let state = Blockstore.get_cbor::<PowerActorState>(&state_cid).unwrap();
Some(RawBytes::serialize(&state).unwrap())
}
#[derive(Debug, Serialize_tuple, Deserialize_tuple, Clone, PartialEq)]
pub struct Claim {
/// Miner's proof type used to determine minimum miner size
pub window_post_proof_type: RegisteredPoStProof,
/// Sum of raw byte power for a miner's sectors.
#[serde(with = "bigint_ser")]
pub raw_byte_power: StoragePower,
/// Sum of quality adjusted power for a miner's sectors.
#[serde(with = "bigint_ser")]
pub quality_adj_power: StoragePower,
}
/// Method num 10.
pub fn get_power_actor_miners(params: u32) -> Option<RawBytes> {
let params = sdk::message::params_raw(params).unwrap().1;
let params = RawBytes::new(params);
let params: CidParams = params.deserialize().unwrap();
let state_cid = params.cid;
let state = Blockstore.get_cbor::<PowerActorState>(&state_cid).unwrap().unwrap();
let claims = Hamt::<Blockstore, _>::load_with_bit_width(&state.claims, Blockstore, HAMT_BIT_WIDTH).unwrap();
let mut miners = Vec::new();
claims.for_each(|k, _: &Claim| {
miners.push(Address::from_bytes(&k.0)?);
Ok(())
}).ok()?;
Some(RawBytes::serialize(&miners).unwrap())
}
`.trim()