From a88ec65459cbaee92870321850bcc48832713d87 Mon Sep 17 00:00:00 2001 From: alon muroch Date: Thu, 4 Dec 2014 10:15:01 +0100 Subject: [PATCH] log tests --- .../java/org/ethereum/jsontestsuite/Logs.java | 5 ++- .../ethereum/jsontestsuite/TestRunner.java | 41 ++++++++++++++++++- .../main/java/org/ethereum/vm/LogInfo.java | 22 +++++----- .../src/main/java/org/ethereum/vm/VM.java | 5 ++- 4 files changed, 58 insertions(+), 15 deletions(-) diff --git a/ethereumj-core/src/main/java/org/ethereum/jsontestsuite/Logs.java b/ethereumj-core/src/main/java/org/ethereum/jsontestsuite/Logs.java index 9542103e..5964d774 100644 --- a/ethereumj-core/src/main/java/org/ethereum/jsontestsuite/Logs.java +++ b/ethereumj-core/src/main/java/org/ethereum/jsontestsuite/Logs.java @@ -7,6 +7,7 @@ import java.util.List; import java.util.Map; import java.util.Set; +import org.ethereum.vm.DataWord; import org.ethereum.vm.LogInfo; import org.json.simple.JSONArray; import org.json.simple.JSONObject; @@ -26,12 +27,12 @@ public class Logs { byte[] address = Hex.decode((String)values.get("address")); byte[] data = Hex.decode(((String)values.get("data")).substring(2)); - List topics = new ArrayList(); + List topics = new ArrayList(); JSONArray jTopics = (JSONArray)values.get("topics"); for(Object t: jTopics.toArray()) { byte[] topic = Hex.decode(((String)t)); - topics.add(topic); + topics.add(new DataWord(topic)); } LogInfo li = new LogInfo(address, topics, data); diff --git a/ethereumj-core/src/main/java/org/ethereum/jsontestsuite/TestRunner.java b/ethereumj-core/src/main/java/org/ethereum/jsontestsuite/TestRunner.java index 152adf7e..2a66aa44 100644 --- a/ethereumj-core/src/main/java/org/ethereum/jsontestsuite/TestRunner.java +++ b/ethereumj-core/src/main/java/org/ethereum/jsontestsuite/TestRunner.java @@ -232,13 +232,15 @@ public class TestRunner { byte[] expectedLogKey = itr.next(); System.out.println("Expected key " + Hex.toHexString(expectedLogKey)); LogInfo expectedLogInfo = logs.getLogBloom(expectedLogKey); - + LogInfo foundLogInfo = null; boolean found = false; for(LogInfo resultLogInfo:logResult) { byte[] resultKey = resultLogInfo.getBloom().getData(); System.out.println("returned key " + Hex.toHexString(resultKey)); if(Arrays.equals(expectedLogKey, resultKey)) { found = true; + foundLogInfo = resultLogInfo; + break; } } @@ -248,6 +250,43 @@ public class TestRunner { 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.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++; + } + } + } } } diff --git a/ethereumj-core/src/main/java/org/ethereum/vm/LogInfo.java b/ethereumj-core/src/main/java/org/ethereum/vm/LogInfo.java index 71a7bae0..b10dec10 100644 --- a/ethereumj-core/src/main/java/org/ethereum/vm/LogInfo.java +++ b/ethereumj-core/src/main/java/org/ethereum/vm/LogInfo.java @@ -21,15 +21,15 @@ import java.util.List; public class LogInfo { byte[] address; - List topics = new ArrayList<>(); + List topics = new ArrayList(); byte[] data; /* Log info in encoded form */ private byte[] rlpEncoded; - public LogInfo(byte[] address, List topics, byte[] data) { + public LogInfo(byte[] address, List topics, byte[] data) { this.address = address; - this.topics = (topics == null) ? new ArrayList() : topics; + this.topics = (topics == null) ? new ArrayList() : topics; this.data = data; } @@ -37,7 +37,7 @@ public class LogInfo { return address; } - public List getTopics() { + public List getTopics() { return topics; } @@ -55,8 +55,9 @@ public class LogInfo { if (topics != null){ topicsEncoded = new byte[topics.size()][]; int i = 0; - for( byte[] topic : topics ){ - topicsEncoded[i] = topic; + for( DataWord topic : topics ){ + byte[] topicData = topic.getData(); + topicsEncoded[i] = topicData; ++i; } } @@ -67,8 +68,9 @@ public class LogInfo { public Bloom getBloom() { Bloom ret = Bloom.create(HashUtil.sha3(address)); - for(byte[] topic:topics) { - ret.or(Bloom.create(HashUtil.sha3(topic))); + for(DataWord topic:topics) { + byte[] topicData = topic.getData(); + ret.or(Bloom.create(HashUtil.sha3(topicData))); } return ret; @@ -80,8 +82,8 @@ public class LogInfo { StringBuffer topicsStr = new StringBuffer(); topicsStr.append("["); - for (byte[] topic: topics){ - String topicStr = Hex.toHexString(topic); + for (DataWord topic: topics){ + String topicStr = Hex.toHexString(topic.getData()); topicsStr.append(topicStr).append(" "); } topicsStr.append("]"); diff --git a/ethereumj-core/src/main/java/org/ethereum/vm/VM.java b/ethereumj-core/src/main/java/org/ethereum/vm/VM.java index fbb5cf15..2e3f8f6e 100644 --- a/ethereumj-core/src/main/java/org/ethereum/vm/VM.java +++ b/ethereumj-core/src/main/java/org/ethereum/vm/VM.java @@ -751,10 +751,11 @@ public class VM { int nTopics = op.val() - OpCode.LOG0.val(); - List topics = new ArrayList<>(); + List topics = new ArrayList(); for (int i = 0; i < nTopics; ++i){ DataWord topic = stack.pop(); - topics.add(topic.getData()); + if(!topics.contains(topic)) + topics.add(topic); } ByteBuffer data = program.memoryChunk(memStart, memOffset);