Change IP to string field (#1496)

* use Ip consistently
* changed ip field from bytes to string

Signed-off-by: Sally MacFarlane <sally.macfarlane@consensys.net>

Co-authored-by: mark-terry <36909937+mark-terry@users.noreply.github.com>
pull/1525/head
Sally MacFarlane 4 years ago committed by GitHub
parent 491a1cef50
commit ea2e9e253c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      CHANGELOG.md
  2. 7
      acceptance-tests/dsl/src/main/java/org/hyperledger/besu/tests/acceptance/dsl/transaction/perm/NodeSmartContractPermissioningAllowNodeV2Transaction.java
  3. 6
      acceptance-tests/dsl/src/main/java/org/hyperledger/besu/tests/acceptance/dsl/transaction/perm/NodeSmartContractPermissioningConnectionIsAllowedV2Transaction.java
  4. 7
      acceptance-tests/dsl/src/main/java/org/hyperledger/besu/tests/acceptance/dsl/transaction/perm/NodeSmartContractPermissioningForbidNodeV2Transaction.java
  5. 18
      acceptance-tests/tests/simple-permissioning-smart-contract/contracts/SimpleNodePermissioningV2.sol
  6. 2
      acceptance-tests/tests/src/test/resources/permissioning/simple_permissioning_v2_genesis.json
  7. 32
      ethereum/permissioning/src/main/java/org/hyperledger/besu/ethereum/permissioning/NodeSmartContractV2PermissioningController.java
  8. 6
      ethereum/permissioning/src/test/java/org/hyperledger/besu/ethereum/permissioning/NodeSmartContractV2PermissioningControllerTest.java

@ -7,7 +7,7 @@
* `miner_changeTargetGasLimit` RPC added. If a target gas limit is set, allows the node operator to change it at runtime.
* Hide deprecated `--host-whitelist` option. [\#1444](https://github.com/hyperledger/besu/pull/1444)
* Prioritize high gas prices during mining. Previously we ordered only by the order in which the transactions were received. This will increase expected profit when mining. [\#1449](https://github.com/hyperledger/besu/pull/1449)
* Added support for the updated smart contract-based [node permissioning EEA interface](https://entethalliance.github.io/client-spec/spec.html#dfn-connectionallowed). [\#1435](https://github.com/hyperledger/besu/pull/1435)
* Added support for the updated smart contract-based [node permissioning EEA interface](https://entethalliance.github.io/client-spec/spec.html#dfn-connectionallowed). [\#1435](https://github.com/hyperledger/besu/pull/1435) and [\#1496](https://github.com/hyperledger/besu/pull/1496)
* Added EvmTool binary to the distribution. EvmTool is a CLI that can execute EVM bytecode and execute ethereum state tests. [\#1465](https://github.com/hyperledger/besu/pull/1465)
* Updated the libraries for secp256k1 and AltBN series precompiles. These updates provide significant performance improvements to those areas. [\#1499](https://github.com/hyperledger/besu/pull/1499)

@ -19,7 +19,6 @@ import static org.web3j.utils.Numeric.toHexString;
import org.hyperledger.besu.ethereum.core.Address;
import org.hyperledger.besu.ethereum.core.Hash;
import org.hyperledger.besu.ethereum.p2p.peers.EnodeURL;
import org.hyperledger.besu.ethereum.permissioning.NodeSmartContractV2PermissioningController;
import org.hyperledger.besu.tests.acceptance.dsl.account.Account;
import org.hyperledger.besu.tests.acceptance.dsl.node.Node;
import org.hyperledger.besu.tests.acceptance.dsl.node.RunnableNode;
@ -81,14 +80,14 @@ public class NodeSmartContractPermissioningAllowNodeV2Transaction implements Tra
private Bytes createPayload(final EnodeURL enodeUrl) {
try {
final String hexNodeIdString = enodeUrl.getNodeId().toUnprefixedHexString();
final byte[] ip = NodeSmartContractV2PermissioningController.encodeIp(enodeUrl.getIp());
final String address = enodeUrl.getIp().getHostAddress();
final int port = enodeUrl.getListeningPortOrZero();
final Function addNodeFunction =
FunctionEncoder.makeFunction(
"addEnode",
List.of("string", "bytes16", "uint16"),
List.of(hexNodeIdString, ip, port),
List.of("string", "string", "uint16"),
List.of(hexNodeIdString, address, port),
Collections.emptyList());
return Bytes.fromHexString(FunctionEncoder.encode(addNodeFunction));
} catch (Exception e) {

@ -66,14 +66,14 @@ public class NodeSmartContractPermissioningConnectionIsAllowedV2Transaction
private Bytes createPayload(final EnodeURL enodeUrl) {
try {
final String hexNodeIdString = enodeUrl.getNodeId().toUnprefixedHexString();
final byte[] ip = NodeSmartContractV2PermissioningController.encodeIp(enodeUrl.getIp());
final String address = enodeUrl.getIp().getHostAddress();
final int port = enodeUrl.getListeningPortOrZero();
final Function connectionAllowedFunction =
FunctionEncoder.makeFunction(
"connectionAllowed",
List.of("string", "bytes16", "uint16"),
List.of(hexNodeIdString, ip, port),
List.of("string", "string", "uint16"),
List.of(hexNodeIdString, address, port),
Collections.emptyList());
return Bytes.fromHexString(FunctionEncoder.encode(connectionAllowedFunction));
} catch (Exception e) {

@ -19,7 +19,6 @@ import static org.web3j.utils.Numeric.toHexString;
import org.hyperledger.besu.ethereum.core.Address;
import org.hyperledger.besu.ethereum.core.Hash;
import org.hyperledger.besu.ethereum.p2p.peers.EnodeURL;
import org.hyperledger.besu.ethereum.permissioning.NodeSmartContractV2PermissioningController;
import org.hyperledger.besu.tests.acceptance.dsl.account.Account;
import org.hyperledger.besu.tests.acceptance.dsl.node.Node;
import org.hyperledger.besu.tests.acceptance.dsl.node.RunnableNode;
@ -81,14 +80,14 @@ public class NodeSmartContractPermissioningForbidNodeV2Transaction implements Tr
private Bytes createPayload(final EnodeURL enodeUrl) {
try {
final String hexNodeIdString = enodeUrl.getNodeId().toUnprefixedHexString();
final byte[] ip = NodeSmartContractV2PermissioningController.encodeIp(enodeUrl.getIp());
final String address = enodeUrl.getIp().getHostAddress();
final int port = enodeUrl.getListeningPortOrZero();
final Function removeNodeFunction =
FunctionEncoder.makeFunction(
"removeEnode",
List.of("string", "bytes16", "uint16"),
List.of(hexNodeIdString, ip, port),
List.of("string", "string", "uint16"),
List.of(hexNodeIdString, address, port),
Collections.emptyList());
return Bytes.fromHexString(FunctionEncoder.encode(removeNodeFunction));
} catch (Exception e) {

@ -8,35 +8,35 @@ contract SimpleNodePermissioning {
*/
struct Enode {
string enodeId;
bytes16 enodeHost;
string enodeIp;
}
mapping(bytes => Enode) private allowlist;
// Port is being ignored
function connectionAllowed(string memory enodeId, bytes16 ip, uint16) public view returns (bool) {
function connectionAllowed(string memory enodeId, string memory ip, uint16) public view returns (bool) {
return enodeAllowed(enodeId, ip);
}
function enodeAllowed(string memory enodeId, bytes16 sourceEnodeIp) private view returns (bool){
function enodeAllowed(string memory enodeId, string memory sourceEnodeIp) private view returns (bool){
bytes memory key = computeKey(enodeId, sourceEnodeIp);
// if enode was found, host (bytes) is greater than zero
return allowlist[key].enodeHost > 0;
// if enode was found, keccak256 will be different
return keccak256(bytes(allowlist[key].enodeIp)) != keccak256("");
}
function addEnode(string memory enodeId, bytes16 enodeIp, uint16) public {
function addEnode(string memory enodeId, string memory enodeIp, uint16) public {
Enode memory newEnode = Enode(enodeId, enodeIp);
bytes memory key = computeKey(enodeId, enodeIp);
allowlist[key] = newEnode;
}
function removeEnode(string memory enodeId, bytes16 enodeIp, uint16) public {
function removeEnode(string memory enodeId, string memory enodeIp, uint16) public {
bytes memory key = computeKey(enodeId, enodeIp);
Enode memory zeros = Enode("", bytes16(0));
Enode memory zeros = Enode("", "");
// replace enode with "zeros"
allowlist[key] = zeros;
}
function computeKey(string memory enodeId, bytes16 enodeIp) private pure returns (bytes memory) {
function computeKey(string memory enodeId, string memory enodeIp) private pure returns (bytes memory) {
return abi.encode(enodeId, enodeIp);
}
}

@ -34,7 +34,7 @@
"0x0000000000000000000000000000000000009999": {
"comment": "Simple node permissioning smart contract V2",
"balance": "0",
"code": "608060405234801561001057600080fd5b50600436106100415760003560e01c80634df2430d146100465780636868e0d114610144578063d0515df61461022a575b600080fd5b61012a6004803603606081101561005c57600080fd5b810190808035906020019064010000000081111561007957600080fd5b82018360208201111561008b57600080fd5b803590602001918460018302840111640100000000831117156100ad57600080fd5b91908080601f016020809104026020016040519081016040528093929190818152602001838380828437600081840152601f19601f82011690508083019250505050505050919291929080356fffffffffffffffffffffffffffffffff19169060200190929190803561ffff169060200190929190505050610310565b604051808215151515815260200191505060405180910390f35b6102286004803603606081101561015a57600080fd5b810190808035906020019064010000000081111561017757600080fd5b82018360208201111561018957600080fd5b803590602001918460018302840111640100000000831117156101ab57600080fd5b91908080601f016020809104026020016040519081016040528093929190818152602001838380828437600081840152601f19601f82011690508083019250505050505050919291929080356fffffffffffffffffffffffffffffffff19169060200190929190803561ffff169060200190929190505050610325565b005b61030e6004803603606081101561024057600080fd5b810190808035906020019064010000000081111561025d57600080fd5b82018360208201111561026f57600080fd5b8035906020019184600183028401116401000000008311171561029157600080fd5b91908080601f016020809104026020016040519081016040528093929190818152602001838380828437600081840152601f19601f82011690508083019250505050505050919291929080356fffffffffffffffffffffffffffffffff19169060200190929190803561ffff169060200190929190505050610439565b005b600061031c848461053a565b90509392505050565b606061033184846105e6565b905061033b6106a3565b6040518060400160405280604051806020016040528060008152508152602001600060801b6fffffffffffffffffffffffffffffffff19168152509050806000836040518082805190602001908083835b602083106103af578051825260208201915060208101905060208303925061038c565b6001836020036101000a038019825116818451168082178552505050505050905001915050908152602001604051809103902060008201518160000190805190602001906103fe9291906106d0565b5060208201518160010160006101000a8154816fffffffffffffffffffffffffffffffff021916908360801c02179055509050505050505050565b6104416106a3565b6040518060400160405280858152602001846fffffffffffffffffffffffffffffffff19168152509050606061047785856105e6565b9050816000826040518082805190602001908083835b602083106104b0578051825260208201915060208101905060208303925061048d565b6001836020036101000a038019825116818451168082178552505050505050905001915050908152602001604051809103902060008201518160000190805190602001906104ff9291906106d0565b5060208201518160010160006101000a8154816fffffffffffffffffffffffffffffffff021916908360801c02179055509050505050505050565b6000606061054884846105e6565b9050600060801b6000826040518082805190602001908083835b602083106105855780518252602082019150602081019050602083039250610562565b6001836020036101000a038019825116818451168082178552505050505050905001915050908152602001604051809103902060010160009054906101000a900460801b6fffffffffffffffffffffffffffffffff19161191505092915050565b606082826040516020018080602001836fffffffffffffffffffffffffffffffff19166fffffffffffffffffffffffffffffffff19168152602001828103825284818151815260200191508051906020019080838360005b8381101561065957808201518184015260208101905061063e565b50505050905090810190601f1680156106865780820380516001836020036101000a031916815260200191505b509350505050604051602081830303815290604052905092915050565b60405180604001604052806060815260200160006fffffffffffffffffffffffffffffffff191681525090565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061071157805160ff191683800117855561073f565b8280016001018555821561073f579182015b8281111561073e578251825591602001919060010190610723565b5b50905061074c9190610750565b5090565b61077291905b8082111561076e576000816000905550600101610756565b5090565b9056fea265627a7a7230582059e21fece50f9faa9389e7ddaac9b6e34e20f4283c820bef12ea74bb18ecd7a664736f6c63430005090032",
"code": "608060405234801561001057600080fd5b50600436106100415760003560e01c806328a4a0781461004657806339d6dd01146101a657806345a59e5b14610306575b600080fd5b6101a46004803603606081101561005c57600080fd5b810190808035906020019064010000000081111561007957600080fd5b82018360208201111561008b57600080fd5b803590602001918460018302840111640100000000831117156100ad57600080fd5b91908080601f016020809104026020016040519081016040528093929190818152602001838380828437600081840152601f19601f8201169050808301925050505050505091929192908035906020019064010000000081111561011057600080fd5b82018360208201111561012257600080fd5b8035906020019184600183028401116401000000008311171561014457600080fd5b91908080601f016020809104026020016040519081016040528093929190818152602001838380828437600081840152601f19601f820116905080830192505050505050509192919290803561ffff16906020019092919050505061047e565b005b610304600480360360608110156101bc57600080fd5b81019080803590602001906401000000008111156101d957600080fd5b8201836020820111156101eb57600080fd5b8035906020019184600183028401116401000000008311171561020d57600080fd5b91908080601f016020809104026020016040519081016040528093929190818152602001838380828437600081840152601f19601f8201169050808301925050505050505091929192908035906020019064010000000081111561027057600080fd5b82018360208201111561028257600080fd5b803590602001918460018302840111640100000000831117156102a457600080fd5b91908080601f016020809104026020016040519081016040528093929190818152602001838380828437600081840152601f19601f820116905080830192505050505050509192919290803561ffff169060200190929190505050610577565b005b6104646004803603606081101561031c57600080fd5b810190808035906020019064010000000081111561033957600080fd5b82018360208201111561034b57600080fd5b8035906020019184600183028401116401000000008311171561036d57600080fd5b91908080601f016020809104026020016040519081016040528093929190818152602001838380828437600081840152601f19601f820116905080830192505050505050509192919290803590602001906401000000008111156103d057600080fd5b8201836020820111156103e257600080fd5b8035906020019184600183028401116401000000008311171561040457600080fd5b91908080601f016020809104026020016040519081016040528093929190818152602001838380828437600081840152601f19601f820116905080830192505050505050509192919290803561ffff169060200190929190505050610652565b604051808215151515815260200191505060405180910390f35b606061048a8484610667565b9050610494610863565b6040518060400160405280604051806020016040528060008152508152602001604051806020016040528060008152508152509050806000836040518082805190602001908083835b6020831061050057805182526020820191506020810190506020830392506104dd565b6001836020036101000a0380198251168184511680821785525050505050509050019150509081526020016040518091039020600082015181600001908051906020019061054f92919061087d565b50602082015181600101908051906020019061056c92919061087d565b509050505050505050565b61057f610863565b604051806040016040528085815260200184815250905060606105a28585610667565b9050816000826040518082805190602001908083835b602083106105db57805182526020820191506020810190506020830392506105b8565b6001836020036101000a0380198251168184511680821785525050505050509050019150509081526020016040518091039020600082015181600001908051906020019061062a92919061087d565b50602082015181600101908051906020019061064792919061087d565b509050505050505050565b600061065e8484610763565b90509392505050565b60608282604051602001808060200180602001838103835285818151815260200191508051906020019080838360005b838110156106b2578082015181840152602081019050610697565b50505050905090810190601f1680156106df5780820380516001836020036101000a031916815260200191505b50838103825284818151815260200191508051906020019080838360005b838110156107185780820151818401526020810190506106fd565b50505050905090810190601f1680156107455780820380516001836020036101000a031916815260200191505b50945050505050604051602081830303815290604052905092915050565b600060606107718484610667565b905060405180600001905060405180910390206000826040518082805190602001908083835b602083106107ba5780518252602082019150602081019050602083039250610797565b6001836020036101000a0380198251168184511680821785525050505050509050019150509081526020016040518091039020600101604051808280546001816001161561010002031660029004801561084b5780601f1061082957610100808354040283529182019161084b565b820191906000526020600020905b815481529060010190602001808311610837575b50509150506040518091039020141591505092915050565b604051806040016040528060608152602001606081525090565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106108be57805160ff19168380011785556108ec565b828001600101855582156108ec579182015b828111156108eb5782518255916020019190600101906108d0565b5b5090506108f991906108fd565b5090565b61091f91905b8082111561091b576000816000905550600101610903565b5090565b9056fea265627a7a72315820ba83db5c3b0856719f22e4ef92bcd9a92396fdde3cd5f183d8763b404368e41d64736f6c63430005110032",
"storage": {}
},
"0x0000000000000000000000000000000000008888": {

@ -21,10 +21,8 @@ import org.hyperledger.besu.ethereum.transaction.TransactionSimulator;
import org.hyperledger.besu.ethereum.transaction.TransactionSimulatorResult;
import org.hyperledger.besu.plugin.services.MetricsSystem;
import java.net.InetAddress;
import java.util.List;
import com.google.common.annotations.VisibleForTesting;
import org.apache.tuweni.bytes.Bytes;
import org.web3j.abi.FunctionEncoder;
import org.web3j.abi.TypeEncoder;
@ -33,7 +31,7 @@ import org.web3j.abi.datatypes.Function;
/**
* Controller that can read from a smart contract that exposes the EEA node permissioning v2 call
* connectionAllowed(string,bytes16,uint16)
* connectionAllowed(string,string,uint16)
*/
public class NodeSmartContractV2PermissioningController
extends AbstractNodeSmartContractPermissioningController {
@ -64,14 +62,14 @@ public class NodeSmartContractV2PermissioningController
private Bytes createPayload(final EnodeURL enodeUrl) {
try {
final String hexNodeIdString = enodeUrl.getNodeId().toUnprefixedHexString();
final byte[] ip = encodeIp(enodeUrl.getIp());
final String address = enodeUrl.getIp().getHostAddress();
final int port = enodeUrl.getListeningPortOrZero();
final Function connectionAllowedFunction =
FunctionEncoder.makeFunction(
"connectionAllowed",
List.of("string", "bytes16", "uint16"),
List.of(hexNodeIdString, ip, port),
List.of("string", "string", "uint16"),
List.of(hexNodeIdString, address, port),
List.of(Bool.TYPE_NAME));
return Bytes.fromHexString(FunctionEncoder.encode(connectionAllowedFunction));
} catch (Exception e) {
@ -80,28 +78,6 @@ public class NodeSmartContractV2PermissioningController
}
}
/*
* IPv4-Compatible IPv6 Address That hybrid address consists of 10 "0" bytes, followed by two "1"
* bytes, followed by the original 4-bytes IPv4 address (RFC 4291 Section 2.5.5.1 -
* https://tools.ietf.org/html/rfc4291#section-2.5.5)
*/
@VisibleForTesting
public static byte[] encodeIp(final InetAddress addr) {
// InetAddress deals with giving us the right number of bytes
final byte[] address = addr.getAddress();
final byte[] res = new byte[16];
if (address.length == 4) {
// lead with 10 bytes of 0's then 2 bytes of 1's
res[10] = (byte) 0xFF;
res[11] = (byte) 0xFF;
// then the ipv4
System.arraycopy(address, 0, res, 12, 4);
} else {
System.arraycopy(address, 0, res, 0, address.length);
}
return res;
}
private boolean parseResult(final TransactionSimulatorResult result) {
switch (result.getResult().getStatus()) {
case INVALID:

@ -44,14 +44,14 @@ import org.junit.Test;
public class NodeSmartContractV2PermissioningControllerTest {
/*
Payloads created using Remix to call method connectionAllowed(string, bytes16, uint16)
Payloads created using Remix to call method connectionAllowed(string, string, uint16)
*/
private static final Bytes SOURCE_ENODE_EXPECTED_PAYLOAD =
Bytes.fromHexString(
"0x4df2430d000000000000000000000000000000000000000000000000000000000000006000000000000000000000ffff7f00000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000765f00000000000000000000000000000000000000000000000000000000000000806663626539663833323138343837623363306235303837383139333838306536633235636664383637303863306130626630636139316630636536333337343661383932666532343061666135623961383830623862636134386538613232373034656639333766646461326437636336336534643431656431623431376165");
"0x45a59e5b00000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000765f0000000000000000000000000000000000000000000000000000000000000080666362653966383332313834383762336330623530383738313933383830653663323563666438363730386330613062663063613931663063653633333734366138393266653234306166613562396138383062386263613438653861323237303465663933376664646132643763633633653464343165643162343137616500000000000000000000000000000000000000000000000000000000000000093132372e302e302e310000000000000000000000000000000000000000000000");
private static final Bytes DESTINATION_ENODE_EXPECTED_PAYLOAD =
Bytes.fromHexString(
"0x4df2430d000000000000000000000000000000000000000000000000000000000000006000000000000000000000ffff7f000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009dd400000000000000000000000000000000000000000000000000000000000000803335343863383762393932306666313661613462646366303163383566323531313761323961653135373464373539626164343863633934363364386539663763336331643165396662306432386537333839383935316639306530323731346162623737306664366432326539303337313838326134353635383830306539");
"0x45a59e5b000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000009dd40000000000000000000000000000000000000000000000000000000000000080333534386338376239393230666631366161346264636630316338356632353131376132396165313537346437353962616434386363393436336438653966376333633164316539666230643238653733383938393531663930653032373134616262373730666436643232653930333731383832613435363538383030653900000000000000000000000000000000000000000000000000000000000000093132372e302e302e310000000000000000000000000000000000000000000000");
private static final EnodeURL SOURCE_ENODE_IPV4 =
EnodeURL.fromString(

Loading…
Cancel
Save