Skip to content

Commit

Permalink
feat(rpc) Implement Filecoin.MarketAddBalance (#4679)
Browse files Browse the repository at this point in the history
  • Loading branch information
elmattic authored Aug 27, 2024
1 parent 59b96b3 commit 867f0a5
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 3 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@
- [#4686](https://github.com/ChainSafe/forest/issues/4686) Add support for the
`Filecoin.EthAddressToFilecoinAddress` RPC method.

- [#4612](https://github.com/ChainSafe/forest/issues/4612) Add support for the
`Filecoin.MarketAddBalance` RPC method.

### Changed

- [#4583](https://github.com/ChainSafe/forest/pull/4583) Removed the expiration
Expand Down
59 changes: 56 additions & 3 deletions scripts/tests/calibnet_wallet_check.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env bash
# This script checks wallet features of the forest node and the forest-cli.
# It also checks some RPC methods that need a remote wallet.
# It requires both `forest` and `forest-cli` to be in the PATH.

set -euxo pipefail
Expand All @@ -21,6 +22,61 @@ echo "$1" > preloaded_wallet.key

forest_init

$FOREST_WALLET_PATH import preloaded_wallet.key
$FOREST_WALLET_PATH --remote-wallet import preloaded_wallet.key

: Begin Filecoin.MarketAddBalance test

FOREST_URL='http://127.0.0.1:2345/rpc/v1'

# Amount to add to the Market actor (in attoFIL)
MARKET_FIL_AMT="23"

# The preloaded address
REMOTE_ADDR=$($FOREST_WALLET_PATH --remote-wallet list | tail -1 | cut -d ' ' -f1)

JSON=$(curl -s -X POST "$FOREST_URL" \
--header 'Accept: application/json' \
--header 'Content-Type: application/json' \
--header "Authorization: Bearer $ADMIN_TOKEN" \
--data "$(jq -n --arg addr "$REMOTE_ADDR" --arg amt "$MARKET_FIL_AMT" '{jsonrpc: "2.0", id: 1, method: "Filecoin.MarketAddBalance", params: [$addr, $addr, $amt]}')")

echo "$JSON"

if [[ $(echo "$JSON" | jq -e '.result') == "null" ]]; then
echo "Error while sending message."
exit 1
fi

MSG_CID=$(echo "$JSON" | jq -r '.result["/"]')
echo "Message cid: $MSG_CID"

# Try 30 times (in other words wait for 5 tipsets)
for i in {1..30}
do
sleep 5s
echo "Attempt $i:"

JSON=$(curl -s -X POST "$FOREST_URL" \
--header 'Content-Type: application/json' \
--data "$(jq -n --arg cid "$MSG_CID" '{jsonrpc: "2.0", id: 1, method: "Filecoin.StateSearchMsg", params: [{"/": $cid}]}')")

echo "$JSON"

# Check if the message has been mined.
if echo "$JSON" | jq -e '.result' > /dev/null; then
echo "Message found, exiting."
break
fi

echo -e "\n"
done

if [[ $(echo "$JSON" | jq -e '.result') == "null" ]]; then
echo "Error while sending message."
exit 1
fi

: Begin wallet tests

# The following steps do basic wallet handling tests.
Expand All @@ -31,9 +87,6 @@ FIL_AMT="500 atto FIL"
# Amount for an empty wallet
FIL_ZERO="0 FIL"

$FOREST_WALLET_PATH import preloaded_wallet.key
$FOREST_WALLET_PATH --remote-wallet import preloaded_wallet.key

# The preloaded address
ADDR_ONE=$($FOREST_WALLET_PATH list | tail -1 | cut -d ' ' -f1)

Expand Down
44 changes: 44 additions & 0 deletions src/rpc/methods/market.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright 2019-2024 ChainSafe Systems
// SPDX-License-Identifier: Apache-2.0, MIT

use crate::rpc::error::ServerError;
use crate::rpc::mpool::MpoolPushMessage;
use crate::rpc::{ApiPaths, Ctx, Permission, RpcMethod};
use crate::shim::{address::Address, message::Message, message::MethodNum};
use cid::Cid;
use fvm_ipld_blockstore::Blockstore;
use fvm_ipld_encoding::RawBytes;
use num_bigint::BigInt;

const METHOD_ADD_BALANCE: MethodNum = 2;

pub enum MarketAddBalance {}
impl RpcMethod<3> for MarketAddBalance {
const NAME: &'static str = "Filecoin.MarketAddBalance";
const PARAM_NAMES: [&'static str; 3] = ["wallet", "address", "amount"];
const API_PATHS: ApiPaths = ApiPaths::V1;
const PERMISSION: Permission = Permission::Sign;

type Params = (Address, Address, BigInt);
type Ok = Cid;

async fn handle(
ctx: Ctx<impl Blockstore + Send + Sync + 'static>,
(wallet, address, amount): Self::Params,
) -> Result<Self::Ok, ServerError> {
let bytes = fvm_ipld_encoding::to_vec(&address)?;
let params = RawBytes::new(bytes);

let message = Message {
to: Address::MARKET_ACTOR,
from: wallet,
value: amount.into(),
method_num: METHOD_ADD_BALANCE,
params,
..Default::default()
};

let smsg = MpoolPushMessage::handle(ctx, (message, None)).await?;
Ok(smsg.cid())
}
}
4 changes: 4 additions & 0 deletions src/rpc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ macro_rules! for_each_method {
$callback!(crate::rpc::gas::GasEstimateFeeCap);
$callback!(crate::rpc::gas::GasEstimateGasPremium);

// market vertical
$callback!(crate::rpc::market::MarketAddBalance);

// miner vertical
$callback!(crate::rpc::miner::MinerCreateBlock);
$callback!(crate::rpc::miner::MinerGetBaseInfo);
Expand Down Expand Up @@ -306,6 +309,7 @@ mod methods {
pub mod eth;
pub mod f3;
pub mod gas;
pub mod market;
pub mod miner;
pub mod misc;
pub mod mpool;
Expand Down

0 comments on commit 867f0a5

Please sign in to comment.