Skip to content

Commit

Permalink
Merge branch 'master' into dependabot/github_actions/actions/setup-ja…
Browse files Browse the repository at this point in the history
…va-4.6.0
  • Loading branch information
fmacleal authored Jan 24, 2025
2 parents 43c4a3d + 95a8f1a commit 4590d15
Show file tree
Hide file tree
Showing 54 changed files with 5,584 additions and 209 deletions.
10 changes: 5 additions & 5 deletions gradle/verification-metadata.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,12 @@
<sha256 value="c717c468bfc91536f1dbc0d98d6116a8e7c49fbaff643ef8710e7505cc450878" origin="Generated by Gradle"/>
</artifact>
</component>
<component group="co.rsk.bitcoinj" name="bitcoinj-thin" version="0.14.4-rsk-16">
<artifact name="bitcoinj-thin-0.14.4-rsk-16.jar">
<sha256 value="53957d6941a882f7aa33188da3dadcf96f15fa2ffd62e9130937eb84e98f11a4" origin="Generated by Gradle"/>
<component group="co.rsk.bitcoinj" name="bitcoinj-thin" version="0.14.4-rsk-17">
<artifact name="bitcoinj-thin-0.14.4-rsk-17.jar">
<sha256 value="beff6002e72162984daa3e7016b1a6fb6d372e5cfb14101a50d150c0adfbda71" origin="Generated by Gradle"/>
</artifact>
<artifact name="bitcoinj-thin-0.14.4-rsk-16.pom">
<sha256 value="ea7726838385d7694f63622398d871b92a247b83418fe4d6cb9c6f3d5991fd3a" origin="Generated by Gradle"/>
<artifact name="bitcoinj-thin-0.14.4-rsk-17.pom">
<sha256 value="32fca4aac555f60d824cda58f69e64a7641f60dbbf5ec733966dbe2ded3214d7" origin="Generated by Gradle"/>
</artifact>
</component>
<component group="com.diffplug.durian" name="durian-collect" version="1.2.0">
Expand Down
2 changes: 1 addition & 1 deletion rskj-core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ ext {
jaxwsRtVer : '2.3.5',
picocliVer : '4.6.3',

bitcoinjThinVer: '0.14.4-rsk-17-SNAPSHOT',
bitcoinjThinVer: '0.14.4-rsk-17',
rskjNativeVer: '1.3.0',
]

Expand Down
6 changes: 3 additions & 3 deletions rskj-core/src/jmh/resources/conf/testnet-3_860_000.conf
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ sendRawTransaction.tx=0xf868826d28840387ee40825208947986b3df570230288501eea3d890
############################
# estimateGas
############################
estimateGas.from=0x05EEEF8B42D583880A24d74853669ee4a36bB530
estimateGas.to=0x51B631722323189ee8d3e4E558eC399d7aa40FcC
estimateGas.data=0xcbf83a0400000000000000000000000000000000000000000000000000000000000000034254435553440000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000409c969dacf4b38000000000000000000000000000005eeef8b42d583880a24d74853669ee4a36bb53000000000000000000000000000000000000000000000000000000000003567dd0000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000018000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000001b000000000000000000000000000000000000000000000000000000000000001c000000000000000000000000000000000000000000000000000000000000001c00000000000000000000000000000000000000000000000000000000000000038e9c434a2b164e2c07758f0b30af4cf4b5ad0dc98e8f13413b8beb36124574b82c84681feac9d982591f4701ba33e2949d1ae0df7363ac90d99d5c16f91a9f0c6c1131f84944ae1dd2bba3bbcf3e592a838286bbcf816247939548ee33ec23e200000000000000000000000000000000000000000000000000000000000000034821d173b4098826fb4d5a42dbfd4582de9ff98efabecb5596ac088ec387e9c765a5d167455ef2de2636857fde728c153e94e6406779063f6d95f76a85a07ef33a0ab20a151a912a49838ad9262524986595531bc0680a502ca04fe736414775
estimateGas.from=0x824dB056754BE52602CF9a2Ea1b8082Dad4dc89C
estimateGas.to=0xB913c7494b918Ed637D4df1308E1B7FF820b75B6
estimateGas.data=0x5a686699000000000000000000000000000000000000000000000000f438a7c30553000000000000000000000000000000000000000000000000000000000000645fa03b0000000000000000000000006951020041bfa2565877bf0eaf7f5df039b490dc

############################
# debug
Expand Down
24 changes: 20 additions & 4 deletions rskj-core/src/main/java/co/rsk/peg/bitcoin/BitcoinUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
import org.slf4j.LoggerFactory;

public class BitcoinUtils {
public static final byte WITNESS_COMMITMENT_LENGTH = 36; // 4 bytes for header, 32 for hash
public static final Sha256Hash WITNESS_RESERVED_VALUE = Sha256Hash.ZERO_HASH;
protected static final byte[] WITNESS_COMMITMENT_HEADER = Hex.decode("aa21a9ed");
protected static final int WITNESS_COMMITMENT_LENGTH = WITNESS_COMMITMENT_HEADER.length + Sha256Hash.LENGTH;
private static final int MINIMUM_WITNESS_COMMITMENT_SIZE = WITNESS_COMMITMENT_LENGTH + 2; // 1 extra by for OP_RETURN and another one for data length
private static final Logger logger = LoggerFactory.getLogger(BitcoinUtils.class);
private static final int FIRST_INPUT_INDEX = 0;

Expand Down Expand Up @@ -90,7 +90,7 @@ public static Optional<Sha256Hash> findWitnessCommitment(BtcTransaction tx) {
for (TransactionOutput output : outputsReversed) {
Script scriptPubKey = output.getScriptPubKey();
if (isWitnessCommitment(scriptPubKey)) {
Sha256Hash witnessCommitment = ScriptPattern.extractWitnessCommitmentHash(scriptPubKey);
Sha256Hash witnessCommitment = extractWitnessCommitmentHash(scriptPubKey);
return Optional.of(witnessCommitment);
}
}
Expand All @@ -99,7 +99,6 @@ public static Optional<Sha256Hash> findWitnessCommitment(BtcTransaction tx) {
}

private static boolean isWitnessCommitment(Script scriptPubKey) {
final int MINIMUM_WITNESS_COMMITMENT_SIZE = 38;
byte[] scriptPubKeyProgram = scriptPubKey.getProgram();

return scriptPubKeyProgram.length >= MINIMUM_WITNESS_COMMITMENT_SIZE
Expand All @@ -115,4 +114,21 @@ private static boolean hasCommitmentStructure(byte[] scriptPubKeyProgram) {
private static boolean hasWitnessCommitmentHeader(byte[] header) {
return Arrays.equals(header, WITNESS_COMMITMENT_HEADER);
}

/**
* Retrieves the hash from a segwit commitment (in an output of the coinbase transaction).
*/
private static Sha256Hash extractWitnessCommitmentHash(Script scriptPubKey) {
byte[] scriptPubKeyProgram = scriptPubKey.getProgram();
Preconditions.checkState(scriptPubKeyProgram.length >= MINIMUM_WITNESS_COMMITMENT_SIZE);

final int WITNESS_COMMITMENT_HASH_START = 6; // 4 bytes for header + OP_RETURN + data length
byte[] witnessCommitmentHash = Arrays.copyOfRange(
scriptPubKeyProgram,
WITNESS_COMMITMENT_HASH_START,
MINIMUM_WITNESS_COMMITMENT_SIZE
);

return Sha256Hash.wrap(witnessCommitmentHash);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,9 @@ public enum ConsensusRule {
RSKIP434("rskip434"),
RSKIP438("rskip438"),
RSKIP445("rskip445"), // From EIP-5656 MCOPY instruction
RSKIP446("rskip446") ,// Transient storage opcodes addition implementing EIP-1153
RSKIP453("rskip453"),
RSKIP454("rskip454"),
RSKIP454("rskip454")
;

private final String configKey;
Expand Down
2 changes: 1 addition & 1 deletion rskj-core/src/main/java/org/ethereum/core/Repository.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

import java.math.BigInteger;

public interface Repository extends RepositorySnapshot {
public interface Repository extends RepositorySnapshot, TransientRepository {
Trie getTrie();

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,8 @@ private void finalization() {
// Traverse list of suicides
result.getDeleteAccounts().forEach(address -> track.delete(new RskAddress(address)));

track.clearTransientStorage();

logger.trace("tx listener done");

logger.trace("tx finalization done");
Expand Down
40 changes: 40 additions & 0 deletions rskj-core/src/main/java/org/ethereum/core/TransientRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* This file is part of RskJ
* Copyright (C) 2024 RSK Labs Ltd.
* (derived from ethereumJ library, Copyright (c) 2016 <ether.camp>)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package org.ethereum.core;

import co.rsk.core.RskAddress;
import org.ethereum.vm.DataWord;

import javax.annotation.Nullable;

public interface TransientRepository {

void addTransientStorageRow(RskAddress addr, DataWord key, DataWord value);

void addTransientStorageBytes(RskAddress addr, DataWord key, byte[] value);

void clearTransientStorage();

@Nullable
DataWord getTransientStorageValue(RskAddress addr, DataWord key);

@Nullable
byte[] getTransientStorageBytes(RskAddress addr, DataWord key);
}
80 changes: 75 additions & 5 deletions rskj-core/src/main/java/org/ethereum/db/MutableRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,29 @@
import co.rsk.crypto.Keccak256;
import co.rsk.db.MutableTrieCache;
import co.rsk.db.MutableTrieImpl;
import co.rsk.trie.*;
import co.rsk.trie.IterationElement;
import co.rsk.trie.MutableTrie;
import co.rsk.trie.Trie;
import co.rsk.trie.TrieKeySlice;
import co.rsk.trie.TrieStore;
import co.rsk.trie.TrieStoreImpl;
import com.google.common.annotations.VisibleForTesting;
import org.ethereum.core.AccountState;
import org.ethereum.core.Repository;
import org.ethereum.crypto.HashUtil;
import org.ethereum.crypto.Keccak256Helper;
import org.ethereum.datasource.HashMapDB;
import org.ethereum.vm.DataWord;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.math.BigInteger;
import java.util.*;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Optional;
import java.util.Set;

public class MutableRepository implements Repository {
private static final Logger logger = LoggerFactory.getLogger("repository");
Expand All @@ -47,21 +57,36 @@ public class MutableRepository implements Repository {

private final TrieKeyMapper trieKeyMapper;
private final MutableTrie mutableTrie;
private MutableTrie transientTrie;
private final IReadWrittenKeysTracker tracker;

public MutableRepository(TrieStore trieStore, Trie trie) {
this(new MutableTrieImpl(trieStore, trie));
this(new MutableTrieImpl(trieStore, trie), aInMemoryMutableTrie());
}

public MutableRepository(MutableTrie mutableTrie) {
this(mutableTrie, aInMemoryMutableTrie());
}

public MutableRepository(MutableTrie mutableTrie, IReadWrittenKeysTracker tracker) {
this(mutableTrie, aInMemoryMutableTrie(), tracker);
}

private static MutableTrieImpl aInMemoryMutableTrie() {
return new MutableTrieImpl(new TrieStoreImpl(new HashMapDB()), new Trie());
}

public MutableRepository(MutableTrie mutableTrie, MutableTrie transientTrie) {
this.trieKeyMapper = new TrieKeyMapper();
this.mutableTrie = mutableTrie;
this.transientTrie = transientTrie;
this.tracker = new DummyReadWrittenKeysTracker();
}

public MutableRepository(MutableTrie mutableTrie, IReadWrittenKeysTracker tracker) {
public MutableRepository(MutableTrie mutableTrie, MutableTrie transientTrie, IReadWrittenKeysTracker tracker) {
this.trieKeyMapper = new TrieKeyMapper();
this.mutableTrie = mutableTrie;
this.transientTrie = transientTrie;
this.tracker = tracker;
}

Expand Down Expand Up @@ -327,7 +352,7 @@ public synchronized Set<RskAddress> getAccountsKeys() {
// To start tracking, a new repository is created, with a MutableTrieCache in the middle
@Override
public synchronized Repository startTracking() {
return new MutableRepository(new MutableTrieCache(mutableTrie), tracker);
return new MutableRepository(new MutableTrieCache(mutableTrie), new MutableTrieCache(transientTrie), tracker);
}

@Override
Expand All @@ -338,11 +363,13 @@ public void save() {
@Override
public synchronized void commit() {
mutableTrie.commit();
transientTrie.commit();
}

@Override
public synchronized void rollback() {
mutableTrie.rollback();
transientTrie.rollback();
}

@Override
Expand Down Expand Up @@ -406,4 +433,47 @@ private Optional<Keccak256> internalGetValueHash(byte[] key) {
tracker.addNewReadKey(new ByteArrayWrapper(key));
return mutableTrie.getValueHash(key);
}

@Override
public void addTransientStorageRow(RskAddress addr, DataWord key, DataWord value) {
addTransientStorageBytes(addr, key, value.getByteArrayForStorage());
}

@Override
public void addTransientStorageBytes(RskAddress addr, DataWord key, byte[] value) {
byte[] triekey = trieKeyMapper.getAccountStorageKey(addr, key);

// Special case: if the value is an empty vector, we pass "null" which commands the trie to remove the item.
// Note that if the call comes from addTransientStorageRow(), this method will already have replaced 0 by null, so the
// conversion here only applies if this is called directly. If suppose this only occurs in tests, but it can
// also occur in precompiled contracts that store data directly using this method.
if (value == null || value.length == 0) {
transientTrie.put(triekey, null);
} else {
transientTrie.put(triekey, value);
}
}

@Override
public void clearTransientStorage() {
this.transientTrie = aInMemoryMutableTrie();
}

@Nullable
@Override
public DataWord getTransientStorageValue(RskAddress addr, DataWord key) {
byte[] value = getTransientStorageBytes(addr, key);
if (value == null) {
return null;
}

return DataWord.valueOf(value);
}

@Nullable
@Override
public byte[] getTransientStorageBytes(RskAddress addr, DataWord key) {
byte[] triekey = trieKeyMapper.getAccountStorageKey(addr, key);
return transientTrie.get(triekey);
}
}
3 changes: 3 additions & 0 deletions rskj-core/src/main/java/org/ethereum/vm/GasCost.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ public class GasCost {
public static final long REFUND_SSTORE = 15000;
public static final long CREATE = 32000;

public static final long TLOAD = 100;
public static final long TSTORE = 100;

public static final long JUMPDEST = 1;
public static final long CREATE_DATA_BYTE = 5;
public static final long CALL = 700;
Expand Down
10 changes: 10 additions & 0 deletions rskj-core/src/main/java/org/ethereum/vm/OpCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,16 @@ public enum OpCode {
*/
MCOPY(0x5e, 3, 0, VERY_LOW_TIER),

/**
* (0x5c) Load word from transient storage at address
*/
TLOAD(0x5c, 1, 1, SPECIAL_TIER), // Will adjust the correct inputs and outputs later

/**
* (0x5c) Store word from transient storage at address
*/
TSTORE(0x5d, 2, 0, SPECIAL_TIER), // Will adjust the correct inputs and outputs later

/* Push Operations */
/**
* (0x5f) Pushes the constant value 0 onto the stack.
Expand Down
8 changes: 8 additions & 0 deletions rskj-core/src/main/java/org/ethereum/vm/OpCodes.java
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,14 @@ private OpCodes() {
* (0x5e)
*/
public static final byte OP_MCOPY = 0x5e;
/**
* (0x5c)
*/
public static final byte OP_TLOAD =0x5c ;
/**
* (0x5d)
*/
public static final byte OP_TSTORE =0x5d ;

/* Push Operations */
/**
Expand Down
Loading

0 comments on commit 4590d15

Please sign in to comment.