Skip to content
Merged
  •  
  •  
  •  
2 changes: 1 addition & 1 deletion CODING-CONVENTIONS.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ Simple does not mean the fewest lines of code. Simple code is:

Besu embraces typical Java idioms including using an Object Oriented approach to design. This includes:
* Providing alternate behaviours via polymorphism instead of having conditional logic scattered through the codebase. For example, `ProtocolSpec` provides a standard interface to blockchain operations and multiple implementations define the different behaviours for each Ethereum milestone.
* Encapsulating behaviour and data together in classes. For example, `BytesValue` encapsulates byte data and methods operating on the byte data. `BytesValue.isZero()` is an instance method instead of accepting a `BytesValue` parameter.
* Encapsulating behaviour and data together in classes. For example, `Bytes` encapsulates byte data and methods operating on the byte data. `Bytes.isZero()` is an instance method instead of accepting a `Bytes` parameter.

`ProtocolSpec` is an exception and does not hold the blockchain data on which it operates. This is because that blockchain data is widely shared and not specifically owned by `ProtocolSpec`.
* Embracing modern Java features like Optional, Streams and lambdas when they make code simpler and clearer.
Expand Down
3 changes: 3 additions & 0 deletions acceptance-tests/dsl/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ dependencies {
implementation 'io.vertx:vertx-core'
implementation 'junit:junit'
implementation 'net.consensys:orion'

implementation 'org.apache.tuweni:tuweni-bytes'
implementation 'org.apache.tuweni:tuweni-units'
implementation 'org.apache.logging.log4j:log4j-api'
implementation 'org.assertj:assertj-core'
implementation 'org.awaitility:awaitility'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
import org.hyperledger.besu.ethereum.core.BlockHeaderFunctions;
import org.hyperledger.besu.ethereum.core.Hash;
import org.hyperledger.besu.ethereum.core.LogsBloomFilter;
import org.hyperledger.besu.util.bytes.BytesValue;
import org.hyperledger.besu.util.uint.UInt256;

import org.apache.tuweni.bytes.Bytes;
import org.apache.tuweni.units.bigints.UInt256;
import org.web3j.protocol.core.methods.response.EthBlock.Block;

public class BlockUtils {
Expand All @@ -47,7 +47,7 @@ public static BlockHeader createBlockHeader(
block.getGasLimit().longValue(),
block.getGasUsed().longValue(),
block.getTimestamp().longValue(),
BytesValue.fromHexString(block.getExtraData()),
Bytes.fromHexString(block.getExtraData()),
mixHash,
block.getNonce().longValue(),
blockHeaderFunctions);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@
import org.hyperledger.besu.tests.acceptance.dsl.condition.account.ExpectAccountBalance;
import org.hyperledger.besu.tests.acceptance.dsl.condition.account.ExpectAccountBalanceNotChanging;
import org.hyperledger.besu.tests.acceptance.dsl.transaction.eth.EthTransactions;
import org.hyperledger.besu.util.bytes.Bytes32;

import java.math.BigDecimal;
import java.math.BigInteger;

import org.apache.tuweni.bytes.Bytes32;
import org.web3j.crypto.Credentials;
import org.web3j.utils.Convert.Unit;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
import static org.web3j.utils.Convert.Unit.ETHER;
import static org.web3j.utils.Convert.Unit.WEI;

import org.hyperledger.besu.ethereum.core.Wei;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should not replace Wei with a type that is shared across multiple units. We need this as Wei for type and unit safety.


import java.math.BigDecimal;
import java.math.BigInteger;

Expand All @@ -43,10 +41,6 @@ public static Amount wei(final BigInteger value) {
return new Amount(new BigDecimal(value), WEI);
}

public static Amount wei(final Wei wei) {
return wei(new BigInteger(wei.toUnprefixedHexString(), 16));
}

public BigDecimal getValue() {
return value;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,18 @@
import org.hyperledger.besu.tests.acceptance.dsl.account.Account;
import org.hyperledger.besu.tests.acceptance.dsl.transaction.NodeRequests;
import org.hyperledger.besu.tests.acceptance.dsl.transaction.Transaction;
import org.hyperledger.besu.util.bytes.BytesValue;
import org.hyperledger.besu.util.bytes.BytesValues;

import java.io.IOException;
import java.math.BigInteger;

import org.apache.tuweni.bytes.Bytes;
import org.web3j.crypto.RawTransaction;
import org.web3j.crypto.TransactionEncoder;

public class AccountSmartContractPermissioningAllowAccountTransaction implements Transaction<Hash> {

private static final BytesValue ADD_ACCOUNT_SIGNATURE =
org.hyperledger.besu.crypto.Hash.keccak256(
BytesValue.of("addAccount(address)".getBytes(UTF_8)))
private static final Bytes ADD_ACCOUNT_SIGNATURE =
org.hyperledger.besu.crypto.Hash.keccak256(Bytes.of("addAccount(address)".getBytes(UTF_8)))
.slice(0, 4);

private final Account sender;
Expand All @@ -62,9 +60,9 @@ public Hash execute(final NodeRequests node) {
}

private String signedTransactionData() {
final BytesValue payload =
BytesValues.concatenate(
ADD_ACCOUNT_SIGNATURE, BytesValue.fromHexString("0x000000000000000000000000"), account);
final Bytes payload =
Bytes.concatenate(
ADD_ACCOUNT_SIGNATURE, Bytes.fromHexString("0x000000000000000000000000"), account);

final RawTransaction transaction =
RawTransaction.createTransaction(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,19 @@
import org.hyperledger.besu.tests.acceptance.dsl.account.Account;
import org.hyperledger.besu.tests.acceptance.dsl.transaction.NodeRequests;
import org.hyperledger.besu.tests.acceptance.dsl.transaction.Transaction;
import org.hyperledger.besu.util.bytes.BytesValue;
import org.hyperledger.besu.util.bytes.BytesValues;

import java.io.IOException;
import java.math.BigInteger;

import org.apache.tuweni.bytes.Bytes;
import org.web3j.crypto.RawTransaction;
import org.web3j.crypto.TransactionEncoder;

public class AccountSmartContractPermissioningForbidAccountTransaction
implements Transaction<Hash> {

private static final BytesValue REMOVE_ACCOUNT_SIGNATURE =
org.hyperledger.besu.crypto.Hash.keccak256(
BytesValue.of("removeAccount(address)".getBytes(UTF_8)))
private static final Bytes REMOVE_ACCOUNT_SIGNATURE =
org.hyperledger.besu.crypto.Hash.keccak256(Bytes.of("removeAccount(address)".getBytes(UTF_8)))
.slice(0, 4);

private final Account sender;
Expand All @@ -63,11 +61,9 @@ public Hash execute(final NodeRequests node) {
}

private String signedTransactionData() {
final BytesValue payload =
BytesValues.concatenate(
REMOVE_ACCOUNT_SIGNATURE,
BytesValue.fromHexString("0x000000000000000000000000"),
account);
final Bytes payload =
Bytes.concatenate(
REMOVE_ACCOUNT_SIGNATURE, Bytes.fromHexString("0x000000000000000000000000"), account);

RawTransaction transaction =
RawTransaction.createTransaction(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,16 @@
import org.hyperledger.besu.ethereum.core.Address;
import org.hyperledger.besu.tests.acceptance.dsl.transaction.NodeRequests;
import org.hyperledger.besu.tests.acceptance.dsl.transaction.Transaction;
import org.hyperledger.besu.util.bytes.BytesValue;
import org.hyperledger.besu.util.bytes.BytesValues;

import java.io.IOException;

import org.apache.tuweni.bytes.Bytes;
import org.web3j.protocol.core.DefaultBlockParameterName;

public class AccountSmartContractPermissioningIsAllowedTransaction implements Transaction<Boolean> {

private static final BytesValue IS_ACCOUNT_ALLOWED_SIGNATURE =
Hash.keccak256(BytesValue.of("whitelistContains(address)".getBytes(UTF_8))).slice(0, 4);
private static final Bytes IS_ACCOUNT_ALLOWED_SIGNATURE =
Hash.keccak256(Bytes.of("whitelistContains(address)".getBytes(UTF_8))).slice(0, 4);

private final Address contractAddress;
private final Address account;
Expand All @@ -46,31 +45,29 @@ public Boolean execute(final NodeRequests node) {
try {
final String value =
node.eth().ethCall(payload(), DefaultBlockParameterName.LATEST).send().getValue();
return checkTransactionResult(BytesValue.fromHexString(value));
return checkTransactionResult(Bytes.fromHexString(value));
} catch (final IOException e) {
throw new RuntimeException(e);
}
}

// Checks the returned bytes from the permissioning contract call to see if it's a value we
// understand
static Boolean checkTransactionResult(final BytesValue result) {
static Boolean checkTransactionResult(final Bytes result) {
// booleans are padded to 32 bytes
if (result.size() != 32) {
throw new IllegalArgumentException("Unexpected result size");
}

// 0 is false
if (result.compareTo(
BytesValue.fromHexString(
"0x0000000000000000000000000000000000000000000000000000000000000000"))
== 0) {
if (result.equals(
Bytes.fromHexString(
"0x0000000000000000000000000000000000000000000000000000000000000000"))) {
return false;
// 1 filled to 32 bytes is true
} else if (result.compareTo(
BytesValue.fromHexString(
"0x0000000000000000000000000000000000000000000000000000000000000001"))
== 0) {
} else if (result.equals(
Bytes.fromHexString(
"0x0000000000000000000000000000000000000000000000000000000000000001"))) {
return true;
// Anything else is wrong
} else {
Expand All @@ -79,10 +76,10 @@ static Boolean checkTransactionResult(final BytesValue result) {
}

private org.web3j.protocol.core.methods.request.Transaction payload() {
final BytesValue payload =
BytesValues.concatenate(
final Bytes payload =
Bytes.concatenate(
IS_ACCOUNT_ALLOWED_SIGNATURE,
BytesValue.fromHexString("0x000000000000000000000000"),
Bytes.fromHexString("0x000000000000000000000000"),
account);

return org.web3j.protocol.core.methods.request.Transaction.createFunctionCallTransaction(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,19 @@
import org.hyperledger.besu.tests.acceptance.dsl.node.RunnableNode;
import org.hyperledger.besu.tests.acceptance.dsl.transaction.NodeRequests;
import org.hyperledger.besu.tests.acceptance.dsl.transaction.Transaction;
import org.hyperledger.besu.util.bytes.BytesValue;

import java.io.IOException;
import java.math.BigInteger;

import org.apache.tuweni.bytes.Bytes;
import org.web3j.crypto.RawTransaction;
import org.web3j.crypto.TransactionEncoder;

public class NodeSmartContractPermissioningAllowNodeTransaction implements Transaction<Hash> {

private static final BytesValue ADD_ENODE_SIGNATURE =
private static final Bytes ADD_ENODE_SIGNATURE =
org.hyperledger.besu.crypto.Hash.keccak256(
BytesValue.of("addEnode(bytes32,bytes32,bytes16,uint16)".getBytes(UTF_8)))
Bytes.of("addEnode(bytes32,bytes32,bytes16,uint16)".getBytes(UTF_8)))
.slice(0, 4);

private final Account sender;
Expand Down Expand Up @@ -66,7 +66,7 @@ public Hash execute(final NodeRequests node) {

private String signedTransactionData() {
final String enodeURL = ((RunnableNode) node).enodeUrl().toASCIIString();
final BytesValue payload =
final Bytes payload =
NodeSmartContractPermissioningController.createPayload(
ADD_ENODE_SIGNATURE, EnodeURL.fromString(enodeURL));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,18 @@
import org.hyperledger.besu.tests.acceptance.dsl.node.RunnableNode;
import org.hyperledger.besu.tests.acceptance.dsl.transaction.NodeRequests;
import org.hyperledger.besu.tests.acceptance.dsl.transaction.Transaction;
import org.hyperledger.besu.util.bytes.BytesValue;

import java.io.IOException;

import org.apache.tuweni.bytes.Bytes;
import org.web3j.protocol.core.DefaultBlockParameterName;

public class NodeSmartContractPermissioningConnectionIsAllowedTransaction
implements Transaction<Boolean> {

private static final BytesValue IS_CONNECTION_ALLOWED_SIGNATURE =
private static final Bytes IS_CONNECTION_ALLOWED_SIGNATURE =
Hash.keccak256(
BytesValue.of(
Bytes.of(
"connectionAllowed(bytes32,bytes32,bytes16,uint16,bytes32,bytes32,bytes16,uint16)"
.getBytes(UTF_8)))
.slice(0, 4);
Expand All @@ -57,7 +57,7 @@ public Boolean execute(final NodeRequests node) {
try {
final String value =
node.eth().ethCall(payload(), DefaultBlockParameterName.LATEST).send().getValue();
return checkTransactionResult(BytesValue.fromHexString(value));
return checkTransactionResult(Bytes.fromHexString(value));
} catch (final IOException e) {
throw new RuntimeException(e);
}
Expand All @@ -66,7 +66,7 @@ public Boolean execute(final NodeRequests node) {
private org.web3j.protocol.core.methods.request.Transaction payload() {
final String sourceEnodeURL = ((RunnableNode) source).enodeUrl().toASCIIString();
final String targetEnodeURL = ((RunnableNode) target).enodeUrl().toASCIIString();
final BytesValue payload =
final Bytes payload =
NodeSmartContractPermissioningController.createPayload(
IS_CONNECTION_ALLOWED_SIGNATURE,
EnodeURL.fromString(sourceEnodeURL),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,19 @@
import org.hyperledger.besu.tests.acceptance.dsl.node.RunnableNode;
import org.hyperledger.besu.tests.acceptance.dsl.transaction.NodeRequests;
import org.hyperledger.besu.tests.acceptance.dsl.transaction.Transaction;
import org.hyperledger.besu.util.bytes.BytesValue;

import java.io.IOException;
import java.math.BigInteger;

import org.apache.tuweni.bytes.Bytes;
import org.web3j.crypto.RawTransaction;
import org.web3j.crypto.TransactionEncoder;

public class NodeSmartContractPermissioningForbidNodeTransaction implements Transaction<Hash> {

private static final BytesValue REMOVE_ENODE_SIGNATURE =
private static final Bytes REMOVE_ENODE_SIGNATURE =
org.hyperledger.besu.crypto.Hash.keccak256(
BytesValue.of("removeEnode(bytes32,bytes32,bytes16,uint16)".getBytes(UTF_8)))
Bytes.of("removeEnode(bytes32,bytes32,bytes16,uint16)".getBytes(UTF_8)))
.slice(0, 4);

private final Account sender;
Expand Down Expand Up @@ -66,7 +66,7 @@ public Hash execute(final NodeRequests node) {

private String signedTransactionData() {
final String enodeURL = ((RunnableNode) node).enodeUrl().toASCIIString();
final BytesValue payload =
final Bytes payload =
NodeSmartContractPermissioningController.createPayload(
REMOVE_ENODE_SIGNATURE, EnodeURL.fromString(enodeURL));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,16 @@
import org.hyperledger.besu.tests.acceptance.dsl.node.RunnableNode;
import org.hyperledger.besu.tests.acceptance.dsl.transaction.NodeRequests;
import org.hyperledger.besu.tests.acceptance.dsl.transaction.Transaction;
import org.hyperledger.besu.util.bytes.BytesValue;

import java.io.IOException;

import org.apache.tuweni.bytes.Bytes;
import org.web3j.protocol.core.DefaultBlockParameterName;

public class NodeSmartContractPermissioningIsAllowedTransaction implements Transaction<Boolean> {

private static final BytesValue IS_NODE_ALLOWED_SIGNATURE =
Hash.keccak256(BytesValue.of("enodeAllowed(bytes32,bytes32,bytes16,uint16)".getBytes(UTF_8)))
private static final Bytes IS_NODE_ALLOWED_SIGNATURE =
Hash.keccak256(Bytes.of("enodeAllowed(bytes32,bytes32,bytes16,uint16)".getBytes(UTF_8)))
.slice(0, 4);

private final Address contractAddress;
Expand All @@ -50,31 +50,29 @@ public Boolean execute(final NodeRequests node) {
try {
final String value =
node.eth().ethCall(payload(), DefaultBlockParameterName.LATEST).send().getValue();
return checkTransactionResult(BytesValue.fromHexString(value));
return checkTransactionResult(Bytes.fromHexString(value));
} catch (final IOException e) {
throw new RuntimeException(e);
}
}

// Checks the returned bytes from the permissioning contract call to see if it's a value we
// understand
static Boolean checkTransactionResult(final BytesValue result) {
static Boolean checkTransactionResult(final Bytes result) {
// booleans are padded to 32 bytes
if (result.size() != 32) {
throw new IllegalArgumentException("Unexpected result size");
}

// 0 is false
if (result.compareTo(
BytesValue.fromHexString(
"0x0000000000000000000000000000000000000000000000000000000000000000"))
== 0) {
if (result.equals(
Bytes.fromHexString(
"0x0000000000000000000000000000000000000000000000000000000000000000"))) {
return false;
// 1 filled to 32 bytes is true
} else if (result.compareTo(
BytesValue.fromHexString(
"0x0000000000000000000000000000000000000000000000000000000000000001"))
== 0) {
} else if (result.equals(
Bytes.fromHexString(
"0x0000000000000000000000000000000000000000000000000000000000000001"))) {
return true;
// Anything else is wrong
} else {
Expand All @@ -84,7 +82,7 @@ static Boolean checkTransactionResult(final BytesValue result) {

private org.web3j.protocol.core.methods.request.Transaction payload() {
final String sourceEnodeURL = ((RunnableNode) node).enodeUrl().toASCIIString();
final BytesValue payload =
final Bytes payload =
NodeSmartContractPermissioningController.createPayload(
IS_NODE_ALLOWED_SIGNATURE, EnodeURL.fromString(sourceEnodeURL));

Expand Down
Loading