Why do I pay fee for reading transaction on Ropsten?

Sep 15 2020

I have a doubt.

I am using truffle with meta mask to deploy Smart Contract, write transaction and read transaction on Blockchain.

Why do I pay fee for reading data on BlockChain? Should it be free? I have thought it is because I using emit event (so the method is'n view methos).

I am testing on Ropsten Blockchain.

  function getVincitore ()  public returns (string memory, string memory,string memory,string memory )
  {
    require(userCountVincitore>0, "non e` stato sorteggiato nessun vincitore");
    emit Winner ("Il Vincitore", vincitore.username, vincitore.nome, vincitore.cognome,vincitore.email );
    return (vincitore.username, vincitore.nome, vincitore.cognome, vincitore.email);

}

Which are the cases where the transaction are free?

Thanks a lot.

回答

1 goodvibration Sep 15 2020 at 19:55

A transaction necessarily changes the state of the blockchain, therefore requires mining, therefore has a gas-cost attached to it, therefore is not free.

Sending a transaction, regardless of what function is called in it, costs gas.

When the function is constant (either pure or view), you do not need to execute it in a transaction, but you can simply call it.

これ(RPCとも呼ばれます)は、おそらくweb3プロバイダー(つまり、ノードサービス)に支払うことを除けば、何の費用もかかりませんが、その支払いはブロックチェーンのコンテキスト外です。

あなたの場合、関数はイベントを発行し、イベントは常にブロックチェーンに書き込まれるため、関数はブロックチェーンの状態を変更します。したがって、定数(pureまたはview)を宣言することはできません。したがって、トランザクションでのみ実行でき、コストがかかります。 。

1 clement Sep 15 2020 at 19:54

ブロックチェーンにデータを書き込まない場合、トランザクションは無料です。この場合、トランザクションではなく呼び出しについて話します(詳細については、ここを参照してください:トランザクションと呼び出しの違いは何ですか?)。

イベントはブロックに書き込まれたログとして見ることができ、ストレージを変更します。だからあなたは料金を払わなければなりません。