Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add encoding decoding tests #12

Closed
wants to merge 31 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
94144ef
chore: forge init
hieronx Jun 12, 2022
9dd1751
forge install: forge-std
hieronx Jun 12, 2022
2037b88
Setup connector contract
hieronx Jun 12, 2022
6f1b342
forge install: monorepo
hieronx Jun 12, 2022
c455f5b
Add nomad dependency
hieronx Jun 12, 2022
4daba72
forge install: openzeppelin-contracts-upgradeable
hieronx Jun 12, 2022
7a3bbc2
Add config, deploy script
hieronx Jun 12, 2022
ce93bb5
Changes
hieronx Jun 16, 2022
df45a2f
Remove dep
hieronx Jun 16, 2022
2887e02
forge install: openzeppelin-contracts-upgradeable
hieronx Jun 16, 2022
927c5da
Fix compilation
hieronx Jun 16, 2022
efda62e
Make basic routing work
hieronx Jun 17, 2022
cbaf4e3
Set up token factory
hieronx Jun 17, 2022
f72cedf
Set up more methods and tests
hieronx Jun 20, 2022
10830a0
Merge branch 'main' of https://github.com/centrifuge/connectors into …
hieronx Jun 20, 2022
2af145a
Fix message parsing
hieronx Jun 20, 2022
950a1cf
Start working on adding tranches
hieronx Jun 21, 2022
d86ec5b
Fix adding tranches
hieronx Jun 21, 2022
e5dfd5c
Support updating members, add more tests
hieronx Jun 21, 2022
8b7a487
Remove comments
hieronx Jun 21, 2022
6a470d8
Add update token price
hieronx Jun 21, 2022
9fc9840
Work on new message encoding and decoding tests
hieronx Jun 26, 2022
32f3da0
Set up XCM router and token name + symbol for tranches
hieronx Jun 29, 2022
2421246
Fix adding tranche compilation
hieronx Jun 30, 2022
7609584
add more encoding/decoding tests for Messages
ilinzweilin Jul 20, 2022
cf78ecb
merge with recent main changes
ilinzweilin Jul 20, 2022
41a0237
resolve conflict in gitmodules
ilinzweilin Jul 20, 2022
9d105c4
resolve comflicts in Messages tests
ilinzweilin Jul 20, 2022
ee54775
increase fuzz run count
ilinzweilin Jul 20, 2022
b0358a5
sign commit
ilinzweilin Jul 21, 2022
6f06bed
sign commit
ilinzweilin Jul 21, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
url = https://github.com/foundry-rs/forge-std
[submodule "lib/monorepo"]
path = lib/monorepo
branch = main
url = https://github.com/nomad-xyz/monorepo
[submodule "lib/openzeppelin-contracts-upgradeable"]
path = lib/openzeppelin-contracts-upgradeable
Expand Down
1 change: 1 addition & 0 deletions foundry.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ src = 'src'
out = 'out'
libs = ['lib']

fuzz-runs = 1000
solc_version = "0.7.6"
4 changes: 2 additions & 2 deletions src/Messages.sol
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ library ConnectorMessages {
function parseAddTranche(bytes29 _msg) internal pure returns (uint64 poolId, bytes16 trancheId, string memory tokenName, string memory tokenSymbol) {
poolId = uint64(_msg.indexUint(1, 8));
trancheId = bytes16(_msg.index(9, 16));
tokenName = bytes32ToString(bytes32(_msg.index(26, 32)));
tokenSymbol = bytes32ToString(bytes32(_msg.index(59, 32)));
tokenName = bytes32ToString(bytes32(_msg.index(25, 32)));
tokenSymbol = bytes32ToString(bytes32(_msg.index(57, 32)));
}

// TODO: should be moved to a util contract
Expand Down
1 change: 1 addition & 0 deletions src/routers/xcm/Router.sol
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ interface ConnectorLike {

contract ConnectorXCMRouter is Router, Test {
using TypedMemView for bytes;
// why bytes29? - https://github.com/summa-tx/memview-sol#why-bytes29
using TypedMemView for bytes29;
using ConnectorMessages for bytes29;

Expand Down
55 changes: 47 additions & 8 deletions test/Messages.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ pragma abicoder v2;

import {TypedMemView} from "@summa-tx/memview-sol/contracts/TypedMemView.sol";
import {ConnectorMessages} from "src/Messages.sol";
import "forge-std/console.sol";
import "forge-std/console2.sol";
import "forge-std/Test.sol";

contract MessagesTest is Test {
Expand All @@ -17,7 +19,7 @@ contract MessagesTest is Test {
assertEq(
ConnectorMessages.formatAddPool(0),
fromHex("010000000000000000")
);
);
assertEq(
ConnectorMessages.formatAddPool(1),
fromHex("010000000000000001")
Expand All @@ -34,16 +36,16 @@ contract MessagesTest is Test {
uint256(actualPoolId1),
0
);

uint64 actualPoolId2 = ConnectorMessages.parseAddPool(fromHex("010000000000000001").ref(0));
assertEq(
uint256(actualPoolId1),
uint256(actualPoolId2),
1
);

uint64 actualPoolId3 = ConnectorMessages.parseAddPool(fromHex("010000000000bce1a4").ref(0));
assertEq(
uint256(actualPoolId1),
uint256(actualPoolId3),
12378532
);
}
Expand All @@ -56,14 +58,23 @@ contract MessagesTest is Test {

function testAddTrancheEncoding() public {
assertEq(
ConnectorMessages.formatAddTranche(0, toBytes16(fromHex("100")), "Some Name", "SYMBOL"),
fromHex("010000000000000000")
ConnectorMessages.formatAddTranche(0, toBytes16(fromHex("010000000000000064")), "Some Name", "SYMBOL"),
fromHex("02000000000000000000000000000000000000000000000009536f6d65204e616d65000000000000000000000000000000000000000000000053594d424f4c0000000000000000000000000000000000000000000000000000")
);
}

function testAddTrancheDecoding() public returns (bytes memory) {
(uint64 decodedPoolId, bytes16 decodedTrancheId, string memory decodedTokenName, string memory decodedTokenSymbol) = ConnectorMessages.parseAddTranche(fromHex("02000000000000000000000000000000000000000000000009536f6d65204e616d65000000000000000000000000000000000000000000000053594d424f4c0000000000000000000000000000000000000000000000000000").ref(0));
assertEq(uint(decodedPoolId), uint(0));
assertEq(decodedTrancheId, toBytes16(fromHex("010000000000000064")));
assertEq(decodedTokenName, "Some Name");
assertEq(decodedTokenSymbol, "SYMBOL");
}

function testAddTrancheEquivalence(uint64 poolId, bytes16 trancheId, string memory tokenName, string memory tokenSymbol)
public
{
// edge case token Symbol - not passing "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
bytes memory _message = ConnectorMessages.formatAddTranche(
poolId,
trancheId,
Expand All @@ -78,6 +89,21 @@ contract MessagesTest is Test {
assertEq(decodedTokenSymbol, tokenSymbol);
}

function testUpdateMemberEncoding() public returns (bytes memory) {
assertEq(
ConnectorMessages.formatUpdateMember(5, toBytes16(fromHex("010000000000000003")), 0x225ef95fa90f4F7938A5b34234d14768cB4263dd, 1657870537),
fromHex("04000000000000000500000000000000000000000000000009225ef95fa90f4f7938a5b34234d14768cb4263dd0000000000000000000000000000000000000000000000000000000062d118c9")
);
}

function testUpdateMemberDecoding() public returns (bytes memory) {
(uint64 decodedPoolId, bytes16 decodedTrancheId, address decodedUser, uint256 decodedValidUntil) = ConnectorMessages.parseUpdateMember(fromHex("04000000000000000500000000000000000000000000000009225ef95fa90f4f7938a5b34234d14768cb4263dd0000000000000000000000000000000000000000000000000000000062d118c9").ref(0));
assertEq(uint(decodedPoolId), uint(5));
assertEq(decodedTrancheId, toBytes16(fromHex("010000000000000003")));
assertEq(decodedUser, 0x225ef95fa90f4F7938A5b34234d14768cB4263dd);
assertEq(decodedValidUntil, uint(1657870537));
}

function testUpdateMemberEquivalence(
uint64 poolId,
bytes16 trancheId,
Expand All @@ -102,6 +128,20 @@ contract MessagesTest is Test {
assertEq(decodedAmount, amount);
}

function testUpdateTokenPriceEncoding() public returns (bytes memory) {
assertEq(
ConnectorMessages.formatUpdateTokenPrice(3, toBytes16(fromHex("010000000000000005")), 100),
fromHex("030000000000000003000000000000000000000000000000090000000000000000000000000000000000000000000000000000000000000064")
);
}

function testUpdateTokenPriceDecoding() public returns (bytes memory) {
(uint64 decodedPoolId, bytes16 decodedTrancheId, uint256 decodedPrice) = ConnectorMessages.parseUpdateTokenPrice(fromHex("030000000000000003000000000000000000000000000000090000000000000000000000000000000000000000000000000000000000000064").ref(0));
assertEq(uint(decodedPoolId), uint(3));
assertEq(decodedTrancheId, toBytes16(fromHex("010000000000000005")));
assertEq(decodedPrice, uint(100));
}

function testUpdateTokenPriceEquivalence(
uint64 poolId,
bytes16 trancheId,
Expand Down Expand Up @@ -154,9 +194,8 @@ contract MessagesTest is Test {

function toBytes16(bytes memory f) internal pure returns (bytes16 fc) {
assembly {
fc := mload(add(f, 32))
fc := mload(add(f, 16))
}
return fc;
}

}