Skip to content

Commit 7e9af82

Browse files
authored
feat(rpc): add v1 RPC method Filecoin.FilecoinAddressToEthAddress (#6323)
1 parent f4008dd commit 7e9af82

File tree

7 files changed

+126
-12
lines changed

7 files changed

+126
-12
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535

3636
- [#6312](https://github.com/ChainSafe/forest/pull/6312) Implemented `Filecoin.StateGetID` for API v2.
3737

38+
- [#6323](https://github.com/ChainSafe/forest/pull/6323) Implemented `Filecoin.FilecoinAddressToEthAddress` for API v1 and v2.
39+
3840
### Changed
3941

4042
### Removed

build.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,10 @@ fn rpc_regression_tests_gen() {
6060

6161
let tests: Vec<&str> = include_str!("src/tool/subcommands/api_cmd/test_snapshots.txt")
6262
.lines()
63-
.map(str::trim)
63+
.map(|i| {
64+
// Remove comment
65+
i.split("#").next().unwrap().trim()
66+
})
6467
.filter(|l| !l.is_empty() && !l.starts_with('#'))
6568
.collect();
6669
let out_dir = PathBuf::from(std::env::var("OUT_DIR").unwrap());

src/rpc/methods/eth.rs

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2747,11 +2747,11 @@ impl RpcMethod<0> for EthSubscribe {
27472747
pub enum EthAddressToFilecoinAddress {}
27482748
impl RpcMethod<1> for EthAddressToFilecoinAddress {
27492749
const NAME: &'static str = "Filecoin.EthAddressToFilecoinAddress";
2750-
const NAME_ALIAS: Option<&'static str> = None;
2751-
const N_REQUIRED_PARAMS: usize = 1;
27522750
const PARAM_NAMES: [&'static str; 1] = ["ethAddress"];
27532751
const API_PATHS: BitFlags<ApiPaths> = ApiPaths::all_with_v2();
27542752
const PERMISSION: Permission = Permission::Read;
2753+
const DESCRIPTION: Option<&'static str> =
2754+
Some("Converts an EthAddress into an f410 Filecoin Address");
27552755
type Params = (EthAddress,);
27562756
type Ok = FilecoinAddress;
27572757
async fn handle(
@@ -2762,6 +2762,39 @@ impl RpcMethod<1> for EthAddressToFilecoinAddress {
27622762
}
27632763
}
27642764

2765+
pub enum FilecoinAddressToEthAddress {}
2766+
impl RpcMethod<2> for FilecoinAddressToEthAddress {
2767+
const NAME: &'static str = "Filecoin.FilecoinAddressToEthAddress";
2768+
const N_REQUIRED_PARAMS: usize = 1;
2769+
const PARAM_NAMES: [&'static str; 2] = ["filecoinAddress", "blockParam"];
2770+
const API_PATHS: BitFlags<ApiPaths> = ApiPaths::all_with_v2();
2771+
const PERMISSION: Permission = Permission::Read;
2772+
const DESCRIPTION: Option<&'static str> =
2773+
Some("Converts any Filecoin address to an EthAddress");
2774+
type Params = (FilecoinAddress, Option<BlockNumberOrPredefined>);
2775+
type Ok = EthAddress;
2776+
async fn handle(
2777+
ctx: Ctx<impl Blockstore + Send + Sync + 'static>,
2778+
(address, block_param): Self::Params,
2779+
) -> Result<Self::Ok, ServerError> {
2780+
if let Ok(eth_address) = EthAddress::from_filecoin_address(&address) {
2781+
Ok(eth_address)
2782+
} else {
2783+
let block_param = block_param.unwrap_or(BlockNumberOrPredefined::PredefinedBlock(
2784+
ExtPredefined::Finalized,
2785+
));
2786+
let ts = tipset_by_ext_block_number_or_hash(
2787+
ctx.chain_store(),
2788+
block_param.into(),
2789+
ResolveNullTipset::TakeOlder,
2790+
)?;
2791+
2792+
let id_address = ctx.state_manager.lookup_required_id(&address, &ts)?;
2793+
Ok(EthAddress::from_filecoin_address(&id_address)?)
2794+
}
2795+
}
2796+
}
2797+
27652798
async fn get_eth_transaction_receipt(
27662799
ctx: Ctx<impl Blockstore + Send + Sync + 'static>,
27672800
tx_hash: EthHash,

src/rpc/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ macro_rules! for_each_rpc_method {
9898
// eth vertical
9999
$callback!($crate::rpc::eth::EthAccounts);
100100
$callback!($crate::rpc::eth::EthAddressToFilecoinAddress);
101+
$callback!($crate::rpc::eth::FilecoinAddressToEthAddress);
101102
$callback!($crate::rpc::eth::EthBlockNumber);
102103
$callback!($crate::rpc::eth::EthCall);
103104
$callback!($crate::rpc::eth::EthChainId);

src/tool/subcommands/api_cmd/api_compare_tests.rs

Lines changed: 77 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,42 @@ static KNOWN_EMPTY_CALIBNET_ADDRESS: LazyLock<Address> = LazyLock::new(|| {
8787
.into()
8888
});
8989

90+
// this is the ID address of the `t1w2zb5a723izlm4q3khclsjcnapfzxcfhvqyfoly` address
91+
static KNOWN_CALIBNET_F0_ADDRESS: LazyLock<Address> = LazyLock::new(|| {
92+
crate::shim::address::Network::Testnet
93+
.parse_address("t0168923")
94+
.unwrap()
95+
.into()
96+
});
97+
98+
static KNOWN_CALIBNET_F1_ADDRESS: LazyLock<Address> = LazyLock::new(|| {
99+
crate::shim::address::Network::Testnet
100+
.parse_address("t1w2zb5a723izlm4q3khclsjcnapfzxcfhvqyfoly")
101+
.unwrap()
102+
.into()
103+
});
104+
105+
static KNOWN_CALIBNET_F2_ADDRESS: LazyLock<Address> = LazyLock::new(|| {
106+
crate::shim::address::Network::Testnet
107+
.parse_address("t2nfplhzpyeck5dcc4fokj5ar6nbs3mhbdmq6xu3q")
108+
.unwrap()
109+
.into()
110+
});
111+
112+
static KNOWN_CALIBNET_F3_ADDRESS: LazyLock<Address> = LazyLock::new(|| {
113+
crate::shim::address::Network::Testnet
114+
.parse_address("t3wmbvnabsj6x2uki33phgtqqemmunnttowpx3chklrchy76pv52g5ajnaqdypxoomq5ubfk65twl5ofvkhshq")
115+
.unwrap()
116+
.into()
117+
});
118+
119+
static KNOWN_CALIBNET_F4_ADDRESS: LazyLock<Address> = LazyLock::new(|| {
120+
crate::shim::address::Network::Testnet
121+
.parse_address("t410fx2cumi6pgaz64varl77xbuub54bgs3k5xsvn3ki")
122+
.unwrap()
123+
.into()
124+
});
125+
90126
const TICKET_QUALITY_GREEDY: f64 = 0.9;
91127
const TICKET_QUALITY_OPTIMAL: f64 = 0.8;
92128
const ZERO_ADDRESS: &str = "0x0000000000000000000000000000000000000000";
@@ -1255,11 +1291,11 @@ fn state_tests_with_tipset<DB: Blockstore>(
12551291
fn wallet_tests(worker_address: Option<Address>) -> Vec<RpcTest> {
12561292
let prefunded_wallets = [
12571293
// the following addresses should have 666 attoFIL each
1258-
Address::from_str("t0168923").unwrap(), // this is the ID address of the `t1w2zb5a723izlm4q3khclsjcnapfzxcfhvqyfoly` address
1259-
Address::from_str("t1w2zb5a723izlm4q3khclsjcnapfzxcfhvqyfoly").unwrap(),
1260-
Address::from_str("t2nfplhzpyeck5dcc4fokj5ar6nbs3mhbdmq6xu3q").unwrap(),
1261-
Address::from_str("t3wmbvnabsj6x2uki33phgtqqemmunnttowpx3chklrchy76pv52g5ajnaqdypxoomq5ubfk65twl5ofvkhshq").unwrap(),
1262-
Address::from_str("t410fx2cumi6pgaz64varl77xbuub54bgs3k5xsvn3ki").unwrap(),
1294+
*KNOWN_CALIBNET_F0_ADDRESS,
1295+
*KNOWN_CALIBNET_F1_ADDRESS,
1296+
*KNOWN_CALIBNET_F2_ADDRESS,
1297+
*KNOWN_CALIBNET_F3_ADDRESS,
1298+
*KNOWN_CALIBNET_F4_ADDRESS,
12631299
// This address should have 0 FIL
12641300
*KNOWN_EMPTY_CALIBNET_ADDRESS,
12651301
];
@@ -1456,12 +1492,26 @@ fn eth_tests() -> Vec<RpcTest> {
14561492
EthUninstallFilter::request_with_alias((FilterID::new().unwrap(),), use_alias).unwrap(),
14571493
));
14581494
tests.push(RpcTest::identity(
1459-
EthAddressToFilecoinAddress::request((EthAddress::from_str(
1460-
"0xff38c072f286e3b20b3954ca9f99c05fbecc64aa",
1461-
)
1462-
.unwrap(),))
1495+
EthAddressToFilecoinAddress::request(("0xff38c072f286e3b20b3954ca9f99c05fbecc64aa"
1496+
.parse()
1497+
.unwrap(),))
14631498
.unwrap(),
14641499
));
1500+
tests.push(RpcTest::identity(
1501+
FilecoinAddressToEthAddress::request((*KNOWN_CALIBNET_F0_ADDRESS, None)).unwrap(),
1502+
));
1503+
tests.push(RpcTest::identity(
1504+
FilecoinAddressToEthAddress::request((*KNOWN_CALIBNET_F1_ADDRESS, None)).unwrap(),
1505+
));
1506+
tests.push(RpcTest::identity(
1507+
FilecoinAddressToEthAddress::request((*KNOWN_CALIBNET_F2_ADDRESS, None)).unwrap(),
1508+
));
1509+
tests.push(RpcTest::identity(
1510+
FilecoinAddressToEthAddress::request((*KNOWN_CALIBNET_F3_ADDRESS, None)).unwrap(),
1511+
));
1512+
tests.push(RpcTest::identity(
1513+
FilecoinAddressToEthAddress::request((*KNOWN_CALIBNET_F4_ADDRESS, None)).unwrap(),
1514+
));
14651515
}
14661516
tests
14671517
}
@@ -1936,9 +1986,27 @@ fn eth_tests_with_tipset<DB: Blockstore>(store: &Arc<DB>, shared_tipset: &Tipset
19361986
];
19371987

19381988
for block in shared_tipset.block_headers() {
1989+
tests.extend([RpcTest::identity(
1990+
FilecoinAddressToEthAddress::request((
1991+
block.miner_address,
1992+
Some(BlockNumberOrPredefined::PredefinedBlock(
1993+
ExtPredefined::Latest,
1994+
)),
1995+
))
1996+
.unwrap(),
1997+
)]);
19391998
let (bls_messages, secp_messages) =
19401999
crate::chain::store::block_messages(store, block).unwrap();
19412000
for msg in sample_messages(bls_messages.iter(), secp_messages.iter()) {
2001+
tests.extend([RpcTest::identity(
2002+
FilecoinAddressToEthAddress::request((
2003+
msg.from(),
2004+
Some(BlockNumberOrPredefined::PredefinedBlock(
2005+
ExtPredefined::Latest,
2006+
)),
2007+
))
2008+
.unwrap(),
2009+
)]);
19422010
if let Ok(eth_to_addr) = msg.to.try_into() {
19432011
tests.extend([RpcTest::identity(
19442012
EthEstimateGas::request((

src/tool/subcommands/api_cmd/test_snapshot.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,8 @@ mod tests {
260260
.trim()
261261
.split("\n")
262262
.map(|i| {
263+
// Remove comment
264+
let i = i.split("#").next().unwrap().trim();
263265
let captures = pattern.captures(i).expect("pattern capture failure");
264266
captures
265267
.name("name")

src/tool/subcommands/api_cmd/test_snapshots.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,11 @@ filecoin_evm_getstorageat_statedecodeparams_1755619756266970.rpcsnap.json.zst
9292
filecoin_evm_invokecontract_statedecodeparams_1755004924714521.rpcsnap.json.zst
9393
filecoin_evm_invokecontractdelegate_statedecodeparams_1755004924714578.rpcsnap.json.zst
9494
filecoin_evm_resurrect_statedecodeparams_1755004924714636.rpcsnap.json.zst
95+
filecoin_filecoinaddresstoethaddress_1765291874419799.rpcsnap.json.zst # F4 address
96+
filecoin_filecoinaddresstoethaddress_1765357908887069.rpcsnap.json.zst # F0 address
97+
filecoin_filecoinaddresstoethaddress_1765363872743134.rpcsnap.json.zst # F1 address
98+
filecoin_filecoinaddresstoethaddress_1765363872743268.rpcsnap.json.zst # F3 address
99+
filecoin_filecoinaddresstoethaddress_1765363872743313.rpcsnap.json.zst # F2 address
95100
filecoin_gasestimategaslimit_1741782110512299.rpcsnap.json.zst
96101
filecoin_getactoreventsraw_1741782590255476.rpcsnap.json.zst
97102
filecoin_market_addbalance_statedecodeparams_1757426002278914.rpcsnap.json.zst

0 commit comments

Comments
 (0)