100100import static org .mockito .Mockito .doReturn ;
101101import static org .mockito .Mockito .spy ;
102102
103- /**
104- * Created by ajlopez on 29/07/2016.
105- */
106103public class BlockExecutorTest {
107104 private static final byte [] EMPTY_TRIE_HASH = sha3 (RLP .encodeElement (EMPTY_BYTE_ARRAY ));
108105 private static final boolean RSKIP_126_IS_ACTIVE = true ;
109106 private static final long MIN_SEQUENTIAL_SET_GAS_LIMIT = Constants .regtest ().getMinSequentialSetGasLimit ();
110107
111108 private final TestSystemProperties config = new TestSystemProperties ();
112109 private final ActivationConfig activationConfig = spy (config .getActivationConfig ());
113- private final BlockFactory BLOCK_FACTORY = new BlockFactory (activationConfig );
114110
115111 @ TempDir
116112 public Path tempDir ;
@@ -171,7 +167,7 @@ private static byte[] sha3(byte[] input) {
171167 }
172168
173169 @ BeforeEach
174- public void setUp () {
170+ void setUp () {
175171 RskTestFactory objects = new RskTestFactory (tempDir , config );
176172 blockchain = objects .getBlockchain ();
177173 trieStore = objects .getTrieStore ();
@@ -1484,6 +1480,7 @@ private BlockExecutor buildBlockExecutor(TrieStore store, RskSystemProperties co
14841480 doReturn (activeRskip144 ).when (activationConfig ).isActive (eq (RSKIP144 ), anyLong ());
14851481 doReturn (activeRskip126 ).when (activationConfig ).isActive (eq (RSKIP126 ), anyLong ());
14861482
1483+ BlockFactory blockFactory = new BlockFactory (activationConfig );
14871484
14881485 StateRootHandler stateRootHandler = new StateRootHandler (cfg .getActivationConfig (), new StateRootsStoreImpl (new HashMapDB ()));
14891486
@@ -1496,17 +1493,18 @@ private BlockExecutor buildBlockExecutor(TrieStore store, RskSystemProperties co
14961493 BlockTxSignatureCache signatureCache = new BlockTxSignatureCache (new ReceivedTxSignatureCache ());
14971494
14981495 return new BlockExecutor (
1499- new RepositoryLocator (store , stateRootHandler ),
1500- new TransactionExecutorFactory (
1501- cfg ,
1502- null ,
1503- null ,
1504- BLOCK_FACTORY ,
1505- new ProgramInvokeFactoryImpl (),
1506- new PrecompiledContracts (cfg , bridgeSupportFactory , signatureCache ),
1507- signatureCache
1508- ),
1509- cfg );
1496+ new RepositoryLocator (store , stateRootHandler ),
1497+ new TransactionExecutorFactory (
1498+ cfg ,
1499+ null ,
1500+ null ,
1501+ blockFactory ,
1502+ new ProgramInvokeFactoryImpl (),
1503+ new PrecompiledContracts (cfg , bridgeSupportFactory , signatureCache ),
1504+ signatureCache
1505+ ),
1506+ cfg
1507+ );
15101508 }
15111509
15121510 @ ParameterizedTest
@@ -1639,18 +1637,19 @@ void executeBlockWithBaseEventFromRepository(boolean activeRskip535) {
16391637 Assertions .assertInstanceOf (BlockHeaderV2 .class , block .getHeader (), "Block should have BlockHeaderV2 header when RSKIP535 is active" );
16401638
16411639 // Create a real repository with the expected baseEvent value
1642- byte [] expectedBaseEvent = new byte []{0x01 , 0x02 , 0x03 , 0x04 , 0x05 };
1643- Repository repository = new MutableRepository (trieStore , new Trie (trieStore ));
1644- repository .startTracking ();
1640+ byte [] baseEvent = new byte [128 ];
1641+ Arrays .fill (baseEvent , (byte ) 0xFF );
1642+ Repository mutableRepository = new MutableRepository (trieStore , new Trie (trieStore ));
1643+ mutableRepository .startTracking ();
16451644
16461645 // Set the baseEvent in the repository at the bridge address
16471646 RskAddress bridgeAddress = PrecompiledContracts .BRIDGE_ADDR ;
16481647 DataWord baseEventKey = UnionBridgeStorageIndexKey .BASE_EVENT .getKey ();
1649- repository .addStorageBytes (bridgeAddress , baseEventKey , expectedBaseEvent );
1650- repository .commit ();
1648+ mutableRepository .addStorageBytes (bridgeAddress , baseEventKey , baseEvent );
1649+ mutableRepository .commit ();
16511650
16521651 // Update the parent block's state root to include our repository changes
1653- parent .setStateRoot (repository .getRoot ());
1652+ parent .setStateRoot (mutableRepository .getRoot ());
16541653
16551654 // when
16561655 BlockExecutor executor = buildBlockExecutor (trieStore , true , RSKIP_126_IS_ACTIVE );
@@ -1659,7 +1658,7 @@ void executeBlockWithBaseEventFromRepository(boolean activeRskip535) {
16591658 BlockResult result = executor .executeAndFill (block , parent .getHeader ());
16601659 Assertions .assertNotNull (result );
16611660 Assertions .assertNotSame (BlockResult .INTERRUPTED_EXECUTION_BLOCK_RESULT , result );
1662- Assertions .assertArrayEquals (expectedBaseEvent , result .getBlock ().getBaseEvent (),
1661+ Assertions .assertArrayEquals (baseEvent , result .getBlock ().getBaseEvent (),
16631662 "BaseEvent should be set from repository when RSKIP535 is active" );
16641663 } else {
16651664 // when RSKIP535 is not active, we should have a different header type
@@ -1677,17 +1676,19 @@ void executeBlockWithBaseEventFromRepository(boolean activeRskip535) {
16771676 }
16781677
16791678 @ Test
1680- void executeBlockWithBaseEventFromRepository_WhenRepositoryIsNull () {
1679+ void executeBlockWithBaseEventFromRepository_WhenNoBaseEventIsSet_ShouldNotFail () {
16811680 // given
1681+ doReturn (true ).when (activationConfig ).isActive (eq (ConsensusRule .RSKIP535 ), anyLong ());
1682+
16821683 // Create a block with BlockHeaderV2
16831684 Block parent = blockchain .getBestBlock ();
16841685 Block block = new BlockGenerator (Constants .regtest (), activationConfig ).createChildBlock (parent );
16851686
16861687 // Create a real repository but don't set any baseEvent
1687- Repository repository = new MutableRepository (trieStore , new Trie (trieStore ));
1688- repository .startTracking ();
1689- repository .commit ();
1690- parent .setStateRoot (repository .getRoot ());
1688+ Repository mutableRepository = new MutableRepository (trieStore , new Trie (trieStore ));
1689+ mutableRepository .startTracking ();
1690+ mutableRepository .commit ();
1691+ parent .setStateRoot (mutableRepository .getRoot ());
16911692
16921693 BlockExecutor executor = buildBlockExecutor (trieStore , true , RSKIP_126_IS_ACTIVE );
16931694
@@ -1701,38 +1702,6 @@ void executeBlockWithBaseEventFromRepository_WhenRepositoryIsNull() {
17011702 "BaseEvent should be set to empty array when repository has no baseEvent" );
17021703 }
17031704
1704- @ Test
1705- void executeBlockWithBaseEventFromRepository_WhenRepositoryThrowsException () {
1706- // given
1707- doReturn (true ).when (activationConfig ).isActive (eq (ConsensusRule .RSKIP535 ), anyLong ());
1708-
1709- Block parent = blockchain .getBestBlock ();
1710- Block block = new BlockGenerator (Constants .regtest (), activationConfig ).createChildBlock (parent );
1711-
1712- // Create a real repository with baseEvent data
1713- Repository repository = new MutableRepository (trieStore , new Trie (trieStore ));
1714- repository .startTracking ();
1715- RskAddress bridgeAddress = PrecompiledContracts .BRIDGE_ADDR ;
1716- DataWord baseEventKey = UnionBridgeStorageIndexKey .BASE_EVENT .getKey ();
1717- byte [] expectedBaseEvent = new byte []{0x01 , 0x02 , 0x03 , 0x04 , 0x05 };
1718- repository .addStorageBytes (bridgeAddress , baseEventKey , expectedBaseEvent );
1719- repository .commit ();
1720- parent .setStateRoot (repository .getRoot ());
1721-
1722- // Create BlockExecutor with real repository
1723- BlockExecutor executor = buildBlockExecutor (trieStore , true , RSKIP_126_IS_ACTIVE );
1724-
1725- // when
1726- BlockResult result = executor .executeAndFill (block , parent .getHeader ());
1727-
1728- // then
1729- Assertions .assertNotNull (result );
1730- Assertions .assertNotSame (BlockResult .INTERRUPTED_EXECUTION_BLOCK_RESULT , result );
1731- // When repository has baseEvent, it should be retrieved correctly
1732- Assertions .assertArrayEquals (expectedBaseEvent , result .getBlock ().getBaseEvent (),
1733- "BaseEvent should be retrieved from repository when available" );
1734- }
1735-
17361705 public static class TestObjects {
17371706 private final TrieStore trieStore ;
17381707 private final Block block ;
0 commit comments