Skip to content

Commit

Permalink
get proof with account proof
Browse files Browse the repository at this point in the history
  • Loading branch information
fedejinich committed May 18, 2021
1 parent 943958c commit 0739143
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import javax.annotation.Nullable;
import java.math.BigInteger;
import java.util.Iterator;
import java.util.List;

public interface AccountInformationProvider {

Expand Down Expand Up @@ -104,4 +105,17 @@ public interface AccountInformationProvider {
* @return Keccak256(storageRoot)
* */
Keccak256 getStorageHash(RskAddress addr);

/**
* According to the EIP-1186 https://eips.ethereum.org/EIPS/eip-1186
*
* Retrieves an account proof for a given address
* An account proof represents all the nodes starting from the state root following the path by the given the address.
* Each node is RLP encoded.
* NOTE: the value is also included at the end of the list
*
* @param addr an address
* @return a list of proofs for a given account
* */
List<String> getAccountProof(RskAddress addr);
}
5 changes: 5 additions & 0 deletions rskj-core/src/main/java/co/rsk/core/bc/PendingState.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,11 @@ public Keccak256 getStorageHash(RskAddress addr) {
return postExecutionReturn(executedRepository -> executedRepository.getStorageHash(addr));
}

@Override
public List<String> getAccountProof(RskAddress addr) {
return postExecutionReturn(executedRepository -> executedRepository.getAccountProof(addr));
}

// sortByPriceTakingIntoAccountSenderAndNonce sorts the transactions by price, but
// first clustering by sender and then each cluster is order by nonce.
//
Expand Down
5 changes: 5 additions & 0 deletions rskj-core/src/main/java/co/rsk/db/MutableTrieCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,11 @@ public Iterator<DataWord> getStorageKeys(RskAddress addr) {
return new StorageKeysIterator(storageKeys, accountItems, addr, trieKeyMapper);
}

@Override
public List<Trie> getNodes(byte[] key) {
return trie.getNodes(key);
}

// This method returns a wrapper with the same content and size expected for a account key
// when the key is from the same size than the original wrapper, it returns the same object
private ByteArrayWrapper getAccountWrapper(ByteArrayWrapper originalWrapper) {
Expand Down
5 changes: 5 additions & 0 deletions rskj-core/src/main/java/co/rsk/db/MutableTrieImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,11 @@ public Iterator<DataWord> getStorageKeys(RskAddress addr) {
return Collections.emptyIterator();
}

@Override
public List<Trie> getNodes(byte[] key) {
return trie.getNodes(key);
}

@Override
public void deleteRecursive(byte[] key) {
trie = trie.deleteRecursive(key);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,8 @@ public ProofDTO getProof(String address, List<String> storageKeys, String blockO
String codeHash = accountInformationProvider.isContract(rskAddress) ?
toUnformattedJsonHex(accountInformationProvider.getCode(rskAddress)) :
"0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470"; // todo(fedejinich) extract constant
List<String> accountProof = accountInformationProvider.getAccountProof(rskAddress);

return new ProofDTO(balance, codeHash, nonce, storageHash, null, null);
return new ProofDTO(balance, codeHash, nonce, storageHash, accountProof, null);
}
}
6 changes: 3 additions & 3 deletions rskj-core/src/main/java/co/rsk/trie/MutableTrie.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@
import org.ethereum.vm.DataWord;

import javax.annotation.Nullable;
import java.util.Iterator;
import java.util.Optional;
import java.util.Set;
import java.util.*;

/**
* Every operation of a MutableTrie mutates the parent trie top node and therefore its stateRoot.
Expand Down Expand Up @@ -72,4 +70,6 @@ public interface MutableTrie {
// the key has to match exactly an account key
// it won't work if it is used with an storage key or any other
Iterator<DataWord> getStorageKeys(RskAddress addr);

List<Trie> getNodes(byte[] key);
}
22 changes: 21 additions & 1 deletion rskj-core/src/main/java/org/ethereum/db/MutableRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,16 @@
import org.ethereum.core.Repository;
import org.ethereum.crypto.HashUtil;
import org.ethereum.crypto.Keccak256Helper;
import org.ethereum.util.ByteUtil;
import org.ethereum.util.RLP;
import org.ethereum.vm.DataWord;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sun.reflect.generics.reflectiveObjects.NotImplementedException;

import javax.annotation.Nonnull;
import java.math.BigInteger;
import java.util.*;
import java.util.stream.Collectors;

public class MutableRepository implements Repository {
private static final Logger logger = LoggerFactory.getLogger("repository");
Expand Down Expand Up @@ -353,6 +355,24 @@ public Keccak256 getStorageHash(RskAddress addr) {
return new Keccak256(storageRoot);
}

@Override
public List<String> getAccountProof(RskAddress addr) {
byte[] accountKey = trieKeyMapper.getAccountKey(addr);

List<String> accountKeys = mutableTrie
.getNodes(accountKey)
.stream()
.map(this::toAccountProof)
.collect(Collectors.toList());

return accountKeys;
}

private String toAccountProof(Trie trie) {
byte[] rlpEncodedNode = RLP.encodeElement(trie.toMessage());
return ByteUtil.toHexString(rlpEncodedNode);
}

@VisibleForTesting
public byte[] getStorageStateRoot(RskAddress addr) {
byte[] prefix = trieKeyMapper.getAccountStoragePrefixKey(addr);
Expand Down
6 changes: 6 additions & 0 deletions rskj-core/src/main/java/org/ethereum/vm/program/Storage.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

import java.math.BigInteger;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

/*
Expand Down Expand Up @@ -184,6 +185,11 @@ public Keccak256 getStorageHash(RskAddress addr) {
return repository.getStorageHash(addr);
}

@Override
public List<String> getAccountProof(RskAddress addr) {
throw new UnsupportedOperationException("Shouldn't get account proof from Storage");
}

@Override
public Coin getBalance(RskAddress addr) {
return repository.getBalance(addr);
Expand Down

0 comments on commit 0739143

Please sign in to comment.