Conflux-Chain / ConfluxWeb

Conflux JavaScript API, under active development
GNU Lesser General Public License v3.0
7 stars 5 forks source link

How to deploy local environment for ConfluxWeb #5

Closed saarshah closed 4 years ago

saarshah commented 4 years ago

I want to interact with localhost, instead of http://testnet-jsonrpc.conflux-chain.org:12537', in order to get responses of lot of transactions immediately without any delay. I have deployed conflux locally as below;

git clone https://github.com/Conflux-Chain/conflux-rust
cd conflux-rust
cargo build --release

then make a .conf file for test mode with following parameters

port=12853
jsonrpc_local_http_port=17853
jsonrpc_http_port=17854
log_file='/Users/amirali/node0/conflux.log'
test_mode=true
log_level="debug"
storage_cache_size=200000
storage_cache_start_size=200000
storage_node_map_size=200000
start_mining=false
p2p_nodes_per_ip=0

then open a cmd terminal and run conflux with this commandconflux --config \Users\amirali\node0\conflux.conf then i run my code node deploy.js, which give this error

{ data:
   { jsonrpc: '2.0',
     id: '15744922684107619942',
     method: 'cfx_estimateGas',
     params: [ [Object] ] },
  error: { code: -32603, message: 'Internal error' },
  duration: 61 }
{ Error: Internal error
    at HttpProvider.call (C:\Users\amirali\Desktop\temp\node_modules\conflux-web\lib\provider\http.js:44:13)
    at process._tickCallback (internal/process/next_tick.js:68:7) code: -32603 }

here is my code:

const cfx = new Conflux({
    url: 'http://localhost:17853',
    defaultGasPrice: 100,
    defaultGas: 100000,
    logger: console,
  });
  const account = cfx.wallet.add(PRIVATE_KEY);  // i am not sure whether my online conflux wallet can be used or not ????????

const contract = cfx.Contract({ abi, code }); // create contract instance
const estimateDeployGas = await contract.constructor(account.address).estimateGas();

 const contractAddress = await contract.constructor(account.address)
 .sendTransaction({
   from:account, 
   gas: estimateDeployGas, 
 })
 .deployed();
console.log("Address of Contract: ", contractAddress);

Is there any ganache (GUI or CLI) like test client to make local environment and uses local accounts balances to deploy contracts/send transaction. If yes, please share the procedure.

boqiu commented 4 years ago

You can try Conflux CLI.

boqiu commented 4 years ago

BTW, when deploy a smart contract, please double confirm if the outcomeState in corresponding receipt is 0 (success).

saarshah commented 4 years ago

thanks a lot , i will try ...

saarshah commented 4 years ago

@boqiu How to get balance into our accounts , which are locally generated ? Are there any default accounts (with balances) which we can used for contract deployment and other transactions... ???

boqiu commented 4 years ago

I suppose that you already created accounts at local machine via Conflux CLI (e.g. ./conflux account new). Now, you can transfer CFX to the new created local account via online wallet. Besides, you could also import external account to manage at local machine.

conflux accounts import --import-path <key_file_path>

When you use wallet to create an account, the key file will be downloaded at any place you specified.

saarshah commented 4 years ago

Thanks for your prompt reply, I have done in both ways (1. send through Online wallet, and 2. Import through key file of my wallet). After that , i have check that accounts (where i transfer CFX and whose key file i have imported), the result is below;

  1. After sending CFX to my local account, online wallet showing my balance ([1])(https://confluxscan.io/accountdetail/0xf882d595afe534448181df6b363d83d48948b4a3) of 3 CFX, correctly. But through CLI command its showing zero ...
  2. When i import account through key, it shows 1 account imported and showing in local account list, but balance is showing zero...

However, i am still confused that how my local client know about online wallet transactions ? Is is connected to online wallet, even being a local client ???

boqiu commented 4 years ago

The conflux rpc sub-commands are used to interact with Conflux node via JSON-RPC protocol. When you use CLI to query balance at local machine, you're asking the balance on your local Conflux node. I think that you did not connect the testnet (by specifying the bootnodes in testnet), so you got zero.

Please note, the wallet and scan are all connect to the testnet. So, you should also connect to the testnet and try the experiment above again.

saarshah commented 4 years ago

Sorry, "you did not connect the testnet (by specifying the bootnodes in testnet)" , I could not got this point. Will you explain me further. You means that in order to run local node, still i need to connect o testnet ???. then i think, i will not got my transactions immediately (as like Ganache, if i am not on wrong direction).. ? BTW: How could i specify bootnodes in testnet ?

boqiu commented 4 years ago

When you launch the Conflux node with default configuration file (default.toml), it will connect to the testnet automatically. Because, the testnet bootnodes are already configured in default.toml file.

If you want to launch a private Conflux node, a simple way is to remove the bootnodes configurations in default.toml file. However, as far as I know, there are some bugs that blocking the startup of Conflux node. Now, private network enablement has low priority, you'd better to still use testnet before we fix that problem.

saarshah commented 4 years ago

thanks , but my all work depend on private conflux node, because i have to run lot of transaction and need immediate results , which is not possible in testnet.

BTW. i have used following config.conf file (instead of default.toml) to run my local node. Is it possible to insert some lines in that file in order to run local node and connect to testnet ? config.conf

port=12539
# jsonrpc_tcp_port=12536
jsonrpc_http_port=12537
# jsonrpc_local_tcp_port=12538
jsonrpc_local_http_port=12539
log_file='/Users/amirali/node0/conflux.log'
test_mode=true
log_level="debug"
storage_cache_size=200000
storage_cache_start_size=200000
storage_node_map_size=200000
start_mining=false
p2p_nodes_per_ip=0
boqiu commented 4 years ago

To connect to the testnet, please refer to the bootnodes in default.toml configuration file. Then, the test_mode should be set to false.

If you have to test with a private Conflux node, then you can call generate_block(num_txs, max_block_size) to generate a block under test mode only. I noticed that you have already specified test_mode=true in your own configuration file.

saarshah commented 4 years ago

now I become totally confused ! . Let me try to explain my question, may be i could not elaborate my question well. Currently, i have local accounts (with zero balance) and running my local node without connected to testnet.

  1. My first question, is/are there any default account(s) in local node , which may have some default CFX balances (just like Ganache has 10 accounts with 100 ETH), so that i can make transactions, using these default account, on local node.
    1. If there are no such default accounts with balances, then what steps (step by step) should I perform to connect my local node to testnet (for balances transfer only , FOR ONE TIME), as just removing bootnodes line from default.toml file is not working solution for me. NOTE: Please accept my apology for being beginner/naive user.
boqiu commented 4 years ago

Maybe you can try to configure genesis accounts, which are used for our internal experiment only:

  1. Prepare a toml file named xxx.toml, each line is a private key in HEX format with 0x prefixed.
  2. Add configuration in your config.conf file:

genesis_secrets="./xxx.toml"

In this way, you can configure any genesis accounts as you want. In addition, each configured genesis account's balance is 10,000 CFX as hardcoded for experiment purpose only. Anyway, it is enough for your test.

As for the address of genesis account, it is calculated as keccak(public_key)[12:32]

saarshah commented 4 years ago

Thanks for your guidance ,now i have successfully, configure accounts with balances and can confirm it through CLI. However, as you said earlier, yes there are bugs/errors , but not in startup. My node running, but i could not deploy smart contract on my local node. At first, i received following errors;

  1. When i used contract.constructor(...).estimateGas(); it gives following error

    RPCError: Internal error
    at HttpProvider.call (C:\Users\amirali\Desktop\temp\node_modules\conflux-web\lib\provider\http.js:44:13)
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
    at async Conflux.estimateGas (C:\Users\amirali\Desktop\temp\node_modules\conflux-web\src\conflux.js:666:20)
    at async deployCon (C:\Users\amirali\Desktop\temp\myCaseDeploy.js:180:27)
    at async main (C:\Users\amirali\Desktop\temp\myCaseDeploy.js:149:26) {
    code: -32603
  2. When i give estimated gas as hard coded, then my program shows calling of method namely cfx_getTransactionReceipt again and again without any result...

    {
    data: {
    jsonrpc: '2.0',
    id: '15748226546811516052',
    method: 'cfx_getTransactionReceipt',
    params: [
      '0xb6b688f2c8f9d0314fa53a9113adcdc0e2bd2a0bc71bf524a19acf8845aa8947'
    ]
    },
    result: null,
    duration: 8
    }
  3. It is supposed that at local node (being test node), any transaction should be confirmed with result, immediately. Why the same case is not with Conflux ?

boqiu commented 4 years ago

For question 1, the internal error of estimateGas, @GeekBerry please help on.

For question 2, I know you specify test mode in your configuration file. As I mentioned above, you need to call RPC generateoneblock to generate block to pack the transaction. Otherwise, the sent transaction will be pending in transaction pool all the time.

    #[rpc(name = "generateoneblock")]
    fn generate_one_block(
        &self, num_txs: usize, block_size_limit: usize,
    ) -> RpcResult<H256>;

Conflux has deferred execution mechanism, and you need to generate 5 blocks to ensure the sent transaction executed. Then, you can get the corresponding receipt via cfx_getTransactionReceipt

GeekBerry commented 4 years ago

Internal error is a json-rpc error, that mean before full node received and after provider send, some thing wrong with json rpc call.

code message meaning
-32700 Parse error Invalid JSON was received by the server.An error occurred on the server while parsing the JSON text.
-32600 Invalid Request The JSON sent is not a valid Request object.
-32601 Method not found The method does not exist / is not available.
-32602 Invalid params Invalid method parameter(s).
-32603 Internal error Internal JSON-RPC error.
-32000 to -32099 Server error Reserved for implementation-defined server-errors.

Can you repeat this error? If you can, please paste json log for us.

saarshah commented 4 years ago

@boqiu : You means i have to generate block and then pack my transactions, then send it through RPC API ? Then, its means there is no directly support of JavaScript APIs like web3js (in ethereum) .

@GeekBerry JSON logs ? How could i got this ? I have a log file as per my config.conf file (see above at first comment ), where i just see following lines

2019-11-27T11:25:33.731855800+08:00 DEBUG jsonrpc-eventloop-0  client::rpc: - RPC Request: cfx_estimateGas
2019-11-27T11:25:33.731855800+08:00 WARN  jsonrpc-eventloop-0  client::rpc: - Transaction execution error "execution error: NotEnoughBaseGas { required: 113512, got: 100000 }"
boqiu commented 4 years ago

@saarshah web3js do not have API to generate a block, which is usually generated by mining. Anyway, you could use curl to send a post request to your local Conflux node.

saarshah commented 4 years ago

I think, we should don't need to generate block, it should be done by local node itself. If i have a local client running, then i think that all the mining related activities (generation of blocks, packing transactions, etc.) should be done automatically by node itself upon receiving/executing my code.... For example, i have tested my JavaScript program for ethereum local node (ganache), then i just send my transaction through .js program, ganache make a block and pack my transaction into it, by itself.

boqiu commented 4 years ago

How to generate a block with packed transactions? Generally, there're 2 ways:

  1. Automatically generate block in a period of time (e.g. 1s or 0.5s). This is what Conflux did on testnet based on POW, or Ethereum did for private network based on POA.
  2. Manually generate block via RPC call, which is used in Conflux experiment. In this way, you can generate blocks as fast as possible.

Anyway, we will support private network for test purpose when the mining algorithm finalized:

saarshah commented 4 years ago

thanks for your all clarifications. Yes, exactly, I need such kind of private network for test purpose ... I hope it will be provided soon !...