Skip to content
This repository has been archived by the owner on Jan 11, 2024. It is now read-only.

Commit

Permalink
Integration testing (#250)
Browse files Browse the repository at this point in the history
* FIX: Take the least significant 20 bytes of the public key hash.

* commit rust binding

* FIX: make fmt

* FIX: fix tests for new pkey length

---------

Co-authored-by: auto-commit <auto-commit@users.noreply.github.com>
Co-authored-by: Alfonso de la Rocha <adlrocha@tutamail.com>
  • Loading branch information
3 people authored Oct 17, 2023
1 parent 7a396cf commit d0620b8
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 12 deletions.
4 changes: 2 additions & 2 deletions binding/src/gateway_diamond.rs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion binding/src/subnet_actor_diamond.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ pub mod subnet_actor_diamond {
pub static SUBNETACTORDIAMOND_ABI: ::ethers::contract::Lazy<::ethers::core::abi::Abi> =
::ethers::contract::Lazy::new(__abi);
#[rustfmt::skip]
const __DEPLOYED_BYTECODE: &[u8] = b"`\x80`@R6\x15`\x91W`\0\x805`\x01`\x01`\xE0\x1B\x03\x19\x16\x80\x82R\x7F\x80n\x0C\xBB\x9F\xCE)k\xBC3jH\xF4+\xF1\xDB\xC6\x97\"\xD1\x8D\x90\xD6\xFEp[u\x82\xC2\xBBK\xD2` R`@\x82 T`\x01`\x01`\xA0\x1B\x03\x16\x90\x81\x15`oWP\x81\x80\x916\x82\x807\x816\x91Z\xF4=\x82\x80>\x15`kW=\x90\xF3[=\x90\xFD[c\n\x82\xDDs`\xE3\x1B`\x80R`\x01`\x01`\xE0\x1B\x03\x19\x16`\x84R`\x7F\x19`\xA4\x01`\x80\xFD[`\x06T`\x01`\x01`\xA0\x1B\x03\x163\x03`\xA4W\0[c\xE7\xE6\x01\xDB`\xE0\x1B`\x80R`\x04`\x80\xFD\xFE\xA2dipfsX\"\x12 \xBAW\xF2\xF2\xF7CC\xEF\xF2\xB9l\xC6(\xAB\xCE\xCD\xE2\x178\xC6\xE5\xF7 \xD0zZ\x90(\xEEZ\xD9\x0CdsolcC\0\x08\x13\x003";
const __DEPLOYED_BYTECODE: &[u8] = b"`\x80`@R6\x15`\x91W`\0\x805`\x01`\x01`\xE0\x1B\x03\x19\x16\x80\x82R\x7F\x80n\x0C\xBB\x9F\xCE)k\xBC3jH\xF4+\xF1\xDB\xC6\x97\"\xD1\x8D\x90\xD6\xFEp[u\x82\xC2\xBBK\xD2` R`@\x82 T`\x01`\x01`\xA0\x1B\x03\x16\x90\x81\x15`oWP\x81\x80\x916\x82\x807\x816\x91Z\xF4=\x82\x80>\x15`kW=\x90\xF3[=\x90\xFD[c\n\x82\xDDs`\xE3\x1B`\x80R`\x01`\x01`\xE0\x1B\x03\x19\x16`\x84R`\x7F\x19`\xA4\x01`\x80\xFD[`\x06T`\x01`\x01`\xA0\x1B\x03\x163\x03`\xA4W\0[c\xE7\xE6\x01\xDB`\xE0\x1B`\x80R`\x04`\x80\xFD\xFE\xA2dipfsX\"\x12 \x8F^@*L[\x91o\x1E:\xB6\xA5\xAAo\xA1\xBA\xDE\x9E\xCB\x07\xF2\xBD\xEEK\x1DC|\xAB\xF7\x1D\x175dsolcC\0\x08\x13\x003";
/// The deployed bytecode of the contract.
pub static SUBNETACTORDIAMOND_DEPLOYED_BYTECODE: ::ethers::core::types::Bytes =
::ethers::core::types::Bytes::from_static(__DEPLOYED_BYTECODE);
Expand Down
43 changes: 43 additions & 0 deletions binding/src/subnet_actor_manager_facet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,17 @@ pub mod subnet_actor_manager_facet {
},
],
),
(
::std::borrow::ToOwned::to_owned("InvalidPublicKeyLength"),
::std::vec![
::ethers::core::abi::ethabi::AbiError {
name: ::std::borrow::ToOwned::to_owned(
"InvalidPublicKeyLength",
),
inputs: ::std::vec![],
},
],
),
(
::std::borrow::ToOwned::to_owned("InvalidSignatureErr"),
::std::vec![
Expand Down Expand Up @@ -909,6 +920,19 @@ pub mod subnet_actor_manager_facet {
abi = "InvalidCheckpointMessagesHash()"
)]
pub struct InvalidCheckpointMessagesHash;
///Custom Error type `InvalidPublicKeyLength` with signature `InvalidPublicKeyLength()` and selector `0x637297a4`
#[derive(
Clone,
::ethers::contract::EthError,
::ethers::contract::EthDisplay,
Default,
Debug,
PartialEq,
Eq,
Hash,
)]
#[etherror(name = "InvalidPublicKeyLength", abi = "InvalidPublicKeyLength()")]
pub struct InvalidPublicKeyLength;
///Custom Error type `InvalidSignatureErr` with signature `InvalidSignatureErr(uint8)` and selector `0x282ef1c1`
#[derive(
Clone,
Expand Down Expand Up @@ -1093,6 +1117,7 @@ pub mod subnet_actor_manager_facet {
EmptyAddress(EmptyAddress),
InvalidCheckpointEpoch(InvalidCheckpointEpoch),
InvalidCheckpointMessagesHash(InvalidCheckpointMessagesHash),
InvalidPublicKeyLength(InvalidPublicKeyLength),
InvalidSignatureErr(InvalidSignatureErr),
NoCollateralToWithdraw(NoCollateralToWithdraw),
NotAllValidatorsHaveLeft(NotAllValidatorsHaveLeft),
Expand Down Expand Up @@ -1147,6 +1172,11 @@ pub mod subnet_actor_manager_facet {
{
return Ok(Self::InvalidCheckpointMessagesHash(decoded));
}
if let Ok(decoded) =
<InvalidPublicKeyLength as ::ethers::core::abi::AbiDecode>::decode(data)
{
return Ok(Self::InvalidPublicKeyLength(decoded));
}
if let Ok(decoded) =
<InvalidSignatureErr as ::ethers::core::abi::AbiDecode>::decode(data)
{
Expand Down Expand Up @@ -1222,6 +1252,9 @@ pub mod subnet_actor_manager_facet {
Self::InvalidCheckpointMessagesHash(element) => {
::ethers::core::abi::AbiEncode::encode(element)
}
Self::InvalidPublicKeyLength(element) => {
::ethers::core::abi::AbiEncode::encode(element)
}
Self::InvalidSignatureErr(element) => {
::ethers::core::abi::AbiEncode::encode(element)
}
Expand Down Expand Up @@ -1281,6 +1314,10 @@ pub mod subnet_actor_manager_facet {
== <InvalidCheckpointMessagesHash as ::ethers::contract::EthError>::selector() => {
true
}
_ if selector
== <InvalidPublicKeyLength as ::ethers::contract::EthError>::selector() => {
true
}
_ if selector
== <InvalidSignatureErr as ::ethers::contract::EthError>::selector() => {
true
Expand Down Expand Up @@ -1342,6 +1379,7 @@ pub mod subnet_actor_manager_facet {
Self::InvalidCheckpointMessagesHash(element) => {
::core::fmt::Display::fmt(element, f)
}
Self::InvalidPublicKeyLength(element) => ::core::fmt::Display::fmt(element, f),
Self::InvalidSignatureErr(element) => ::core::fmt::Display::fmt(element, f),
Self::NoCollateralToWithdraw(element) => ::core::fmt::Display::fmt(element, f),
Self::NotAllValidatorsHaveLeft(element) => ::core::fmt::Display::fmt(element, f),
Expand Down Expand Up @@ -1394,6 +1432,11 @@ pub mod subnet_actor_manager_facet {
Self::InvalidCheckpointMessagesHash(value)
}
}
impl ::core::convert::From<InvalidPublicKeyLength> for SubnetActorManagerFacetErrors {
fn from(value: InvalidPublicKeyLength) -> Self {
Self::InvalidPublicKeyLength(value)
}
}
impl ::core::convert::From<InvalidSignatureErr> for SubnetActorManagerFacetErrors {
fn from(value: InvalidSignatureErr) -> Self {
Self::InvalidSignatureErr(value)
Expand Down
1 change: 1 addition & 0 deletions src/errors/IPCErrors.sol
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ error InvalidRetentionHeight();
error InvalidSignature();
error InvalidSignatureErr(uint8);
error InvalidSignatureLength();
error InvalidPublicKeyLength();
error InvalidSubmissionPeriod();
error InvalidSubnet();
error NoCollateralToWithdraw();
Expand Down
19 changes: 14 additions & 5 deletions src/subnet/SubnetActorManagerFacet.sol
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: MIT OR Apache-2.0
pragma solidity 0.8.19;

import {CollateralIsZero, NotOwnerOfPublicKey, EmptyAddress, NotEnoughBalanceForRewards, NotValidator, NotAllValidatorsHaveLeft, NotStakedBefore, InvalidSignatureErr, InvalidCheckpointEpoch, InvalidCheckpointMessagesHash} from "../errors/IPCErrors.sol";
import {CollateralIsZero, NotOwnerOfPublicKey, EmptyAddress, NotEnoughBalanceForRewards, NotValidator, NotAllValidatorsHaveLeft, NotStakedBefore, InvalidSignatureErr, InvalidCheckpointEpoch, InvalidCheckpointMessagesHash, InvalidPublicKeyLength} from "../errors/IPCErrors.sol";
import {IGateway} from "../interfaces/IGateway.sol";
import {ISubnetActor} from "../interfaces/ISubnetActor.sol";
import {BottomUpCheckpoint, CrossMsg} from "../structs/Checkpoint.sol";
Expand Down Expand Up @@ -89,12 +89,17 @@ contract SubnetActorManagerFacet is ISubnetActor, SubnetActorModifiers, Reentran
}

/// @notice method that allows a validator to join the subnet
/// @param publicKey The off-chain public key that should be associated with the validator
/// @param publicKey The off-chain 65 byte public key that should be associated with the validator
function join(bytes calldata publicKey) external payable nonReentrant notKilled {
if (msg.value == 0) {
revert CollateralIsZero();
}

if (publicKey.length != 65) {
// Taking 65 bytes because the FVM libraries have some assertions checking it, it's more convenient.
revert InvalidPublicKeyLength();
}

address convertedAddress = publicKeyToAddress(publicKey);
if (convertedAddress != msg.sender) {
revert NotOwnerOfPublicKey();
Expand Down Expand Up @@ -254,8 +259,12 @@ contract SubnetActorManagerFacet is ISubnetActor, SubnetActorModifiers, Reentran
}
}

function publicKeyToAddress(bytes memory publicKey) internal pure returns (address) {
bytes32 hashed = keccak256(publicKey);
return address(uint160(bytes20(hashed)));
/**
* @notice Hash a 65 byte public key and return the corresponding address.
*/
function publicKeyToAddress(bytes calldata publicKey) internal pure returns (address) {
assert(publicKey.length == 65);
bytes32 hashed = keccak256(publicKey[1:]);
return address(uint160(uint256(hashed)));
}
}
14 changes: 10 additions & 4 deletions test/SubnetActorDiamond.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,16 @@ contract SubnetActorDiamondTest is Test {
}

function deriveValidatorAddress(uint8 seq) internal pure returns (address addr, bytes memory data) {
data = new bytes(64);
data[0] = bytes1(seq);
data = new bytes(65);
data[1] = bytes1(seq);

addr = address(uint160(bytes20(keccak256(data))));
// use data[1:] for the hash
bytes memory dataSubset = new bytes(data.length - 1);
for (uint i = 1; i < data.length; i++) {
dataSubset[i - 1] = data[i];
}

addr = address(uint160(uint256(keccak256(dataSubset))));
}

function testSubnetActorDiamond_Deployment_Works(
Expand Down Expand Up @@ -170,7 +176,7 @@ contract SubnetActorDiamondTest is Test {
vm.prank(validator);
vm.expectRevert(NotOwnerOfPublicKey.selector);

saManager.join{value: 10}(new bytes(20));
saManager.join{value: 10}(new bytes(65));
}

function testSubnetActorDiamond_Join_Fail_ZeroColalteral() public {
Expand Down

0 comments on commit d0620b8

Please sign in to comment.