Skip to content

Commit e42e2e1

Browse files
committed
added some tests
1 parent bf1134d commit e42e2e1

File tree

2 files changed

+129
-1
lines changed

2 files changed

+129
-1
lines changed

inputformat/src/test/java/org/zuinnote/hadoop/ethereum/format/mapred/EthereumFormatHadoopTest.java

Lines changed: 108 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.io.File;
2525
import java.io.IOException;
2626
import java.text.ParseException;
27+
import java.util.List;
2728

2829
import org.apache.hadoop.conf.Configuration;
2930
import org.apache.hadoop.fs.FileSystem;
@@ -40,6 +41,7 @@
4041
import org.junit.jupiter.api.BeforeEach;
4142
import org.junit.jupiter.api.Test;
4243
import org.zuinnote.hadoop.ethereum.format.common.EthereumBlock;
44+
import org.zuinnote.hadoop.ethereum.format.common.EthereumBlockHeader;
4345
import org.zuinnote.hadoop.ethereum.format.exception.EthereumBlockReadException;
4446

4547
/**
@@ -50,7 +52,17 @@ public class EthereumFormatHadoopTest {
5052
private static Configuration defaultConf = new Configuration();
5153
private static FileSystem localFs = null;
5254
private static Reporter reporter = Reporter.NULL;
53-
55+
56+
private final static char[] hexArray = "0123456789ABCDEF".toCharArray();
57+
private static String bytesToHex(byte[] bytes) {
58+
char[] hexChars = new char[bytes.length * 2];
59+
for ( int j = 0; j < bytes.length; j++ ) {
60+
int v = bytes[j] & 0xFF;
61+
hexChars[j * 2] = hexArray[v >>> 4];
62+
hexChars[j * 2 + 1] = hexArray[v & 0x0F];
63+
}
64+
return new String(hexChars);
65+
}
5466
@BeforeAll
5567
public static void oneTimeSetUp() throws IOException {
5668
// one-time initialization code
@@ -160,6 +172,27 @@ public void checkTestDataBlock1346406AGzipCompressedvailable() {
160172
assertFalse( file.isDirectory(),"Test Data File \""+fileName+"\" is not a directory");
161173
}
162174

175+
@Test
176+
public void checkTestDataBlock403419() {
177+
ClassLoader classLoader = getClass().getClassLoader();
178+
String fileName="block403419.bin";
179+
String fileNameGenesis=classLoader.getResource("testdata/"+fileName).getFile();
180+
assertNotNull(fileNameGenesis,"Test Data File \""+fileName+"\" is not null in resource path");
181+
File file = new File(fileNameGenesis);
182+
assertTrue( file.exists(),"Test Data File \""+fileName+"\" exists");
183+
assertFalse( file.isDirectory(),"Test Data File \""+fileName+"\" is not a directory");
184+
}
185+
@Test
186+
public void checkTestDataBlock447533() {
187+
ClassLoader classLoader = getClass().getClassLoader();
188+
String fileName="block447533.bin";
189+
String fileNameGenesis=classLoader.getResource("testdata/"+fileName).getFile();
190+
assertNotNull(fileNameGenesis,"Test Data File \""+fileName+"\" is not null in resource path");
191+
File file = new File(fileNameGenesis);
192+
assertTrue( file.exists(),"Test Data File \""+fileName+"\" exists");
193+
assertFalse( file.isDirectory(),"Test Data File \""+fileName+"\" is not a directory");
194+
}
195+
163196
@Test
164197
public void readEthereumBlockInputFormatGenesisBlock() throws IOException, EthereumBlockReadException, ParseException, InterruptedException {
165198
JobConf job = new JobConf(defaultConf);
@@ -183,6 +216,80 @@ public void readEthereumBlockInputFormatGenesisBlock() throws IOException, Ether
183216
reader.close();
184217
}
185218

219+
220+
@Test
221+
public void readEthereumBlockInputFormatBlock403419() throws IOException, EthereumBlockReadException, ParseException, InterruptedException {
222+
JobConf job = new JobConf(defaultConf);
223+
ClassLoader classLoader = getClass().getClassLoader();
224+
String fileName="block403419.bin";
225+
String fileNameBlock=classLoader.getResource("testdata/"+fileName).getFile();
226+
Path file = new Path(fileNameBlock);
227+
FileInputFormat.setInputPaths(job, file);
228+
EthereumBlockFileInputFormat format = new EthereumBlockFileInputFormat();
229+
format.configure(job);
230+
InputSplit[] inputSplits = format.getSplits(job,1);
231+
232+
assertEquals( 1, inputSplits.length,"Only one split generated for block 403419");
233+
RecordReader<BytesWritable, EthereumBlock> reader = format.getRecordReader(inputSplits[0], job, reporter);
234+
assertNotNull( reader,"Format returned null RecordReader");
235+
BytesWritable key = new BytesWritable();
236+
EthereumBlock block = new EthereumBlock();
237+
assertTrue( reader.next(key,block),"Input Split for block 403419 contains at least one block");
238+
assertEquals( 2, block.getEthereumTransactions().size(),"Block 403419 must have 2 transactions");
239+
EthereumBlockHeader ethereumBlockHeader = block.getEthereumBlockHeader();
240+
assertEquals(
241+
"f8b483dba2c3b7176a3da549ad41a48bb3121069",
242+
bytesToHex(ethereumBlockHeader.getCoinBase()).toLowerCase(),
243+
"Block 403419 was mined by f8b483dba2c3b7176a3da549ad41a48bb3121069"
244+
);
245+
assertEquals(
246+
"08741fa532c05804d9c1086a311e47cc024bbc43980f561041ad1fbb3c223322",
247+
bytesToHex(ethereumBlockHeader.getParentHash()).toLowerCase(),
248+
"The parent of block 403419 has hash 08741fa532c05804d9c1086a311e47cc024bbc43980f561041ad1fbb3c223322"
249+
);
250+
assertFalse( reader.next(key,block),"No further lock 403419 in genesis Block");
251+
252+
reader.close();
253+
254+
}
255+
256+
@Test
257+
public void readEthereumBlockInputFormatBlock447533() throws IOException, EthereumBlockReadException, ParseException, InterruptedException {
258+
JobConf job = new JobConf(defaultConf);
259+
ClassLoader classLoader = getClass().getClassLoader();
260+
String fileName="block447533.bin";
261+
String fileNameBlock=classLoader.getResource("testdata/"+fileName).getFile();
262+
Path file = new Path(fileNameBlock);
263+
FileInputFormat.setInputPaths(job, file);
264+
EthereumBlockFileInputFormat format = new EthereumBlockFileInputFormat();
265+
format.configure(job);
266+
InputSplit[] inputSplits = format.getSplits(job,1);
267+
268+
assertEquals( 1, inputSplits.length,"Only one split generated for block 447533");
269+
RecordReader<BytesWritable, EthereumBlock> reader = format.getRecordReader(inputSplits[0], job, reporter);
270+
assertNotNull( reader,"Format returned null RecordReader");
271+
BytesWritable key = new BytesWritable();
272+
EthereumBlock block = new EthereumBlock();
273+
assertTrue( reader.next(key,block),"Input Split for block 447533 contains at least one block");
274+
assertEquals( 2, block.getEthereumTransactions().size(),"Block 447533 must have 2 transactions");
275+
EthereumBlockHeader ethereumBlockHeader = block.getEthereumBlockHeader();
276+
assertEquals(
277+
"a027231f42c80ca4125b5cb962a21cd4f812e88f",
278+
bytesToHex(ethereumBlockHeader.getCoinBase()).toLowerCase(),
279+
"Block 447533 was mined by a027231f42c80ca4125b5cb962a21cd4f812e88f"
280+
);
281+
assertEquals(
282+
"043559b70c54f0eea6a90b384286d7ab312129603e750075d09fd35e66f8068a",
283+
bytesToHex(ethereumBlockHeader.getParentHash()).toLowerCase(),
284+
"The parent of block 447533 has hash 043559b70c54f0eea6a90b384286d7ab312129603e750075d09fd35e66f8068a"
285+
);
286+
assertFalse( reader.next(key,block),"No further block in block 447533");
287+
288+
reader.close();
289+
290+
}
291+
292+
186293
@Test
187294
public void readEthereumBlockInputFormatBlock1() throws IOException, EthereumBlockReadException, ParseException, InterruptedException {
188295
JobConf job = new JobConf(defaultConf);

inputformat/src/test/java/org/zuinnote/hadoop/ethereum/format/mapreduce/EthereumFormatHadoopTest.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,27 @@ public void checkTestDataBlock1346406AGzipCompressedvailable() {
175175
assertTrue( file.exists(),"Test Data File \""+fileName+"\" exists");
176176
assertFalse( file.isDirectory(),"Test Data File \""+fileName+"\" is not a directory");
177177
}
178+
179+
@Test
180+
public void checkTestDataBlock403419() {
181+
ClassLoader classLoader = getClass().getClassLoader();
182+
String fileName="block403419.bin";
183+
String fileNameGenesis=classLoader.getResource("testdata/"+fileName).getFile();
184+
assertNotNull(fileNameGenesis,"Test Data File \""+fileName+"\" is not null in resource path");
185+
File file = new File(fileNameGenesis);
186+
assertTrue( file.exists(),"Test Data File \""+fileName+"\" exists");
187+
assertFalse( file.isDirectory(),"Test Data File \""+fileName+"\" is not a directory");
188+
}
189+
@Test
190+
public void checkTestDataBlock447533() {
191+
ClassLoader classLoader = getClass().getClassLoader();
192+
String fileName="block447533.bin";
193+
String fileNameGenesis=classLoader.getResource("testdata/"+fileName).getFile();
194+
assertNotNull(fileNameGenesis,"Test Data File \""+fileName+"\" is not null in resource path");
195+
File file = new File(fileNameGenesis);
196+
assertTrue( file.exists(),"Test Data File \""+fileName+"\" exists");
197+
assertFalse( file.isDirectory(),"Test Data File \""+fileName+"\" is not a directory");
198+
}
178199

179200
@Test
180201
public void readEthereumBlockInputFormatGenesisBlock() throws IOException, EthereumBlockReadException, ParseException, InterruptedException {

0 commit comments

Comments
 (0)