Skip to content

Commit 27fe87e

Browse files
authored
chore: add more tests (#47)
1 parent 1072c28 commit 27fe87e

File tree

3 files changed

+228
-1
lines changed

3 files changed

+228
-1
lines changed

bytecode/src/lib.rs

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

test/AssetsLib.t.sol

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
// SPDX-License-Identifier: UNLICENSED
2+
pragma solidity ^0.8.20;
3+
4+
import {Test} from "forge-std/Test.sol";
5+
import {Assets} from "../src/AssetsLib.sol";
6+
7+
contract AssetsLibTest is Test {
8+
// Test constants
9+
address constant TEST_ERC20_ADDRESS = address(0x1234567890123456789012345678901234567890);
10+
bytes32 constant TEST_ASSET_ID = bytes32(uint256(123));
11+
12+
function setUp() public {
13+
// No setup required as we're testing a library
14+
}
15+
16+
function testErc20AssetConversion() public {
17+
Assets.Asset memory asset = Assets.Asset({
18+
kind: Assets.Kind.Erc20,
19+
data: bytes32(uint256(uint160(TEST_ERC20_ADDRESS)))
20+
});
21+
22+
address result = Assets.toAddress(asset);
23+
assertEq(result, TEST_ERC20_ADDRESS, "ERC20 address conversion failed");
24+
}
25+
26+
function testCustomAssetConversion() public {
27+
Assets.Asset memory asset = Assets.Asset({
28+
kind: Assets.Kind.Custom,
29+
data: TEST_ASSET_ID
30+
});
31+
32+
address result = Assets.toAddress(asset);
33+
assertTrue(Assets.isAssetIdCompatible(result), "Custom asset address should be compatible");
34+
assertEq(Assets.toAssetId(result), TEST_ASSET_ID, "Asset ID conversion failed");
35+
}
36+
37+
function testAssetIdToAddressConversion() public {
38+
address result = Assets.toAddress(bytes32(uint256(0xAB))); // Using 0xAB as test value
39+
assertTrue(Assets.isAssetIdCompatible(result), "Result should be asset ID compatible");
40+
assertEq(result, 0xffffFfFF000000000000000000000000000000AB, "Asset ID to address conversion failed");
41+
}
42+
43+
function testInvalidAssetIdReversion() public {
44+
address invalidAddress = address(0x1234567890123456789012345678901234567890);
45+
vm.expectRevert(abi.encodeWithSelector(Assets.InvalidAssetId.selector, invalidAddress));
46+
Assets.toAssetId(invalidAddress);
47+
}
48+
49+
function testAssetTypeChecks() public {
50+
Assets.Asset memory erc20Asset = Assets.Asset({
51+
kind: Assets.Kind.Erc20,
52+
data: bytes32(uint256(uint160(TEST_ERC20_ADDRESS)))
53+
});
54+
55+
Assets.Asset memory customAsset = Assets.Asset({
56+
kind: Assets.Kind.Custom,
57+
data: TEST_ASSET_ID
58+
});
59+
60+
assertTrue(Assets.isErc20(erc20Asset), "Should be identified as ERC20");
61+
assertFalse(Assets.isErc20(customAsset), "Should not be identified as ERC20");
62+
assertTrue(Assets.isCustom(customAsset), "Should be identified as Custom");
63+
assertFalse(Assets.isCustom(erc20Asset), "Should not be identified as Custom");
64+
}
65+
66+
function testToAssetConversion() public {
67+
// Test ERC20 address conversion
68+
Assets.Asset memory erc20Result = Assets.toAsset(TEST_ERC20_ADDRESS);
69+
assertTrue(Assets.isErc20(erc20Result), "Should convert to ERC20 asset");
70+
assertEq(address(uint160(uint256(erc20Result.data))), TEST_ERC20_ADDRESS, "ERC20 address mismatch");
71+
72+
// Test Custom asset address conversion
73+
Assets.Asset memory customResult = Assets.toAsset(0xffffFfFF000000000000000000000000000000AB);
74+
assertTrue(Assets.isCustom(customResult), "Should convert to Custom asset");
75+
assertEq(customResult.data, bytes32(uint256(0xAB)), "Custom asset ID mismatch");
76+
}
77+
78+
function testNativeAssetChecks() public {
79+
// Test native ERC20 (address(0))
80+
Assets.Asset memory nativeErc20 = Assets.Asset({
81+
kind: Assets.Kind.Erc20,
82+
data: bytes32(0)
83+
});
84+
assertTrue(Assets.isNative(nativeErc20), "Should identify native ERC20");
85+
86+
// Test native Custom asset (id = 0)
87+
Assets.Asset memory nativeCustom = Assets.Asset({
88+
kind: Assets.Kind.Custom,
89+
data: bytes32(0)
90+
});
91+
assertTrue(Assets.isNative(nativeCustom), "Should identify native Custom asset");
92+
93+
// Test non-native assets
94+
Assets.Asset memory nonNativeErc20 = Assets.Asset({
95+
kind: Assets.Kind.Erc20,
96+
data: bytes32(uint256(uint160(TEST_ERC20_ADDRESS)))
97+
});
98+
assertFalse(Assets.isNative(nonNativeErc20), "Should not identify as native ERC20");
99+
100+
Assets.Asset memory nonNativeCustom = Assets.Asset({
101+
kind: Assets.Kind.Custom,
102+
data: TEST_ASSET_ID
103+
});
104+
assertFalse(Assets.isNative(nonNativeCustom), "Should not identify as native Custom asset");
105+
}
106+
}

test/ServiceOperatorsLib.t.sol

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
// SPDX-License-Identifier: UNLICENSED
2+
pragma solidity ^0.8.20;
3+
4+
import {Test} from "forge-std/Test.sol";
5+
import {ServiceOperators} from "../src/ServiceOperatorsLib.sol";
6+
import {Assets} from "../src/AssetsLib.sol";
7+
8+
contract ServiceOperatorsLibTest is Test {
9+
// Test constants
10+
bytes constant TEST_PUBLIC_KEY = hex"04d2688b6bc2ce7676a3a9d2f85e178d1964e0fdc1cc8d8ed3d196b5ca6d7932d18f1a48789057ed03d100147b365627427b1918c405c932c2ca81625fd8a23975";
11+
address constant EXPECTED_OPERATOR = address(0x9C7483fb4D62C4f48E5d1049BB87B3E54b013E6b);
12+
13+
function setUp() public {
14+
// No setup required as we're testing a library
15+
}
16+
17+
function testStructInitialization() public {
18+
// Test PriceTargets initialization
19+
ServiceOperators.PriceTargets memory prices = ServiceOperators.PriceTargets({
20+
cpu: 100,
21+
mem: 200,
22+
storage_hdd: 300,
23+
storage_ssd: 400,
24+
storage_nvme: 500
25+
});
26+
27+
assertEq(prices.cpu, 100, "CPU price not set correctly");
28+
assertEq(prices.mem, 200, "Memory price not set correctly");
29+
assertEq(prices.storage_hdd, 300, "HDD storage price not set correctly");
30+
assertEq(prices.storage_ssd, 400, "SSD storage price not set correctly");
31+
assertEq(prices.storage_nvme, 500, "NVMe storage price not set correctly");
32+
33+
// Test OperatorPreferences initialization
34+
ServiceOperators.OperatorPreferences memory prefs = ServiceOperators.OperatorPreferences({
35+
ecdsaPublicKey: TEST_PUBLIC_KEY,
36+
priceTargets: prices
37+
});
38+
39+
assertEq(prefs.ecdsaPublicKey, TEST_PUBLIC_KEY, "ECDSA public key not set correctly");
40+
assertEq(prefs.priceTargets.cpu, prices.cpu, "Price targets not set correctly");
41+
}
42+
43+
function testRequestParamsInitialization() public {
44+
// Create operator preferences array
45+
ServiceOperators.OperatorPreferences[] memory operators = new ServiceOperators.OperatorPreferences[](2);
46+
47+
// Set up price targets for operators
48+
ServiceOperators.PriceTargets memory prices1 = ServiceOperators.PriceTargets({
49+
cpu: 100,
50+
mem: 200,
51+
storage_hdd: 300,
52+
storage_ssd: 400,
53+
storage_nvme: 500
54+
});
55+
56+
ServiceOperators.PriceTargets memory prices2 = ServiceOperators.PriceTargets({
57+
cpu: 150,
58+
mem: 250,
59+
storage_hdd: 350,
60+
storage_ssd: 450,
61+
storage_nvme: 550
62+
});
63+
64+
// Set up operator preferences
65+
operators[0] = ServiceOperators.OperatorPreferences({
66+
ecdsaPublicKey: TEST_PUBLIC_KEY,
67+
priceTargets: prices1
68+
});
69+
70+
operators[1] = ServiceOperators.OperatorPreferences({
71+
ecdsaPublicKey: hex"04e2688b6bc2ce7676a3a9d2f85e178d1964e0fdc1cc8d8ed3d196b5ca6d7932d18f1a48789057ed03d100147b365627427b1918c405c932c2ca81625fd8a23976",
72+
priceTargets: prices2
73+
});
74+
75+
// Set up permitted callers
76+
address[] memory permittedCallers = new address[](2);
77+
permittedCallers[0] = address(0x1234);
78+
permittedCallers[1] = address(0x5678);
79+
80+
// Create request params
81+
ServiceOperators.RequestParams memory params = ServiceOperators.RequestParams({
82+
requestId: 12345,
83+
requester: address(this),
84+
operators: operators,
85+
requestInputs: hex"1234567890",
86+
permittedCallers: permittedCallers,
87+
ttl: 3600,
88+
paymentAsset: Assets.Asset({
89+
kind: Assets.Kind.Erc20,
90+
data: bytes32(uint256(uint160(address(0x1))))
91+
}),
92+
amount: 1000
93+
});
94+
95+
// Verify request params
96+
assertEq(params.requestId, 12345, "Request ID not set correctly");
97+
assertEq(params.requester, address(this), "Requester not set correctly");
98+
assertEq(params.operators.length, 2, "Operators array length incorrect");
99+
assertEq(params.requestInputs, hex"1234567890", "Request inputs not set correctly");
100+
assertEq(params.permittedCallers.length, 2, "Permitted callers array length incorrect");
101+
assertEq(params.ttl, 3600, "TTL not set correctly");
102+
assertEq(params.amount, 1000, "Amount not set correctly");
103+
104+
// Verify first operator
105+
assertEq(params.operators[0].ecdsaPublicKey, TEST_PUBLIC_KEY, "First operator public key not set correctly");
106+
assertEq(params.operators[0].priceTargets.cpu, 100, "First operator CPU price not set correctly");
107+
108+
// Verify permitted callers
109+
assertEq(params.permittedCallers[0], address(0x1234), "First permitted caller not set correctly");
110+
assertEq(params.permittedCallers[1], address(0x5678), "Second permitted caller not set correctly");
111+
}
112+
113+
function testDifferentPublicKeysProduceDifferentAddresses(bytes calldata key1, bytes calldata key2) public {
114+
vm.assume(keccak256(key1) != keccak256(key2));
115+
116+
address operator1 = ServiceOperators.asOperatorAddress(key1);
117+
address operator2 = ServiceOperators.asOperatorAddress(key2);
118+
119+
assertTrue(operator1 != operator2, "Different public keys should produce different addresses");
120+
}
121+
}

0 commit comments

Comments
 (0)