|
| 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