Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Settup and add initial svp tests. #200

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions config/node-configs/rsk-reg-1.conf
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,23 @@ federator {
}

rpc {
providers : {
web: {
cors: "*",
http: {
enabled: true,
bind_address = "0.0.0.0",
hosts = ["localhost"]
}
}
}
# Enabled RPC Modules. If the module is NOT in the list, and mark as "enabled", the rpc calls will be discard.
modules = [
{
name: "rsk",
version: "1.0",
enabled: "true",
},
{
name: "eth",
version: "1.0",
Expand Down
15 changes: 15 additions & 0 deletions config/node-configs/rsk-reg-2.conf
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,23 @@ federator {
}

rpc {
providers : {
web: {
cors: "*",
http: {
enabled: true,
bind_address = "0.0.0.0",
hosts = ["localhost"]
}
}
}
# Enabled RPC Modules. If the module is NOT in the list, and mark as "enabled", the rpc calls will be discard.
modules = [
{
name: "rsk",
version: "1.0",
enabled: "true",
},
{
name: "eth",
version: "1.0",
Expand Down
15 changes: 15 additions & 0 deletions config/node-configs/rsk-reg-3.conf
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,23 @@ federator {
}

rpc {
providers : {
web: {
cors: "*",
http: {
enabled: true,
bind_address = "0.0.0.0",
hosts = ["localhost"]
}
}
}
# Enabled RPC Modules. If the module is NOT in the list, and mark as "enabled", the rpc calls will be discard.
modules = [
{
name: "rsk",
version: "1.0",
enabled: "true",
},
{
name: "eth",
version: "1.0",
Expand Down
15 changes: 14 additions & 1 deletion lib/constants/federation-constants.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const { getBridgeStorageIndexFromKey } = require('../utils');

const KEY_TYPE_BTC = 'btc';
const KEY_TYPE_RSK = 'rsk';
const KEY_TYPE_MST = 'mst';
Expand Down Expand Up @@ -35,10 +37,16 @@ const REGTEST_FEDERATION_CHANGE_ADDRESSES = [
const GENESIS_FEDERATION_ADDRESS = '2N5muMepJizJE1gR7FbHJU6CD18V3BpNF9p';
const GENESIS_FEDERATION_REDEEM_SCRIPT = '0x522102cd53fc53a07f211641a677d250f6de99caf620e8e77071e811a28b3bcddf0be1210362634ab57dae9cb373a5d536e66a8c4f67468bbcfb063809bab643072d78a1242103c5946b3fbae03a654237da863c9ed534e0878657175b132b8ca630f245df04db53ae';

const FEDERATION_ACTIVATION_AGE = 20;
const VALIDATION_PERIOD_DURATION_IN_BLOCKS = 125;
const FEDERATION_ACTIVATION_AGE = 150;
const FUNDS_MIGRATION_AGE_SINCE_ACTIVATION_BEGIN = 15;
const FUNDS_MIGRATION_AGE_SINCE_ACTIVATION_END = 150;

const svpFundTxHashUnsignedStorageIndex = getBridgeStorageIndexFromKey('svpFundTxHashUnsigned');
const svpFundTxSignedStorageIndex = getBridgeStorageIndexFromKey('svpFundTxSigned');
const svpSpendTxHashUnsignedStorageIndex = getBridgeStorageIndexFromKey('svpSpendTxHashUnsigned');
const svpSpendTxWaitingForSignaturesStorageIndex = getBridgeStorageIndexFromKey('svpSpendTxWaitingForSignatures');

module.exports = {
KEY_TYPE_BTC,
KEY_TYPE_RSK,
Expand All @@ -54,4 +62,9 @@ module.exports = {
FEDERATION_ACTIVATION_AGE,
FUNDS_MIGRATION_AGE_SINCE_ACTIVATION_BEGIN,
FUNDS_MIGRATION_AGE_SINCE_ACTIVATION_END,
VALIDATION_PERIOD_DURATION_IN_BLOCKS,
svpFundTxHashUnsignedStorageIndex,
svpFundTxSignedStorageIndex,
svpSpendTxHashUnsignedStorageIndex,
svpSpendTxWaitingForSignaturesStorageIndex,
};
74 changes: 74 additions & 0 deletions lib/federation-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,81 @@ var compareFederateKeys = (publicKeysA, publicKeysB) => {
return comparison;
};

const getActiveFederationKeys = async (bridge) => {

const initialFederationKeys = [];

const initialFederationSize = Number(await bridge.methods.getFederationSize().call());

for(let i = 0; i < initialFederationSize; i++) {

const federatorBtcPublicKey = await bridge.methods.getFederatorPublicKeyOfType(i, KEY_TYPE_BTC).call();
const federatorRskPublicKey = await bridge.methods.getFederatorPublicKeyOfType(i, KEY_TYPE_RSK).call();
const federatorMstPublicKey = await bridge.methods.getFederatorPublicKeyOfType(i, KEY_TYPE_MST).call();

initialFederationKeys.push({
[KEY_TYPE_BTC]: federatorBtcPublicKey,
[KEY_TYPE_RSK]: federatorRskPublicKey,
[KEY_TYPE_MST]: federatorMstPublicKey
});

}

return initialFederationKeys;

};

const getProposedFederationPublicKeys = async (bridge) => {

const proposedFederationKeys = [];

const proposedFederationSize = Number(await bridge.methods.getProposedFederationSize().call());

for(let i = 0; i < proposedFederationSize; i++) {

const federatorBtcPublicKey = await bridge.methods.getProposedFederatorPublicKeyOfType(i, KEY_TYPE_BTC).call();
const federatorRskPublicKey = await bridge.methods.getProposedFederatorPublicKeyOfType(i, KEY_TYPE_RSK).call();
const federatorMstPublicKey = await bridge.methods.getProposedFederatorPublicKeyOfType(i, KEY_TYPE_MST).call();

proposedFederationKeys.push({
[KEY_TYPE_BTC]: federatorBtcPublicKey,
[KEY_TYPE_RSK]: federatorRskPublicKey,
[KEY_TYPE_MST]: federatorMstPublicKey
});

}

return proposedFederationKeys;

};

const getProposedFederationInfo = async (bridge) => {

const proposedFederationInfoResponses = await Promise.all([
bridge.methods.getProposedFederationSize().call(),
bridge.methods.getProposedFederationAddress().call(),
bridge.methods.getProposedFederationCreationBlockNumber().call(),
bridge.methods.getProposedFederationCreationTime().call()
]);

const proposedFederationSize = Number(proposedFederationInfoResponses[0]);
const proposedFederationAddress = proposedFederationInfoResponses[1];
const proposedFederationCreationBlockNumber = Number(proposedFederationInfoResponses[2]);
const proposedFederationCreationTime = Number(proposedFederationInfoResponses[3]);

return {
proposedFederationSize,
proposedFederationAddress,
proposedFederationCreationBlockNumber,
proposedFederationCreationTime
};

};

module.exports = {
comparePublicKeys,
compareFederateKeys,
getProposedFederationPublicKeys,
getProposedFederationInfo,
getActiveFederationKeys,
};
9 changes: 8 additions & 1 deletion lib/rsk-tx-helper-provider.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const { RskTransactionHelper } = require('rsk-transaction-helper');
const { extendWeb3WithRskModule } = require('../lib/web3-utils');

/**
* Creates and returns a list of RskTransactionHelper instances for each federate node
Expand All @@ -7,7 +8,12 @@ const { RskTransactionHelper } = require('rsk-transaction-helper');
*/
const getRskTransactionHelpers = (federates) => {
federates = federates || Runners.hosts.federates;
const rskTransactionHelpers = federates.map(federate => getRskTransactionHelper(federate.host));
const rskTransactionHelpers = federates.map(federate => {
const rskTxHelper = getRskTransactionHelper(federate.host);
extendWeb3WithRskModule(rskTxHelper.getClient());
return rskTxHelper;
});

return rskTransactionHelpers;
};

Expand All @@ -19,6 +25,7 @@ const getRskTransactionHelpers = (federates) => {
*/
const getRskTransactionHelper = (host, maxAttempts = 5) => {
const rskTransactionHelper = new RskTransactionHelper({ hostUrl: host || Runners.hosts.federate.host, maxAttempts });
extendWeb3WithRskModule(rskTransactionHelper.getClient());
return rskTransactionHelper;
};

Expand Down
5 changes: 4 additions & 1 deletion lib/rsk-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,9 @@ const sendTxWithCheck = async (rskTxHelper, method, from, checkCallback) => {
* @returns {BridgeEvent} event
*/
const findEventInBlock = async (rskTxHelper, eventName, fromBlockHashOrBlockNumber, toBlockHashOrBlockNumber, check) => {
if(typeof eventName !== 'string') {
throw new Error('`eventName` parameter must be a string. It is of type: ' + typeof eventName);
}
if(!fromBlockHashOrBlockNumber) {
fromBlockHashOrBlockNumber = await rskTxHelper.getBlockNumber();
}
Expand Down Expand Up @@ -477,7 +480,7 @@ const findBridgeTransactionsInThisBlock = async (web3Client, blockHashOrBlockNum
// const bridgeTransactionParser = new BridgeTransactionParser(web3Client);
// return bridgeTransactionParser.getBridgeTransactionsInThisBlock(blockHashOrBlockNumber);

return bridgeTxParser.getBridgeTransactionsInThisBlock(blockHashOrBlockNumber);
return await bridgeTxParser.getBridgeTransactionsInThisBlock(blockHashOrBlockNumber);
}

/**
Expand Down
37 changes: 37 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var fs = require('fs-extra');
var utils = require('peglib').utils;
const RLP = require('rlp');

var sequentialPromise = function(n, promiseReturn) {
if (n <= 0) {
Expand Down Expand Up @@ -160,6 +161,37 @@ function splitStringIntoChunks(inputString, chunkSize) {
return inputString.match(regex);
}

const bytesToHexString = (bytes) => {
return bytes.reduce((str, byte) => str + byte.toString(16).padStart(2, '0'), '');
};

const removeEmptyLeftBytes = (storageValue) => {
const result = `${storageValue.replaceAll(/^0x0+/g, '')}`;
if(!result.startsWith('0x')) {
return `0x${result}`;
}
return result;
};

const decodeRlp = (rlpEncoded) => {
const uint8ArrayDecoded = RLP.decode(rlpEncoded);
const bytesStr = bytesToHexString(uint8ArrayDecoded);
return bytesStr;
};

const getBridgeStorageValueDecodedHexString = (bridgeStorageValueEncodedAsRlp, append0xPrefix = true) => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we implemented this in one of our libraries didn't we? Might be a good idea anyway to have maybe a specialized library that reads values from the Bridge storage

const rlpBytesWithoutEmptyBytes = removeEmptyLeftBytes(bridgeStorageValueEncodedAsRlp);
const decodedHexFromRlp = decodeRlp(rlpBytesWithoutEmptyBytes);
return append0xPrefix ? `0x${decodedHexFromRlp}` : decodedHexFromRlp;
};

const getBridgeStorageIndexFromKey = (storageKey) => {
return Buffer.from(storageKey)
.toString('hex')
.padStart(64, '0')
.padStart(66, '0x');
};

module.exports = {
sequentialPromise,
mapPromiseAll,
Expand All @@ -180,4 +212,9 @@ module.exports = {
remove: removeAdditionalFederationAddress
},
splitStringIntoChunks,
bytesToHexString,
removeEmptyLeftBytes,
decodeRlp,
getBridgeStorageValueDecodedHexString,
getBridgeStorageIndexFromKey,
}
15 changes: 15 additions & 0 deletions lib/web3-utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const extendWeb3WithRskModule = (web3) => {
web3.extend({
property: 'rsk',
methods: [{
name: 'getStorageBytesAt',
call: 'rsk_getStorageBytesAt',
params: 3,
inputFormatter: [web3.extend.formatters.inputAddressFormatter, web3.extend.formatters.inputDefaultBlockNumberFormatter, web3.extend.formatters.inputDefaultBlockNumberFormatter]
}]
});
};

module.exports = {
extendWeb3WithRskModule,
};
Loading
Loading