You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1.4_Blockchain_version.ts 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import * as fs from 'fs';
  2. import {toUtf8String,parseUnits,ContractFactory, parseEther,JsonRpcProvider, ethers ,Contract, JsonRpcSigner, Wallet,ContractTransactionResponse, MaxInt256
  3. ,Transaction,formatEther,
  4. decodeBase64
  5. } from 'ethers'
  6. // const chainRpc = 'http://193.123.252.127';
  7. // const port = ':80';
  8. const chainRpc = 'http://localhost';
  9. const port = ':21001';
  10. // const chainRpc = 'http://10.96.35.108';
  11. // const port = ':8545';
  12. const rpcUrl = chainRpc + port;
  13. const provider = new JsonRpcProvider(rpcUrl);
  14. async function getValidators(): Promise<any> {
  15. const url = rpcUrl;
  16. const res = await fetch(url,{
  17. method: 'POST',
  18. headers: {
  19. 'Content-Type' :'application/json',
  20. },
  21. body: JSON.stringify({
  22. jsonrpc: "2.0",
  23. id: 1,
  24. method: "qbft_getValidatorsByBlockNumber",
  25. params: ["latest"]
  26. })
  27. });
  28. const json = await res.json();
  29. // console.log(await res.json());
  30. return json;
  31. }
  32. async function getBlockNumber(validatorAddress: string) : Promise<number> {
  33. //eth_blockNumber
  34. const url = rpcUrl;
  35. const res = await fetch(url,{
  36. method: 'POST',
  37. headers: {
  38. 'Content-Type' :'application/json',
  39. },
  40. body: JSON.stringify({
  41. jsonrpc: "2.0",
  42. id: 1,
  43. method: "eth_blockNumber",
  44. params: [""]
  45. })
  46. });
  47. const json = await res.json();
  48. //console.log(await res.json());
  49. return json.result;
  50. return 0;
  51. }
  52. async function getAddress(privateKey: string): Promise<string> {
  53. const wallet = new Wallet(privateKey, provider);
  54. return wallet.address;
  55. }
  56. const genesisFile = './blockchain-info/genesis.json';
  57. const secretsFile = './blockchain-info/validator1.secret.json';
  58. async function main() {
  59. // let json = fs.readFileSync(genesisFile);
  60. // const genesis = JSON.parse(json.toString());
  61. // console.log('genesis.json 정보')
  62. // console.log('genesis.config = ')
  63. // console.log(genesis.config);
  64. // console.log(`최대 블록 크기 (gasLimit) : ${genesis.gasLimit}`);
  65. // // 벨리데이터 노드 어드레스 가져오기
  66. // const validators = (await getValidators()).result;
  67. // console.log(validators);
  68. // // validator 1 노드의 노드키에서 유도된 주소를 확인 (벨리데이터 노드의 주소목록에 있는지 확인)
  69. // json = fs.readFileSync(secretsFile);
  70. // const secrets = JSON.parse(json.toString());
  71. // let nodeAddress = Buffer.from(secrets.data.nodekey,'base64').toString('utf8');
  72. // nodeAddress = await getAddress(nodeAddress);
  73. // console.log(nodeAddress);
  74. // const found = validators.find((el) => el == nodeAddress.toLowerCase());
  75. // console.log(`node address ${found} is found on validators`);
  76. let bn = await getBlockNumber(chainRpc + '21001');
  77. console.log(bn);
  78. bn = await getBlockNumber(chainRpc + '21002');
  79. console.log(bn);
  80. bn = await getBlockNumber(chainRpc + '21003');
  81. console.log(bn);
  82. bn = await getBlockNumber(chainRpc + '21004');
  83. console.log(bn);
  84. }
  85. main();