log tests

This commit is contained in:
alon muroch 2014-12-04 10:15:01 +01:00
parent a2b00ff8dd
commit a88ec65459
4 changed files with 58 additions and 15 deletions

View File

@ -7,6 +7,7 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import org.ethereum.vm.DataWord;
import org.ethereum.vm.LogInfo; import org.ethereum.vm.LogInfo;
import org.json.simple.JSONArray; import org.json.simple.JSONArray;
import org.json.simple.JSONObject; import org.json.simple.JSONObject;
@ -26,12 +27,12 @@ public class Logs {
byte[] address = Hex.decode((String)values.get("address")); byte[] address = Hex.decode((String)values.get("address"));
byte[] data = Hex.decode(((String)values.get("data")).substring(2)); byte[] data = Hex.decode(((String)values.get("data")).substring(2));
List<byte[]> topics = new ArrayList<byte[]>(); List<DataWord> topics = new ArrayList<DataWord>();
JSONArray jTopics = (JSONArray)values.get("topics"); JSONArray jTopics = (JSONArray)values.get("topics");
for(Object t: jTopics.toArray()) { for(Object t: jTopics.toArray()) {
byte[] topic = Hex.decode(((String)t)); byte[] topic = Hex.decode(((String)t));
topics.add(topic); topics.add(new DataWord(topic));
} }
LogInfo li = new LogInfo(address, topics, data); LogInfo li = new LogInfo(address, topics, data);

View File

@ -232,13 +232,15 @@ public class TestRunner {
byte[] expectedLogKey = itr.next(); byte[] expectedLogKey = itr.next();
System.out.println("Expected key " + Hex.toHexString(expectedLogKey)); System.out.println("Expected key " + Hex.toHexString(expectedLogKey));
LogInfo expectedLogInfo = logs.getLogBloom(expectedLogKey); LogInfo expectedLogInfo = logs.getLogBloom(expectedLogKey);
LogInfo foundLogInfo = null;
boolean found = false; boolean found = false;
for(LogInfo resultLogInfo:logResult) { for(LogInfo resultLogInfo:logResult) {
byte[] resultKey = resultLogInfo.getBloom().getData(); byte[] resultKey = resultLogInfo.getBloom().getData();
System.out.println("returned key " + Hex.toHexString(resultKey)); System.out.println("returned key " + Hex.toHexString(resultKey));
if(Arrays.equals(expectedLogKey, resultKey)) { if(Arrays.equals(expectedLogKey, resultKey)) {
found = true; found = true;
foundLogInfo = resultLogInfo;
break;
} }
} }
@ -248,6 +250,43 @@ public class TestRunner {
logger.info(output); logger.info(output);
results.add(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.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 i=0;
for(DataWord topic: expectedLogInfo.getTopics()) {
byte[] foundTopic = foundLogInfo.getTopics().get(i).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);
}
i++;
}
}
}
} }
} }

View File

@ -21,15 +21,15 @@ import java.util.List;
public class LogInfo { public class LogInfo {
byte[] address; byte[] address;
List<byte[]> topics = new ArrayList<>(); List<DataWord> topics = new ArrayList<DataWord>();
byte[] data; byte[] data;
/* Log info in encoded form */ /* Log info in encoded form */
private byte[] rlpEncoded; private byte[] rlpEncoded;
public LogInfo(byte[] address, List<byte[]> topics, byte[] data) { public LogInfo(byte[] address, List<DataWord> topics, byte[] data) {
this.address = address; this.address = address;
this.topics = (topics == null) ? new ArrayList<byte[]>() : topics; this.topics = (topics == null) ? new ArrayList<DataWord>() : topics;
this.data = data; this.data = data;
} }
@ -37,7 +37,7 @@ public class LogInfo {
return address; return address;
} }
public List<byte[]> getTopics() { public List<DataWord> getTopics() {
return topics; return topics;
} }
@ -55,8 +55,9 @@ public class LogInfo {
if (topics != null){ if (topics != null){
topicsEncoded = new byte[topics.size()][]; topicsEncoded = new byte[topics.size()][];
int i = 0; int i = 0;
for( byte[] topic : topics ){ for( DataWord topic : topics ){
topicsEncoded[i] = topic; byte[] topicData = topic.getData();
topicsEncoded[i] = topicData;
++i; ++i;
} }
} }
@ -67,8 +68,9 @@ public class LogInfo {
public Bloom getBloom() { public Bloom getBloom() {
Bloom ret = Bloom.create(HashUtil.sha3(address)); Bloom ret = Bloom.create(HashUtil.sha3(address));
for(byte[] topic:topics) { for(DataWord topic:topics) {
ret.or(Bloom.create(HashUtil.sha3(topic))); byte[] topicData = topic.getData();
ret.or(Bloom.create(HashUtil.sha3(topicData)));
} }
return ret; return ret;
@ -80,8 +82,8 @@ public class LogInfo {
StringBuffer topicsStr = new StringBuffer(); StringBuffer topicsStr = new StringBuffer();
topicsStr.append("["); topicsStr.append("[");
for (byte[] topic: topics){ for (DataWord topic: topics){
String topicStr = Hex.toHexString(topic); String topicStr = Hex.toHexString(topic.getData());
topicsStr.append(topicStr).append(" "); topicsStr.append(topicStr).append(" ");
} }
topicsStr.append("]"); topicsStr.append("]");

View File

@ -751,10 +751,11 @@ public class VM {
int nTopics = op.val() - OpCode.LOG0.val(); int nTopics = op.val() - OpCode.LOG0.val();
List<byte[]> topics = new ArrayList<>(); List<DataWord> topics = new ArrayList<DataWord>();
for (int i = 0; i < nTopics; ++i){ for (int i = 0; i < nTopics; ++i){
DataWord topic = stack.pop(); DataWord topic = stack.pop();
topics.add(topic.getData()); if(!topics.contains(topic))
topics.add(topic);
} }
ByteBuffer data = program.memoryChunk(memStart, memOffset); ByteBuffer data = program.memoryChunk(memStart, memOffset);