Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
lidangzzz committed Feb 22, 2024
1 parent acd063e commit 473b0be
Show file tree
Hide file tree
Showing 5 changed files with 257 additions and 56 deletions.
10 changes: 2 additions & 8 deletions darc-protocol/contracts/protocol/Plugin/Condition_External.sol
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ contract Condition_External is MachineStateManager {
function ID_181_EXTERNAL_CALL_UINT256_RESULT_EQUALS(NodeParam memory param) private view returns (bool) {
require(param.ADDRESS_2DARRAY.length == 1, "CE ID_181: The ADDRESS_2DARRAY length is not 1");
require(param.ADDRESS_2DARRAY[0].length == 1, "CE ID_181: The ADDRESS_2DARRAY[0] length is not 1");
require(param.UINT256_2DARRAY.length == 0, "CE ID_181: The BYTES length is not 0");
require(param.UINT256_2DARRAY[0].length == 0, "CE ID_181: The BYTES length is not 0");
(bool success, uint256 value) = ExternalValueReader.tryReadUINT256(param.ADDRESS_2DARRAY[0][0], param.BYTES);
if (!success) { return false; }
if (value == param.UINT256_2DARRAY[0][0]) { return true; }
Expand All @@ -43,8 +41,6 @@ contract Condition_External is MachineStateManager {
function ID_182_EXTERNAL_CALL_UINT256_RESULT_GREATER_THAN(NodeParam memory param) private view returns (bool) {
require(param.ADDRESS_2DARRAY.length == 1, "CE ID_182: The ADDRESS_2DARRAY length is not 1");
require(param.ADDRESS_2DARRAY[0].length == 1, "CE ID_182: The ADDRESS_2DARRAY[0] length is not 1");
require(param.UINT256_2DARRAY.length == 0, "CE ID_182: The BYTES length is not 0");
require(param.UINT256_2DARRAY[0].length == 0, "CE ID_182: The BYTES length is not 0");
(bool success, uint256 value) = ExternalValueReader.tryReadUINT256(param.ADDRESS_2DARRAY[0][0], param.BYTES);
if (!success) { return false; }
if (value > param.UINT256_2DARRAY[0][0]) { return true; }
Expand All @@ -54,8 +50,7 @@ contract Condition_External is MachineStateManager {
function ID_183_EXTERNAL_CALL_UINT256_RESULT_LESS_THAN(NodeParam memory param) private view returns (bool) {
require(param.ADDRESS_2DARRAY.length == 1, "CE ID_183: The ADDRESS_2DARRAY length is not 1");
require(param.ADDRESS_2DARRAY[0].length == 1, "CE ID_183: The ADDRESS_2DARRAY[0] length is not 1");
require(param.UINT256_2DARRAY.length == 0, "CE ID_183: The BYTES length is not 0");
require(param.UINT256_2DARRAY[0].length == 0, "CE ID_183: The BYTES length is not 0");

(bool success, uint256 value) = ExternalValueReader.tryReadUINT256(param.ADDRESS_2DARRAY[0][0], param.BYTES);
if (!success) { return false; }
if (value < param.UINT256_2DARRAY[0][0]) { return true; }
Expand All @@ -65,8 +60,7 @@ contract Condition_External is MachineStateManager {
function ID_184_EXTERNAL_CALL_UINT256_RESULT_IN_RANGE(NodeParam memory param) private view returns (bool) {
require(param.ADDRESS_2DARRAY.length == 1, "CE ID_184: The ADDRESS_2DARRAY length is not 1");
require(param.ADDRESS_2DARRAY[0].length == 1, "CE ID_184: The ADDRESS_2DARRAY[0] length is not 1");
require(param.UINT256_2DARRAY.length == 1, "CE ID_184: The BYTES length is not 1");
require(param.UINT256_2DARRAY[0].length == 2, "CE ID_184: The BYTES[0] length is not 2");

(bool success, uint256 value) = ExternalValueReader.tryReadUINT256(param.ADDRESS_2DARRAY[0][0], param.BYTES);
if (!success) { return false; }
if (value >= param.UINT256_2DARRAY[0][0] && value <= param.UINT256_2DARRAY[0][1]) { return true; }
Expand Down
28 changes: 28 additions & 0 deletions darc-protocol/contracts/test-contracts/TestOracleContract.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// SPDX-License-Identifier: BUSL-1.1
pragma solidity ^0.8.19;
contract TestOracleContract{
uint256 x;
constructor() {
x = 0;
}

function A_plus_B(uint256 a, uint256 b) public pure returns (uint256){
return a + b;
}

function StringA_concat_StringB(string memory a, string memory b) public pure returns (string memory){
return string(abi.encodePacked(a, b));
}

function is_X_1() public view returns (bool){
return x == 1;
}

function set_X(uint256 a) public{
x = a;
}

function get_X_plus(uint256 a) public view returns (uint256){
return x + a;
}
}

This file was deleted.

1 change: 0 additions & 1 deletion darc-protocol/test/MultiDARCTest/Multi_DARC_call_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ describe("test for multi DARC call test", function () {
votingRuleIndex: 0,
notes: "0x70997970C51812dc3A010C7d01b50e0d17dc79C8 should operate",
bIsEnabled: true,
bIsInitialized: true,
bIsBeforeOperation: true,
}
],
Expand Down
227 changes: 227 additions & 0 deletions darc-protocol/test/conditionTest/externalCallTest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,227 @@
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, PluginStruct } from "../../typechain-types/contracts/protocol/DARC";

const programOperatorAddress = "0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266";

const target1 = "0x3C44CdDdB6a900fa2b585dd299e03d12FA4293BC";

const target2 = "0x90F79bf6EB2c4f870365E785982E1f101E93b906";

const target3 = "0x870526b7973b56163a6997bb7c886f5e4ea53638";

describe.only("external oracle condition test", function () {
it("should pass external oracle condition test", async function () {
const TestOracleFactory = await ethers.getContractFactory(
"TestOracleContract"
);
const testOracleContract = await TestOracleFactory.deploy();
await testOracleContract.deployed();

const testOracleContractAddress = testOracleContract.address;
testOracleContract.set

// deploy a darc with conditions
const DARCFactory = await ethers.getContractFactory("DARC");
const darc = await DARCFactory.deploy();
await darc.deployed();
await darc.initialize();

// encoding testOracleContract function is_X_1() with signature
// const encodedBytes = ethers.utils.encodedWithSignature
const theABI = ["function get_X_plus(uint256 a)"];
const abiInterface = new ethers.utils.Interface(theABI);
const resultBytes = abiInterface.encodeFunctionData("get_X_plus", [BigNumber.from(100)]);

const pluginList: PluginStruct[] = [

// pluigin 1
{
returnType: BigNumber.from(2),
level: 3,
conditionNodes:[
{
id: BigNumber.from(0),
nodeType: BigNumber.from(3), // true,
conditionExpression: 0,
childList: [],
logicalOperator: 0,
param: {
STRING_ARRAY: [],
UINT256_2DARRAY: [],
ADDRESS_2DARRAY: [],
BYTES: [],
}
}
],
votingRuleIndex: 0,
notes: "plugin 1",
bIsBeforeOperation: true,
bIsEnabled: true,
},


// plugin 2
{
returnType: BigNumber.from(4), // yes and skip sandbox
level: 10,
conditionNodes:[
{
id: 0,
nodeType: 1,
logicalOperator: 0,
conditionExpression: 181,
childList: [],
param: {
STRING_ARRAY: [],
UINT256_2DARRAY: [[200]], // if the value is 200, then the condition is true, then yes and skip sandbox
ADDRESS_2DARRAY: [[testOracleContractAddress]],
BYTES: resultBytes,
}
}
],
votingRuleIndex: 0,
notes: "plugin 2",
bIsBeforeOperation: true,
bIsEnabled: true
}
]

// create a token class, then add and enable plugin
await darc.entrance(
{
programOperatorAddress: programOperatorAddress,
notes: "create token class",
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: [],
},
},
{
operatorAddress: programOperatorAddress,
opcode: 15, // add and enable plugin
param: {
STRING_ARRAY: [],
BOOL_ARRAY: [],
VOTING_RULE_ARRAY: [],
PARAMETER_ARRAY: [],
PLUGIN_ARRAY: pluginList,
UINT256_2DARRAY: [],
ADDRESS_2DARRAY: [],
BYTES: [],
},
}
],
}
);

// now try to set the value of the oracle to 100
//await testOracleContract.set_X(100);
// try to mint token
let bIsException = false;
try{
const result = await darc.entrance(
{
programOperatorAddress: programOperatorAddress,
notes: "mint token",
operations: [
{
operatorAddress: programOperatorAddress,
opcode: 1, // mint token
param: {
STRING_ARRAY: [],
BOOL_ARRAY: [],
VOTING_RULE_ARRAY: [],
PARAMETER_ARRAY: [],
PLUGIN_ARRAY: [],
UINT256_2DARRAY: [[0],[100]],
ADDRESS_2DARRAY: [[programOperatorAddress]],
BYTES: [],
},
}
],
}
);
}
catch (err) {
bIsException = true;
}
expect(bIsException).to.equal(true);


//set value of the oracle to 100, so that this time the oracle will return 200, which will make the condition true
await testOracleContract.set_X(100);
// try to mint token again
const result = await darc.entrance(
{
programOperatorAddress: programOperatorAddress,
notes: "mint token",
operations: [
{
operatorAddress: programOperatorAddress,
opcode: 1, // mint token
param: {
STRING_ARRAY: [],
BOOL_ARRAY: [],
VOTING_RULE_ARRAY: [],
PARAMETER_ARRAY: [],
PLUGIN_ARRAY: [],
UINT256_2DARRAY: [[0],[100]],
ADDRESS_2DARRAY: [[programOperatorAddress]],
BYTES: [],
},
}
],
}
);
expect(true).equal(true);

bIsException = false;
await testOracleContract.set_X(200);
try{
const result = await darc.entrance(
{
programOperatorAddress: programOperatorAddress,
notes: "mint token",
operations: [
{
operatorAddress: programOperatorAddress,
opcode: 1, // mint token
param: {
STRING_ARRAY: [],
BOOL_ARRAY: [],
VOTING_RULE_ARRAY: [],
PARAMETER_ARRAY: [],
PLUGIN_ARRAY: [],
UINT256_2DARRAY: [[0],[100]],
ADDRESS_2DARRAY: [[programOperatorAddress]],
BYTES: [],
},
}
],
}
);
}
catch(err) {
bIsException = true;
}
expect(bIsException).to.equal(true);
});
});

0 comments on commit 473b0be

Please sign in to comment.