use of co.rsk.core.RskAddress 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 co.rsk.core.RskAddress in project rskj by rsksmart.
the class Transaction method getSender.
public synchronized RskAddress getSender() {
if (sender != null) {
return sender;
}
try {
ECKey key = ECKey.signatureToKey(getRawHash().getBytes(), getSignature().toBase64());
sender = new RskAddress(key.getAddress());
} catch (SignatureException e) {
logger.error(e.getMessage(), e);
panicProcessor.panic("transaction", e.getMessage());
sender = RskAddress.nullAddress();
}
return sender;
}
use of co.rsk.core.RskAddress 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 co.rsk.core.RskAddress 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());
}
use of co.rsk.core.RskAddress 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());
}
Aggregations