Skip to content
This repository has been archived by the owner on Sep 26, 2019. It is now read-only.

Commit

Permalink
Merge branch 'master' of github.com:PegaSysEng/pantheon into pipeline…
Browse files Browse the repository at this point in the history
…-download-fetch-data
  • Loading branch information
ajsutton committed Apr 4, 2019
2 parents d3ac761 + 4fce251 commit 0658ffc
Show file tree
Hide file tree
Showing 10 changed files with 92 additions and 183 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,75 +3,40 @@ pragma solidity >=0.4.0 <0.6.0;
// DO NOT USE THIS CONTRACT IN PRODUCTION APPLICATIONS

contract SimplePermissioning {
struct EnodeIpv6 {
struct Enode {
bytes32 enodeHigh;
bytes32 enodeLow;
bytes16 enodeHost; // Ipv6
bytes16 enodeHost;
uint16 enodePort;
}
struct EnodeIpv4 {
bytes32 enodeHigh;
bytes32 enodeLow;
bytes4 enodeHost; // Ipv4
uint16 enodePort;
}
mapping(bytes => EnodeIpv6) private whitelistIpv6; // should there be a size for the whitelists?
mapping(bytes => EnodeIpv4) private whitelistIpv4;
mapping(bytes => Enode) private whitelist; // should there be a size for the whitelists?

function connectionAllowedIpv6(
bytes32 sourceEnodeHigh, bytes32 sourceEnodeLow, bytes16 sourceEnodeIpv6, uint16 sourceEnodePort,
bytes32 destinationEnodeHigh, bytes32 destinationEnodeLow, bytes16 destinationEnodeIpv6, uint16 destinationEnodePort)
function connectionAllowed(
bytes32 sourceEnodeHigh, bytes32 sourceEnodeLow, bytes16 sourceEnodeIp, uint16 sourceEnodePort,
bytes32 destinationEnodeHigh, bytes32 destinationEnodeLow, bytes16 destinationEnodeIp, uint16 destinationEnodePort)
public view returns (bool) {
return (enodeAllowedIpv6(sourceEnodeHigh, sourceEnodeLow, sourceEnodeIpv6, sourceEnodePort) &&
enodeAllowedIpv6(destinationEnodeHigh, destinationEnodeLow, destinationEnodeIpv6, destinationEnodePort));
return (enodeAllowed(sourceEnodeHigh, sourceEnodeLow, sourceEnodeIp, sourceEnodePort) &&
enodeAllowed(destinationEnodeHigh, destinationEnodeLow, destinationEnodeIp, destinationEnodePort));
}
function connectionAllowedIpv4(
bytes32 sourceEnodeHigh, bytes32 sourceEnodeLow, bytes4 sourceEnodeIpv4, uint16 sourceEnodePort,
bytes32 destinationEnodeHigh, bytes32 destinationEnodeLow, bytes4 destinationEnodeIpv4, uint16 destinationEnodePort)
public view returns (bool){
return (enodeAllowedIpv4(sourceEnodeHigh, sourceEnodeLow, sourceEnodeIpv4, sourceEnodePort) &&
enodeAllowedIpv4(destinationEnodeHigh, destinationEnodeLow, destinationEnodeIpv4, destinationEnodePort));
}
function enodeAllowedIpv6(bytes32 sourceEnodeHigh, bytes32 sourceEnodeLow, bytes16 sourceEnodeIpv6, uint16 sourceEnodePort)
function enodeAllowed(bytes32 sourceEnodeHigh, bytes32 sourceEnodeLow, bytes16 sourceEnodeIp, uint16 sourceEnodePort)
public view returns (bool){
bytes memory key = computeKeyIpv6(sourceEnodeHigh, sourceEnodeLow, sourceEnodeIpv6, sourceEnodePort);
EnodeIpv6 storage whitelistSource = whitelistIpv6[key];
bytes memory key = computeKey(sourceEnodeHigh, sourceEnodeLow, sourceEnodeIp, sourceEnodePort);
Enode storage whitelistSource = whitelist[key];
if (whitelistSource.enodeHost > 0) {
return true;
}
}
function enodeAllowedIpv4(bytes32 sourceEnodeHigh, bytes32 sourceEnodeLow, bytes4 sourceEnodeIpv4, uint16 sourceEnodePort)
public view returns (bool){
bytes memory key = computeKeyIpv4(sourceEnodeHigh, sourceEnodeLow, sourceEnodeIpv4, sourceEnodePort);
EnodeIpv4 storage whitelistSource = whitelistIpv4[key];
if (whitelistSource.enodeHost > 0) {
return true;
}
}
function addEnodeIpv6(bytes32 enodeHigh, bytes32 enodeLow, bytes16 enodeIpv6, uint16 enodePort) public {
EnodeIpv6 memory newEnode = EnodeIpv6(enodeHigh, enodeLow, enodeIpv6, enodePort);
bytes memory key = computeKeyIpv6(enodeHigh, enodeLow, enodeIpv6, enodePort);
whitelistIpv6[key] = newEnode;
}
function addEnodeIpv4(bytes32 enodeHigh, bytes32 enodeLow, bytes4 enodeIpv4, uint16 enodePort) public {
EnodeIpv4 memory newEnode = EnodeIpv4(enodeHigh, enodeLow, enodeIpv4, enodePort);
bytes memory key = computeKeyIpv4(enodeHigh, enodeLow, enodeIpv4, enodePort);
whitelistIpv4[key] = newEnode;
}
function removeEnodeIpv6(bytes32 enodeHigh, bytes32 enodeLow, bytes16 enodeIpv6, uint16 enodePort) public {
bytes memory key = computeKeyIpv6(enodeHigh, enodeLow, enodeIpv6, enodePort);
EnodeIpv6 memory zeros = EnodeIpv6(bytes32(0), bytes32(0), bytes16(0), 0);
whitelistIpv6[key] = zeros;
}
function removeEnodeIpv4(bytes32 enodeHigh, bytes32 enodeLow, bytes4 enodeIpv4, uint16 enodePort) public {
bytes memory key = computeKeyIpv4(enodeHigh, enodeLow, enodeIpv4, enodePort);
EnodeIpv4 memory zeros = EnodeIpv4(bytes32(0), bytes32(0), bytes4(0), 0);
whitelistIpv4[key] = zeros;
function addEnode(bytes32 enodeHigh, bytes32 enodeLow, bytes16 enodeIp, uint16 enodePort) public {
Enode memory newEnode = Enode(enodeHigh, enodeLow, enodeIp, enodePort);
bytes memory key = computeKey(enodeHigh, enodeLow, enodeIp, enodePort);
whitelist[key] = newEnode;
}
function computeKeyIpv6(bytes32 enodeHigh, bytes32 enodeLow, bytes16 enodeIpv6, uint16 enodePort) public pure returns (bytes memory) {
return abi.encode(enodeHigh, enodeLow, enodeIpv6, enodePort);
function removeEnode(bytes32 enodeHigh, bytes32 enodeLow, bytes16 enodeIp, uint16 enodePort) public {
bytes memory key = computeKey(enodeHigh, enodeLow, enodeIp, enodePort);
Enode memory zeros = Enode(bytes32(0), bytes32(0), bytes16(0), 0);
whitelist[key] = zeros;
}
function computeKeyIpv4(bytes32 enodeHigh, bytes32 enodeLow, bytes4 enodeIpv4, uint16 enodePort) public pure returns (bytes memory) {
return abi.encode(enodeHigh, enodeLow, enodeIpv4, enodePort);
function computeKey(bytes32 enodeHigh, bytes32 enodeLow, bytes16 enodeIp, uint16 enodePort) public pure returns (bytes memory) {
return abi.encode(enodeHigh, enodeLow, enodeIp, enodePort);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -11,50 +11,50 @@ var node2Low = "0x892092b7fcb320c1b62f3759bd359fdc3a2ed5df436c3d8914b15327401289
var node2Host = "0x596c3d8914b1532fdc3a2ed5df439bd3";
var node2Port = 30304;

contract('Permissioning Ipv6', () => {
describe('Function: permissioning Ipv6', () => {
contract('Permissioning', () => {
describe('Function: permissioning', () => {

it('Should NOT permit any node when none have been added', async () => {
proxy = await TestPermissioning.new();
let permitted = await proxy.enodeAllowedIpv6(node1High, node1Low, node1Host, node1Port);
let permitted = await proxy.enodeAllowed(node1High, node1Low, node1Host, node1Port);
assert.equal(permitted, false, 'expected node NOT permitted');
});

it('Should compute key', async () => {
let key1 = await proxy.computeKeyIpv6(node1High, node1Low, node1Host, node1Port);
let key2 = await proxy.computeKeyIpv6(node1High, node1Low, node1Host, node1Port);
let key1 = await proxy.computeKey(node1High, node1Low, node1Host, node1Port);
let key2 = await proxy.computeKey(node1High, node1Low, node1Host, node1Port);
assert.equal(key1, key2, "computed keys should be the same");

let key3 = await proxy.computeKeyIpv6(node1High, node1Low, node1Host, node2Port);
let key3 = await proxy.computeKey(node1High, node1Low, node1Host, node2Port);
assert(key3 != key2, "keys for different ports should be different");
});

it('Should add a node to the whitelist and then permit that node', async () => {
await proxy.addEnodeIpv6(node1High, node1Low, node1Host, node1Port);
let permitted = await proxy.enodeAllowedIpv6(node1High, node1Low, node1Host, node1Port);
await proxy.addEnode(node1High, node1Low, node1Host, node1Port);
let permitted = await proxy.enodeAllowed(node1High, node1Low, node1Host, node1Port);
assert.equal(permitted, true, 'expected node added to be permitted');

// await another
await proxy.addEnodeIpv6(node2High, node2Low, node2Host, node2Port);
permitted = await proxy.enodeAllowedIpv6(node2High, node2Low, node2Host, node2Port);
await proxy.addEnode(node2High, node2Low, node2Host, node2Port);
permitted = await proxy.enodeAllowed(node2High, node2Low, node2Host, node2Port);
assert.equal(permitted, true, 'expected node 2 added to be permitted');

// first one still permitted
permitted = await proxy.enodeAllowedIpv6(node1High, node1Low, node1Host, node1Port);
permitted = await proxy.enodeAllowed(node1High, node1Low, node1Host, node1Port);
assert.equal(permitted, true, 'expected node 1 added to be permitted');
});

it('Should allow a connection between 2 added nodes', async () => {
let permitted = await proxy.connectionAllowedIpv6(node1High, node1Low, node1Host, node1Port, node2High, node2Low, node2Host, node2Port);
let permitted = await proxy.connectionAllowed(node1High, node1Low, node1Host, node1Port, node2High, node2Low, node2Host, node2Port);
assert.equal(permitted, true, 'expected 2 added nodes to work as source <> destination');
});

it('Should remove a node from the whitelist and then NOT permit that node', async () => {
await proxy.removeEnodeIpv6(node1High, node1Low, node1Host, node1Port);
let permitted = await proxy.enodeAllowedIpv6(node1High, node1Low, node1Host, node1Port);
await proxy.removeEnode(node1High, node1Low, node1Host, node1Port);
let permitted = await proxy.enodeAllowed(node1High, node1Low, node1Host, node1Port);
assert.equal(permitted, false, 'expected removed node NOT permitted');

permitted = await proxy.connectionAllowedIpv6(node1High, node1Low, node1Host, node1Port, node2High, node2Low, node2Host, node2Port);
permitted = await proxy.connectionAllowed(node1High, node1Low, node1Host, node1Port, node2High, node2Low, node2Host, node2Port);
assert.equal(permitted, false, 'expected source disallowed since it was removed');

});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
*/
package tech.pegasys.pantheon.tests.acceptance.dsl.transaction.perm;

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.web3j.utils.Numeric.toHexString;

import tech.pegasys.pantheon.ethereum.core.Address;
Expand All @@ -33,7 +34,10 @@

public class SmartContractPermissioningAllowNodeTransaction implements Transaction<Hash> {

private static final String ADD_ENODE_IPV4_SIGNATURE = "0x680fc99c";
private static final BytesValue ADD_ENODE_SIGNATURE =
tech.pegasys.pantheon.crypto.Hash.keccak256(
BytesValue.of("addEnode(bytes32,bytes32,bytes16,uint16)".getBytes(UTF_8)))
.slice(0, 4);

private final Account sender;
private final Address contractAddress;
Expand Down Expand Up @@ -62,7 +66,7 @@ private String signedTransactionData() {
final String enodeURL = ((RunnableNode) node).enodeUrl().toASCIIString();
final BytesValue payload =
SmartContractPermissioningController.createPayload(
BytesValue.fromHexString(ADD_ENODE_IPV4_SIGNATURE), new EnodeURL(enodeURL));
ADD_ENODE_SIGNATURE, new EnodeURL(enodeURL));

RawTransaction transaction =
RawTransaction.createTransaction(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
*/
package tech.pegasys.pantheon.tests.acceptance.dsl.transaction.perm;

import static java.nio.charset.StandardCharsets.UTF_8;
import static tech.pegasys.pantheon.ethereum.permissioning.SmartContractPermissioningController.checkTransactionResult;

import tech.pegasys.pantheon.ethereum.core.Address;
Expand All @@ -30,7 +31,12 @@
public class SmartContractPermissioningConnectionIsAllowedTransaction
implements Transaction<Boolean> {

private static final String IS_CONNECTION_ALLOWED_IPV4_SIGNATURE = "0x9b9d2bce";
private static final BytesValue IS_CONNECTION_ALLOWED_SIGNATURE =
tech.pegasys.pantheon.crypto.Hash.keccak256(
BytesValue.of(
"connectionAllowed(bytes32,bytes32,bytes16,uint16,bytes32,bytes32,bytes16,uint16)"
.getBytes(UTF_8)))
.slice(0, 4);

private final Address contractAddress;
private final Node source;
Expand Down Expand Up @@ -59,7 +65,7 @@ private org.web3j.protocol.core.methods.request.Transaction payload() {
final String targetEnodeURL = ((RunnableNode) target).enodeUrl().toASCIIString();
final BytesValue payload =
SmartContractPermissioningController.createPayload(
BytesValue.fromHexString(IS_CONNECTION_ALLOWED_IPV4_SIGNATURE),
IS_CONNECTION_ALLOWED_SIGNATURE,
new EnodeURL(sourceEnodeURL),
new EnodeURL(targetEnodeURL));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
*/
package tech.pegasys.pantheon.tests.acceptance.dsl.transaction.perm;

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.web3j.utils.Numeric.toHexString;

import tech.pegasys.pantheon.ethereum.core.Address;
Expand All @@ -33,7 +34,10 @@

public class SmartContractPermissioningForbidNodeTransaction implements Transaction<Hash> {

private static final String REMOVE_ENODE_IPV4_SIGNATURE = "c909760d";
private static final BytesValue REMOVE_ENODE_SIGNATURE =
tech.pegasys.pantheon.crypto.Hash.keccak256(
BytesValue.of("removeEnode(bytes32,bytes32,bytes16,uint16)".getBytes(UTF_8)))
.slice(0, 4);

private final Account sender;
private final Address contractAddress;
Expand Down Expand Up @@ -62,7 +66,7 @@ private String signedTransactionData() {
final String enodeURL = ((RunnableNode) node).enodeUrl().toASCIIString();
final BytesValue payload =
SmartContractPermissioningController.createPayload(
BytesValue.fromHexString(REMOVE_ENODE_IPV4_SIGNATURE), new EnodeURL(enodeURL));
REMOVE_ENODE_SIGNATURE, new EnodeURL(enodeURL));

RawTransaction transaction =
RawTransaction.createTransaction(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
*/
package tech.pegasys.pantheon.tests.acceptance.dsl.transaction.perm;

import static java.nio.charset.StandardCharsets.UTF_8;
import static tech.pegasys.pantheon.ethereum.permissioning.SmartContractPermissioningController.checkTransactionResult;

import tech.pegasys.pantheon.ethereum.core.Address;
Expand All @@ -29,7 +30,10 @@

public class SmartContractPermissioningNodeIsAllowedTransaction implements Transaction<Boolean> {

private static final String IS_NODE_ALLOWED_IPV4_SIGNATURE = "0x6863d6c0";
private static final BytesValue IS_NODE_ALLOWED_SIGNATURE =
tech.pegasys.pantheon.crypto.Hash.keccak256(
BytesValue.of("enodeAllowed(bytes32,bytes32,bytes16,uint16)".getBytes(UTF_8)))
.slice(0, 4);

private final Address contractAddress;
private final Node node;
Expand All @@ -55,7 +59,7 @@ private org.web3j.protocol.core.methods.request.Transaction payload() {
final String sourceEnodeURL = ((RunnableNode) node).enodeUrl().toASCIIString();
final BytesValue payload =
SmartContractPermissioningController.createPayload(
BytesValue.fromHexString(IS_NODE_ALLOWED_IPV4_SIGNATURE), new EnodeURL(sourceEnodeURL));
IS_NODE_ALLOWED_SIGNATURE, new EnodeURL(sourceEnodeURL));

return org.web3j.protocol.core.methods.request.Transaction.createFunctionCallTransaction(
null, null, null, null, contractAddress.toString(), payload.toString());
Expand Down
Loading

0 comments on commit 0658ffc

Please sign in to comment.