Skip to content

Commit

Permalink
TrustedMacros: added more comments
Browse files Browse the repository at this point in the history
  • Loading branch information
hellwolf committed Jan 24, 2024
1 parent bf85baf commit 49f677c
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,16 @@ pragma solidity >=0.8.11;

import { ISuperfluid } from "../superfluid/ISuperfluid.sol";

// User-defined macro
/**
* @dev User-defined macro used in implementations of TrustedMacros.
*/
interface IUserDefinedMacro {
function executeMacro(ISuperfluid host, bytes memory params) external view
/**
* @dev Build batch operations according the parameters provided by the host contract.
* @param host The executing host contract.
* @param params The encoded form of the parameters.
* @return operations The batch operations built.
*/
function buildBatchOperations(ISuperfluid host, bytes memory params) external view
returns (ISuperfluid.Operation[] memory operations);
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,23 @@ import { ForwarderBase } from "../utils/ForwarderBase.sol";
contract TrustedMacrosVanilla is ForwarderBase {
constructor(ISuperfluid host) ForwarderBase(host) {}

/**
* @dev Simulate the macro run.
* @param m Target macro.
* @param params Parameters to simulate the macro.
* @return operations Operations returned by the macro after the simulation.
*/
function simulateMacro(IUserDefinedMacro m, bytes memory params) public view
returns (ISuperfluid.Operation[] memory operations)
{
operations = m.executeMacro(_host, params);
operations = m.buildBatchOperations(_host, params);
}

/**
* @dev Run the macro.
* @param m Target macro.
* @param params Parameters to run the macro.
*/
function runMacro(IUserDefinedMacro m, bytes memory params) external returns (bool)
{
ISuperfluid.Operation[] memory operations = simulateMacro(m, params);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,25 @@ import { TrustedMacrosVanilla, IUserDefinedMacro } from "../../../contracts/util
import { FoundrySuperfluidTester, SuperTokenV1Library } from "../FoundrySuperfluidTester.sol";


contract DummyMacro is IUserDefinedMacro {
function executeMacro(ISuperfluid, bytes memory) override external pure
returns (ISuperfluid.Operation[] memory operations)
{
operations = new ISuperfluid.Operation[](0);
}
}

contract NautyMacro {
uint naughtyCounter;
int naughtyCounter = -1;

function executeMacro(ISuperfluid, bytes memory) external
constructor(bool beNaughty) {
if (beNaughty) naughtyCounter = 0;
}

function buildBatchOperations(ISuperfluid, bytes memory) external
returns (ISuperfluid.Operation[] memory operation)
{
naughtyCounter++;
// Do the naughty thing (updating state as an expected view function)
if (naughtyCounter >= 0) {
naughtyCounter++;
}
}
}

contract GoodMacro is IUserDefinedMacro {
function executeMacro(ISuperfluid host, bytes memory params) external view
function buildBatchOperations(ISuperfluid host, bytes memory params) external view
returns (ISuperfluid.Operation[] memory operations)
{
// host-agnostic deployment. alternatively, you may hard code cfa too
Expand Down Expand Up @@ -71,12 +70,12 @@ contract TrustedMacrosVanillaTest is FoundrySuperfluidTester {
}

function testDummyMacro() external {
DummyMacro m = new DummyMacro();
trustedMacros.runMacro(m, new bytes(0));
NautyMacro m = new NautyMacro(false /* not naughty */);
trustedMacros.runMacro(IUserDefinedMacro(address(m)), new bytes(0));
}

function testNaugtyMacro() external {
NautyMacro m = new NautyMacro();
NautyMacro m = new NautyMacro(true /* naughty */);
vm.expectRevert();
// Note: need to cast the naughty macro
trustedMacros.runMacro(IUserDefinedMacro(address(m)), new bytes(0));
Expand Down

0 comments on commit 49f677c

Please sign in to comment.