use of org.ethereum.core.Repository in project rskj by rsksmart.
the class ProgramTrace method getContractDetails.
private static ContractDetails getContractDetails(ProgramInvoke programInvoke) {
Repository repository = programInvoke.getRepository();
if (repository instanceof RepositoryTrack) {
repository = ((RepositoryTrack) repository).getOriginRepository();
}
RskAddress addr = new RskAddress(programInvoke.getOwnerAddress());
return repository.getContractDetails(addr);
}
use of org.ethereum.core.Repository in project rskj by rsksmart.
the class BlockTxsValidationRule method isValid.
@Override
public boolean isValid(Block block, Block parent) {
if (block == null || parent == null) {
logger.warn("BlockTxsValidationRule - block or parent are null");
return false;
}
List<Transaction> txs = block.getTransactionsList();
if (txs.isEmpty()) {
return true;
}
Repository parentRepo = repository.getSnapshotTo(parent.getStateRoot());
Map<RskAddress, BigInteger> curNonce = new HashMap<>();
for (Transaction tx : txs) {
try {
tx.verify();
} catch (RuntimeException e) {
logger.warn("Unable to verify transaction", e);
return false;
}
RskAddress sender = tx.getSender();
BigInteger expectedNonce = curNonce.get(sender);
if (expectedNonce == null) {
expectedNonce = parentRepo.getNonce(sender);
}
curNonce.put(sender, expectedNonce.add(ONE));
BigInteger txNonce = new BigInteger(1, tx.getNonce());
if (!expectedNonce.equals(txNonce)) {
logger.warn("Invalid transaction: Tx nonce {} != expected nonce {} (parent nonce: {}): {}", txNonce, expectedNonce, parentRepo.getNonce(sender), tx);
panicProcessor.panic("invalidtransaction", String.format("Invalid transaction: Tx nonce %s != expected nonce %s (parent nonce: %s): %s", txNonce, expectedNonce, parentRepo.getNonce(sender), tx.getHash()));
return false;
}
}
return true;
}
use of org.ethereum.core.Repository in project rskj by rsksmart.
the class BridgeStateTest method recreateFromEmptyStorageProvider.
@Test
public void recreateFromEmptyStorageProvider() throws IOException {
RskSystemProperties config = new RskSystemProperties();
Repository repository = new RepositoryImpl(config, new TrieStoreImpl(new HashMapDB()));
BridgeConstants bridgeConstants = config.getBlockchainConfig().getCommonConstants().getBridgeConstants();
BridgeStorageProvider provider = new BridgeStorageProvider(repository, PrecompiledContracts.BRIDGE_ADDR, bridgeConstants);
BridgeState state = new BridgeState(42, provider);
BridgeState clone = BridgeState.create(bridgeConstants, state.getEncoded());
Assert.assertNotNull(clone);
Assert.assertEquals(42, clone.getBtcBlockchainBestChainHeight());
Assert.assertTrue(clone.getBtcTxHashesAlreadyProcessed().isEmpty());
Assert.assertTrue(clone.getActiveFederationBtcUTXOs().isEmpty());
Assert.assertTrue(clone.getRskTxsWaitingForSignatures().isEmpty());
}
use of org.ethereum.core.Repository in project rskj by rsksmart.
the class BridgeStorageProviderTest method getNewFederation_nullBytes.
@Test
public void getNewFederation_nullBytes() throws IOException {
List<Integer> storageBytesCalls = new ArrayList<>();
List<Integer> deserializeCalls = new ArrayList<>();
Context contextMock = mock(Context.class);
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) -> {
storageBytesCalls.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 null;
});
PowerMockito.when(BridgeSerializationUtils.deserializeFederation(any(byte[].class), any(Context.class))).then((InvocationOnMock invocation) -> {
deserializeCalls.add(0);
return null;
});
Assert.assertEquals(null, storageProvider.getNewFederation());
Assert.assertEquals(null, storageProvider.getNewFederation());
// 2 for the calls to getStorageBytes
Assert.assertEquals(2, storageBytesCalls.size());
// 2 for the calls to getStorageBytes
Assert.assertEquals(0, deserializeCalls.size());
}
use of org.ethereum.core.Repository in project rskj by rsksmart.
the class BridgeStorageProviderTest method getFeePerKbElection_emptyVotes.
@Test
public void getFeePerKbElection_emptyVotes() {
AddressBasedAuthorizer authorizerMock = mock(AddressBasedAuthorizer.class);
Repository repositoryMock = mock(Repository.class);
BridgeStorageProvider storageProvider = new BridgeStorageProvider(repositoryMock, mockAddress("aabbccdd"), config.getBlockchainConfig().getCommonConstants().getBridgeConstants());
HashMap<ABICallSpec, List<RskAddress>> electionVotes = new HashMap<>();
byte[] serializedElection = BridgeSerializationUtils.serializeElection(new ABICallElection(authorizerMock, electionVotes));
when(repositoryMock.getStorageBytes(any(RskAddress.class), any(DataWord.class))).thenReturn(serializedElection);
when(authorizerMock.getRequiredAuthorizedKeys()).thenReturn(1);
ABICallElection result = storageProvider.getFeePerKbElection(authorizerMock);
assertThat(result.getVotes().isEmpty(), is(true));
assertThat(result.getWinner(), nullValue());
}
Aggregations