7
7
import java .util .stream .Collectors ;
8
8
import java .util .stream .IntStream ;
9
9
import lombok .extern .slf4j .Slf4j ;
10
+ import org .junit .After ;
10
11
import org .junit .AfterClass ;
11
12
import org .junit .Assert ;
13
+ import org .junit .Before ;
12
14
import org .junit .BeforeClass ;
13
15
import org .junit .Test ;
14
16
import org .springframework .context .annotation .AnnotationConfigApplicationContext ;
@@ -53,13 +55,10 @@ public class ManagerTest {
53
55
private static BlockCapsule blockCapsule2 ;
54
56
private static String dbPath = "output_manager_test" ;
55
57
56
- static {
58
+ @ Before
59
+ public void init () {
57
60
Args .setParam (new String []{"-d" , dbPath , "-w" }, Constant .TEST_CONF );
58
61
context = new AnnotationConfigApplicationContext (DefaultConfig .class );
59
- }
60
-
61
- @ BeforeClass
62
- public static void init () {
63
62
64
63
dbManager = context .getBean (Manager .class );
65
64
@@ -80,8 +79,8 @@ public static void init() {
80
79
ByteArray .fromHexString (Args .getInstance ().getLocalWitnesses ().getPrivateKey ()));
81
80
}
82
81
83
- @ AfterClass
84
- public static void removeDb () {
82
+ @ After
83
+ public void removeDb () {
85
84
Args .clearParam ();
86
85
FileUtil .deleteDir (new File (dbPath ));
87
86
context .destroy ();
@@ -275,6 +274,104 @@ public void fork()
275
274
dbManager .getDynamicPropertiesStore ().getLatestBlockHeaderHash ());
276
275
}
277
276
277
+ @ Test
278
+ public void doNotSwitch ()
279
+ throws ValidateSignatureException , ContractValidateException , ContractExeException ,
280
+ UnLinkedBlockException , ValidateScheduleException , BadItemException ,
281
+ ItemNotFoundException , HeaderNotFound , AccountResourceInsufficientException ,
282
+ TransactionExpirationException , TooBigTransactionException ,
283
+ DupTransactionException , BadBlockException ,
284
+ TaposException , BadNumberBlockException , NonCommonBlockException {
285
+ Args .setParam (new String []{"--witness" }, Constant .TEST_CONF );
286
+ long size = dbManager .getBlockStore ().dbSource .allKeys ().size ();
287
+ System .out .print ("block store size:" + size + "\n " );
288
+ String key = "f31db24bfbd1a2ef19beddca0a0fa37632eded9ac666a05d3bd925f01dde1f62" ;
289
+ byte [] privateKey = ByteArray .fromHexString (key );
290
+ final ECKey ecKey = ECKey .fromPrivate (privateKey );
291
+ byte [] address = ecKey .getAddress ();
292
+ WitnessCapsule witnessCapsule = new WitnessCapsule (ByteString .copyFrom (address ));
293
+ dbManager .addWitness (ByteString .copyFrom (address ));
294
+ dbManager .generateBlock (witnessCapsule , System .currentTimeMillis (), privateKey );
295
+
296
+ Map <ByteString , String > addressToProvateKeys = addTestWitnessAndAccount ();
297
+
298
+ long num = dbManager .getDynamicPropertiesStore ().getLatestBlockHeaderNumber ();
299
+ BlockCapsule blockCapsule0 =
300
+ createTestBlockCapsule (
301
+ num + 1 ,
302
+ dbManager .getDynamicPropertiesStore ().getLatestBlockHeaderHash ().getByteString (),
303
+ addressToProvateKeys );
304
+
305
+ BlockCapsule blockCapsule1 =
306
+ createTestBlockCapsule (
307
+ num + 1 ,
308
+ dbManager .getDynamicPropertiesStore ().getLatestBlockHeaderHash ().getByteString (),
309
+ addressToProvateKeys );
310
+
311
+ BlockCapsule blockCapsule2 =
312
+ createTestBlockCapsule (
313
+ num + 2 , blockCapsule1 .getBlockId ().getByteString (), addressToProvateKeys );
314
+
315
+ dbManager .pushBlock (blockCapsule0 );
316
+ dbManager .pushBlock (blockCapsule1 );
317
+ context .getBean (KhaosDatabase .class ).removeBlk (dbManager .getBlockIdByNum (num ));
318
+ try {
319
+ dbManager .pushBlock (blockCapsule2 );
320
+ } catch (NonCommonBlockException e ) {
321
+ logger .info ("do not switch fork" );
322
+ Assert .assertNotNull (dbManager .getBlockStore ().get (blockCapsule0 .getBlockId ().getBytes ()));
323
+ }
324
+ }
325
+
326
+ @ Test
327
+ public void switchBack ()
328
+ throws ValidateSignatureException , ContractValidateException , ContractExeException ,
329
+ UnLinkedBlockException , ValidateScheduleException , BadItemException ,
330
+ ItemNotFoundException , HeaderNotFound , AccountResourceInsufficientException ,
331
+ TransactionExpirationException , TooBigTransactionException ,
332
+ DupTransactionException , BadBlockException ,
333
+ TaposException , BadNumberBlockException , NonCommonBlockException {
334
+ Args .setParam (new String []{"--witness" }, Constant .TEST_CONF );
335
+ long size = dbManager .getBlockStore ().dbSource .allKeys ().size ();
336
+ System .out .print ("block store size:" + size + "\n " );
337
+ String key = "f31db24bfbd1a2ef19beddca0a0fa37632eded9ac666a05d3bd925f01dde1f62" ;
338
+ byte [] privateKey = ByteArray .fromHexString (key );
339
+ final ECKey ecKey = ECKey .fromPrivate (privateKey );
340
+ byte [] address = ecKey .getAddress ();
341
+ WitnessCapsule witnessCapsule = new WitnessCapsule (ByteString .copyFrom (address ));
342
+ dbManager .addWitness (ByteString .copyFrom (address ));
343
+ dbManager .generateBlock (witnessCapsule , System .currentTimeMillis (), privateKey );
344
+
345
+ Map <ByteString , String > addressToProvateKeys = addTestWitnessAndAccount ();
346
+
347
+ long num = dbManager .getDynamicPropertiesStore ().getLatestBlockHeaderNumber ();
348
+ BlockCapsule blockCapsule0 =
349
+ createTestBlockCapsule (
350
+ num + 1 ,
351
+ dbManager .getDynamicPropertiesStore ().getLatestBlockHeaderHash ().getByteString (),
352
+ addressToProvateKeys );
353
+
354
+ BlockCapsule blockCapsule1 =
355
+ createTestBlockCapsule (
356
+ num + 1 ,
357
+ dbManager .getDynamicPropertiesStore ().getLatestBlockHeaderHash ().getByteString (),
358
+ addressToProvateKeys );
359
+
360
+ BlockCapsule blockCapsule2 =
361
+ createTestBlockCapsuleError (
362
+ num + 2 , blockCapsule1 .getBlockId ().getByteString (), addressToProvateKeys );
363
+
364
+ dbManager .pushBlock (blockCapsule0 );
365
+ dbManager .pushBlock (blockCapsule1 );
366
+ try {
367
+ dbManager .pushBlock (blockCapsule2 );
368
+ } catch (Exception e ) {
369
+ logger .info ("the fork chain has error block" );
370
+ }
371
+
372
+ Assert .assertNotNull (dbManager .getBlockStore ().get (blockCapsule0 .getBlockId ().getBytes ()));
373
+ }
374
+
278
375
private Map <ByteString , String > addTestWitnessAndAccount () {
279
376
dbManager .getWitnesses ().clear ();
280
377
return IntStream .range (0 , 2 )
@@ -309,4 +406,17 @@ private BlockCapsule createTestBlockCapsule(
309
406
blockCapsule .sign (ByteArray .fromHexString (addressToProvateKeys .get (witnessAddress )));
310
407
return blockCapsule ;
311
408
}
409
+
410
+ private BlockCapsule createTestBlockCapsuleError (
411
+ long number , ByteString hash , Map <ByteString , String > addressToProvateKeys ) {
412
+ long time = System .currentTimeMillis ();
413
+ WitnessController witnessController = dbManager .getWitnessController ();
414
+ ByteString witnessAddress =
415
+ witnessController .getScheduledWitness (witnessController .getSlotAtTime (time ));
416
+ BlockCapsule blockCapsule = new BlockCapsule (number , Sha256Hash .wrap (hash ), time , ByteString .copyFromUtf8 ("onlyTest" ));
417
+ blockCapsule .generatedByMyself = true ;
418
+ blockCapsule .setMerkleRoot ();
419
+ blockCapsule .sign (ByteArray .fromHexString (addressToProvateKeys .get (witnessAddress )));
420
+ return blockCapsule ;
421
+ }
312
422
}
0 commit comments