use of org.ethereum.core.Repository in project rskj by rsksmart.
the class BridgeStorageProviderTest method getFeePerKbElection_withVotes.
@Test
public void getFeePerKbElection_withVotes() {
AddressBasedAuthorizer authorizerMock = mock(AddressBasedAuthorizer.class);
Repository repositoryMock = mock(Repository.class);
when(authorizerMock.getRequiredAuthorizedKeys()).thenReturn(1);
when(authorizerMock.isAuthorized(any(RskAddress.class))).thenReturn(true);
BridgeStorageProvider storageProvider = new BridgeStorageProvider(repositoryMock, mockAddress("aabbccdd"), config.getBlockchainConfig().getCommonConstants().getBridgeConstants());
byte[] electionFee = new byte[] { 0x43, 0x19 };
ABICallSpec expectedWinner = new ABICallSpec("setFeePerKb", new byte[][] { electionFee });
List<RskAddress> voters = new ArrayList<>();
voters.add(new RskAddress("0000000000000000000000000000000000001321"));
voters.add(new RskAddress("0000000000000000000000000000000000004049"));
HashMap<ABICallSpec, List<RskAddress>> electionVotes = new HashMap<>();
electionVotes.put(expectedWinner, voters);
byte[] serializedElection = BridgeSerializationUtils.serializeElection(new ABICallElection(authorizerMock, electionVotes));
when(repositoryMock.getStorageBytes(any(RskAddress.class), any(DataWord.class))).thenReturn(serializedElection);
ABICallElection result = storageProvider.getFeePerKbElection(authorizerMock);
assertThat(result.getVotes(), is(electionVotes));
assertThat(result.getWinner(), is(expectedWinner));
}
use of org.ethereum.core.Repository in project rskj by rsksmart.
the class BridgeStorageProviderTest method createSaveAndRecreateInstanceWithTxsWaitingForSignatures.
@Test
public void createSaveAndRecreateInstanceWithTxsWaitingForSignatures() throws IOException {
BtcTransaction tx1 = createTransaction();
BtcTransaction tx2 = createTransaction();
BtcTransaction tx3 = createTransaction();
Keccak256 hash1 = PegTestUtils.createHash3();
Keccak256 hash2 = PegTestUtils.createHash3();
Keccak256 hash3 = PegTestUtils.createHash3();
Repository repository = new RepositoryImpl(config);
Repository track = repository.startTracking();
BridgeStorageProvider provider0 = new BridgeStorageProvider(track, PrecompiledContracts.BRIDGE_ADDR, config.getBlockchainConfig().getCommonConstants().getBridgeConstants());
provider0.getRskTxsWaitingForSignatures().put(hash1, tx1);
provider0.getRskTxsWaitingForSignatures().put(hash2, tx2);
provider0.getRskTxsWaitingForSignatures().put(hash3, tx3);
provider0.save();
track.commit();
track = repository.startTracking();
BridgeStorageProvider provider = new BridgeStorageProvider(track, PrecompiledContracts.BRIDGE_ADDR, config.getBlockchainConfig().getCommonConstants().getBridgeConstants());
SortedMap<Keccak256, BtcTransaction> signatures = provider.getRskTxsWaitingForSignatures();
Assert.assertNotNull(signatures);
Assert.assertTrue(signatures.containsKey(hash1));
Assert.assertTrue(signatures.containsKey(hash2));
Assert.assertTrue(signatures.containsKey(hash3));
Assert.assertEquals(tx1.getHash(), signatures.get(hash1).getHash());
Assert.assertEquals(tx2.getHash(), signatures.get(hash2).getHash());
Assert.assertEquals(tx3.getHash(), signatures.get(hash3).getHash());
}
use of org.ethereum.core.Repository 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());
}
use of org.ethereum.core.Repository in project rskj by rsksmart.
the class SamplePrecompiledContractTest method samplePrecompiledContractIncrementResultOk.
@Test
public void samplePrecompiledContractIncrementResultOk() {
DataWord addr = new DataWord(PrecompiledContracts.SAMPLE_ADDR.getBytes());
SamplePrecompiledContract contract = (SamplePrecompiledContract) precompiledContracts.getContractForAddress(addr);
String funcJson = "{\n" + " 'constant':false, \n" + " 'inputs':[], \n" + " 'name':'IncrementResult', \n" + " 'outputs':[], \n" + " 'type':'function' \n" + "}\n";
funcJson = funcJson.replaceAll("'", "\"");
CallTransaction.Function function = CallTransaction.Function.fromJsonInterface(funcJson);
byte[] data = function.encode();
Repository repository = new RepositoryImpl(config);
Repository track = repository.startTracking();
contract.init(null, null, track, null, null, new ArrayList<LogInfo>());
contract.execute(data);
track.commit();
int result = this.GetResult(repository);
assertEquals(1, result);
}
use of org.ethereum.core.Repository in project rskj by rsksmart.
the class MinerUtilsTest method harmfulTransactionTest.
@Test
public void harmfulTransactionTest() {
Transaction tx = Tx.create(config, 0, 50000, 1, 0, 0, 0);
List<Transaction> txs = new LinkedList<>();
txs.add(tx);
Mockito.when(tx.getGasPrice()).thenReturn(null);
Map<RskAddress, BigInteger> accountNounces = new HashMap();
byte[] addressBytes = ByteUtil.leftPadBytes(BigInteger.valueOf(new Random(0).nextLong()).toByteArray(), 20);
accountNounces.put(new RskAddress(addressBytes), BigInteger.valueOf(0));
Repository repository = Mockito.mock(Repository.class);
Coin minGasPrice = Coin.valueOf(2L);
LinkedList<Transaction> txsToRemove = new LinkedList<>();
List<Transaction> res = new MinerUtils().filterTransactions(txsToRemove, txs, accountNounces, repository, minGasPrice);
Assert.assertEquals(0, res.size());
Assert.assertEquals(1, txsToRemove.size());
}
Aggregations