24
24
import java .io .File ;
25
25
import java .io .IOException ;
26
26
import java .text .ParseException ;
27
+ import java .util .List ;
27
28
28
29
import org .apache .hadoop .conf .Configuration ;
29
30
import org .apache .hadoop .fs .FileSystem ;
40
41
import org .junit .jupiter .api .BeforeEach ;
41
42
import org .junit .jupiter .api .Test ;
42
43
import org .zuinnote .hadoop .ethereum .format .common .EthereumBlock ;
44
+ import org .zuinnote .hadoop .ethereum .format .common .EthereumBlockHeader ;
43
45
import org .zuinnote .hadoop .ethereum .format .exception .EthereumBlockReadException ;
44
46
45
47
/**
@@ -50,7 +52,17 @@ public class EthereumFormatHadoopTest {
50
52
private static Configuration defaultConf = new Configuration ();
51
53
private static FileSystem localFs = null ;
52
54
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
+ }
54
66
@ BeforeAll
55
67
public static void oneTimeSetUp () throws IOException {
56
68
// one-time initialization code
@@ -160,6 +172,27 @@ public void checkTestDataBlock1346406AGzipCompressedvailable() {
160
172
assertFalse ( file .isDirectory (),"Test Data File \" " +fileName +"\" is not a directory" );
161
173
}
162
174
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
+
163
196
@ Test
164
197
public void readEthereumBlockInputFormatGenesisBlock () throws IOException , EthereumBlockReadException , ParseException , InterruptedException {
165
198
JobConf job = new JobConf (defaultConf );
@@ -183,6 +216,80 @@ public void readEthereumBlockInputFormatGenesisBlock() throws IOException, Ether
183
216
reader .close ();
184
217
}
185
218
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
+
186
293
@ Test
187
294
public void readEthereumBlockInputFormatBlock1 () throws IOException , EthereumBlockReadException , ParseException , InterruptedException {
188
295
JobConf job = new JobConf (defaultConf );
0 commit comments