Skip to content

Commit

Permalink
Offchain concat (#687)
Browse files Browse the repository at this point in the history
* Remove concat from DAOFactory contract

* Fix

* test fail.

* fix bug in setSchemes

* update package-lock.json

* update test
  • Loading branch information
ben-kaufman authored and orenyodfat committed Nov 20, 2019
1 parent bbecb89 commit 02434cf
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 52 deletions.
16 changes: 3 additions & 13 deletions contracts/utils/DAOFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ import "@openzeppelin/upgrades/contracts/upgradeability/AdminUpgradeabilityProxy
import "solidity-bytes-utils/contracts/BytesLib.sol";
import "../controller/Controller.sol";
import "../utils/DAOTracker.sol";
import "@openzeppelin/contracts-ethereum-package/contracts/math/SafeMath.sol";


contract DAOFactory is Initializable {
using BytesLib for bytes;
using SafeMath for uint256;

event NewOrg (address indexed _avatar);
event InitialSchemesSet (address indexed _avatar);
Expand Down Expand Up @@ -139,18 +141,6 @@ contract DAOFactory is Initializable {
_metaData);
}

//this function is an helper function to concate 2 bytes vars and return its length.
//todo: implement that offlince and remove it from the contract
function bytesConcat(bytes calldata _preBytes, bytes calldata _postBytes)
external
pure
returns (bytes memory, uint256, uint256) {
if (_postBytes.length == 0) {
return (_preBytes, _preBytes.length, 0);
}
return (_preBytes.concat(_postBytes), _preBytes.length, _postBytes.length);
}

/**
* @dev Creates a new proxy for the given contract and forwards a function call to it.
* This is useful to initialize the proxied contract.
Expand Down Expand Up @@ -212,7 +202,7 @@ contract DAOFactory is Initializable {
_schemesData.slice(startIndex, _schemesInitilizeDataLens[i])));
emit SchemeInstance(scheme, bytes32ToStr(_schemesNames[i]));
controller.registerScheme(scheme, _permissions[i]);
startIndex = _schemesInitilizeDataLens[i];
startIndex = startIndex.add(_schemesInitilizeDataLens[i]);
}
controller.metaData(_metaData);
// Unregister self:
Expand Down
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 3 additions & 5 deletions test/contributionreward.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,12 @@ const setup = async function (accounts,genesisProtocol = false,tokenAddress=0) {
tokenAddress,
testSetup.org.avatar.address);
var permissions = "0x00000000";

var bytesConcate = await registration.daoFactory.bytesConcat(testSetup.contributionRewardParams.initdata,"0x");


var tx = await registration.daoFactory.setSchemes(
testSetup.org.avatar.address,
[web3.utils.fromAscii("ContributionReward")],
bytesConcate[0],
[bytesConcate[1]],
testSetup.contributionRewardParams.initdata,
[helpers.getBytesLength(testSetup.contributionRewardParams.initdata)],
[permissions],
"metaData",{from:testSetup.proxyAdmin});

Expand Down
64 changes: 33 additions & 31 deletions test/daofactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const Reputation = artifacts.require("./Reputation.sol");
const Avatar = artifacts.require("./Avatar.sol");
const Controller = artifacts.require("./Controller.sol");
const SchemeMock = artifacts.require('./test/SchemeMock.sol');

const Wallet = artifacts.require('./test/Wallet.sol');

var avatar;
var daoToken;
Expand Down Expand Up @@ -73,38 +73,45 @@ contract('DaoFactory', function(accounts) {
assert.equal(controllerReputationAddress,reputationAddress);
});

it("setSchemes", async function() {
it("setSchemes", async function() {
var amountToMint = 10;
await setup(accounts,amountToMint,amountToMint);
var schemeMockData1 = await new web3.eth.Contract(registration.schemeMock.abi)
.methods
.initialize(avatar.address,1)
.encodeABI();

var schemeMockData2 = await new web3.eth.Contract(registration.schemeMock.abi)
.methods
.initialize(avatar.address,2)
.encodeABI();
var bytesConcate = await registration.daoFactory.bytesConcat(schemeMockData1,schemeMockData2);
var walletData = await new web3.eth.Contract(registration.wallet.abi)
.methods
.initialize(avatar.address)
.encodeABI();

var tx = await registration.daoFactory.setSchemes(
avatar.address,
[web3.utils.fromAscii("SchemeMock"),web3.utils.fromAscii("SchemeMock")],
bytesConcate[0],
[bytesConcate[1],bytesConcate[2]],
["0x0000000F","0x0000000F"],
[web3.utils.fromAscii("Wallet"),
web3.utils.fromAscii("SchemeMock"),
web3.utils.fromAscii("SchemeMock")],
helpers.concatBytes(helpers.concatBytes(walletData,schemeMockData1),schemeMockData2),
[helpers.getBytesLength(walletData), helpers.getBytesLength(schemeMockData1),helpers.getBytesLength(schemeMockData2)],
["0x0000000F","0x0000000F","0x0000000F"],
"metaData");
assert.equal(tx.logs.length, 5);
assert.equal(tx.logs[4].event, "InitialSchemesSet");
assert.equal(tx.logs[4].args._avatar, avatar.address);

assert.equal(tx.logs.length, 7);
assert.equal(tx.logs[6].event, "InitialSchemesSet");
assert.equal(tx.logs[6].args._avatar, avatar.address);
assert.equal(tx.logs[1].event, "SchemeInstance");
var scheme1Instance = new SchemeMock(tx.logs[1].args._scheme);
var scheme2Instance = new SchemeMock(tx.logs[3].args._scheme);
var walletInstance = new Wallet(tx.logs[1].args._scheme);
var scheme1Instance = new SchemeMock(tx.logs[3].args._scheme);
var scheme2Instance = new SchemeMock(tx.logs[5].args._scheme);
assert.equal(await scheme1Instance.testData({from:accounts[1]}), 1);
assert.equal(await scheme2Instance.testData({from:accounts[1]}), 2);

assert.equal(await walletInstance.owner({from:accounts[1]}), avatar.address);
});


it("setSchemes from account that does not hold the lock", async function() {
var amountToMint = 10;
await setup(accounts,amountToMint,amountToMint);
Expand All @@ -116,14 +123,13 @@ contract('DaoFactory', function(accounts) {
.methods
.initialize(avatar.address,2)
.encodeABI();
var bytesConcate = await registration.daoFactory.bytesConcat(schemeMockData1,schemeMockData2);

try {
await registration.daoFactory.setSchemes(
avatar.address,
[web3.utils.fromAscii("SchemeMock"),web3.utils.fromAscii("SchemeMock")],
bytesConcate[0],
[bytesConcate[1],bytesConcate[2]],
helpers.concatBytes(schemeMockData1, schemeMockData2),
[helpers.getBytesLength(schemeMockData1), helpers.getBytesLength(schemeMockData2)],
["0x0000000F","0x0000000F"],
"metaData",{from:accounts[1]});
assert(false,"should fail because accounts[1] does not hold the lock");
Expand All @@ -141,13 +147,12 @@ contract('DaoFactory', function(accounts) {
.methods
.initialize(avatar.address,1)
.encodeABI();
var bytesConcate = await registration.daoFactory.bytesConcat(schemeMockData1,"0x");

var tx = await registration.daoFactory.setSchemes(
avatar.address,
[web3.utils.fromAscii("SchemeMock")],
bytesConcate[0],
[bytesConcate[1]],
schemeMockData1,
[helpers.getBytesLength(schemeMockData1)],
["0x0000000F"],
"metaData");
controllerAddress = await avatar.owner({from:accounts[1]});
Expand All @@ -169,13 +174,12 @@ contract('DaoFactory', function(accounts) {
.methods
.initialize(avatar.address,1)
.encodeABI();
var bytesConcate = await registration.daoFactory.bytesConcat(schemeMockData1,"0x");

await registration.daoFactory.setSchemes(
avatar.address,
[web3.utils.fromAscii("SchemeMock")],
bytesConcate[0],
[bytesConcate[1]],
schemeMockData1,
[helpers.getBytesLength(schemeMockData1)],
["0x0000000F"],
"metaData");
isSchemeRegistered = await controller.isSchemeRegistered(registration.daoFactory.address,{from:accounts[1]});
Expand All @@ -189,21 +193,20 @@ contract('DaoFactory', function(accounts) {
.methods
.initialize(avatar.address,1)
.encodeABI();
var bytesConcate = await registration.daoFactory.bytesConcat(schemeMockData1,"0x");

await registration.daoFactory.setSchemes(
avatar.address,
[web3.utils.fromAscii("SchemeMock")],
bytesConcate[0],
[bytesConcate[1]],
schemeMockData1,
[helpers.getBytesLength(schemeMockData1)],
["0x0000000F"],
"metaData");
try {
await registration.daoFactory.setSchemes(
avatar.address,
[web3.utils.fromAscii("SchemeMock")],
bytesConcate[0],
[bytesConcate[1]],
schemeMockData1,
[helpers.getBytesLength(schemeMockData1)],
["0x0000000F"],
"metaData");
assert(false,"should fail because lock for account[0] suppose to be deleted by the first call");
Expand Down Expand Up @@ -271,13 +274,12 @@ contract('DaoFactory', function(accounts) {
.methods
.initialize(avatar.address,1)
.encodeABI();
var bytesConcate = await registration.daoFactory.bytesConcat(schemeMockData1,"0x");

var tx = await registration.daoFactory.setSchemes(
avatar.address,
[web3.utils.fromAscii("SchemeMock")],
bytesConcate[0],
[bytesConcate[1]],
schemeMockData1,
[helpers.getBytesLength(schemeMockData1)],
["0x0000000F"],
"metaData");
assert.equal(tx.logs.length, 3);
Expand Down
11 changes: 11 additions & 0 deletions test/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const constants = require('./constants');
const GenesisProtocol = artifacts.require("./GenesisProtocol.sol");
const DAOFactory = artifacts.require("./DAOFactory.sol");
const SchemeMock = artifacts.require('./test/SchemeMock.sol');
const Wallet = artifacts.require('./test/Wallet.sol');
const DAOTracker = artifacts.require("./DAOTracker.sol");
const App = artifacts.require("./App.sol");
const Package = artifacts.require("./Package.sol");
Expand Down Expand Up @@ -143,12 +144,14 @@ export const registrationAddVersionToPackege = async function (registration,vers
registration.avatar = await Avatar.new();
registration.controller = await Controller.new();
registration.schemeMock = await SchemeMock.new();
registration.wallet = await Wallet.new();
registration.contributionReward = await ContributionReward.new();
await implementationDirectory.setImplementation("DAOToken",registration.daoToken.address);
await implementationDirectory.setImplementation("Reputation",registration.reputation.address);
await implementationDirectory.setImplementation("Avatar",registration.avatar.address);
await implementationDirectory.setImplementation("Controller",registration.controller.address);
await implementationDirectory.setImplementation("SchemeMock",registration.schemeMock.address);
await implementationDirectory.setImplementation("Wallet",registration.wallet.address);
await implementationDirectory.setImplementation("ContributionReward",registration.contributionReward.address);
return registration;
};
Expand Down Expand Up @@ -355,3 +358,11 @@ export const increaseTime = async function(duration) {
});
});
};

export const concatBytes = function (bytes1, bytes2) {
return bytes1 + (bytes2.slice(2));
};

export const getBytesLength = function (bytes) {
return web3.utils.toBN(Number(bytes.slice(2).length) / 2);
};

0 comments on commit 02434cf

Please sign in to comment.