Skip to content

Commit c9e01e7

Browse files
committed
Merge branch 'integration/builtin-api' into asr/merge-apis-into-next
2 parents 0aa4a4c + 6f734d8 commit c9e01e7

File tree

15 files changed

+887
-809
lines changed

15 files changed

+887
-809
lines changed

actors/datacap/src/lib.rs

Lines changed: 44 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -51,27 +51,29 @@ lazy_static! {
5151
#[repr(u64)]
5252
pub enum Method {
5353
Constructor = METHOD_CONSTRUCTOR,
54-
// Non-standard.
55-
Mint = 2,
56-
Destroy = 3,
57-
// Static method numbers for token standard methods, for private use.
58-
Name = 10,
59-
Symbol = 11,
60-
TotalSupply = 12,
61-
BalanceOf = 13,
62-
Transfer = 14,
63-
TransferFrom = 15,
64-
IncreaseAllowance = 16,
65-
DecreaseAllowance = 17,
66-
RevokeAllowance = 18,
67-
Burn = 19,
68-
BurnFrom = 20,
69-
Allowance = 21,
54+
// Deprecated in v10
55+
// Mint = 2,
56+
// Destroy = 3,
57+
// Name = 10,
58+
// Symbol = 11,
59+
// TotalSupply = 12,
60+
// BalanceOf = 13,
61+
// Transfer = 14,
62+
// TransferFrom = 15,
63+
// IncreaseAllowance = 16,
64+
// DecreaseAllowance = 17,
65+
// RevokeAllowance = 18,
66+
// Burn = 19,
67+
// BurnFrom = 20,
68+
// Allowance = 21,
7069
// Method numbers derived from FRC-0042 standards
70+
MintExported = frc42_dispatch::method_hash!("Mint"),
71+
DestroyExported = frc42_dispatch::method_hash!("Destroy"),
7172
NameExported = frc42_dispatch::method_hash!("Name"),
7273
SymbolExported = frc42_dispatch::method_hash!("Symbol"),
74+
GranularityExported = frc42_dispatch::method_hash!("GranularityExported"),
7375
TotalSupplyExported = frc42_dispatch::method_hash!("TotalSupply"),
74-
BalanceOfExported = frc42_dispatch::method_hash!("BalanceOf"),
76+
BalanceExported = frc42_dispatch::method_hash!("Balance"),
7577
TransferExported = frc42_dispatch::method_hash!("Transfer"),
7678
TransferFromExported = frc42_dispatch::method_hash!("TransferFrom"),
7779
IncreaseAllowanceExported = frc42_dispatch::method_hash!("IncreaseAllowance"),
@@ -108,6 +110,11 @@ impl Actor {
108110
Ok("DCAP".to_string())
109111
}
110112

113+
pub fn granularity(rt: &mut impl Runtime) -> Result<GranularityReturn, ActorError> {
114+
rt.validate_immediate_caller_accept_any()?;
115+
Ok(GranularityReturn { granularity: DATACAP_GRANULARITY })
116+
}
117+
111118
pub fn total_supply(rt: &mut impl Runtime, _: ()) -> Result<TokenAmount, ActorError> {
112119
rt.validate_immediate_caller_accept_any()?;
113120
let mut st: State = rt.state()?;
@@ -116,7 +123,7 @@ impl Actor {
116123
Ok(token.total_supply())
117124
}
118125

119-
pub fn balance_of(rt: &mut impl Runtime, address: Address) -> Result<TokenAmount, ActorError> {
126+
pub fn balance(rt: &mut impl Runtime, address: Address) -> Result<TokenAmount, ActorError> {
120127
// NOTE: mutability and method caller here are awkward for a read-only call
121128
rt.validate_immediate_caller_accept_any()?;
122129
let mut st: State = rt.state()?;
@@ -477,59 +484,63 @@ impl ActorCode for Actor {
477484
Self::constructor(rt, cbor::deserialize_params(params)?)?;
478485
Ok(RawBytes::default())
479486
}
480-
Some(Method::Mint) => {
487+
Some(Method::MintExported) => {
481488
let ret = Self::mint(rt, cbor::deserialize_params(params)?)?;
482489
serialize(&ret, "mint result")
483490
}
484-
Some(Method::Destroy) => {
491+
Some(Method::DestroyExported) => {
485492
let ret = Self::destroy(rt, cbor::deserialize_params(params)?)?;
486493
serialize(&ret, "destroy result")
487494
}
488-
Some(Method::Name) | Some(Method::NameExported) => {
495+
Some(Method::NameExported) => {
489496
let ret = Self::name(rt)?;
490497
serialize(&ret, "name result")
491498
}
492-
Some(Method::Symbol) | Some(Method::SymbolExported) => {
499+
Some(Method::SymbolExported) => {
493500
let ret = Self::symbol(rt)?;
494501
serialize(&ret, "symbol result")
495502
}
496-
Some(Method::TotalSupply) | Some(Method::TotalSupplyExported) => {
503+
Some(Method::GranularityExported) => {
504+
let ret = Self::granularity(rt)?;
505+
serialize(&ret, "granularity result")
506+
}
507+
Some(Method::TotalSupplyExported) => {
497508
let ret = Self::total_supply(rt, cbor::deserialize_params(params)?)?;
498509
serialize(&ret, "total_supply result")
499510
}
500-
Some(Method::BalanceOf) | Some(Method::BalanceOfExported) => {
501-
let ret = Self::balance_of(rt, cbor::deserialize_params(params)?)?;
511+
Some(Method::BalanceExported) => {
512+
let ret = Self::balance(rt, cbor::deserialize_params(params)?)?;
502513
serialize(&ret, "balance_of result")
503514
}
504-
Some(Method::Transfer) | Some(Method::TransferExported) => {
515+
Some(Method::TransferExported) => {
505516
let ret = Self::transfer(rt, cbor::deserialize_params(params)?)?;
506517
serialize(&ret, "transfer result")
507518
}
508-
Some(Method::TransferFrom) | Some(Method::TransferFromExported) => {
519+
Some(Method::TransferFromExported) => {
509520
let ret = Self::transfer_from(rt, cbor::deserialize_params(params)?)?;
510521
serialize(&ret, "transfer_from result")
511522
}
512-
Some(Method::IncreaseAllowance) | Some(Method::IncreaseAllowanceExported) => {
523+
Some(Method::IncreaseAllowanceExported) => {
513524
let ret = Self::increase_allowance(rt, cbor::deserialize_params(params)?)?;
514525
serialize(&ret, "increase_allowance result")
515526
}
516-
Some(Method::DecreaseAllowance) | Some(Method::DecreaseAllowanceExported) => {
527+
Some(Method::DecreaseAllowanceExported) => {
517528
let ret = Self::decrease_allowance(rt, cbor::deserialize_params(params)?)?;
518529
serialize(&ret, "decrease_allowance result")
519530
}
520-
Some(Method::RevokeAllowance) | Some(Method::RevokeAllowanceExported) => {
531+
Some(Method::RevokeAllowanceExported) => {
521532
Self::revoke_allowance(rt, cbor::deserialize_params(params)?)?;
522533
Ok(RawBytes::default())
523534
}
524-
Some(Method::Burn) | Some(Method::BurnExported) => {
535+
Some(Method::BurnExported) => {
525536
let ret = Self::burn(rt, cbor::deserialize_params(params)?)?;
526537
serialize(&ret, "burn result")
527538
}
528-
Some(Method::BurnFrom) | Some(Method::BurnFromExported) => {
539+
Some(Method::BurnFromExported) => {
529540
let ret = Self::burn_from(rt, cbor::deserialize_params(params)?)?;
530541
serialize(&ret, "burn_from result")
531542
}
532-
Some(Method::Allowance) | Some(Method::AllowanceExported) => {
543+
Some(Method::AllowanceExported) => {
533544
let ret = Self::allowance(rt, cbor::deserialize_params(params)?)?;
534545
serialize(&ret, "allowance result")
535546
}

actors/datacap/src/types.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,9 @@ pub struct DestroyParams {
2222
}
2323

2424
impl Cbor for DestroyParams {}
25+
26+
#[derive(Clone, Debug, PartialEq, Eq, Serialize_tuple, Deserialize_tuple)]
27+
#[serde(transparent)]
28+
pub struct GranularityReturn {
29+
pub granularity: u64,
30+
}

actors/datacap/tests/datacap_actor_test.rs

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,26 @@ lazy_static! {
1616

1717
mod construction {
1818
use crate::*;
19+
use fil_actor_datacap::{Actor, GranularityReturn, Method, DATACAP_GRANULARITY};
1920
use fil_actors_runtime::VERIFIED_REGISTRY_ACTOR_ADDR;
21+
use fvm_ipld_encoding::RawBytes;
22+
use fvm_shared::MethodNum;
2023

2124
#[test]
2225
fn construct_with_verified() {
2326
let mut rt = new_runtime();
2427
let h = Harness { governor: VERIFIED_REGISTRY_ACTOR_ADDR };
2528
h.construct_and_verify(&mut rt, &h.governor);
2629
h.check_state(&rt);
30+
31+
rt.expect_validate_caller_any();
32+
let ret: GranularityReturn = rt
33+
.call::<Actor>(Method::GranularityExported as MethodNum, &RawBytes::default())
34+
.unwrap()
35+
.deserialize()
36+
.unwrap();
37+
rt.verify();
38+
assert_eq!(ret.granularity, DATACAP_GRANULARITY)
2739
}
2840
}
2941

@@ -53,13 +65,13 @@ mod mint {
5365
assert_eq!(amt, ret.supply);
5466
assert_eq!(amt, ret.balance);
5567
assert_eq!(amt, h.get_supply(&rt));
56-
assert_eq!(amt, h.get_balance(&rt, &*ALICE));
68+
assert_eq!(amt, h.get_balance(&mut rt, &*ALICE));
5769

5870
let ret = h.mint(&mut rt, &*BOB, &amt, vec![]).unwrap();
5971
assert_eq!(&amt * 2, ret.supply);
6072
assert_eq!(amt, ret.balance);
6173
assert_eq!(&amt * 2, h.get_supply(&rt));
62-
assert_eq!(amt, h.get_balance(&rt, &*BOB));
74+
assert_eq!(amt, h.get_balance(&mut rt, &*BOB));
6375

6476
h.check_state(&rt);
6577
}
@@ -74,7 +86,10 @@ mod mint {
7486
expect_abort_contains_message(
7587
ExitCode::USR_FORBIDDEN,
7688
"must be built-in",
77-
rt.call::<Actor>(Method::Mint as MethodNum, &serialize(&params, "params").unwrap()),
89+
rt.call::<Actor>(
90+
Method::MintExported as MethodNum,
91+
&serialize(&params, "params").unwrap(),
92+
),
7893
);
7994
h.check_state(&rt);
8095
}
@@ -90,7 +105,10 @@ mod mint {
90105
expect_abort_contains_message(
91106
ExitCode::USR_FORBIDDEN,
92107
"caller address",
93-
rt.call::<Actor>(Method::Mint as MethodNum, &serialize(&params, "params").unwrap()),
108+
rt.call::<Actor>(
109+
Method::MintExported as MethodNum,
110+
&serialize(&params, "params").unwrap(),
111+
),
94112
);
95113
h.check_state(&rt);
96114
}
@@ -228,7 +246,10 @@ mod destroy {
228246
expect_abort_contains_message(
229247
ExitCode::USR_FORBIDDEN,
230248
"must be built-in",
231-
rt.call::<Actor>(Method::Destroy as MethodNum, &serialize(&params, "params").unwrap()),
249+
rt.call::<Actor>(
250+
Method::DestroyExported as MethodNum,
251+
&serialize(&params, "params").unwrap(),
252+
),
232253
);
233254
h.check_state(&rt);
234255
}
@@ -248,7 +269,10 @@ mod destroy {
248269
expect_abort_contains_message(
249270
ExitCode::USR_FORBIDDEN,
250271
"caller address",
251-
rt.call::<Actor>(Method::Destroy as MethodNum, &serialize(&params, "params").unwrap()),
272+
rt.call::<Actor>(
273+
Method::DestroyExported as MethodNum,
274+
&serialize(&params, "params").unwrap(),
275+
),
252276
);
253277

254278
// Destroying from 0 allowance having governor works

actors/datacap/tests/harness/mod.rs

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,10 @@ impl Harness {
9393

9494
let params = MintParams { to: *to, amount: amount.clone(), operators };
9595
rt.set_caller(*VERIFREG_ACTOR_CODE_ID, VERIFIED_REGISTRY_ACTOR_ADDR);
96-
let ret =
97-
rt.call::<DataCapActor>(Method::Mint as MethodNum, &serialize(&params, "params")?)?;
96+
let ret = rt.call::<DataCapActor>(
97+
Method::MintExported as MethodNum,
98+
&serialize(&params, "params")?,
99+
)?;
98100

99101
rt.verify();
100102
Ok(ret.deserialize().unwrap())
@@ -111,8 +113,10 @@ impl Harness {
111113
let params = DestroyParams { owner: *owner, amount: amount.clone() };
112114

113115
rt.set_caller(*VERIFREG_ACTOR_CODE_ID, VERIFIED_REGISTRY_ACTOR_ADDR);
114-
let ret =
115-
rt.call::<DataCapActor>(Method::Destroy as MethodNum, &serialize(&params, "params")?)?;
116+
let ret = rt.call::<DataCapActor>(
117+
Method::DestroyExported as MethodNum,
118+
&serialize(&params, "params")?,
119+
)?;
116120

117121
rt.verify();
118122
Ok(ret.deserialize().unwrap())
@@ -155,8 +159,10 @@ impl Harness {
155159
);
156160

157161
let params = TransferParams { to: *to, amount: amount.clone(), operator_data };
158-
let ret =
159-
rt.call::<DataCapActor>(Method::Transfer as MethodNum, &serialize(&params, "params")?)?;
162+
let ret = rt.call::<DataCapActor>(
163+
Method::TransferExported as MethodNum,
164+
&serialize(&params, "params")?,
165+
)?;
160166

161167
rt.verify();
162168
Ok(ret.deserialize().unwrap())
@@ -202,7 +208,7 @@ impl Harness {
202208
let params =
203209
TransferFromParams { to: *to, from: *from, amount: amount.clone(), operator_data };
204210
let ret = rt.call::<DataCapActor>(
205-
Method::TransferFrom as MethodNum,
211+
Method::TransferFromExported as MethodNum,
206212
&serialize(&params, "params")?,
207213
)?;
208214

@@ -216,8 +222,18 @@ impl Harness {
216222
}
217223

218224
// Reads a balance from state directly.
219-
pub fn get_balance(&self, rt: &MockRuntime, address: &Address) -> TokenAmount {
220-
rt.get_state::<State>().token.get_balance(rt.store(), address.id().unwrap()).unwrap()
225+
pub fn get_balance(&self, rt: &mut MockRuntime, address: &Address) -> TokenAmount {
226+
rt.expect_validate_caller_any();
227+
let ret = rt
228+
.call::<DataCapActor>(
229+
Method::BalanceExported as MethodNum,
230+
&serialize(&address, "params").unwrap(),
231+
)
232+
.unwrap()
233+
.deserialize()
234+
.unwrap();
235+
rt.verify();
236+
ret
221237
}
222238

223239
// Reads allowance from state directly

actors/init/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ pub enum Method {
3636
InstallCode = 4,
3737
// Method numbers derived from FRC-0042 standards
3838
ExecExported = frc42_dispatch::method_hash!("Exec"),
39+
// TODO: Export new methods if appropriate
3940
}
4041

4142
/// Init actor

actors/market/src/ext.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ pub mod verifreg {
8080
}
8181

8282
pub mod datacap {
83-
pub const TRANSFER_FROM_METHOD: u64 = 15;
83+
pub const TRANSFER_FROM_METHOD: u64 = frc42_dispatch::method_hash!("TransferFrom");
8484
}
8585

8686
pub mod reward {

0 commit comments

Comments
 (0)