use of org.ethereum.vm.DataWord in project rskj by rsksmart.
the class BridgeStorageProviderTest method getNewFederation.
@Test
public void getNewFederation() throws IOException {
List<Integer> calls = new ArrayList<>();
Context contextMock = mock(Context.class);
Federation newFederation = new Federation(Arrays.asList(new BtcECKey[] { BtcECKey.fromPrivate(BigInteger.valueOf(100)) }), Instant.ofEpochMilli(1000), 0L, NetworkParameters.fromID(NetworkParameters.ID_REGTEST));
PowerMockito.mockStatic(BridgeSerializationUtils.class);
Repository repositoryMock = mock(Repository.class);
BridgeStorageProvider storageProvider = new BridgeStorageProvider(repositoryMock, mockAddress("aabbccdd"), config.getBlockchainConfig().getCommonConstants().getBridgeConstants());
Whitebox.setInternalState(storageProvider, "btcContext", contextMock);
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 get 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("newFederation".getBytes(StandardCharsets.UTF_8)), address);
return new byte[] { (byte) 0xaa };
});
PowerMockito.when(BridgeSerializationUtils.deserializeFederation(any(byte[].class), any(Context.class))).then((InvocationOnMock invocation) -> {
calls.add(0);
byte[] data = invocation.getArgumentAt(0, byte[].class);
Context btcContext = invocation.getArgumentAt(1, Context.class);
// Make sure we're deserializing what just came from the repo with the correct BTC context
Assert.assertTrue(Arrays.equals(new byte[] { (byte) 0xaa }, data));
Assert.assertEquals(contextMock, btcContext);
return newFederation;
});
Assert.assertEquals(newFederation, storageProvider.getNewFederation());
Assert.assertEquals(newFederation, storageProvider.getNewFederation());
// 1 for each call to deserializeFederation & getStorageBytes
Assert.assertEquals(2, calls.size());
}
use of org.ethereum.vm.DataWord in project rskj by rsksmart.
the class BridgeStorageProviderTest method getFederationElection_nonNullBytes.
@Test
public void getFederationElection_nonNullBytes() throws IOException {
List<Integer> calls = new ArrayList<>();
AddressBasedAuthorizer authorizerMock = mock(AddressBasedAuthorizer.class);
ABICallElection electionMock = mock(ABICallElection.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("federationElection".getBytes(StandardCharsets.UTF_8)), address);
return new byte[] { (byte) 0xaa };
});
PowerMockito.when(BridgeSerializationUtils.deserializeElection(any(byte[].class), any(AddressBasedAuthorizer.class))).then((InvocationOnMock invocation) -> {
calls.add(0);
byte[] data = invocation.getArgumentAt(0, byte[].class);
AddressBasedAuthorizer authorizer = invocation.getArgumentAt(1, AddressBasedAuthorizer.class);
// Make sure we're deserializing what just came from the repo with the correct AddressBasedAuthorizer
Assert.assertTrue(Arrays.equals(new byte[] { (byte) 0xaa }, data));
Assert.assertEquals(authorizerMock, authorizer);
return electionMock;
});
Assert.assertSame(electionMock, storageProvider.getFederationElection(authorizerMock));
// 1 for each call to deserializeFederation & getStorageBytes
Assert.assertEquals(2, calls.size());
}
use of org.ethereum.vm.DataWord in project rskj by rsksmart.
the class BridgeStorageProviderTest method saveNewFederation.
@Test
public void saveNewFederation() throws IOException {
Federation newFederation = new Federation(Arrays.asList(new BtcECKey[] { BtcECKey.fromPrivate(BigInteger.valueOf(100)) }), Instant.ofEpochMilli(1000), 0L, NetworkParameters.fromID(NetworkParameters.ID_REGTEST));
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.serializeFederation(any(Federation.class))).then((InvocationOnMock invocation) -> {
Federation federation = invocation.getArgumentAt(0, Federation.class);
Assert.assertEquals(newFederation, federation);
serializeCalls.add(0);
return new byte[] { (byte) 0xbb };
});
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("newFederation".getBytes(StandardCharsets.UTF_8)), address);
Assert.assertTrue(Arrays.equals(new byte[] { (byte) 0xbb }, data));
return null;
}).when(repositoryMock).addStorageBytes(any(RskAddress.class), any(DataWord.class), any(byte[].class));
storageProvider.saveNewFederation();
// Shouldn't have tried to save nor serialize anything
Assert.assertEquals(0, storageBytesCalls.size());
Assert.assertEquals(0, serializeCalls.size());
storageProvider.setNewFederation(newFederation);
storageProvider.saveNewFederation();
Assert.assertEquals(1, storageBytesCalls.size());
Assert.assertEquals(1, serializeCalls.size());
}
use of org.ethereum.vm.DataWord in project rskj by rsksmart.
the class BridgeStorageProviderTest method saveLockWhitelist.
@Test
public void saveLockWhitelist() throws IOException {
LockWhitelist whitelistMock = mock(LockWhitelist.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.serializeLockWhitelist(any(LockWhitelist.class))).then((InvocationOnMock invocation) -> {
LockWhitelist whitelist = invocation.getArgumentAt(0, LockWhitelist.class);
Assert.assertSame(whitelistMock, whitelist);
serializeCalls.add(0);
return Hex.decode("ccdd");
});
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(Hex.decode("aabbccdd"), contractAddress.getBytes()));
Assert.assertEquals(new DataWord("lockWhitelist".getBytes(StandardCharsets.UTF_8)), address);
Assert.assertTrue(Arrays.equals(Hex.decode("ccdd"), data));
return null;
}).when(repositoryMock).addStorageBytes(any(RskAddress.class), any(DataWord.class), any(byte[].class));
storageProvider.saveLockWhitelist();
// Shouldn't have tried to save nor serialize anything
Assert.assertEquals(0, storageBytesCalls.size());
Assert.assertEquals(0, serializeCalls.size());
Whitebox.setInternalState(storageProvider, "lockWhitelist", whitelistMock);
storageProvider.saveLockWhitelist();
Assert.assertEquals(1, storageBytesCalls.size());
Assert.assertEquals(1, serializeCalls.size());
}
use of org.ethereum.vm.DataWord in project rskj by rsksmart.
the class BridgeStorageProviderTest method getLockWhitelist_nullBytes.
@Test
public void getLockWhitelist_nullBytes() throws IOException {
List<Integer> calls = new ArrayList<>();
PowerMockito.mockStatic(BridgeSerializationUtils.class);
Repository repositoryMock = mock(Repository.class);
BridgeStorageProvider storageProvider = new BridgeStorageProvider(repositoryMock, mockAddress("aabbccdd"), config.getBlockchainConfig().getCommonConstants().getBridgeConstants());
Context contextMock = mock(Context.class);
when(contextMock.getParams()).thenReturn(NetworkParameters.fromID(NetworkParameters.ID_REGTEST));
Whitebox.setInternalState(storageProvider, "btcContext", contextMock);
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("lockWhitelist".getBytes(StandardCharsets.UTF_8)), address);
return null;
});
PowerMockito.when(BridgeSerializationUtils.deserializeLockWhitelist(any(byte[].class), any(NetworkParameters.class))).then((InvocationOnMock invocation) -> {
calls.add(0);
return null;
});
LockWhitelist result = storageProvider.getLockWhitelist();
Assert.assertNotNull(result);
Assert.assertEquals(0, result.getSize().intValue());
// 1 for each call to deserializeFederation & getStorageBytes
Assert.assertEquals(1, calls.size());
}
Aggregations