Skip to content

Commit

Permalink
Merge branch 'master' into mining_decoupling
Browse files Browse the repository at this point in the history
  • Loading branch information
atoulme committed Mar 5, 2021
2 parents bc70025 + 711bbfb commit 66fc1c6
Show file tree
Hide file tree
Showing 26 changed files with 510 additions and 122 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
import org.hyperledger.besu.ethereum.core.Address;
import org.hyperledger.besu.ethereum.core.Gas;
import org.hyperledger.besu.ethereum.core.Wei;
import org.hyperledger.besu.ethereum.core.deserializer.GasDeserializer;
import org.hyperledger.besu.ethereum.core.deserializer.HexStringDeserializer;
import org.hyperledger.besu.ethereum.core.json.GasDeserializer;
import org.hyperledger.besu.ethereum.core.json.HexStringDeserializer;
import org.hyperledger.besu.ethereum.transaction.CallParameter;

import java.util.Optional;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,7 @@ public class TransactionCompleteResult implements TransactionResult {
public TransactionCompleteResult(final TransactionWithMetadata tx) {
final Transaction transaction = tx.getTransaction();
final TransactionType transactionType = transaction.getType();
this.accessList =
transactionType.equals(TransactionType.ACCESS_LIST) ? transaction.getAccessList() : null;
this.accessList = transaction.getAccessList().orElse(null);
this.blockHash = tx.getBlockHash().get().toString();
this.blockNumber = Quantity.create(tx.getBlockNumber().get());
this.chainId = transaction.getChainId().map(Quantity::create).orElse(null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,7 @@ public class TransactionPendingResult implements TransactionResult {

public TransactionPendingResult(final Transaction transaction) {
final TransactionType transactionType = transaction.getType();
this.accessList =
transactionType.equals(TransactionType.ACCESS_LIST) ? transaction.getAccessList() : null;
this.accessList = transaction.getAccessList().orElse(null);
this.chainId = transaction.getChainId().map(Quantity::create).orElse(null);
this.from = transaction.getSender().toString();
this.gas = Quantity.create(transaction.getGasLimit());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.BlockResultFactory;
import org.hyperledger.besu.ethereum.api.query.BlockchainQueries;
import org.hyperledger.besu.ethereum.blockcreation.MiningCoordinator;
import org.hyperledger.besu.ethereum.core.PrivacyParameters;
import org.hyperledger.besu.ethereum.core.Synchronizer;
import org.hyperledger.besu.ethereum.eth.transactions.TransactionPool;
import org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule;
Expand All @@ -83,6 +84,7 @@ public class EthJsonRpcMethods extends ApiGroupJsonRpcMethods {
private final TransactionPool transactionPool;
private final MiningCoordinator miningCoordinator;
private final Set<Capability> supportedCapabilities;
private final PrivacyParameters privacyParameters;

public EthJsonRpcMethods(
final BlockchainQueries blockchainQueries,
Expand All @@ -91,14 +93,16 @@ public EthJsonRpcMethods(
final FilterManager filterManager,
final TransactionPool transactionPool,
final MiningCoordinator miningCoordinator,
final Set<Capability> supportedCapabilities) {
final Set<Capability> supportedCapabilities,
final PrivacyParameters privacyParameters) {
this.blockchainQueries = blockchainQueries;
this.synchronizer = synchronizer;
this.protocolSchedule = protocolSchedule;
this.filterManager = filterManager;
this.transactionPool = transactionPool;
this.miningCoordinator = miningCoordinator;
this.supportedCapabilities = supportedCapabilities;
this.privacyParameters = privacyParameters;
}

@Override
Expand All @@ -121,7 +125,8 @@ protected Map<String, JsonRpcMethod> create() {
new TransactionSimulator(
blockchainQueries.getBlockchain(),
blockchainQueries.getWorldStateArchive(),
protocolSchedule)),
protocolSchedule,
privacyParameters)),
new EthGetCode(blockchainQueries),
new EthGetLogs(blockchainQueries),
new EthGetProof(blockchainQueries),
Expand Down Expand Up @@ -149,7 +154,8 @@ protected Map<String, JsonRpcMethod> create() {
new TransactionSimulator(
blockchainQueries.getBlockchain(),
blockchainQueries.getWorldStateArchive(),
protocolSchedule)),
protocolSchedule,
privacyParameters)),
new EthMining(miningCoordinator),
new EthCoinbase(miningCoordinator),
new EthProtocolVersion(supportedCapabilities),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ public Map<String, JsonRpcMethod> methods(
filterManager,
transactionPool,
miningCoordinator,
supportedCapabilities),
supportedCapabilities,
privacyParameters),
new NetJsonRpcMethods(
p2pNetwork,
networkId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,9 @@ public void commit() {
} else {
updatedAccount.setBalance(tracked.getBalance());
updatedAccount.setNonce(tracked.getNonce());
updatedAccount.setCode(tracked.getCode());
if (tracked.codeWasUpdated()) {
updatedAccount.setCode(tracked.getCode());
}
if (tracked.getStorageWasCleared()) {
updatedAccount.clearStorage();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,17 @@
*/
package org.hyperledger.besu.ethereum.core;

import java.io.IOException;
import org.hyperledger.besu.ethereum.core.json.AccessListEntryDeserializer;
import org.hyperledger.besu.ethereum.core.json.AccessListEntrySerializer;

import java.util.List;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
import org.apache.tuweni.bytes.Bytes32;

@JsonSerialize(using = AccessListEntry.Serializer.class)
@JsonSerialize(using = AccessListEntrySerializer.class)
@JsonDeserialize(using = AccessListEntryDeserializer.class)
public class AccessListEntry {
private final Address address;
private final List<Bytes32> storageKeys;
Expand All @@ -41,33 +42,4 @@ public Address getAddress() {
public List<Bytes32> getStorageKeys() {
return storageKeys;
}

public static class Serializer extends StdSerializer<AccessListEntry> {

Serializer() {
this(null);
}

protected Serializer(final Class<AccessListEntry> t) {
super(t);
}

@Override
public void serialize(
final AccessListEntry accessListEntry,
final JsonGenerator gen,
final SerializerProvider provider)
throws IOException {
gen.writeStartObject();
gen.writeFieldName("address");
gen.writeString(accessListEntry.getAddress().toHexString());
gen.writeFieldName("storageKeys");
final List<Bytes32> storageKeys = accessListEntry.getStorageKeys();
gen.writeArray(
storageKeys.stream().map(Bytes32::toHexString).toArray(String[]::new),
0,
storageKeys.size());
gen.writeEndObject();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package org.hyperledger.besu.ethereum.core;

import static com.google.common.base.Preconditions.checkState;
import static java.util.Collections.emptyList;
import static org.hyperledger.besu.crypto.Hash.keccak256;

import org.hyperledger.besu.crypto.KeyPair;
Expand Down Expand Up @@ -77,7 +76,7 @@ public class Transaction implements org.hyperledger.besu.plugin.data.Transaction

private final Bytes payload;

private final List<AccessListEntry> accessList;
private final Optional<List<AccessListEntry>> maybeAccessList;

private final Optional<BigInteger> chainId;

Expand Down Expand Up @@ -119,7 +118,8 @@ public static Transaction readFrom(final RLPInput rlpInput) {
* @param value the value being transferred to the recipient
* @param signature the signature
* @param payload the payload
* @param accessList the list of addresses/storage slots this transaction intends to preload
* @param maybeAccessList the optional list of addresses/storage slots this transaction intends to
* preload
* @param sender the transaction sender
* @param chainId the chain id to apply the transaction to
* @param v the v value. This is only passed in directly for GoQuorum private transactions
Expand All @@ -141,14 +141,22 @@ public Transaction(
final Wei value,
final SECPSignature signature,
final Bytes payload,
final List<AccessListEntry> accessList,
final Optional<List<AccessListEntry>> maybeAccessList,
final Address sender,
final Optional<BigInteger> chainId,
final Optional<BigInteger> v) {
if (v.isPresent() && chainId.isPresent()) {
throw new IllegalStateException(
String.format("chainId '%s' and v '%s' cannot both be provided", chainId.get(), v.get()));
}
if (Objects.equals(transactionType, TransactionType.ACCESS_LIST)) {
checkState(
maybeAccessList.isPresent(), "Must specify access list for access list transaction");
} else {
checkState(
maybeAccessList.isEmpty(),
"Must not specify access list for non-access list transaction");
}
this.transactionType = transactionType;
this.nonce = nonce;
this.gasPrice = gasPrice;
Expand All @@ -159,7 +167,7 @@ public Transaction(
this.value = value;
this.signature = signature;
this.payload = payload;
this.accessList = accessList;
this.maybeAccessList = maybeAccessList;
this.sender = sender;
this.chainId = chainId;
this.v = v;
Expand Down Expand Up @@ -189,7 +197,7 @@ public Transaction(
value,
signature,
payload,
emptyList(),
Optional.empty(),
sender,
chainId,
v);
Expand Down Expand Up @@ -380,8 +388,8 @@ public Optional<Bytes> getData() {
return getTo().isPresent() ? Optional.of(payload) : Optional.empty();
}

public List<AccessListEntry> getAccessList() {
return accessList;
public Optional<List<AccessListEntry>> getAccessList() {
return maybeAccessList;
}

/**
Expand Down Expand Up @@ -441,7 +449,7 @@ private Bytes32 getOrComputeSenderRecoveryHash() {
to,
value,
payload,
accessList,
maybeAccessList,
chainId);
}
return hashNoSignature;
Expand Down Expand Up @@ -564,7 +572,7 @@ private static Bytes32 computeSenderRecoveryHash(
final Optional<Address> to,
final Wei value,
final Bytes payload,
final List<AccessListEntry> accessList,
final Optional<List<AccessListEntry>> accessList,
final Optional<BigInteger> chainId) {
final Bytes preimage;
switch (transactionType) {
Expand All @@ -578,7 +586,18 @@ private static Bytes32 computeSenderRecoveryHash(
break;
case ACCESS_LIST:
preimage =
accessListPreimage(nonce, gasPrice, gasLimit, to, value, payload, accessList, chainId);
accessListPreimage(
nonce,
gasPrice,
gasLimit,
to,
value,
payload,
accessList.orElseThrow(
() ->
new IllegalStateException(
"Developer error: the transaction should be guaranteed to have an access list here")),
chainId);
break;
default:
throw new IllegalStateException(
Expand Down Expand Up @@ -705,9 +724,9 @@ public String toString() {
sb.append("sig=").append(getSignature()).append(", ");
if (chainId.isPresent()) sb.append("chainId=").append(getChainId().get()).append(", ");
if (v.isPresent()) sb.append("v=").append(v.get()).append(", ");
sb.append("payload=").append(getPayload()).append(", ");
sb.append("payload=").append(getPayload());
if (transactionType.equals(TransactionType.ACCESS_LIST)) {
sb.append("accessList=").append(accessList);
sb.append(", ").append("accessList=").append(maybeAccessList);
}
return sb.append("}").toString();
}
Expand Down Expand Up @@ -741,7 +760,7 @@ public static class Builder {

protected Bytes payload;

protected List<AccessListEntry> accessList = emptyList();
protected Optional<List<AccessListEntry>> accessList = Optional.empty();

protected Address sender;

Expand Down Expand Up @@ -805,7 +824,7 @@ public Builder payload(final Bytes payload) {
}

public Builder accessList(final List<AccessListEntry> accessList) {
this.accessList = accessList;
this.accessList = Optional.ofNullable(accessList);
return this;
}

Expand All @@ -820,7 +839,7 @@ public Builder signature(final SECPSignature signature) {
}

public Builder guessType() {
if (!accessList.isEmpty()) {
if (accessList.isPresent()) {
transactionType = TransactionType.ACCESS_LIST;
} else if (gasPremium != null || feeCap != null) {
transactionType = TransactionType.EIP1559;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,12 @@ static void encodeAccessList(final Transaction transaction, final RLPOutput rlpO
transaction.getTo(),
transaction.getValue(),
transaction.getPayload(),
transaction.getAccessList(),
transaction
.getAccessList()
.orElseThrow(
() ->
new IllegalStateException(
"Developer error: access list should be guaranteed to be present")),
rlpOutput);
rlpOutput.writeIntScalar(transaction.getSignature().getRecId());
writeSignature(transaction, rlpOutput);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright ConsenSys AG.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/

package org.hyperledger.besu.ethereum.core.json;

import static com.google.common.base.Preconditions.checkState;

import org.hyperledger.besu.ethereum.core.AccessListEntry;
import org.hyperledger.besu.ethereum.core.Address;

import java.io.IOException;
import java.util.ArrayList;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import org.apache.tuweni.bytes.Bytes32;

public class AccessListEntryDeserializer extends StdDeserializer<AccessListEntry> {
private AccessListEntryDeserializer() {
this(null);
}

protected AccessListEntryDeserializer(final Class<?> vc) {
super(vc);
}

@Override
public AccessListEntry deserialize(final JsonParser p, final DeserializationContext ctxt)
throws IOException {
checkState(p.nextFieldName().equals("address"));
final Address address = Address.fromHexString(p.nextTextValue());
checkState(p.nextFieldName().equals("storageKeys"));
checkState(p.nextToken().equals(JsonToken.START_ARRAY));
final ArrayList<Bytes32> storageKeys = new ArrayList<>();
while (!p.nextToken().equals(JsonToken.END_ARRAY)) {
storageKeys.add(Bytes32.fromHexString(p.getText()));
}
p.nextToken(); // consume end of object
return new AccessListEntry(address, storageKeys);
}
}
Loading

0 comments on commit 66fc1c6

Please sign in to comment.