Allow to add nodes

This commit is contained in:
Andrea Maria Piana 2019-03-20 09:50:54 +01:00
parent 1245b52d95
commit 7966cda2bb
No known key found for this signature in database
GPG Key ID: AA6CCA6DE0E06424
8 changed files with 3833 additions and 12 deletions

24
.eslintrc.js Normal file
View File

@ -0,0 +1,24 @@
module.exports = {
env: {
browser: true,
es6: true,
},
extends: 'airbnb-base',
globals: {
Atomics: 'readonly',
SharedArrayBuffer: 'readonly',
artifacts: 'readonly',
contract: 'readonly',
describe: 'readonly',
beforeEach: 'readonly',
before: 'readonly',
afterEach: 'readonly',
after: 'readonly',
it: 'readonly',
assert: 'readonly',
},
parserOptions: {
ecmaVersion: 2018,
sourceType: 'module',
},
};

39
contracts/Nodes.sol Normal file
View File

@ -0,0 +1,39 @@
pragma solidity >=0.4.21 <0.6.0;
contract Nodes
{
address owner;
string[] public nodes;
uint public nodeCount;
modifier onlyOwner() {
require(msg.sender == owner);
_;
}
constructor()
public
{
owner = msg.sender;
}
function addNode(string memory _node)
public
onlyOwner
{
nodes.push(_node);
nodeCount++;
}
function deleteAll()
public
onlyOwner
{
delete nodes;
nodeCount = 0;
}
function () external payable {
require(msg.data.length == 0);
}
}

View File

@ -1,5 +1,5 @@
const Migrations = artifacts.require("Migrations");
const Migrations = artifacts.require('Migrations');
module.exports = function(deployer) {
module.exports = function deploy(deployer) {
deployer.deploy(Migrations);
};

View File

@ -0,0 +1,5 @@
const Nodes = artifacts.require('./Nodes.sol');
module.exports = function deploy(deployer) {
deployer.deploy(Nodes);
};

3650
package-lock.json generated

File diff suppressed because it is too large Load Diff

26
package.json Normal file
View File

@ -0,0 +1,26 @@
{
"name": "network-incentivisation-contract",
"version": "1.0.0",
"description": "",
"main": "truffle-config.js",
"directories": {
"test": "test"
},
"dependencies": {
"truffle-hdwallet-provider": "^1.0.5"
},
"devDependencies": {
"eslint": "^5.15.3",
"eslint-config-airbnb-base": "^13.1.0",
"eslint-plugin-import": "^2.16.0",
"nodemon": "^1.18.10"
},
"scripts": {
"test": "truffle test",
"lint": "eslint . --ext .js",
"lint-watch": "nodemon --exec 'npm run lint'",
"validate": "npm test && npm run lint"
},
"author": "",
"license": "ISC"
}

80
test/TestNodes.js Normal file
View File

@ -0,0 +1,80 @@
const Nodes = artifacts.require('Nodes');
const node1 = 'enode://da61e9eff86a56633b635f887d8b91e0ff5236bbc05b8169834292e92afb92929dcf6efdbf373a37903da8fe0384d5a0a8247e83f1ce211aa429200b6d28c548@47.91.156.93:443';
const node2 = 'enode://7de99e4cb1b3523bd26ca212369540646607c721ad4f3e5c821ed9148150ce6ce2e72631723002210fac1fd52dfa8bbdf3555e05379af79515e1179da37cc3db@35.188.19.210:443';
contract('Nodes', async (accounts) => {
let instance;
beforeEach(async () => {
instance = await Nodes.deployed();
await instance.deleteAll();
});
describe('addNode', async () => {
describe('called by the owner', async () => {
beforeEach(async () => {
await instance.addNode(node1);
await instance.addNode(node2);
});
it('adds the first node', async () => {
const actualNode1 = await instance.nodes(0);
assert.equal(actualNode1, node1);
});
it('adds the second node', async () => {
const actualNode2 = await instance.nodes(1);
assert.equal(actualNode2, node2);
});
it('sets the count', async () => {
const actualNodeCount = await instance.nodeCount();
assert.equal(2, actualNodeCount);
});
});
describe('called by someone else', async () => {
it('throws an exception', async () => {
try {
await instance.addNode(node1, { from: accounts[1] });
} catch (error) {
return;
}
assert.fail('it should throw an exception');
});
});
});
describe('deleteAll', async () => {
beforeEach(async () => {
await instance.addNode(node1);
await instance.addNode(node2);
await instance.deleteAll();
});
it('empties the array', async () => {
try {
await instance.nodes(0);
} catch (error) {
return;
}
assert.fail('it should throw an exception');
});
it('sets the count', async () => {
const actualNodeCount = await instance.nodeCount();
assert.equal(0, actualNodeCount);
});
describe('called by someone else', async () => {
it('throws an exception', async () => {
try {
await instance.deleteAll({ from: accounts[1] });
} catch (error) {
return;
}
assert.fail('it should throw an exception');
});
});
});
});

View File

@ -1,3 +1,4 @@
const HDWalletProvider = require('truffle-hdwallet-provider');
/**
* Use this file to configure your truffle project. It's seeded with some
* common settings for different networks and features like migrations,
@ -76,10 +77,8 @@ module.exports = {
// production: true // Treats this network as if it was a public net. (default: false)
// }
goerli: {
provider: () => {
return new HDWalletProvider(process.env.MNEMONIC, 'https://goerli.infura.io/v3/' + process.env.INFURA_API_KEY)
},
network_id: '5', // eslint-disable-line camelcase
provider: () => new HDWalletProvider(process.env.MNEMONIC, `https://goerli.infura.io/v3/${process.env.INFURA_API_KEY}`),
network_id: '5',
gas: 4465030,
gasPrice: 10000000000,
},
@ -102,6 +101,6 @@ module.exports = {
// },
// evmVersion: "byzantium"
// }
}
}
}
},
},
};