Skip to content

Commit

Permalink
Adopt JSON test suite for logs hash instead of logs list
Browse files Browse the repository at this point in the history
  • Loading branch information
Nashatyrev committed Aug 24, 2017
1 parent 0768bb6 commit dc45947
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 122 deletions.
Expand Up @@ -17,6 +17,10 @@
*/
package org.ethereum.jsontestsuite.suite;

import org.ethereum.crypto.HashUtil;
import org.ethereum.util.ByteUtil;
import org.ethereum.util.FastByteComparisons;
import org.ethereum.util.RLP;
import org.ethereum.vm.DataWord;
import org.ethereum.vm.LogInfo;

Expand All @@ -26,14 +30,28 @@
import org.spongycastle.util.encoders.Hex;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

public class Logs {
List<LogInfo> logs = new ArrayList<>();
List<LogInfo> logs;
byte[] logsHash;

public Logs(Object logs) {
if (logs instanceof JSONArray) {
init((JSONArray) logs);
} else {
init((String) logs);
}
}

public Logs(JSONArray jLogs) {
private void init(String logsHash) {
this.logsHash = Utils.parseData(logsHash);
}

private void init(JSONArray jLogs) {
logs = new ArrayList<>();
for (Object jLog1 : jLogs) {

JSONObject jLog = (JSONObject) jLog1;
Expand All @@ -59,66 +77,78 @@ public Iterator<LogInfo> getIterator() {
}


public List<String> compareToReal(List<LogInfo> logs) {

public List<String> compareToReal(List<LogInfo> logResult) {
List<String> results = new ArrayList<>();

int i = 0;
for (LogInfo postLog : this.logs) {

LogInfo realLog = logs.get(i);

String postAddress = Hex.toHexString(postLog.getAddress());
String realAddress = Hex.toHexString(realLog.getAddress());

if (!postAddress.equals(realAddress)) {

String formattedString = String.format("Log: %s: has unexpected address, expected address: %s found address: %s",
i, postAddress, realAddress);
results.add(formattedString);
if (logsHash != null) {
byte[][] logInfoListE = new byte[logResult.size()][];
for (int i = 0; i < logResult.size(); i++) {
logInfoListE[i] = logResult.get(i).getEncoded();
}

String postData = Hex.toHexString(postLog.getData());
String realData = Hex.toHexString(realLog.getData());

if (!postData.equals(realData)) {

String formattedString = String.format("Log: %s: has unexpected data, expected data: %s found data: %s",
i, postData, realData);
results.add(formattedString);
byte[] logInfoListRLP = RLP.encodeList(logInfoListE);
byte[] resHash = HashUtil.sha3(logInfoListRLP);
if (!FastByteComparisons.equal(logsHash, resHash)) {
results.add("Logs hash doesn't match expected: " + Hex.toHexString(resHash) + " != " + Hex.toHexString(logsHash));
}

String postBloom = Hex.toHexString(postLog.getBloom().getData());
String realBloom = Hex.toHexString(realLog.getBloom().getData());

if (!postData.equals(realData)) {

String formattedString = String.format("Log: %s: has unexpected bloom, expected bloom: %s found bloom: %s",
i, postBloom, realBloom);
results.add(formattedString);
}

List<DataWord> postTopics = postLog.getTopics();
List<DataWord> realTopics = realLog.getTopics();

int j = 0;
for (DataWord postTopic : postTopics) {

DataWord realTopic = realTopics.get(j);

if (!postTopic.equals(realTopic)) {

String formattedString = String.format("Log: %s: has unexpected topic: %s, expected topic: %s found topic: %s",
i, j, postTopic, realTopic);
results.add(formattedString);
} else {
Iterator<LogInfo> postLogs = getIterator();
int i = 0;
while (postLogs.hasNext()) {

LogInfo expectedLogInfo = postLogs.next();

LogInfo foundLogInfo = null;
if (logResult.size() > i)
foundLogInfo = logResult.get(i);

if (foundLogInfo == null) {
String output =
String.format("Expected log [ %s ]", expectedLogInfo.toString());
results.add(output);
} else {
if (!Arrays.equals(expectedLogInfo.getAddress(), foundLogInfo.getAddress())) {
String output =
String.format("Expected address [ %s ], found [ %s ]", Hex.toHexString(expectedLogInfo.getAddress()), Hex.toHexString(foundLogInfo.getAddress()));
results.add(output);
}

if (!Arrays.equals(expectedLogInfo.getData(), foundLogInfo.getData())) {
String output =
String.format("Expected data [ %s ], found [ %s ]", Hex.toHexString(expectedLogInfo.getData()), Hex.toHexString(foundLogInfo.getData()));
results.add(output);
}

if (!expectedLogInfo.getBloom().equals(foundLogInfo.getBloom())) {
String output =
String.format("Expected bloom [ %s ], found [ %s ]",
Hex.toHexString(expectedLogInfo.getBloom().getData()),
Hex.toHexString(foundLogInfo.getBloom().getData()));
results.add(output);
}

if (expectedLogInfo.getTopics().size() != foundLogInfo.getTopics().size()) {
String output =
String.format("Expected number of topics [ %d ], found [ %d ]",
expectedLogInfo.getTopics().size(), foundLogInfo.getTopics().size());
results.add(output);
} else {
int j = 0;
for (DataWord topic : expectedLogInfo.getTopics()) {
byte[] foundTopic = foundLogInfo.getTopics().get(j).getData();

if (!Arrays.equals(topic.getData(), foundTopic)) {
String output =
String.format("Expected topic [ %s ], found [ %s ]", Hex.toHexString(topic.getData()), Hex.toHexString(foundTopic));
results.add(output);
}

++j;
}
}
}
++j;
}

++i;
++i;
}
}

return results;
}

}
Expand Up @@ -91,9 +91,9 @@ public TestCase(JSONObject testCaseJSONObj) throws ParseException {
if (testCaseJSONObj.containsKey("callcreates"))
callCreates = (JSONArray) testCaseJSONObj.get("callcreates");

JSONArray logsJSON = new JSONArray();
Object logsJSON = new JSONArray();
if (testCaseJSONObj.containsKey("logs"))
logsJSON = (JSONArray) testCaseJSONObj.get("logs");
logsJSON = testCaseJSONObj.get("logs");
logs = new Logs(logsJSON);

String gasString = "0";
Expand Down
Expand Up @@ -353,70 +353,8 @@ public List<String> runTestCase(TestCase testCase) {
/* asset logs */
List<LogInfo> logResult = program.getResult().getLogInfoList();

Iterator<LogInfo> postLogs = logs.getIterator();
int i = 0;
while (postLogs.hasNext()) {

LogInfo expectedLogInfo = postLogs.next();

LogInfo foundLogInfo = null;
if (logResult.size() > i)
foundLogInfo = logResult.get(i);

if (foundLogInfo == null) {
String output =
String.format("Expected log [ %s ]", expectedLogInfo.toString());
logger.info(output);
results.add(output);
} else {
if (!Arrays.equals(expectedLogInfo.getAddress(), foundLogInfo.getAddress())) {
String output =
String.format("Expected address [ %s ], found [ %s ]", Hex.toHexString(expectedLogInfo.getAddress()), Hex.toHexString(foundLogInfo.getAddress()));
logger.info(output);
results.add(output);
}

if (!Arrays.equals(expectedLogInfo.getData(), foundLogInfo.getData())) {
String output =
String.format("Expected data [ %s ], found [ %s ]", Hex.toHexString(expectedLogInfo.getData()), Hex.toHexString(foundLogInfo.getData()));
logger.info(output);
results.add(output);
}

if (!expectedLogInfo.getBloom().equals(foundLogInfo.getBloom())) {
String output =
String.format("Expected bloom [ %s ], found [ %s ]",
Hex.toHexString(expectedLogInfo.getBloom().getData()),
Hex.toHexString(foundLogInfo.getBloom().getData()));
logger.info(output);
results.add(output);
}

if (expectedLogInfo.getTopics().size() != foundLogInfo.getTopics().size()) {
String output =
String.format("Expected number of topics [ %d ], found [ %d ]",
expectedLogInfo.getTopics().size(), foundLogInfo.getTopics().size());
logger.info(output);
results.add(output);
} else {
int j = 0;
for (DataWord topic : expectedLogInfo.getTopics()) {
byte[] foundTopic = foundLogInfo.getTopics().get(j).getData();

if (!Arrays.equals(topic.getData(), foundTopic)) {
String output =
String.format("Expected topic [ %s ], found [ %s ]", Hex.toHexString(topic.getData()), Hex.toHexString(foundTopic));
logger.info(output);
results.add(output);
}

++j;
}
}
}

++i;
}
List<String> logResults = logs.compareToReal(logResult);
results.addAll(logResults);
}
}

Expand Down

0 comments on commit dc45947

Please sign in to comment.