use of co.rsk.peg.Federation in project rskj by rsksmart.
the class StateForBtcReleaseClientTest method getInitializer.
private BridgeStorageProviderInitializer getInitializer() {
final int minNumTxs = 1;
final int maxNumTxs = 100;
final int minNumInputs = 1;
final int maxNumInputs = 10;
return (BridgeStorageProvider provider, Repository repository, int executionIndex) -> {
Map<Keccak256, BtcTransaction> txsWaitingForSignatures;
try {
txsWaitingForSignatures = provider.getRskTxsWaitingForSignatures();
} catch (IOException e) {
throw new RuntimeException("Exception while trying to gather txs waiting for signatures for storage initialization");
}
int numTxs = Helper.randomInRange(minNumTxs, maxNumTxs);
for (int i = 0; i < numTxs; i++) {
BtcTransaction releaseTx = new BtcTransaction(networkParameters);
Federation federation = bridgeConstants.getGenesisFederation();
// Receiver and amounts
Address toAddress = new BtcECKey().toAddress(networkParameters);
Coin releaseAmount = Coin.CENT.multiply(Helper.randomInRange(10, 100));
releaseTx.addOutput(releaseAmount, toAddress);
// Input generation
int numInputs = Helper.randomInRange(minNumInputs, maxNumInputs);
for (int j = 0; j < numInputs; j++) {
Coin inputAmount = releaseAmount.divide(numInputs);
BtcTransaction inputTx = new BtcTransaction(networkParameters);
inputTx.addOutput(inputAmount, federation.getAddress());
releaseTx.addInput(inputTx.getOutput(0)).setScriptSig(PegTestUtils.createBaseInputScriptThatSpendsFromTheFederation(federation));
}
Keccak256 rskTxHash = new Keccak256(HashUtil.keccak256(BigInteger.valueOf(new Random().nextLong()).toByteArray()));
txsWaitingForSignatures.put(rskTxHash, releaseTx);
}
};
}
use of co.rsk.peg.Federation in project rskj by rsksmart.
the class ActiveFederationTest method buildInitializer.
private BridgeStorageProviderInitializer buildInitializer(boolean genesis) {
final int minFederators = 10;
final int maxFederators = 16;
return (BridgeStorageProvider provider, Repository repository, int executionIndex) -> {
if (!genesis) {
int numFederators = Helper.randomInRange(minFederators, maxFederators);
List<BtcECKey> federatorKeys = new ArrayList<>();
for (int i = 0; i < numFederators; i++) {
federatorKeys.add(new BtcECKey());
}
federation = new Federation(federatorKeys, Instant.ofEpochMilli(new Random().nextLong()), Helper.randomInRange(1, 10), networkParameters);
provider.setNewFederation(federation);
} else {
federation = bridgeConstants.getGenesisFederation();
}
};
}
use of co.rsk.peg.Federation in project rskj by rsksmart.
the class TxValidatorTest method createBridgeTx.
public static Transaction createBridgeTx(RskSystemProperties config, long value, long gaslimit, long gasprice, long nonce, long data, long sender) {
Transaction transaction = Tx.create(config, value, gaslimit, gasprice, nonce, data, sender);
Mockito.when(transaction.getReceiveAddress()).thenReturn(PrecompiledContracts.BRIDGE_ADDR);
Mockito.when(transaction.getSignature()).thenReturn(new ECKey.ECDSASignature(BigInteger.ONE, BigInteger.ONE));
Mockito.when(transaction.transactionCost(eq(config), Mockito.any())).thenReturn(new Long(0));
Mockito.when(transaction.getGasLimitAsInteger()).thenReturn(BigInteger.ZERO);
// Federation is the genesis federation ATM
Federation federation = config.getBlockchainConfig().getCommonConstants().getBridgeConstants().getGenesisFederation();
byte[] federator0PubKey = federation.getPublicKeys().get(0).getPubKey();
Mockito.when(transaction.getKey()).thenReturn(ECKey.fromPublicOnly(federator0PubKey));
return transaction;
}
use of co.rsk.peg.Federation in project rskj by rsksmart.
the class RetiringFederationTest method buildInitializer.
private BridgeStorageProviderInitializer buildInitializer(boolean present) {
final int minFederators = 10;
final int maxFederators = 16;
return (BridgeStorageProvider provider, Repository repository, int executionIndex) -> {
if (present) {
int numFederators = Helper.randomInRange(minFederators, maxFederators);
List<BtcECKey> federatorKeys = new ArrayList<>();
for (int i = 0; i < numFederators; i++) {
federatorKeys.add(new BtcECKey());
}
retiringFederation = new Federation(federatorKeys, Instant.ofEpochMilli(new Random().nextLong()), Helper.randomInRange(1, 10), networkParameters);
provider.setNewFederation(bridgeConstants.getGenesisFederation());
provider.setOldFederation(retiringFederation);
} else {
retiringFederation = null;
}
};
}
use of co.rsk.peg.Federation in project rskj by rsksmart.
the class BridgeEventLoggerImplTest method logCommitFederation.
@Test
public void logCommitFederation() {
// Setup event logger
BridgeConstants constantsMock = mock(BridgeConstants.class);
when(constantsMock.getFederationActivationAge()).thenReturn(BridgeRegTestConstants.getInstance().getFederationActivationAge());
List<LogInfo> eventLogs = new LinkedList<>();
BridgeEventLogger eventLogger = new BridgeEventLoggerImpl(constantsMock, eventLogs);
// Setup parameters for test method call
Block executionBlock = mock(Block.class);
when(executionBlock.getTimestamp()).thenReturn(15005L);
when(executionBlock.getNumber()).thenReturn(15L);
Federation oldFederation = new Federation(Arrays.asList(BtcECKey.fromPublicOnly(Hex.decode("036bb9eab797eadc8b697f0e82a01d01cabbfaaca37e5bafc06fdc6fdd38af894a")), BtcECKey.fromPublicOnly(Hex.decode("031da807c71c2f303b7f409dd2605b297ac494a563be3b9ca5f52d95a43d183cc5")), BtcECKey.fromPublicOnly(Hex.decode("025eefeeeed5cdc40822880c7db1d0a88b7b986945ed3fc05a0b45fe166fe85e12")), BtcECKey.fromPublicOnly(Hex.decode("03c67ad63527012fd4776ae892b5dc8c56f80f1be002dc65cd520a2efb64e37b49"))), Instant.ofEpochMilli(15005L), 15L, NetworkParameters.fromID(NetworkParameters.ID_REGTEST));
Federation newFederation = new Federation(Arrays.asList(BtcECKey.fromPublicOnly(Hex.decode("0346cb6b905e4dee49a862eeb2288217d06afcd4ace4b5ca77ebedfbc6afc1c19d")), BtcECKey.fromPublicOnly(Hex.decode("0269a0dbe7b8f84d1b399103c466fb20531a56b1ad3a7b44fe419e74aad8c46db7")), BtcECKey.fromPublicOnly(Hex.decode("026192d8ab41bd402eb0431457f6756a3f3ce15c955c534d2b87f1e0372d8ba338"))), Instant.ofEpochMilli(5005L), 0L, NetworkParameters.fromID(NetworkParameters.ID_REGTEST));
// Do method call
eventLogger.logCommitFederation(executionBlock, oldFederation, newFederation);
// Assert
Assert.assertEquals(1, eventLogs.size());
// Assert address that made the log
LogInfo result = eventLogs.get(0);
Assert.assertArrayEquals(PrecompiledContracts.BRIDGE_ADDR.getBytes(), result.getAddress());
// Assert log topics
Assert.assertEquals(1, result.getTopics().size());
Assert.assertEquals(Bridge.COMMIT_FEDERATION_TOPIC, result.getTopics().get(0));
// Assert log data
Assert.assertNotNull(result.getData());
List<RLPElement> rlpData = RLP.decode2(result.getData());
Assert.assertEquals(1, rlpData.size());
RLPList dataList = (RLPList) rlpData.get(0);
Assert.assertEquals(3, dataList.size());
// Assert old federation data
RLPList oldFedData = (RLPList) dataList.get(0);
Assert.assertEquals(2, oldFedData.size());
Assert.assertArrayEquals(oldFederation.getAddress().getHash160(), oldFedData.get(0).getRLPData());
RLPList oldFedPubKeys = (RLPList) oldFedData.get(1);
Assert.assertEquals(4, oldFedPubKeys.size());
for (int i = 0; i < 4; i++) {
Assert.assertEquals(oldFederation.getPublicKeys().get(i), BtcECKey.fromPublicOnly(oldFedPubKeys.get(i).getRLPData()));
}
// Assert new federation data
RLPList newFedData = (RLPList) dataList.get(1);
Assert.assertEquals(2, newFedData.size());
Assert.assertArrayEquals(newFederation.getAddress().getHash160(), newFedData.get(0).getRLPData());
RLPList newFedPubKeys = (RLPList) newFedData.get(1);
Assert.assertEquals(3, newFedPubKeys.size());
for (int i = 0; i < 3; i++) {
Assert.assertEquals(newFederation.getPublicKeys().get(i), BtcECKey.fromPublicOnly(newFedPubKeys.get(i).getRLPData()));
}
// Assert new federation activation block number
Assert.assertEquals(15L + BridgeRegTestConstants.getInstance().getFederationActivationAge(), Long.valueOf(new String(dataList.get(2).getRLPData(), StandardCharsets.UTF_8)).longValue());
}
Aggregations