Skip to content

Commit

Permalink
hundreds of state tests added again.
Browse files Browse the repository at this point in the history
  • Loading branch information
SergioDemianLerner authored and lsebrie committed May 6, 2019
1 parent 86f4b67 commit c486d6c
Show file tree
Hide file tree
Showing 12 changed files with 294 additions and 97 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
"createWithInvalidOpcode": {
"env" : {
"currentCoinbase" : "2adc25665018aa1fe0e6bc666dac8fc2697ff9ba",
"currentDifficulty" : "115792089237316195423570985008687907853269984665640564039457584007913129639935",
"currentDifficulty" : "0x1fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
"currentGasLimit" : "1000000",
"currentNumber" : "2463000",
"currentTimestamp" : "2",
Expand Down
205 changes: 170 additions & 35 deletions rskj-core/src/test/java/co/rsk/jsontestsuite/LocalStateTest.java

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -263,8 +263,9 @@ public static void runStateTest(String jsonSuite, Set<String> excluded) throws I
List<String> result = StateTestRunner.run(testCases.get(testName));


if (!result.isEmpty())
if (!result.isEmpty()) {
summary.put(testName, false);
}
else
summary.put(testName, true);
}
Expand Down
37 changes: 28 additions & 9 deletions rskj-core/src/test/java/org/ethereum/jsontestsuite/TestRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import org.ethereum.jsontestsuite.model.TransactionTck;
import org.ethereum.jsontestsuite.validators.BlockHeaderValidator;
import org.ethereum.jsontestsuite.validators.RepositoryValidator;
import org.ethereum.jsontestsuite.validators.ValidationStats;
import org.ethereum.listener.CompositeEthereumListener;
import org.ethereum.listener.TestCompositeEthereumListener;
import org.ethereum.util.ByteUtil;
Expand Down Expand Up @@ -86,6 +87,23 @@ public class TestRunner {
private ProgramTrace trace = null;
private boolean setNewStateRoot;
private boolean validateGasUsed = false; // until EIP150 test cases are ready.
private boolean validateBalances = true;
private boolean validateStateRoots =false;

public TestRunner setValidateGasUsed( boolean v) {
validateGasUsed= v;
return this;
}

public TestRunner setValidateStateRoots ( boolean v) {
validateStateRoots = v;
return this;
}

public TestRunner setValidateBalances(boolean v) {
validateBalances = v;
return this;
}

public List<String> runTestSuite(TestSuite testSuite) {

Expand All @@ -107,6 +125,8 @@ public List<String> runTestSuite(TestSuite testSuite) {

public List<String> runTestCase(BlockTestCase testCase) {
/* 1 */ // Create genesis + init pre state
ValidationStats vStats = new ValidationStats();

Block genesis = build(testCase.getGenesisBlockHeader(), null, null);
Repository repository = RepositoryBuilder.build(testCase.getPre());

Expand Down Expand Up @@ -165,7 +185,7 @@ public List<String> runTestCase(BlockTestCase testCase) {
tBlock = blockFactory.decodeBlock(rlp);

ArrayList<String> outputSummary =
BlockHeaderValidator.valid(tBlock.getHeader(), block.getHeader());
BlockHeaderValidator.valid(tBlock.getHeader(), block.getHeader(),null);

if (!outputSummary.isEmpty()){
for (String output : outputSummary)
Expand Down Expand Up @@ -193,17 +213,16 @@ public List<String> runTestCase(BlockTestCase testCase) {
byte[] bestHash = Hex.decode(testCase.getLastblockhash());
String finalRoot = Hex.toHexString(blockStore.getBlockByHash(bestHash).getStateRoot());

/*
if (!blockchain.byTest) // If this comes from ETH, it won't match
if (!finalRoot.equals(currRoot)){
String formattedString = String.format("Root hash doesn't match best: expected: %s current: %s",
finalRoot, currRoot);
results.add(formattedString);
if (validateStateRoots) {
if (!finalRoot.equals(currRoot)) {
String formattedString = String.format("Root hash doesn't match best: expected: %s current: %s",
finalRoot, currRoot);
results.add(formattedString);
}
}
*/

Repository postRepository = RepositoryBuilder.build(testCase.getPostState());
List<String> repoResults = RepositoryValidator.valid(repository, postRepository, false /*!blockchain.byTest*/);
List<String> repoResults = RepositoryValidator.valid(repository, postRepository, validateStateRoots,validateBalances,null);
results.addAll(repoResults);

return results;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.ethereum.jsontestsuite.validators.LogsValidator;
import org.ethereum.jsontestsuite.validators.OutputValidator;
import org.ethereum.jsontestsuite.validators.RepositoryValidator;
import org.ethereum.jsontestsuite.validators.ValidationStats;
import org.ethereum.listener.EthereumListenerAdapter;
import org.ethereum.util.ByteUtil;
import org.ethereum.vm.LogInfo;
Expand Down Expand Up @@ -74,6 +75,7 @@ public static List<String> run(StateTestCase stateTestCase2) {
protected Env env;
protected ProgramInvokeFactory invokeFactory;
protected Block block;
protected ValidationStats vStats;

// Enable when State tests are changed to support paying
// the fees into REMASC instead that into the coinbase account
Expand Down Expand Up @@ -128,7 +130,7 @@ protected ProgramResult executeTransaction(Transaction tx) {
}

public List<String> runImpl() {

vStats = new ValidationStats();
logger.info("");
repository = RepositoryBuilder.build(stateTestCase.getPre());
logger.info("loaded repository");
Expand Down Expand Up @@ -180,14 +182,16 @@ public List<String> runImpl() {
List<LogInfo> origLogs = programResult.getLogInfoList();
List<LogInfo> postLogs = LogBuilder.build(stateTestCase.getLogs());

List<String> logsResult = LogsValidator.valid(origLogs, postLogs);
List<String> logsResult = LogsValidator.valid(origLogs, postLogs,vStats);

Repository postRepository = RepositoryBuilder.build(stateTestCase.getPost());
List<String> repoResults = RepositoryValidator.valid(repository, postRepository, false /*!blockchain.byTest*/);

// Balances cannot be validated because has consumption for CALLs differ.
List<String> repoResults = RepositoryValidator.valid(repository, postRepository, false ,false,vStats);

logger.info("--------- POST Validation---------");
List<String> outputResults =
OutputValidator.valid(Hex.toHexString(programResult.getHReturn()), stateTestCase.getOut());
OutputValidator.valid(Hex.toHexString(programResult.getHReturn()), stateTestCase.getOut(),vStats);

List<String> results = new ArrayList<>();
results.addAll(repoResults);
Expand All @@ -198,14 +202,25 @@ public List<String> runImpl() {
logger.error(result);
}

if ((vStats.storageChecks==0) && (vStats.logChecks==0) &&
(vStats.balancetChecks==0) && (vStats.outputChecks==0) &&
(vStats.blockChecks==0)) {
// This generally mean that the test didn't check anything
// AccountChecks are considered not indicative of the result of the test
logger.info("IRRELEVANT\n");
}
logger.info("\n\n");
return results;
}

public static final byte[] ZERO32_BYTE_ARRAY = new byte[32];
public Block build(Env env) {
return new Block(
blockFactory.newHeader(
ByteUtil.EMPTY_BYTE_ARRAY, ByteUtil.EMPTY_BYTE_ARRAY, env.getCurrentCoinbase(),
// Don't use the empty parent hash because it's used to log and
// when log entries are printed with empty parent hash it throws
// an exception.
ZERO32_BYTE_ARRAY , ByteUtil.EMPTY_BYTE_ARRAY, env.getCurrentCoinbase(),
EMPTY_TRIE_HASH, EMPTY_TRIE_HASH, EMPTY_TRIE_HASH,
ByteUtil.EMPTY_BYTE_ARRAY, env.getCurrentDifficulty(), byteArrayToLong(env.getCurrentNumber()),
env.getCurrentGasLimit(), 0L, byteArrayToLong(env.getCurrentTimestamp()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,34 +35,38 @@
public class AccountValidator {
private static final byte[] EMPTY_DATA_HASH = HashUtil.keccak256(EMPTY_BYTE_ARRAY);

public static List<String> valid(RskAddress addr, Repository currentRepository, Repository expectedRepository) {
public static List<String> valid(RskAddress addr, Repository currentRepository, Repository expectedRepository,boolean validateBalance,ValidationStats vStats) {
AccountState currentState = currentRepository.getAccountState(addr);
AccountState expectedState = expectedRepository.getAccountState(addr);

List<String> results = new ArrayList<>();

if (vStats!=null) vStats.accountChecks++;
if (currentState == null || !currentRepository.isContract(addr)) {
String formattedString = String.format("Account: %s: expected but doesn't exist",
addr);
results.add(formattedString);
return results;
}

if (vStats!=null) vStats.accountChecks++;
if (expectedState == null || !expectedRepository.isContract(addr)) {
String formattedString = String.format("Account: %s: unexpected account in the repository",
addr);
results.add(formattedString);
return results;
}


Coin expectedBalance = expectedState.getBalance();
if (!currentState.getBalance().equals(expectedBalance)) {
String formattedString = String.format("Account: %s: has unexpected balance, expected balance: %s found balance: %s",
addr, expectedBalance.toString(), currentState.getBalance().toString());
results.add(formattedString);
if (validateBalance) {
if (vStats!=null) vStats.balancetChecks++;
Coin expectedBalance = expectedState.getBalance();
if (!currentState.getBalance().equals(expectedBalance)) {
String formattedString = String.format("Account: %s: has unexpected balance, expected balance: %s found balance: %s",
addr, expectedBalance.toString(), currentState.getBalance().toString());
results.add(formattedString);
}
}

if (vStats!=null) vStats.accountChecks++;
BigInteger expectedNonce = expectedState.getNonce();
if (currentState.getNonce().compareTo(expectedNonce) != 0) {
String formattedString = String.format("Account: %s: has unexpected nonce, expected nonce: %s found nonce: %s",
Expand All @@ -72,6 +76,8 @@ public static List<String> valid(RskAddress addr, Repository currentRepository,

byte[] code = Arrays.equals(currentState.getCodeHash(), EMPTY_DATA_HASH) ?
new byte[0] : currentRepository.getCode(addr);

if (vStats!=null) vStats.accountChecks++;
if (!Arrays.equals(expectedRepository.getCode(addr), code)) {
String formattedString = String.format("Account: %s: has unexpected code, expected code: %s found code: %s",
addr, Hex.toHexString(expectedRepository.getCode(addr)), Hex.toHexString(currentRepository.getCode(addr)));
Expand All @@ -89,6 +95,7 @@ public static List<String> valid(RskAddress addr, Repository currentRepository,

DataWord currentValue = currentRepository.getStorageValue(addr, key);
DataWord expectedValue = expectedRepository.getStorageValue(addr, key);
if (vStats!=null) vStats.storageChecks++;
if (expectedValue == null) {

String formattedString = String.format("Account: %s: has unexpected storage data: %s = %s",
Expand All @@ -99,7 +106,7 @@ public static List<String> valid(RskAddress addr, Repository currentRepository,
results.add(formattedString);
continue;
}

if (vStats!=null) vStats.storageChecks++;
if (!expectedValue.equals(currentValue)) {

String formattedString = String.format("Account: %s: has unexpected value, for key: %s , expectedValue: %s real value: %s",
Expand All @@ -115,9 +122,9 @@ public static List<String> valid(RskAddress addr, Repository currentRepository,

while (expectedKeys.hasNext()) {
DataWord key = expectedKeys.next();

if (vStats!=null) vStats.storageChecks++;
if (!checked.contains(key)) {
String formattedString = String.format("Account: %s: doesn't exist expected storage key: %s",
String formattedString = String.format("Account: %s: doesn't exist. Expected storage key: %s",
addr, key.toString());
results.add(formattedString);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@
public class BlockHeaderValidator {


public static ArrayList<String> valid(BlockHeader orig, BlockHeader valid) {
public static ArrayList<String> valid(BlockHeader orig, BlockHeader valid,ValidationStats vStats) {

ArrayList<String> outputSummary = new ArrayList<>();

if (vStats!=null) vStats.blockChecks++;
if (!orig.getParentHash().equals(valid.getParentHash())) {

String output =
Expand All @@ -43,7 +43,7 @@ public static ArrayList<String> valid(BlockHeader orig, BlockHeader valid) {

outputSummary.add(output);
}

if (vStats!=null) vStats.blockChecks++;
if (!toHexString(orig.getUnclesHash())
.equals(toHexString(valid.getUnclesHash()))) {

Expand All @@ -55,7 +55,7 @@ public static ArrayList<String> valid(BlockHeader orig, BlockHeader valid) {

outputSummary.add(output);
}

if (vStats!=null) vStats.blockChecks++;
if (!orig.getCoinbase().equals(valid.getCoinbase())) {

String output =
Expand All @@ -66,7 +66,7 @@ public static ArrayList<String> valid(BlockHeader orig, BlockHeader valid) {

outputSummary.add(output);
}

if (vStats!=null) vStats.blockChecks++;
if (!toHexString(orig.getStateRoot())
.equals(toHexString(valid.getStateRoot()))) {

Expand All @@ -78,7 +78,7 @@ public static ArrayList<String> valid(BlockHeader orig, BlockHeader valid) {

outputSummary.add(output);
}

if (vStats!=null) vStats.blockChecks++;
if (!toHexString(orig.getTxTrieRoot())
.equals(toHexString(valid.getTxTrieRoot()))) {

Expand All @@ -90,7 +90,7 @@ public static ArrayList<String> valid(BlockHeader orig, BlockHeader valid) {

outputSummary.add(output);
}

if (vStats!=null) vStats.blockChecks++;
if (!toHexString(orig.getReceiptsRoot())
.equals(toHexString(valid.getReceiptsRoot()))) {

Expand All @@ -102,7 +102,7 @@ public static ArrayList<String> valid(BlockHeader orig, BlockHeader valid) {

outputSummary.add(output);
}

if (vStats!=null) vStats.blockChecks++;
if (!toHexString(orig.getLogsBloom())
.equals(toHexString(valid.getLogsBloom()))) {

Expand All @@ -114,7 +114,7 @@ public static ArrayList<String> valid(BlockHeader orig, BlockHeader valid) {

outputSummary.add(output);
}

if (vStats!=null) vStats.blockChecks++;
if (!orig.getDifficulty().equals(valid.getDifficulty())) {

String output =
Expand All @@ -125,7 +125,7 @@ public static ArrayList<String> valid(BlockHeader orig, BlockHeader valid) {

outputSummary.add(output);
}

if (vStats!=null) vStats.blockChecks++;
if (orig.getTimestamp() != valid.getTimestamp()) {

String output =
Expand All @@ -136,7 +136,7 @@ public static ArrayList<String> valid(BlockHeader orig, BlockHeader valid) {

outputSummary.add(output);
}

if (vStats!=null) vStats.blockChecks++;
if (orig.getNumber() != valid.getNumber()) {

String output =
Expand All @@ -147,7 +147,7 @@ public static ArrayList<String> valid(BlockHeader orig, BlockHeader valid) {

outputSummary.add(output);
}

if (vStats!=null) vStats.blockChecks++;
if (!new BigInteger(1, orig.getGasLimit()).equals(new BigInteger(1, valid.getGasLimit()))) {

String output =
Expand All @@ -158,7 +158,7 @@ public static ArrayList<String> valid(BlockHeader orig, BlockHeader valid) {

outputSummary.add(output);
}

if (vStats!=null) vStats.blockChecks++;
if (orig.getGasUsed() != valid.getGasUsed()) {

String output =
Expand All @@ -169,7 +169,7 @@ public static ArrayList<String> valid(BlockHeader orig, BlockHeader valid) {

outputSummary.add(output);
}

if (vStats!=null) vStats.blockChecks++;
if (!toHexString(orig.getExtraData())
.equals(toHexString(valid.getExtraData()))) {

Expand Down
Loading

0 comments on commit c486d6c

Please sign in to comment.