use of org.ethereum.vm.DataWord in project rskj by rsksmart.
the class BridgeStorageProviderTest method getLockWhitelist_nonNullBytes.
@Test
public void getLockWhitelist_nonNullBytes() throws IOException {
List<Integer> calls = new ArrayList<>();
LockWhitelist whitelistMock = mock(LockWhitelist.class);
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 new byte[] { (byte) 0xaa };
});
PowerMockito.when(BridgeSerializationUtils.deserializeLockWhitelist(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 with the correct AddressBasedAuthorizer
Assert.assertTrue(Arrays.equals(new byte[] { (byte) 0xaa }, data));
return whitelistMock;
});
Assert.assertSame(whitelistMock, storageProvider.getLockWhitelist());
// 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 SamplePrecompiledContractTest method samplePrecompiledContractMethod1WrongData.
@Test
public void samplePrecompiledContractMethod1WrongData() {
DataWord addr = new DataWord(PrecompiledContracts.SAMPLE_ADDR.getBytes());
SamplePrecompiledContract contract = (SamplePrecompiledContract) precompiledContracts.getContractForAddress(addr);
String funcJson = "{\n" + " 'constant':false, \n" + " 'inputs':[{'name':'param0','type':'int'}, \n" + " {'name':'param1','type':'bytes'}, \n" + " {'name':'param2','type':'int'}], \n" + " 'name':'Method1', \n" + " 'outputs':[{'name':'output0','type':'int'}], \n" + " 'type':'function' \n" + "}\n";
funcJson = funcJson.replaceAll("'", "\"");
CallTransaction.Function function = CallTransaction.Function.fromJsonInterface(funcJson);
byte[] data = new byte[] { (byte) 0xab, (byte) 0xcd, (byte) 0xef };
contract.init(null, null, new RepositoryImpl(config), null, null, new ArrayList<LogInfo>());
byte[] result = contract.execute(data);
assertNull(result);
}
use of org.ethereum.vm.DataWord in project rskj by rsksmart.
the class SamplePrecompiledContractTest method samplePrecompiledContractMethod1Ok.
@Test
public void samplePrecompiledContractMethod1Ok() {
DataWord addr = new DataWord(PrecompiledContracts.SAMPLE_ADDR.getBytes());
SamplePrecompiledContract contract = (SamplePrecompiledContract) precompiledContracts.getContractForAddress(addr);
String funcJson = "{\n" + " 'constant':false, \n" + " 'inputs':[{'name':'param0','type':'int'}, \n" + " {'name':'param1','type':'bytes'}, \n" + " {'name':'param2','type':'int'}], \n" + " 'name':'Method1', \n" + " 'outputs':[{'name':'output0','type':'int'}], \n" + " 'type':'function' \n" + "}\n";
funcJson = funcJson.replaceAll("'", "\"");
CallTransaction.Function function = CallTransaction.Function.fromJsonInterface(funcJson);
byte[] bytes = new byte[] { (byte) 0xab, (byte) 0xcd, (byte) 0xef };
byte[] data = function.encode(111, bytes, 222);
contract.init(null, null, new RepositoryImpl(config), null, null, new ArrayList<LogInfo>());
byte[] result = contract.execute(data);
Object[] results = function.decodeResult(result);
assertEquals(new BigInteger("1"), results[0]);
}
use of org.ethereum.vm.DataWord in project rskj by rsksmart.
the class SamplePrecompiledContractTest method samplePrecompiledContractMethod1LargeData.
@Test
public void samplePrecompiledContractMethod1LargeData() {
DataWord addr = new DataWord(PrecompiledContracts.SAMPLE_ADDR.getBytes());
SamplePrecompiledContract contract = (SamplePrecompiledContract) precompiledContracts.getContractForAddress(addr);
String funcJson = "{\n" + " 'constant':false, \n" + " 'inputs':[{'name':'param0','type':'int'}, \n" + " {'name':'param1','type':'string'}], \n" + " 'name':'Method1', \n" + " 'outputs':[{'name':'output0','type':'int'}], \n" + " 'type':'function' \n" + "}\n";
funcJson = funcJson.replaceAll("'", "\"");
CallTransaction.Function function = CallTransaction.Function.fromJsonInterface(funcJson);
byte[] data = function.encode(111, StringUtils.leftPad("foobar", 1000000, '*'));
contract.init(null, null, new RepositoryImpl(config), null, null, new ArrayList<LogInfo>());
byte[] result = contract.execute(data);
Object[] results = function.decodeResult(result);
assertEquals(new BigInteger("1"), results[0]);
}
use of org.ethereum.vm.DataWord in project rskj by rsksmart.
the class SamplePrecompiledContractTest method GetBalance.
private int GetBalance(Repository repository) {
DataWord addr = new DataWord(PrecompiledContracts.SAMPLE_ADDR.getBytes());
SamplePrecompiledContract contract = (SamplePrecompiledContract) precompiledContracts.getContractForAddress(addr);
String funcJson = "{\n" + " 'constant':false, \n" + " 'inputs':[], \n" + " 'name':'GetBalance', \n" + " 'outputs':[{'name':'balance','type':'int'}], \n" + " 'type':'function' \n" + "}\n";
funcJson = funcJson.replaceAll("'", "\"");
CallTransaction.Function function = CallTransaction.Function.fromJsonInterface(funcJson);
byte[] data = function.encode();
contract.init(null, null, repository, null, null, new ArrayList<LogInfo>());
byte[] result = contract.execute(data);
Object[] results = function.decodeResult(result);
return ((BigInteger) results[0]).intValue();
}
Aggregations