|
|
@ -20,7 +20,9 @@ import static org.hyperledger.besu.ethereum.trie.verkle.util.Parameters.CODE_OFF |
|
|
|
import static org.hyperledger.besu.ethereum.trie.verkle.util.Parameters.VERKLE_NODE_WIDTH; |
|
|
|
import static org.hyperledger.besu.ethereum.trie.verkle.util.Parameters.VERKLE_NODE_WIDTH; |
|
|
|
import static org.hyperledger.besu.evm.internal.Words.clampedAdd; |
|
|
|
import static org.hyperledger.besu.evm.internal.Words.clampedAdd; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import org.hyperledger.besu.datatypes.AccessWitness; |
|
|
|
import org.hyperledger.besu.datatypes.Address; |
|
|
|
import org.hyperledger.besu.datatypes.Address; |
|
|
|
|
|
|
|
import org.hyperledger.besu.datatypes.Wei; |
|
|
|
import org.hyperledger.besu.ethereum.trie.verkle.adapter.TrieKeyAdapter; |
|
|
|
import org.hyperledger.besu.ethereum.trie.verkle.adapter.TrieKeyAdapter; |
|
|
|
import org.hyperledger.besu.ethereum.trie.verkle.hasher.PedersenHasher; |
|
|
|
import org.hyperledger.besu.ethereum.trie.verkle.hasher.PedersenHasher; |
|
|
|
|
|
|
|
|
|
|
@ -28,146 +30,174 @@ import java.util.HashMap; |
|
|
|
import java.util.List; |
|
|
|
import java.util.List; |
|
|
|
import java.util.Map; |
|
|
|
import java.util.Map; |
|
|
|
import java.util.Objects; |
|
|
|
import java.util.Objects; |
|
|
|
|
|
|
|
import java.util.Optional; |
|
|
|
|
|
|
|
|
|
|
|
import org.apache.tuweni.units.bigints.UInt256; |
|
|
|
import org.apache.tuweni.units.bigints.UInt256; |
|
|
|
|
|
|
|
import org.slf4j.Logger; |
|
|
|
|
|
|
|
import org.slf4j.LoggerFactory; |
|
|
|
|
|
|
|
|
|
|
|
public class Eip4762AccessWitness implements org.hyperledger.besu.datatypes.AccessWitness { |
|
|
|
public class Eip4762AccessWitness implements AccessWitness { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static final Logger LOG = LoggerFactory.getLogger(Eip4762AccessWitness.class); |
|
|
|
private static final TrieKeyAdapter TRIE_KEY_ADAPTER = new TrieKeyAdapter(new PedersenHasher()); |
|
|
|
private static final TrieKeyAdapter TRIE_KEY_ADAPTER = new TrieKeyAdapter(new PedersenHasher()); |
|
|
|
private static final long WITNESS_BRANCH_READ_COST = 1900; |
|
|
|
private static final long WITNESS_BRANCH_COST = 1900; |
|
|
|
private static final long WITNESS_CHUNK_READ_COST = 200; |
|
|
|
private static final long WITNESS_CHUNK_COST = 200; |
|
|
|
private static final long WITNESS_BRANCH_WRITE_COST = 3000; |
|
|
|
private static final long SUBTREE_EDIT_COST = 3000; |
|
|
|
private static final long WITNESS_CHUNK_WRITE_COST = 500; |
|
|
|
private static final long CHUNK_EDIT_COST = 500; |
|
|
|
private static final long WITNESS_CHUNK_FILL_COST = 6200; |
|
|
|
private static final long CHUNK_FILL_COST = 6200; |
|
|
|
|
|
|
|
|
|
|
|
private static final UInt256 zeroTreeIndex = UInt256.ZERO; |
|
|
|
private static final UInt256 zeroTreeIndex = UInt256.ZERO; |
|
|
|
private static final byte AccessWitnessReadFlag = 1; |
|
|
|
private final Map<LeafAccessKey, Integer> leaves; |
|
|
|
private static final byte AccessWitnessWriteFlag = 2; |
|
|
|
private final Map<BranchAccessKey, Integer> branches; |
|
|
|
private final Map<BranchAccessKey, Byte> branches; |
|
|
|
|
|
|
|
private final Map<ChunkAccessKey, Byte> chunks; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public Eip4762AccessWitness() { |
|
|
|
public Eip4762AccessWitness() { |
|
|
|
this(new HashMap<>(), new HashMap<>()); |
|
|
|
this(new HashMap<>(), new HashMap<>()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public Eip4762AccessWitness( |
|
|
|
public Eip4762AccessWitness( |
|
|
|
final Map<BranchAccessKey, Byte> branches, final Map<ChunkAccessKey, Byte> chunks) { |
|
|
|
final Map<LeafAccessKey, Integer> leaves, final Map<BranchAccessKey, Integer> branches) { |
|
|
|
this.branches = branches; |
|
|
|
this.branches = branches; |
|
|
|
this.chunks = chunks; |
|
|
|
this.leaves = leaves; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public List<Address> keys() { |
|
|
|
public long touchAddressAndChargeRead(final Address address, final UInt256 leafKey) { |
|
|
|
return this.chunks.keySet().stream() |
|
|
|
return touchAddressOnReadAndComputeGas(address, zeroTreeIndex, leafKey); |
|
|
|
.map(chunkAccessKey -> chunkAccessKey.branchAccessKey().address()) |
|
|
|
|
|
|
|
.toList(); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public long touchAndChargeProofOfAbsence(final Address address) { |
|
|
|
public long touchAndChargeValueTransfer( |
|
|
|
|
|
|
|
final Address caller, |
|
|
|
|
|
|
|
final Address target, |
|
|
|
|
|
|
|
final boolean isAccountCreation, |
|
|
|
|
|
|
|
final long warmReadCost) { |
|
|
|
|
|
|
|
|
|
|
|
long gas = 0; |
|
|
|
long gas = 0; |
|
|
|
|
|
|
|
|
|
|
|
gas = |
|
|
|
gas = |
|
|
|
clampedAdd( |
|
|
|
clampedAdd( |
|
|
|
gas, touchAddressOnReadAndComputeGas(address, zeroTreeIndex, BASIC_DATA_LEAF_KEY)); |
|
|
|
gas, touchAddressOnWriteResetAndComputeGas(caller, zeroTreeIndex, BASIC_DATA_LEAF_KEY)); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (isAccountCreation) { |
|
|
|
gas = |
|
|
|
gas = |
|
|
|
clampedAdd( |
|
|
|
clampedAdd( |
|
|
|
gas, touchAddressOnReadAndComputeGas(address, zeroTreeIndex, CODE_HASH_LEAF_KEY)); |
|
|
|
gas, touchAddressOnWriteSetAndComputeGas(target, zeroTreeIndex, BASIC_DATA_LEAF_KEY)); |
|
|
|
|
|
|
|
gas = |
|
|
|
|
|
|
|
clampedAdd( |
|
|
|
|
|
|
|
gas, touchAddressOnWriteSetAndComputeGas(target, zeroTreeIndex, CODE_HASH_LEAF_KEY)); |
|
|
|
return gas; |
|
|
|
return gas; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
long readTargetStatelessGas = |
|
|
|
public long touchAndChargeMessageCall(final Address address) { |
|
|
|
touchAddressOnReadAndComputeGas(target, zeroTreeIndex, BASIC_DATA_LEAF_KEY); |
|
|
|
return touchAddressOnReadAndComputeGas(address, zeroTreeIndex, BASIC_DATA_LEAF_KEY); |
|
|
|
if (readTargetStatelessGas == 0) { |
|
|
|
|
|
|
|
readTargetStatelessGas = clampedAdd(readTargetStatelessGas, warmReadCost); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
|
|
public long touchAndChargeValueTransfer(final Address caller, final Address target) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
long gas = 0; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
gas = |
|
|
|
|
|
|
|
clampedAdd( |
|
|
|
|
|
|
|
gas, touchAddressOnWriteAndComputeGas(caller, zeroTreeIndex, BASIC_DATA_LEAF_KEY)); |
|
|
|
|
|
|
|
gas = |
|
|
|
gas = |
|
|
|
clampedAdd( |
|
|
|
clampedAdd( |
|
|
|
gas, touchAddressOnWriteAndComputeGas(target, zeroTreeIndex, BASIC_DATA_LEAF_KEY)); |
|
|
|
gas, touchAddressOnWriteResetAndComputeGas(target, zeroTreeIndex, BASIC_DATA_LEAF_KEY)); |
|
|
|
|
|
|
|
|
|
|
|
return gas; |
|
|
|
return clampedAdd(gas, readTargetStatelessGas); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public long touchAndChargeContractCreateInit( |
|
|
|
public long touchAndChargeValueTransferSelfDestruct( |
|
|
|
final Address address, final boolean createSendsValue) { |
|
|
|
final Address caller, |
|
|
|
|
|
|
|
final Address target, |
|
|
|
|
|
|
|
final boolean isAccountCreation, |
|
|
|
|
|
|
|
final long warmReadCost) { |
|
|
|
|
|
|
|
|
|
|
|
long gas = 0; |
|
|
|
long gas = 0; |
|
|
|
|
|
|
|
|
|
|
|
gas = |
|
|
|
gas = |
|
|
|
clampedAdd( |
|
|
|
clampedAdd( |
|
|
|
gas, touchAddressOnWriteAndComputeGas(address, zeroTreeIndex, BASIC_DATA_LEAF_KEY)); |
|
|
|
gas, touchAddressOnWriteResetAndComputeGas(caller, zeroTreeIndex, BASIC_DATA_LEAF_KEY)); |
|
|
|
|
|
|
|
|
|
|
|
if (createSendsValue) { |
|
|
|
if (caller.equals(target)) { |
|
|
|
|
|
|
|
return gas; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (isAccountCreation) { |
|
|
|
gas = |
|
|
|
gas = |
|
|
|
clampedAdd( |
|
|
|
clampedAdd( |
|
|
|
gas, touchAddressOnWriteAndComputeGas(address, zeroTreeIndex, BASIC_DATA_LEAF_KEY)); |
|
|
|
gas, touchAddressOnWriteSetAndComputeGas(target, zeroTreeIndex, BASIC_DATA_LEAF_KEY)); |
|
|
|
} |
|
|
|
gas = |
|
|
|
|
|
|
|
clampedAdd( |
|
|
|
|
|
|
|
gas, touchAddressOnWriteSetAndComputeGas(target, zeroTreeIndex, CODE_HASH_LEAF_KEY)); |
|
|
|
return gas; |
|
|
|
return gas; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
long readTargetStatelessGas = |
|
|
|
public long touchAndChargeContractCreateCompleted(final Address address) { |
|
|
|
touchAddressOnReadAndComputeGas(target, zeroTreeIndex, BASIC_DATA_LEAF_KEY); |
|
|
|
|
|
|
|
if (readTargetStatelessGas == 0) { |
|
|
|
long gas = 0; |
|
|
|
readTargetStatelessGas = clampedAdd(readTargetStatelessGas, warmReadCost); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
gas = |
|
|
|
gas = |
|
|
|
clampedAdd( |
|
|
|
clampedAdd( |
|
|
|
gas, touchAddressOnWriteAndComputeGas(address, zeroTreeIndex, BASIC_DATA_LEAF_KEY)); |
|
|
|
gas, touchAddressOnWriteResetAndComputeGas(target, zeroTreeIndex, BASIC_DATA_LEAF_KEY)); |
|
|
|
gas = |
|
|
|
|
|
|
|
clampedAdd( |
|
|
|
|
|
|
|
gas, touchAddressOnWriteAndComputeGas(address, zeroTreeIndex, CODE_HASH_LEAF_KEY)); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return gas; |
|
|
|
return clampedAdd(gas, readTargetStatelessGas); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@SuppressWarnings("unused") |
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public long touchTxOriginAndComputeGas(final Address origin) { |
|
|
|
public long touchAndChargeProofOfAbsence(final Address address) { |
|
|
|
|
|
|
|
|
|
|
|
long gas = 0; |
|
|
|
long gas = 0; |
|
|
|
|
|
|
|
|
|
|
|
gas = |
|
|
|
gas = |
|
|
|
clampedAdd( |
|
|
|
clampedAdd( |
|
|
|
gas, touchAddressOnReadAndComputeGas(origin, zeroTreeIndex, BASIC_DATA_LEAF_KEY)); |
|
|
|
gas, touchAddressOnReadAndComputeGas(address, zeroTreeIndex, BASIC_DATA_LEAF_KEY)); |
|
|
|
|
|
|
|
|
|
|
|
gas = |
|
|
|
gas = |
|
|
|
clampedAdd( |
|
|
|
clampedAdd( |
|
|
|
gas, touchAddressOnWriteAndComputeGas(origin, zeroTreeIndex, BASIC_DATA_LEAF_KEY)); |
|
|
|
gas, touchAddressOnReadAndComputeGas(address, zeroTreeIndex, CODE_HASH_LEAF_KEY)); |
|
|
|
gas = |
|
|
|
|
|
|
|
clampedAdd(gas, touchAddressOnReadAndComputeGas(origin, zeroTreeIndex, CODE_HASH_LEAF_KEY)); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// modifying this after update on EIP-4762 to not charge simple transfers
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return 0; |
|
|
|
return gas; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@SuppressWarnings("unused") |
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public long touchTxExistingAndComputeGas(final Address target, final boolean sendsValue) { |
|
|
|
public long touchAndChargeContractCreateCompleted(final Address address) { |
|
|
|
|
|
|
|
|
|
|
|
long gas = 0; |
|
|
|
long gas = 0; |
|
|
|
|
|
|
|
|
|
|
|
gas = |
|
|
|
gas = |
|
|
|
clampedAdd( |
|
|
|
clampedAdd( |
|
|
|
gas, touchAddressOnReadAndComputeGas(target, zeroTreeIndex, BASIC_DATA_LEAF_KEY)); |
|
|
|
gas, |
|
|
|
gas = |
|
|
|
touchAddressOnWriteResetAndComputeGas(address, zeroTreeIndex, BASIC_DATA_LEAF_KEY)); |
|
|
|
clampedAdd(gas, touchAddressOnReadAndComputeGas(target, zeroTreeIndex, CODE_HASH_LEAF_KEY)); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (sendsValue) { |
|
|
|
|
|
|
|
gas = |
|
|
|
gas = |
|
|
|
clampedAdd( |
|
|
|
clampedAdd( |
|
|
|
gas, touchAddressOnWriteAndComputeGas(target, zeroTreeIndex, BASIC_DATA_LEAF_KEY)); |
|
|
|
gas, touchAddressOnWriteResetAndComputeGas(address, zeroTreeIndex, CODE_HASH_LEAF_KEY)); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return gas; |
|
|
|
} |
|
|
|
} |
|
|
|
// modifying this after update on EIP-4762 to not charge simple transfers
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return 0; |
|
|
|
@Override |
|
|
|
|
|
|
|
public void touchBaseTx(final Address origin, final Optional<Address> target, final Wei value) { |
|
|
|
|
|
|
|
LOG.atDebug().log("START OF UNCHARGED COSTS"); |
|
|
|
|
|
|
|
touchTxOrigin(origin); |
|
|
|
|
|
|
|
if (target.isPresent()) { // is not contract creation
|
|
|
|
|
|
|
|
final Address to = target.get(); |
|
|
|
|
|
|
|
final boolean sendsValue = !Wei.ZERO.equals(value); |
|
|
|
|
|
|
|
touchTxExisting(to, sendsValue); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
LOG.atDebug().log("END OF UNCHARGED COSTS"); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void touchTxOrigin(final Address origin) { |
|
|
|
|
|
|
|
touchAddressOnWriteResetAndComputeGas(origin, zeroTreeIndex, BASIC_DATA_LEAF_KEY); |
|
|
|
|
|
|
|
touchAddressOnReadAndComputeGas(origin, zeroTreeIndex, CODE_HASH_LEAF_KEY); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void touchTxExisting(final Address target, final boolean sendsValue) { |
|
|
|
|
|
|
|
touchAddressOnReadAndComputeGas(target, zeroTreeIndex, CODE_HASH_LEAF_KEY); |
|
|
|
|
|
|
|
if (!sendsValue) { |
|
|
|
|
|
|
|
touchAddressOnReadAndComputeGas(target, zeroTreeIndex, BASIC_DATA_LEAF_KEY); |
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
// TODO: not done right now on Geth either - implement case if target does not exist yet - a
|
|
|
|
|
|
|
|
// CODEHASH_LEAF will be touched too and WriteSet will be called
|
|
|
|
|
|
|
|
touchAddressOnWriteResetAndComputeGas(target, zeroTreeIndex, BASIC_DATA_LEAF_KEY); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
@ -177,7 +207,7 @@ public class Eip4762AccessWitness implements org.hyperledger.besu.datatypes.Acce |
|
|
|
gas = |
|
|
|
gas = |
|
|
|
clampedAdd( |
|
|
|
clampedAdd( |
|
|
|
gas, |
|
|
|
gas, |
|
|
|
touchAddressOnWriteAndComputeGas( |
|
|
|
touchAddressOnWriteSetAndComputeGas( |
|
|
|
address, |
|
|
|
address, |
|
|
|
CODE_OFFSET.add(i).divide(VERKLE_NODE_WIDTH), |
|
|
|
CODE_OFFSET.add(i).divide(VERKLE_NODE_WIDTH), |
|
|
|
CODE_OFFSET.add(i).mod(VERKLE_NODE_WIDTH))); |
|
|
|
CODE_OFFSET.add(i).mod(VERKLE_NODE_WIDTH))); |
|
|
@ -187,173 +217,217 @@ public class Eip4762AccessWitness implements org.hyperledger.besu.datatypes.Acce |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public long touchCodeChunks( |
|
|
|
public long touchCodeChunks( |
|
|
|
final Address address, final long startPc, final long readSize, final long codeLength) { |
|
|
|
final Address contractAddress, |
|
|
|
|
|
|
|
final boolean isContractInDeployment, |
|
|
|
|
|
|
|
final long startPc, |
|
|
|
|
|
|
|
final long readSize, |
|
|
|
|
|
|
|
final long codeLength) { |
|
|
|
long gas = 0; |
|
|
|
long gas = 0; |
|
|
|
if ((readSize == 0 && codeLength == 0) || startPc > codeLength) { |
|
|
|
|
|
|
|
|
|
|
|
if (isContractInDeployment || readSize == 0 || startPc >= codeLength) { |
|
|
|
return 0; |
|
|
|
return 0; |
|
|
|
} |
|
|
|
} |
|
|
|
long endPc = startPc + readSize; |
|
|
|
|
|
|
|
if (endPc > codeLength) { |
|
|
|
// last byte read is limited by code length, and it is an index, hence the decrement
|
|
|
|
endPc = codeLength; |
|
|
|
long endPc = Math.min(startPc + readSize, codeLength) - 1L; |
|
|
|
} |
|
|
|
for (long i = startPc / 31L; i <= endPc / 31L; i++) { |
|
|
|
if (endPc > 0) { |
|
|
|
|
|
|
|
endPc -= 1; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
for (long i = startPc / 31; i <= endPc / 31; i++) { |
|
|
|
|
|
|
|
gas = |
|
|
|
gas = |
|
|
|
clampedAdd( |
|
|
|
clampedAdd( |
|
|
|
gas, |
|
|
|
gas, |
|
|
|
touchAddressOnReadAndComputeGas( |
|
|
|
touchAddressOnReadAndComputeGas( |
|
|
|
address, |
|
|
|
contractAddress, |
|
|
|
CODE_OFFSET.add(i).divide(VERKLE_NODE_WIDTH), |
|
|
|
CODE_OFFSET.add(i).divide(VERKLE_NODE_WIDTH), |
|
|
|
CODE_OFFSET.add(i).mod(VERKLE_NODE_WIDTH))); |
|
|
|
CODE_OFFSET.add(i).mod(VERKLE_NODE_WIDTH))); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return gas; |
|
|
|
return gas; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
private long touchAddressOnWriteResetAndComputeGas( |
|
|
|
public long touchAddressOnWriteAndComputeGas( |
|
|
|
|
|
|
|
final Address address, final UInt256 treeIndex, final UInt256 subIndex) { |
|
|
|
final Address address, final UInt256 treeIndex, final UInt256 subIndex) { |
|
|
|
|
|
|
|
return touchAddressAndChargeGas(address, treeIndex, subIndex, AccessMode.WRITE_RESET); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return touchAddressAndChargeGas(address, treeIndex, subIndex, true); |
|
|
|
private long touchAddressOnWriteSetAndComputeGas( |
|
|
|
|
|
|
|
final Address address, final UInt256 treeIndex, final UInt256 subIndex) { |
|
|
|
|
|
|
|
// TODO: change to WRITE_SET when CHUNK_FILL is implemented. Still not implemented in devnet-7
|
|
|
|
|
|
|
|
return touchAddressAndChargeGas(address, treeIndex, subIndex, AccessMode.WRITE_RESET); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
private long touchAddressOnReadAndComputeGas( |
|
|
|
public long touchAddressOnReadAndComputeGas( |
|
|
|
|
|
|
|
final Address address, final UInt256 treeIndex, final UInt256 subIndex) { |
|
|
|
final Address address, final UInt256 treeIndex, final UInt256 subIndex) { |
|
|
|
return touchAddressAndChargeGas(address, treeIndex, subIndex, false); |
|
|
|
return touchAddressAndChargeGas(address, treeIndex, subIndex, AccessMode.READ); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private List<UInt256> getStorageSlotTreeIndexes(final UInt256 storageKey) { |
|
|
|
|
|
|
|
return List.of( |
|
|
|
|
|
|
|
TRIE_KEY_ADAPTER.locateStorageKeyOffset(storageKey), |
|
|
|
|
|
|
|
TRIE_KEY_ADAPTER.locateStorageKeySuffix(storageKey)); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
|
|
public long touchAndChargeStorageLoad(final Address address, final UInt256 storageKey) { |
|
|
|
|
|
|
|
List<UInt256> treeIndexes = getStorageSlotTreeIndexes(storageKey); |
|
|
|
|
|
|
|
return touchAddressOnReadAndComputeGas(address, treeIndexes.get(0), treeIndexes.get(1)); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
|
|
public long touchAndChargeStorageStore( |
|
|
|
|
|
|
|
final Address address, final UInt256 storageKey, final boolean hasPreviousValue) { |
|
|
|
|
|
|
|
List<UInt256> treeIndexes = getStorageSlotTreeIndexes(storageKey); |
|
|
|
|
|
|
|
if (!hasPreviousValue) { |
|
|
|
|
|
|
|
return touchAddressOnWriteSetAndComputeGas(address, treeIndexes.get(0), treeIndexes.get(1)); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return touchAddressOnWriteResetAndComputeGas(address, treeIndexes.get(0), treeIndexes.get(1)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public long touchAddressAndChargeGas( |
|
|
|
public long touchAddressAndChargeGas( |
|
|
|
final Address address, |
|
|
|
final Address address, |
|
|
|
final UInt256 treeIndex, |
|
|
|
final UInt256 treeIndex, |
|
|
|
final UInt256 subIndex, |
|
|
|
final UInt256 subIndex, |
|
|
|
final boolean isWrite) { |
|
|
|
final int accessMode) { |
|
|
|
|
|
|
|
final short accessEvents = touchAddress(address, treeIndex, subIndex, accessMode); |
|
|
|
AccessEvents accessEvent = touchAddress(address, treeIndex, subIndex, isWrite); |
|
|
|
|
|
|
|
boolean logEnabled = false; |
|
|
|
|
|
|
|
long gas = 0; |
|
|
|
long gas = 0; |
|
|
|
if (accessEvent.isBranchRead()) { |
|
|
|
if (AccessEvents.isBranchRead(accessEvents)) { |
|
|
|
gas = clampedAdd(gas, WITNESS_BRANCH_READ_COST); |
|
|
|
gas = clampedAdd(gas, WITNESS_BRANCH_COST); |
|
|
|
if (logEnabled) { |
|
|
|
final long gasView = gas; |
|
|
|
System.out.println( |
|
|
|
LOG.atDebug().log( |
|
|
|
"touchAddressAndChargeGas WitnessBranchReadCost " |
|
|
|
() -> |
|
|
|
|
|
|
|
"touchAddressAndChargeGas WITNESS_BRANCH_COST " |
|
|
|
+ address |
|
|
|
+ address |
|
|
|
+ " " |
|
|
|
+ " " |
|
|
|
+ treeIndex |
|
|
|
+ treeIndex |
|
|
|
+ " " |
|
|
|
+ " " |
|
|
|
+ subIndex |
|
|
|
+ subIndex |
|
|
|
+ " " |
|
|
|
+ " " |
|
|
|
+ isWrite |
|
|
|
+ AccessMode.toString(accessMode) |
|
|
|
+ " " |
|
|
|
+ " " |
|
|
|
+ gas); |
|
|
|
+ gasView); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
if (AccessEvents.isLeafRead(accessEvents)) { |
|
|
|
if (accessEvent.isChunkRead()) { |
|
|
|
gas = clampedAdd(gas, WITNESS_CHUNK_COST); |
|
|
|
gas = clampedAdd(gas, WITNESS_CHUNK_READ_COST); |
|
|
|
final long gasView = gas; |
|
|
|
if (logEnabled) { |
|
|
|
LOG.atDebug().log( |
|
|
|
System.out.println( |
|
|
|
() -> |
|
|
|
"touchAddressAndChargeGas WitnessChunkReadCost " |
|
|
|
"touchAddressAndChargeGas WITNESS_CHUNK_COST " |
|
|
|
+ address |
|
|
|
+ address |
|
|
|
+ " " |
|
|
|
+ " " |
|
|
|
+ treeIndex |
|
|
|
+ treeIndex |
|
|
|
+ " " |
|
|
|
+ " " |
|
|
|
+ subIndex |
|
|
|
+ subIndex |
|
|
|
+ " " |
|
|
|
+ " " |
|
|
|
+ isWrite |
|
|
|
+ AccessMode.toString(accessMode) |
|
|
|
+ " " |
|
|
|
+ " " |
|
|
|
+ gas); |
|
|
|
+ gasView); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
if (AccessEvents.isBranchWrite(accessEvents)) { |
|
|
|
if (accessEvent.isBranchWrite()) { |
|
|
|
gas = clampedAdd(gas, SUBTREE_EDIT_COST); |
|
|
|
gas = clampedAdd(gas, WITNESS_BRANCH_WRITE_COST); |
|
|
|
final long gasView = gas; |
|
|
|
if (logEnabled) { |
|
|
|
LOG.atDebug().log( |
|
|
|
System.out.println( |
|
|
|
() -> |
|
|
|
"touchAddressAndChargeGas WitnessBranchWriteCost " |
|
|
|
"touchAddressAndChargeGas SUBTREE_EDIT_COST " |
|
|
|
+ address |
|
|
|
+ address |
|
|
|
+ " " |
|
|
|
+ " " |
|
|
|
+ treeIndex |
|
|
|
+ treeIndex |
|
|
|
+ " " |
|
|
|
+ " " |
|
|
|
+ subIndex |
|
|
|
+ subIndex |
|
|
|
+ " " |
|
|
|
+ " " |
|
|
|
+ isWrite |
|
|
|
+ AccessMode.toString(accessMode) |
|
|
|
+ " " |
|
|
|
+ " " |
|
|
|
+ gas); |
|
|
|
+ gasView); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
if (AccessEvents.isLeafReset(accessEvents)) { |
|
|
|
if (accessEvent.isChunkWrite()) { |
|
|
|
gas = clampedAdd(gas, CHUNK_EDIT_COST); |
|
|
|
gas = clampedAdd(gas, WITNESS_CHUNK_WRITE_COST); |
|
|
|
final long gasView = gas; |
|
|
|
if (logEnabled) { |
|
|
|
LOG.atDebug().log( |
|
|
|
System.out.println( |
|
|
|
() -> |
|
|
|
"touchAddressAndChargeGas WitnessChunkWriteCost " |
|
|
|
"touchAddressAndChargeGas CHUNK_EDIT_COST " |
|
|
|
+ address |
|
|
|
+ address |
|
|
|
+ " " |
|
|
|
+ " " |
|
|
|
+ treeIndex |
|
|
|
+ treeIndex |
|
|
|
+ " " |
|
|
|
+ " " |
|
|
|
+ subIndex |
|
|
|
+ subIndex |
|
|
|
+ " " |
|
|
|
+ " " |
|
|
|
+ isWrite |
|
|
|
+ AccessMode.toString(accessMode) |
|
|
|
+ " " |
|
|
|
+ " " |
|
|
|
+ gas); |
|
|
|
+ gasView); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
if (AccessEvents.isLeafSet(accessEvents)) { |
|
|
|
if (accessEvent.isChunkFill()) { |
|
|
|
gas = clampedAdd(gas, CHUNK_FILL_COST); |
|
|
|
gas = clampedAdd(gas, WITNESS_CHUNK_FILL_COST); |
|
|
|
final long gasView = gas; |
|
|
|
if (logEnabled) { |
|
|
|
LOG.atDebug().log( |
|
|
|
System.out.println( |
|
|
|
() -> |
|
|
|
"touchAddressAndChargeGas WitnessChunkFillCost " |
|
|
|
"touchAddressAndChargeGas CHUNK_FILL_COST " |
|
|
|
+ address |
|
|
|
+ address |
|
|
|
+ " " |
|
|
|
+ " " |
|
|
|
+ treeIndex |
|
|
|
+ treeIndex |
|
|
|
+ " " |
|
|
|
+ " " |
|
|
|
+ subIndex |
|
|
|
+ subIndex |
|
|
|
+ " " |
|
|
|
+ " " |
|
|
|
+ isWrite |
|
|
|
+ AccessMode.toString(accessMode) |
|
|
|
+ " " |
|
|
|
+ " " |
|
|
|
+ gas); |
|
|
|
+ gasView); |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return gas; |
|
|
|
return gas; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public AccessEvents touchAddress( |
|
|
|
public short touchAddress( |
|
|
|
final Address addr, final UInt256 treeIndex, final UInt256 subIndex, final boolean isWrite) { |
|
|
|
final Address addr, final UInt256 treeIndex, final UInt256 leafIndex, final int accessMode) { |
|
|
|
AccessEvents accessEvents = new AccessEvents(); |
|
|
|
short accessEvents = AccessEvents.NONE; |
|
|
|
BranchAccessKey branchKey = new BranchAccessKey(addr, treeIndex); |
|
|
|
BranchAccessKey branchKey = new BranchAccessKey(addr, treeIndex); |
|
|
|
|
|
|
|
|
|
|
|
ChunkAccessKey chunkKey = new ChunkAccessKey(addr, treeIndex, subIndex); |
|
|
|
accessEvents |= touchAddressForBranch(branchKey, accessMode); |
|
|
|
|
|
|
|
if (leafIndex != null) { |
|
|
|
|
|
|
|
accessEvents |= touchAddressForLeaf(branchKey, leafIndex, accessMode); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return accessEvents; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private short touchAddressForBranch(final BranchAccessKey branchKey, final int accessMode) { |
|
|
|
|
|
|
|
short accessEvents = AccessEvents.NONE; |
|
|
|
|
|
|
|
|
|
|
|
// Read access.
|
|
|
|
|
|
|
|
if (!this.branches.containsKey(branchKey)) { |
|
|
|
if (!this.branches.containsKey(branchKey)) { |
|
|
|
accessEvents.setBranchRead(true); |
|
|
|
accessEvents |= AccessEvents.BRANCH_READ; |
|
|
|
this.branches.put(branchKey, AccessWitnessReadFlag); |
|
|
|
this.branches.put(branchKey, AccessMode.READ); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// A write is always a read
|
|
|
|
|
|
|
|
if (AccessMode.isWrite(accessMode)) { |
|
|
|
|
|
|
|
int previousAccessMode = |
|
|
|
|
|
|
|
!this.branches.containsKey(branchKey) ? AccessMode.NONE : this.branches.get(branchKey); |
|
|
|
|
|
|
|
if (!AccessMode.isWrite(previousAccessMode)) { |
|
|
|
|
|
|
|
accessEvents |= AccessEvents.BRANCH_WRITE; |
|
|
|
|
|
|
|
this.branches.put(branchKey, (previousAccessMode | accessMode)); |
|
|
|
} |
|
|
|
} |
|
|
|
if (!this.chunks.containsKey(chunkKey)) { |
|
|
|
|
|
|
|
accessEvents.setChunkRead(true); |
|
|
|
|
|
|
|
this.chunks.put(chunkKey, AccessWitnessReadFlag); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// TODO VERKLE: for now testnet doesn't charge
|
|
|
|
return accessEvents; |
|
|
|
// chunk filling costs if the leaf was previously empty in the state
|
|
|
|
} |
|
|
|
// boolean chunkFill = false;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (isWrite) { |
|
|
|
private short touchAddressForLeaf( |
|
|
|
|
|
|
|
final BranchAccessKey branchKey, final UInt256 subIndex, final int accessMode) { |
|
|
|
|
|
|
|
LeafAccessKey leafKey = new LeafAccessKey(branchKey, subIndex); |
|
|
|
|
|
|
|
short accessEvents = AccessEvents.NONE; |
|
|
|
|
|
|
|
|
|
|
|
if ((this.branches.get(branchKey) & AccessWitnessWriteFlag) == 0) { |
|
|
|
if (!this.leaves.containsKey(leafKey)) { |
|
|
|
accessEvents.setBranchWrite(true); |
|
|
|
accessEvents |= AccessEvents.LEAF_READ; |
|
|
|
this.branches.put( |
|
|
|
this.leaves.put(leafKey, AccessMode.READ); |
|
|
|
branchKey, (byte) (this.branches.get(branchKey) | AccessWitnessWriteFlag)); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
byte chunkValue = this.chunks.get(chunkKey); |
|
|
|
// A write is always a read
|
|
|
|
|
|
|
|
if (AccessMode.isWrite(accessMode)) { |
|
|
|
if ((chunkValue & AccessWitnessWriteFlag) == 0) { |
|
|
|
int previousAccessMode = |
|
|
|
accessEvents.setChunkWrite(true); |
|
|
|
!this.leaves.containsKey(leafKey) ? AccessMode.NONE : this.leaves.get(leafKey); |
|
|
|
this.chunks.put(chunkKey, (byte) (this.chunks.get(chunkKey) | AccessWitnessWriteFlag)); |
|
|
|
if (!AccessMode.isWrite(previousAccessMode)) { |
|
|
|
|
|
|
|
accessEvents |= AccessEvents.LEAF_RESET; |
|
|
|
|
|
|
|
if (AccessMode.isWriteSet(accessMode)) { |
|
|
|
|
|
|
|
accessEvents |= AccessEvents.LEAF_SET; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
this.leaves.put(leafKey, (previousAccessMode | accessMode)); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return accessEvents; |
|
|
|
return accessEvents; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -362,41 +436,20 @@ public class Eip4762AccessWitness implements org.hyperledger.besu.datatypes.Acce |
|
|
|
if (this == o) return true; |
|
|
|
if (this == o) return true; |
|
|
|
if (o == null || getClass() != o.getClass()) return false; |
|
|
|
if (o == null || getClass() != o.getClass()) return false; |
|
|
|
Eip4762AccessWitness that = (Eip4762AccessWitness) o; |
|
|
|
Eip4762AccessWitness that = (Eip4762AccessWitness) o; |
|
|
|
return Objects.equals(branches, that.branches) && Objects.equals(chunks, that.chunks); |
|
|
|
return Objects.equals(branches, that.branches) && Objects.equals(leaves, that.leaves); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public int hashCode() { |
|
|
|
public int hashCode() { |
|
|
|
return Objects.hash(branches, chunks); |
|
|
|
return Objects.hash(branches, leaves); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public String toString() { |
|
|
|
public String toString() { |
|
|
|
return "AccessWitness{" + "branches=" + branches + ", chunks=" + chunks + '}'; |
|
|
|
return "AccessWitness{" + "leaves=" + leaves + ", branches=" + branches + '}'; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public Map<BranchAccessKey, Byte> getBranches() { |
|
|
|
|
|
|
|
return branches; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public Map<ChunkAccessKey, Byte> getChunks() { |
|
|
|
|
|
|
|
return chunks; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public record BranchAccessKey(Address address, UInt256 treeIndex) {} |
|
|
|
public record BranchAccessKey(Address address, UInt256 treeIndex) {} |
|
|
|
; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public record ChunkAccessKey(BranchAccessKey branchAccessKey, UInt256 chunkIndex) { |
|
|
|
public record LeafAccessKey(BranchAccessKey branchAccessKey, UInt256 leafIndex) {} |
|
|
|
public ChunkAccessKey( |
|
|
|
|
|
|
|
final Address address, final UInt256 treeIndex, final UInt256 chunkIndex) { |
|
|
|
|
|
|
|
this(new BranchAccessKey(address, treeIndex), chunkIndex); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
|
|
public List<UInt256> getStorageSlotTreeIndexes(final UInt256 storageKey) { |
|
|
|
|
|
|
|
return List.of( |
|
|
|
|
|
|
|
TRIE_KEY_ADAPTER.locateStorageKeyOffset(storageKey), |
|
|
|
|
|
|
|
TRIE_KEY_ADAPTER.locateStorageKeySuffix(storageKey)); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|