-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
90 lines (76 loc) · 3.19 KB
/
Copy pathindex.js
File metadata and controls
90 lines (76 loc) · 3.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
const Bridge = require('@rsksmart/rsk-precompiled-abis').bridge;
const { RLP } = require('@ethereumjs/rlp');
const { Interface } = require('ethers');
const BridgeState = require('./bridge-state');
const { parseRLPToPegoutRequests } = require('./pegout-request');
const { parseRLPToActiveFederationUtxos } = require('./active-federation-utxos');
const { parseRLPToPegoutWaitingSignatures } = require('./pegout-waiting-signature');
const { parseRLPToPegoutWaitingConfirmations } = require('./pegout-waiting-confirmation');
const { parseRLPToNextPegoutCreationBlockNumber } = require('./next-pegout-creation-block-number');
const GET_STATE_FOR_DEBUGGING_METHOD_NAME = 'getStateForDebugging';
const bridgeInterface = new Interface(Bridge.abi);
let requestId = 0;
const call = async (host, rpcMethod, rpcParams = []) => {
const body = {
jsonrpc: '2.0',
id: `bridge-state-data-parser-${requestId++}`,
method: rpcMethod,
params: rpcParams
};
const response = await fetch(host, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(body)
});
try {
const data = await response.json();
if ('error' in data) {
return null;
}
return data.result;
} catch {
return null;
}
};
const getStateForDebugging = async (host, blockToSearch) => {
const getMethodEncodedCall = bridgeInterface.encodeFunctionData(GET_STATE_FOR_DEBUGGING_METHOD_NAME);
const callArguments = {
data: getMethodEncodedCall,
to: '0x0000000000000000000000000000000001000006',
from: '0x0000000000000000000000000000000000000000'
};
const callToBridge = await call(host, 'eth_call', [callArguments, blockToSearch]);
if (!callToBridge) {
return null;
}
return bridgeInterface.decodeFunctionResult(GET_STATE_FOR_DEBUGGING_METHOD_NAME, callToBridge)[0];
};
const getLatestBlockNumber = async host => {
const block = await call(host, 'eth_getBlockByNumber', ['latest', false]);
return block ? parseInt(block.number, 16) : null;
};
const getBridgeState = async (host, blockToSearch = 'latest') => {
const bridgeStateEncoded = await getStateForDebugging(host, blockToSearch);
const decodedListOfStates = RLP.decode(bridgeStateEncoded);
const activeFederationUtxos = parseRLPToActiveFederationUtxos(decodedListOfStates[1]);
const pegoutWaitingSignatures = parseRLPToPegoutWaitingSignatures(decodedListOfStates[2]);
const pegoutRequests = parseRLPToPegoutRequests(decodedListOfStates[3]);
const pegoutWaitingConfirmations = parseRLPToPegoutWaitingConfirmations(decodedListOfStates[4]);
const nextPegoutCreationBlockNumber = parseRLPToNextPegoutCreationBlockNumber(decodedListOfStates[5]);
return new BridgeState(
activeFederationUtxos,
pegoutRequests,
pegoutWaitingConfirmations,
pegoutWaitingSignatures,
nextPegoutCreationBlockNumber
);
};
module.exports = {
parseRLPToActiveFederationUtxos,
parseRLPToPegoutWaitingSignatures,
parseRLPToPegoutRequests,
parseRLPToPegoutWaitingConfirmations,
parseRLPToNextPegoutCreationBlockNumber,
getLatestBlockNumber,
getBridgeState
};