Skip to content
This repository has been archived by the owner on Aug 29, 2023. It is now read-only.

Commit

Permalink
feat: add function to create Filecoin Address from ActorID (#317)
Browse files Browse the repository at this point in the history
* feat: add function to create Filecoin Address from ActorID

* added a test

* typo
  • Loading branch information
rllola committed Feb 27, 2023
1 parent 224b591 commit 24b1ec6
Show file tree
Hide file tree
Showing 6 changed files with 192 additions and 1 deletion.
57 changes: 57 additions & 0 deletions .github/workflows/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -774,6 +774,63 @@ jobs:
rustup target add wasm32-unknown-unknown
cargo test leb128_test
address-test:
name: "Address Tests"
timeout-minutes: 20
runs-on: ubuntu-22.04
steps:
- name: Checkout
uses: actions/checkout@v2
with:
submodules: 'true'
- name: Instal deps
run: make install-opencl
- name: Install node
uses: actions/setup-node@v2
with:
node-version: '16.16.0'
- name: Install yarn
run: npm install -g yarn
- name: Install latest stable
uses: actions-rs/toolchain@v1
with:
toolchain: nightly
override: true
- name: Install dependencies
run: |
yarn install
make install_solc_linux
- name: Restore crates
uses: actions/cache@v3
with:
path: |
~/.cargo/bin/
~/.cargo/registry/index/
~/.cargo/registry/cache/
~/.cargo/git/db/
key: ${{ runner.os }}-cargo-${{ hashFiles('./testing/Cargo.lock') }}
- name: Restore previous compilation
uses: actions/cache@v3
with:
path: |
./testing/target
key: ${{ runner.os }}-testing-${{ hashFiles('./testing/Cargo.lock') }}
- name: Restore builtin-actors
uses: actions/cache@v3
with:
path: |
./testing/builtin-actors/target
key: ${{ runner.os }}-actors-${{ hashFiles('./testing/builtin-actors/Cargo.lock') }}
- name: Build builtin-actors
run: make build_builtin_actors
- name: Build contracts
run: make
- name: Run tests for address
run: |
cd testing
rustup target add wasm32-unknown-unknown
cargo test address
comment-result:
name: "Comment the result on PR"
runs-on: ubuntu-22.04
Expand Down
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ build_tests: verify_solc build_leb128_test
./bin/solc contracts/v0.8/tests/precompiles.test.sol --output-dir ./build/v0.8/tests --overwrite --bin --hashes --opcodes --abi
./bin/solc contracts/v0.8/tests/send.test.sol --output-dir ./build/v0.8/tests --overwrite --bin --hashes --opcodes --abi
./bin/solc contracts/v0.8/tests/cbor.decode.test.sol --output-dir ./build/v0.8/tests --overwrite --bin --hashes --opcodes --abi
./bin/solc contracts/v0.8/tests/address.test.sol --output-dir ./build/v0.8/tests --overwrite --bin --hashes --opcodes --abi
./bin/solc contracts/v0.8/mocks/tests/market.test.sol --output-dir ./build/v0.8/mocks/tests --overwrite --bin --hashes --opcodes --abi
./bin/solc contracts/v0.8/mocks/tests/miner.test.sol --output-dir ./build/v0.8/mocks/tests --overwrite --bin --hashes --opcodes --abi

Expand Down Expand Up @@ -118,6 +119,9 @@ test_cbor_decode: build build_builtin_actors
test_leb128: build build_builtin_actors
cd testing && cargo test leb128 -- --nocapture

test_address: build build_builtin_actors
cd testing && cargo test address -- --nocapture

################ TESTS SECURITY ################

security_account_api:
Expand Down
40 changes: 40 additions & 0 deletions contracts/v0.8/tests/address.test.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*******************************************************************************
* (c) 2023 Zondax AG
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
********************************************************************************/
//
// DRAFT!! THIS CODE HAS NOT BEEN AUDITED - USE ONLY FOR PROTOTYPING

// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.17;

import "../utils/Leb128.sol";
import "../utils/FilAddresses.sol";
import "../types/CommonTypes.sol";
import "../external/Buffer.sol";

/// @notice This file is meant to serve as a deployable contract of the Address lib, as the library by itself is not.
/// @notice It imports the library and create a callable method for each method in the library
/// @author Zondax AG
contract AddressTest {

function actorid_conversion() public pure {
uint64 actorID = 1;
CommonTypes.FilAddress memory result = FilAddresses.fromActorID(actorID);

require(keccak256(result.data) == keccak256(hex"0001"), "'1' actorID is not returning '0001'");
}


}
3 changes: 2 additions & 1 deletion contracts/v0.8/tests/leb128.test.sol
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
pragma solidity ^0.8.17;

import "../utils/Leb128.sol";
import "../utils/FilAddresses.sol";
import "../types/CommonTypes.sol";
import "../external/Buffer.sol";

/// @notice This file is meant to serve as a deployable contract of the Leb128 lib, as the library by itself is not.
Expand All @@ -36,5 +38,4 @@ contract Leb128Test {

require(keccak256(result.buf) == keccak256(expected), "'624485' is not returning 'e58e26'");
}

}
12 changes: 12 additions & 0 deletions contracts/v0.8/utils/FilAddresses.sol
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,14 @@
pragma solidity ^0.8.17;

import "../types/CommonTypes.sol";
import "../utils/Leb128.sol";
import "../external/Buffer.sol";

/// @notice This library is a set a functions that allows to handle filecoin addresses conversions and validations
/// @author Zondax AG
library FilAddresses {
using Buffer for Buffer.buffer;

/// @notice allow to get a delegated address (f4) from an eth address
/// @param addr eth address to convert
/// @return delegated filecoin address
Expand All @@ -47,4 +51,12 @@ library FilAddresses {

return addr.data.length <= 256;
}

/// @notice allow to create a Filecoin address from an actorID
/// @param actorID uint64 actorID
/// @return address filecoin address
function fromActorID(uint64 actorID) internal pure returns (CommonTypes.FilAddress memory) {
Buffer.buffer memory result = Leb128.encodeUnsignedLeb128FromUInt64(actorID);
return CommonTypes.FilAddress(abi.encodePacked(hex"00", result.buf));
}
}
77 changes: 77 additions & 0 deletions testing/tests/address.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
use fil_actor_eam::Return;
use fil_actor_evm::Method as EvmMethods;
use fil_actors_runtime::EAM_ACTOR_ADDR;
use fvm::executor::{ApplyKind, Executor};
use fvm_integration_tests::dummy::DummyExterns;
use fvm_integration_tests::tester::Account;
use fvm_ipld_encoding::strict_bytes;
use fvm_ipld_encoding::RawBytes;
use fvm_shared::address::Address;
use fvm_shared::message::Message;
use serde::{Deserialize as SerdeDeserialize, Serialize as SerdeSerialize};

use testing::setup;

const WASM_COMPILED_PATH: &str = "../build/v0.8/tests/AddressTest.bin";

#[derive(SerdeSerialize, SerdeDeserialize)]
#[serde(transparent)]
pub struct CreateExternalParams(#[serde(with = "strict_bytes")] pub Vec<u8>);

#[test]
fn address_tests() {
println!("Testing Address lib");

let (mut tester, _manifest) = setup::setup_tester();

let sender: [Account; 1] = tester.create_accounts().unwrap();

// Instantiate machine
tester.instantiate_machine(DummyExterns).unwrap();

let executor = tester.executor.as_mut().unwrap();

println!("Calling init actor (EVM)");

let evm_bin = setup::load_evm(WASM_COMPILED_PATH);

let constructor_params = CreateExternalParams(evm_bin);

let message = Message {
from: sender[0].1,
to: EAM_ACTOR_ADDR,
gas_limit: 1000000000,
method_num: 4,
sequence: 0,
params: RawBytes::serialize(constructor_params).unwrap(),
..Message::default()
};

let res = executor
.execute_message(message, ApplyKind::Explicit, 100)
.unwrap();

assert_eq!(res.msg_receipt.exit_code.value(), 0);

let exec_return: Return = RawBytes::deserialize(&res.msg_receipt.return_data).unwrap();

let contract_actor_id = exec_return.actor_id;

println!("Calling `fromActorID`");

let message = Message {
from: sender[0].1,
to: Address::new_id(contract_actor_id),
gas_limit: 1000000000,
method_num: EvmMethods::InvokeContract as u64,
sequence: 1,
params: RawBytes::new(hex::decode("44a0a86647").unwrap()),
..Message::default()
};

let res = executor
.execute_message(message, ApplyKind::Explicit, 100)
.unwrap();

assert_eq!(res.msg_receipt.exit_code.value(), 0);
}

0 comments on commit 24b1ec6

Please sign in to comment.