use of org.ethereum.core.Repository in project rskj by rsksmart.
the class BridgeStorageProviderTest method getReleaseRequestQueue.
@Test
public void getReleaseRequestQueue() throws IOException {
List<Integer> calls = new ArrayList<>();
ReleaseRequestQueue requestQueueMock = mock(ReleaseRequestQueue.class);
PowerMockito.mockStatic(BridgeSerializationUtils.class);
Repository repositoryMock = mock(Repository.class);
BridgeStorageProvider storageProvider = new BridgeStorageProvider(repositoryMock, mockAddress("aabbccdd"), config.getBlockchainConfig().getCommonConstants().getBridgeConstants());
when(repositoryMock.getStorageBytes(any(RskAddress.class), any(DataWord.class))).then((InvocationOnMock invocation) -> {
calls.add(0);
RskAddress contractAddress = invocation.getArgumentAt(0, RskAddress.class);
DataWord address = invocation.getArgumentAt(1, DataWord.class);
// Make sure the bytes are got from the correct address in the repo
Assert.assertTrue(Arrays.equals(new byte[] { (byte) 0xaa, (byte) 0xbb, (byte) 0xcc, (byte) 0xdd }, contractAddress.getBytes()));
Assert.assertEquals(new DataWord("releaseRequestQueue".getBytes(StandardCharsets.UTF_8)), address);
return new byte[] { (byte) 0xaa };
});
PowerMockito.when(BridgeSerializationUtils.deserializeReleaseRequestQueue(any(byte[].class), any(NetworkParameters.class))).then((InvocationOnMock invocation) -> {
calls.add(0);
byte[] data = invocation.getArgumentAt(0, byte[].class);
NetworkParameters parameters = invocation.getArgumentAt(1, NetworkParameters.class);
Assert.assertEquals(NetworkParameters.fromID(NetworkParameters.ID_REGTEST), parameters);
// Make sure we're deserializing what just came from the repo
Assert.assertTrue(Arrays.equals(new byte[] { (byte) 0xaa }, data));
return requestQueueMock;
});
Assert.assertSame(requestQueueMock, storageProvider.getReleaseRequestQueue());
// 1 for each call to deserializeFederation & getStorageBytes
Assert.assertEquals(2, calls.size());
}
use of org.ethereum.core.Repository in project rskj by rsksmart.
the class BridgeStorageProviderTest method saveFederationElection.
@Test
public void saveFederationElection() throws IOException {
ABICallElection electionMock = mock(ABICallElection.class);
List<Integer> storageBytesCalls = new ArrayList<>();
List<Integer> serializeCalls = new ArrayList<>();
PowerMockito.mockStatic(BridgeSerializationUtils.class);
Repository repositoryMock = mock(Repository.class);
BridgeStorageProvider storageProvider = new BridgeStorageProvider(repositoryMock, mockAddress("aabbccdd"), config.getBlockchainConfig().getCommonConstants().getBridgeConstants());
PowerMockito.when(BridgeSerializationUtils.serializeElection(any(ABICallElection.class))).then((InvocationOnMock invocation) -> {
ABICallElection election = invocation.getArgumentAt(0, ABICallElection.class);
Assert.assertSame(electionMock, election);
serializeCalls.add(0);
return Hex.decode("aabb");
});
Mockito.doAnswer((InvocationOnMock invocation) -> {
storageBytesCalls.add(0);
RskAddress contractAddress = invocation.getArgumentAt(0, RskAddress.class);
DataWord address = invocation.getArgumentAt(1, DataWord.class);
byte[] data = invocation.getArgumentAt(2, byte[].class);
// Make sure the bytes are set to the correct address in the repo and that what's saved is what was serialized
Assert.assertTrue(Arrays.equals(new byte[] { (byte) 0xaa, (byte) 0xbb, (byte) 0xcc, (byte) 0xdd }, contractAddress.getBytes()));
Assert.assertEquals(new DataWord("federationElection".getBytes(StandardCharsets.UTF_8)), address);
Assert.assertTrue(Arrays.equals(Hex.decode("aabb"), data));
return null;
}).when(repositoryMock).addStorageBytes(any(RskAddress.class), any(DataWord.class), any(byte[].class));
storageProvider.saveFederationElection();
// Shouldn't have tried to save nor serialize anything
Assert.assertEquals(0, storageBytesCalls.size());
Assert.assertEquals(0, serializeCalls.size());
Whitebox.setInternalState(storageProvider, "federationElection", electionMock);
storageProvider.saveFederationElection();
Assert.assertEquals(1, storageBytesCalls.size());
Assert.assertEquals(1, serializeCalls.size());
}
use of org.ethereum.core.Repository in project rskj by rsksmart.
the class BridgeStorageProviderTest method createInstance.
@Test
public void createInstance() throws IOException {
Repository repository = new RepositoryImpl(config);
BridgeStorageProvider provider = new BridgeStorageProvider(repository, PrecompiledContracts.BRIDGE_ADDR, config.getBlockchainConfig().getCommonConstants().getBridgeConstants());
Map<Sha256Hash, Long> processed = provider.getBtcTxHashesAlreadyProcessed();
Assert.assertNotNull(processed);
Assert.assertTrue(processed.isEmpty());
ReleaseRequestQueue releaseRequestQueue = provider.getReleaseRequestQueue();
Assert.assertNotNull(releaseRequestQueue);
Assert.assertEquals(0, releaseRequestQueue.getEntries().size());
ReleaseTransactionSet releaseTransactionSet = provider.getReleaseTransactionSet();
Assert.assertNotNull(releaseTransactionSet);
Assert.assertEquals(0, releaseTransactionSet.getEntries().size());
SortedMap<Keccak256, BtcTransaction> signatures = provider.getRskTxsWaitingForSignatures();
Assert.assertNotNull(signatures);
Assert.assertTrue(signatures.isEmpty());
List<UTXO> utxos = provider.getNewFederationBtcUTXOs();
Assert.assertNotNull(utxos);
Assert.assertTrue(utxos.isEmpty());
}
use of org.ethereum.core.Repository in project rskj by rsksmart.
the class BridgeStorageProviderTest method getReleaseTransactionSet.
@Test
public void getReleaseTransactionSet() throws IOException {
List<Integer> calls = new ArrayList<>();
ReleaseTransactionSet transactionSetMock = mock(ReleaseTransactionSet.class);
PowerMockito.mockStatic(BridgeSerializationUtils.class);
Repository repositoryMock = mock(Repository.class);
BridgeStorageProvider storageProvider = new BridgeStorageProvider(repositoryMock, mockAddress("aabbccdd"), config.getBlockchainConfig().getCommonConstants().getBridgeConstants());
when(repositoryMock.getStorageBytes(any(RskAddress.class), any(DataWord.class))).then((InvocationOnMock invocation) -> {
calls.add(0);
RskAddress contractAddress = invocation.getArgumentAt(0, RskAddress.class);
DataWord address = invocation.getArgumentAt(1, DataWord.class);
// Make sure the bytes are got from the correct address in the repo
Assert.assertTrue(Arrays.equals(new byte[] { (byte) 0xaa, (byte) 0xbb, (byte) 0xcc, (byte) 0xdd }, contractAddress.getBytes()));
Assert.assertEquals(new DataWord("releaseTransactionSet".getBytes(StandardCharsets.UTF_8)), address);
return new byte[] { (byte) 0xaa };
});
PowerMockito.when(BridgeSerializationUtils.deserializeReleaseTransactionSet(any(byte[].class), any(NetworkParameters.class))).then((InvocationOnMock invocation) -> {
calls.add(0);
byte[] data = invocation.getArgumentAt(0, byte[].class);
NetworkParameters parameters = invocation.getArgumentAt(1, NetworkParameters.class);
Assert.assertEquals(NetworkParameters.fromID(NetworkParameters.ID_REGTEST), parameters);
// Make sure we're deserializing what just came from the repo
Assert.assertTrue(Arrays.equals(new byte[] { (byte) 0xaa }, data));
return transactionSetMock;
});
Assert.assertSame(transactionSetMock, storageProvider.getReleaseTransactionSet());
// 1 for each call to deserializeFederation & getStorageBytes
Assert.assertEquals(2, calls.size());
}
use of org.ethereum.core.Repository in project rskj by rsksmart.
the class BridgeStorageProviderTest method createSaveAndRecreateInstance.
@Test
public void createSaveAndRecreateInstance() throws IOException {
Repository repository = new RepositoryImpl(config);
Repository track = repository.startTracking();
BridgeStorageProvider provider0 = new BridgeStorageProvider(track, PrecompiledContracts.BRIDGE_ADDR, config.getBlockchainConfig().getCommonConstants().getBridgeConstants());
provider0.getBtcTxHashesAlreadyProcessed();
provider0.getReleaseRequestQueue();
provider0.getReleaseTransactionSet();
provider0.getRskTxsWaitingForSignatures();
provider0.getNewFederationBtcUTXOs();
provider0.getOldFederationBtcUTXOs();
provider0.save();
track.commit();
track = repository.startTracking();
RskAddress contractAddress = PrecompiledContracts.BRIDGE_ADDR;
Assert.assertNotNull(repository.getContractDetails(contractAddress));
Assert.assertNotNull(repository.getStorageBytes(contractAddress, new DataWord("btcTxHashesAP".getBytes())));
Assert.assertNotNull(repository.getStorageBytes(contractAddress, new DataWord("releaseRequestQueue".getBytes())));
Assert.assertNotNull(repository.getStorageBytes(contractAddress, new DataWord("releaseTransactionSet".getBytes())));
Assert.assertNotNull(repository.getStorageBytes(contractAddress, new DataWord("rskTxsWaitingFS".getBytes())));
Assert.assertNotNull(repository.getStorageBytes(contractAddress, new DataWord("newFederationBtcUTXOs".getBytes())));
Assert.assertNotNull(repository.getStorageBytes(contractAddress, new DataWord("oldFederationBtcUTXOs".getBytes())));
BridgeStorageProvider provider = new BridgeStorageProvider(track, PrecompiledContracts.BRIDGE_ADDR, config.getBlockchainConfig().getCommonConstants().getBridgeConstants());
Map<Sha256Hash, Long> processed = provider.getBtcTxHashesAlreadyProcessed();
Assert.assertNotNull(processed);
Assert.assertTrue(processed.isEmpty());
ReleaseRequestQueue releaseRequestQueue = provider.getReleaseRequestQueue();
Assert.assertNotNull(releaseRequestQueue);
Assert.assertEquals(0, releaseRequestQueue.getEntries().size());
ReleaseTransactionSet releaseTransactionSet = provider.getReleaseTransactionSet();
Assert.assertNotNull(releaseTransactionSet);
Assert.assertEquals(0, releaseTransactionSet.getEntries().size());
SortedMap<Keccak256, BtcTransaction> signatures = provider.getRskTxsWaitingForSignatures();
Assert.assertNotNull(signatures);
Assert.assertTrue(signatures.isEmpty());
List<UTXO> newUtxos = provider.getNewFederationBtcUTXOs();
Assert.assertNotNull(newUtxos);
Assert.assertTrue(newUtxos.isEmpty());
List<UTXO> oldUtxos = provider.getOldFederationBtcUTXOs();
Assert.assertNotNull(oldUtxos);
Assert.assertTrue(oldUtxos.isEmpty());
}
Aggregations