Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
lidangzzz committed Jan 18, 2024
1 parent 6014c51 commit c59dcae
Show file tree
Hide file tree
Showing 2 changed files with 238 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import "../Utilities/OpcodeMap.sol";
import "../Plugin.sol";

contract Condition_CreateTokenClass is MachineStateManager {
function createTokenClassExpressionCheck(Operation memory op, NodeParam memory param, uint256 id) internal view returns (bool) {
function createTokenClassExpressionCheck(Operation memory op, NodeParam memory param, uint256 id) internal pure returns (bool) {

if (id == 501) { return ID_501_CREATE_TOKEN_CLASSES_ANY_TOKEN_DIVIDEND_WEIGHT_GREATER_THAN(op, param); }
if (id == 502) { return ID_502_CREATE_TOKEN_CLASSES_ANY_TOKEN_DIVIDEND_WEIGHT_LESS_THAN(op, param); }
Expand All @@ -27,7 +27,7 @@ contract Condition_CreateTokenClass is MachineStateManager {
return false;
}

function ID_501_CREATE_TOKEN_CLASSES_ANY_TOKEN_DIVIDEND_WEIGHT_GREATER_THAN(Operation memory op, NodeParam memory param) private view returns (bool) {
function ID_501_CREATE_TOKEN_CLASSES_ANY_TOKEN_DIVIDEND_WEIGHT_GREATER_THAN(Operation memory op, NodeParam memory param) private pure returns (bool) {
require(param.UINT256_2DARRAY.length == 1, "CE ID_501: The UINT256_2DARRAY length is not 1");
require(param.UINT256_2DARRAY[0].length == 1, "CE ID_501: The UINT256_2DARRAY[0] length is not 1");
if (op.opcode != EnumOpcode.BATCH_CREATE_TOKEN_CLASSES) return false;
Expand All @@ -37,7 +37,7 @@ contract Condition_CreateTokenClass is MachineStateManager {
return false;
}

function ID_502_CREATE_TOKEN_CLASSES_ANY_TOKEN_DIVIDEND_WEIGHT_LESS_THAN(Operation memory op, NodeParam memory param) private view returns (bool) {
function ID_502_CREATE_TOKEN_CLASSES_ANY_TOKEN_DIVIDEND_WEIGHT_LESS_THAN(Operation memory op, NodeParam memory param) private pure returns (bool) {
require(param.UINT256_2DARRAY.length == 1, "CE ID_502: The UINT256_2DARRAY length is not 1");
require(param.UINT256_2DARRAY[0].length == 1, "CE ID_502: The UINT256_2DARRAY[0] length is not 1");
if (op.opcode != EnumOpcode.BATCH_CREATE_TOKEN_CLASSES) return false;
Expand All @@ -47,7 +47,7 @@ contract Condition_CreateTokenClass is MachineStateManager {
return false;
}

function ID_503_CREATE_TOKEN_CLASSES_ANY_TOKEN_DIVIDEND_WEIGHT_IN_RANGE(Operation memory op, NodeParam memory param) private view returns (bool) {
function ID_503_CREATE_TOKEN_CLASSES_ANY_TOKEN_DIVIDEND_WEIGHT_IN_RANGE(Operation memory op, NodeParam memory param) private pure returns (bool) {
require(param.UINT256_2DARRAY.length == 1, "CE ID_503: The UINT256_2DARRAY length is not 1");
require(param.UINT256_2DARRAY[0].length == 2, "CE ID_503: The UINT256_2DARRAY[0] length is not 2");
if (op.opcode != EnumOpcode.BATCH_CREATE_TOKEN_CLASSES) return false;
Expand All @@ -57,7 +57,7 @@ contract Condition_CreateTokenClass is MachineStateManager {
return false;
}

function ID_504_CREATE_TOKEN_CLASSES_ANY_TOKEN_DIVIDEND_WEIGHT_EQUALS(Operation memory op, NodeParam memory param) private view returns (bool) {
function ID_504_CREATE_TOKEN_CLASSES_ANY_TOKEN_DIVIDEND_WEIGHT_EQUALS(Operation memory op, NodeParam memory param) private pure returns (bool) {
require(param.UINT256_2DARRAY.length == 1, "CE ID_504: The UINT256_2DARRAY length is not 1");
require(param.UINT256_2DARRAY[0].length == 1, "CE ID_504: The UINT256_2DARRAY[0] length is not 1");
if (op.opcode != EnumOpcode.BATCH_CREATE_TOKEN_CLASSES) return false;
Expand All @@ -67,7 +67,7 @@ contract Condition_CreateTokenClass is MachineStateManager {
return false;
}

function ID_505_CREATE_TOKEN_CLASSES_ANY_VOTING_WEIGHT_GREATER_THAN(Operation memory op, NodeParam memory param) private view returns (bool) {
function ID_505_CREATE_TOKEN_CLASSES_ANY_VOTING_WEIGHT_GREATER_THAN(Operation memory op, NodeParam memory param) private pure returns (bool) {
require(param.UINT256_2DARRAY.length == 1, "CE ID_505: The UINT256_2DARRAY length is not 1");
require(param.UINT256_2DARRAY[0].length == 1, "CE ID_505: The UINT256_2DARRAY[0] length is not 1");
if (op.opcode != EnumOpcode.BATCH_CREATE_TOKEN_CLASSES) return false;
Expand All @@ -77,7 +77,7 @@ contract Condition_CreateTokenClass is MachineStateManager {
return false;
}

function ID_506_CREATE_TOKEN_CLASSES_ANY_VOTING_WEIGHT_LESS_THAN(Operation memory op, NodeParam memory param) private view returns (bool) {
function ID_506_CREATE_TOKEN_CLASSES_ANY_VOTING_WEIGHT_LESS_THAN(Operation memory op, NodeParam memory param) private pure returns (bool) {
require(param.UINT256_2DARRAY.length == 1, "CE ID_506: The UINT256_2DARRAY length is not 1");
require(param.UINT256_2DARRAY[0].length == 1, "CE ID_506: The UINT256_2DARRAY[0] length is not 1");
if (op.opcode != EnumOpcode.BATCH_CREATE_TOKEN_CLASSES) return false;
Expand All @@ -87,7 +87,7 @@ contract Condition_CreateTokenClass is MachineStateManager {
return false;
}

function ID_507_CREATE_TOKEN_CLASSES_ANY_VOTING_WEIGHT_IN_RANGE(Operation memory op, NodeParam memory param) private view returns (bool) {
function ID_507_CREATE_TOKEN_CLASSES_ANY_VOTING_WEIGHT_IN_RANGE(Operation memory op, NodeParam memory param) private pure returns (bool) {
require(param.UINT256_2DARRAY.length == 1, "CE ID_507: The UINT256_2DARRAY length is not 1");
require(param.UINT256_2DARRAY[0].length == 2, "CE ID_507: The UINT256_2DARRAY[0] length is not 2");
if (op.opcode != EnumOpcode.BATCH_CREATE_TOKEN_CLASSES) return false;
Expand All @@ -97,7 +97,7 @@ contract Condition_CreateTokenClass is MachineStateManager {
return false;
}

function ID_508_CREATE_TOKEN_CLASSES_ANY_VOTING_WEIGHT_EQUALS(Operation memory op, NodeParam memory param) private view returns (bool) {
function ID_508_CREATE_TOKEN_CLASSES_ANY_VOTING_WEIGHT_EQUALS(Operation memory op, NodeParam memory param) private pure returns (bool) {
require(param.UINT256_2DARRAY.length == 1, "CE ID_508: The UINT256_2DARRAY length is not 1");
require(param.UINT256_2DARRAY[0].length == 1, "CE ID_508: The UINT256_2DARRAY[0] length is not 1");
if (op.opcode != EnumOpcode.BATCH_CREATE_TOKEN_CLASSES) return false;
Expand Down
229 changes: 229 additions & 0 deletions darc-protocol/test/conditionTest/operationLogTest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,229 @@
import { time, loadFixture } from "@nomicfoundation/hardhat-network-helpers";
import { anyValue } from "@nomicfoundation/hardhat-chai-matchers/withArgs";
import { expect } from "chai";
import { ethers } from "hardhat";
import { BigNumber } from "ethers";
import { ConditionNodeStruct } from "../../typechain-types/contracts/protocol/DARC"


const programOperatorAddress = "0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266";

const target1 = '0x3C44CdDdB6a900fa2b585dd299e03d12FA4293BC';

const target2 = '0x90F79bf6EB2c4f870365E785982E1f101E93b906';

const target3 = '0x870526b7973b56163a6997bb7c886f5e4ea53638';

describe.only("operation log test", function () {
it ("should pass operation log test", async function () {


const PluginTestFactory = await ethers.getContractFactory("TestBaseContract");
const pluginTest = await PluginTestFactory.deploy();
await pluginTest.deployed();
await pluginTest.initialize();

// first set role of operator target 1 as level-1 operator

// add a plugin to disable all program
await pluginTest.addBeforeOpPlugin({
returnType: BigNumber.from(2), // no
level: BigNumber.from(3),
votingRuleIndex: BigNumber.from(0),
notes: "disable all program",
bIsEnabled: true,
bIsInitialized: true,
bIsBeforeOperation: true,
conditionNodes:[{
id: BigNumber.from(0),
nodeType: BigNumber.from(3), // expression
logicalOperator: BigNumber.from(0), // no operator
conditionExpression: BigNumber.from(0), // always true
childList: [],
param: {
STRING_ARRAY: [],
UINT256_2DARRAY: [],
ADDRESS_2DARRAY: [],
BYTES: []
}
}]
});
//return;
// add a plugin operatorAddress == target1 | operatorAddress == target2
// level == 4
// return type: yes and skip sandbox
await pluginTest.addBeforeOpPlugin({
returnType: BigNumber.from(4), // yes and skip sandbox
level: BigNumber.from(103),
votingRuleIndex: BigNumber.from(0),
notes: "allow operatorAddress == target1 | operatorAddress == target2",
bIsEnabled: true,
bIsInitialized: true,
bIsBeforeOperation: true,
conditionNodes:[
// node 0: boolean operator OR
{
id: BigNumber.from(0),
nodeType: BigNumber.from(2), // logical operator
logicalOperator: BigNumber.from(2), // OR
conditionExpression: 0, // no expression
childList: [BigNumber.from(1), BigNumber.from(2)],
param: {
STRING_ARRAY: [],
UINT256_2DARRAY: [],
ADDRESS_2DARRAY: [],
BYTES: []
}
},

// node 1: operatorAddress == target1
{
id : BigNumber.from(1),
nodeType: BigNumber.from(1), // expression
logicalOperator: BigNumber.from(0), // no operator
conditionExpression: BigNumber.from(3), // OPERATOR_ADDRESS_EQUALS
childList: [],
param: {
STRING_ARRAY: [],
UINT256_2DARRAY: [],
ADDRESS_2DARRAY: [[target1]],
BYTES: []
}
},

// node 2: operatorAddress == target2
{
id : BigNumber.from(2),
nodeType: BigNumber.from(1), // expression
logicalOperator: BigNumber.from(0), // no operator
conditionExpression: BigNumber.from(3), // OPERATOR_ADDRESS_EQUALS
childList: [],
param: {
STRING_ARRAY: [],
UINT256_2DARRAY: [],
ADDRESS_2DARRAY: [[target2]],
BYTES: []
}
}
]

});

const r1 = await pluginTest.checkProgram_beforeOp({
programOperatorAddress: programOperatorAddress,
operations: [{
operatorAddress: programOperatorAddress,
opcode: 2, // create token class
param: {


STRING_ARRAY: ["Class1", "Class2"],
BOOL_ARRAY: [],
VOTING_RULE_ARRAY: [],
PARAMETER_ARRAY: [],
PLUGIN_ARRAY: [],
UINT256_2DARRAY: [
[BigNumber.from(0), BigNumber.from(1)],
[BigNumber.from(10), BigNumber.from(1)],
[BigNumber.from(10), BigNumber.from(1)],
],
ADDRESS_2DARRAY: [],
BYTES: []
}
}],
notes: "create token class"
}
);


expect(r1.toString()).to.equal("2");
//console.log(await pluginTest.getBeforeOpPlugins());

// next check if program with operator address == target1 can be aprove by the plugin
const returnType = await pluginTest.checkProgram_beforeOp(
{
programOperatorAddress: target1,
notes: "",
operations: [
{
operatorAddress: target1,
opcode: 1, // mint token
param: {
STRING_ARRAY: [],
BOOL_ARRAY: [],
VOTING_RULE_ARRAY: [],
PARAMETER_ARRAY: [],
PLUGIN_ARRAY: [],
UINT256_2DARRAY: [],
ADDRESS_2DARRAY: [],
BYTES: []
}
}
]
}

);

//return;
// it should return "YES and skip sandbox" (4)
expect(returnType.toString()).to.equal("4");

// next check if program with operator address == target2 can be aprove by the plugin
const returnType2 = await pluginTest.checkProgram_beforeOp(
{
programOperatorAddress: target2,
notes: "",
operations: [
{
operatorAddress: target2,
opcode: 0, // mint token
param: {
STRING_ARRAY: [],
BOOL_ARRAY: [],
VOTING_RULE_ARRAY: [],
PARAMETER_ARRAY: [],
PLUGIN_ARRAY: [],
UINT256_2DARRAY: [],
ADDRESS_2DARRAY: [],
BYTES: []
}
}
]
}

);

// it should return "YES and skip sandbox" (4)
expect(returnType2.toString()).to.equal("4");

console.log("final test");
const returnType3 = await pluginTest.checkProgram_beforeOp(
{
programOperatorAddress: target3,
notes: "",
operations: [
{
operatorAddress: target3,
opcode: 0, // mint token
param: {
STRING_ARRAY: [],
BOOL_ARRAY: [],
VOTING_RULE_ARRAY: [],
PARAMETER_ARRAY: [],
PLUGIN_ARRAY: [],
UINT256_2DARRAY: [],
ADDRESS_2DARRAY: [],
BYTES: []
}
}
]
}

);

expect(returnType3.toString()).to.equal("2");

});


});

0 comments on commit c59dcae

Please sign in to comment.