Skip to content
This repository has been archived by the owner on May 17, 2022. It is now read-only.

Commit

Permalink
Finished account-types-gas-and-transactions page. Cleaned up dapp dev…
Browse files Browse the repository at this point in the history
…elopment.finished client implimentations. finished disclaimer.
  • Loading branch information
Souptacular committed Feb 10, 2016
1 parent 18474ea commit e5af05c
Show file tree
Hide file tree
Showing 6 changed files with 232 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,27 @@ Account Types, Gas, and Transactions

EOA vs Contract Accounts
================================================================================
There are two types of accounts in Ethereum state:
- Externally Controlled Accounts

There are two types of accounts in Ethereum
- Externally Owned Accounts
- Contracts Accounts

Externally Owned Accounts (EOAs):
- Can have an ether balance.
- Can send transactions.
- Are controlled by private keys.
- Has no code.
Externally Owned Accounts (EOAs)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

- Can have an Ether balance.
- Can send transactions.
- Are controlled by private keys.
- Has no code.

Contract Accounts:
- Can have an ether balance.
- Can send transactions.
- Can send messages.
- Contracts are controlled by their contract code.
- Only send transactions in response to other transactions that they have received. Therefore, all action on the Ethereum block chain is set in motion by transactions fired from externally owned accounts.
- Every time a contract account receives a transaction it's code activates, allowing it to read and write to internal storage and send other transactions/messages or create contracts.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- Can have an Ether balance.
- Can send transactions.
- Can send messages.
- Contracts are controlled by their contract code.
- Only send transactions in response to other transactions that they have received. Therefore, all action on the Ethereum block chain is set in motion by transactions fired from externally owned accounts.
- Every time a contract account receives a transaction it's code activates, allowing it to read and write to internal storage and send other transactions/messages or create contracts.

Note that "contracts" in Ethereum should not be seen as something that should be "fulfilled" or "complied with"; rather, they are more like "autonomous agents" that live inside of the Ethereum execution environment, always executing a specific piece of code when "poked" by a message or transaction, and having direct control over their own ether balance and their own key/value store to keep track of persistent variables.

Expand All @@ -31,7 +35,7 @@ The term "transaction" is used in Ethereum to refer to the signed data package t
Transactions contain:
- The recipient of the message.
- A signature identifying the sender.
- The amount of ether to transfer from the sender to the recipient
- ``VALUE`` field - The amount of ether to transfer from the sender to the recipient.
- An optional data field.
- A ``STARTGAS`` value, representing the maximum number of computational steps the transaction execution is allowed to take.
- A ``GASPRICE`` value, representing the fee the sender pays per computational step.
Expand All @@ -43,38 +47,64 @@ Contracts have the ability to send "messages" to other contracts. Messages are v
A message contains:
- The sender of the message (implicit).
- The recipient of the message.
- The amount of ether to transfer alongside the message.
- ``VALUE`` field - The amount of ether to transfer alongside the message to the contract address.
- An optional data field.
- A ``STARTGAS`` value.

Essentially, a message is like a transaction, except it is produced by a
contract and not an external actor. A message is produced when a
contract currently executing code executes the ``CALL`` opcode, which
produces and executes a message. Like a transaction, a message leads to
the recipient account running its code. Thus, contracts can have
relationships with other contracts in exactly the same way that external
actors can.

Note that the gas allowance assigned by a transaction or contract
applies to the total gas consumed by that transaction and all
sub-executions. For example, if an external actor A sends a transaction
to B with 1000 gas, and B consumes 600 gas before sending a message to
C, and the internal execution of C consumes 300 gas before returning,
then B can spend another 100 gas before running out of gas.

Essentially, a message is like a transaction, except it is produced by a contract and not an external actor. A message is produced when a contract currently executing code executes the ``CALL`` opcode, which produces and executes a message. Like a transaction, a message leads to the recipient account running its code. Thus, contracts can have relationships with other contracts in exactly the same way that external actors can.

What is Gas?
================================================================================
[explain that it is the fuel for Ethereum transactions]
Ethereum implements an execution environment on the blockchain called the Ethereum Virtual Machine (EVM). When you are running a decentralized application (dApp), every instruction is executed on every node of the network. This has a cost: for every operation in a contract that can be executed there is a specified cost, expressed in number of gas units.

Estimating Gas
================================================================================
Gas is name for the execution fee for every operation made on an Ethereum blockchain. Its price is expressed in ether and it's decided by the miners, which can refuse to process transaction with less than a certain gas price. To get gas you simply need to add ether to your account. The Ethereum client automatically converts Ether to gas and gas to Ether when transactions are processed.

The Ethereum protocol charges a fee per computational step that is executed in a contract or transaction to prevent deliberate attacks and abuse on the Ethereum network. Every transaction is required to include a gas limit and a fee that it is willing to pay per gas. Miners have the choice of including the transaction and collecting the fee or not. If the total number of gas used by the computational steps spawned by the transaction, including the original message and any sub-messages that may be triggered, is less than or equal to the gas limit, then the transaction processes. If the total gas exceeds the gas limit, then all changes are reverted, except that the transaction is still valid and the fee can still be collected by the miner. This means that it is wiser to send transactions with a gas limit well above the estimates

Transaction Pool
Estimating Transaction Costs
================================================================================
The total cost of a transaction is based on 2 factors:

``gasUsed`` is the total gas that is consumed by the transaction

``gasPrice`` price (in ether) of one unit of gas specified in the transaction

**Total cost = gasUsed * gasPrice**

gasUsed
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Each operation in the EVM was assigned a number of how much gas it consumes. ``gasUsed`` is summing up all the gas for all the operations executed. There is a `spreadsheet`_ which offers a glimpse to some of the analysis behind them.

For estimating ``gasUsed``, there is an `estimateGas API that can be used but has some caveats`_.

gasPrice
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
A user constructs and signs a transaction, and each user may specify whatever ``gasPrice`` they desire, this includes zero. However, the Ethereum clients launched at Frontier had a default gasPrice of 0.05e12 wei. As miners optimize for their revenue, if most transactions are being submitted with a gasPrice of 0.05e12 wei, it would be difficult to convince a miner to accept a transaction that specified a lower, or zero, gasPrice.

Example Transaction Cost
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Let’s take a contract that just adds 2 numbers. The EVM OPCODE ``ADD`` consumes 3 gas.

The approximate cost, using the default gas price (as of January 2016), would be:

3 \* 0.05e12 = 1.5e11 wei

Since 1 Ether is 1e18 wei, the total cost would be 0.00000015 Ether.

This is a simplification since it ignores some costs, such as the cost
of passing the 2 numbers to contract, before they can even be added.

.. _spreadsheet: http://ethereum.stackexchange.com/q/52/42
.. _estimateGas API that can be used but has some caveats: http://ethereum.stackexchange.com/q/266/42
.. _question: http://ethereum.stackexchange.com/q/324/42


Lifecycle of a Transaction
================================================================================
.. todo::
Lifecycle of a Transaction

Signing Transactions Offline
================================================================================
================================================================================
[ Maybe add this to the FAQ and point to the ethkey section of turboethereum guide? ]
85 changes: 50 additions & 35 deletions source/developing-on-ethereum/dapp-development.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@
ÐApp Development
********************************************************************************

Three Primary Resources
Four Primary Resources
================================================================================
ÐApp development requires an understanding of the Web3 Javascript API, the JSON RPC API, and the Solidity programming language. Note: There are developer tools that help you develop, test, and deploy ÐApps.
ÐApp development requires an understanding of the Web3 Javascript API, the JSON RPC API, and the Solidity programming language. Note: There are developer tools that help you develop, test, and deploy ÐApps in a way that automatically utilizes the resources listed below.

.. todo::
Add cross reference to developer tools page.
Add cross reference to developer tools page in the first paragraph.

- `Web3 JavaScript API <https://github.com/ethereum/wiki/wiki/JavaScript-API>`__ - This is the main JavaScript SDK to use when you want to interact with an Ethereum node.
- `JSON RPC API <https://github.com/ethereum/wiki/wiki/JSON-RPC>`__ - This is the low level JSON RPC 2.0 interface to interface with a node. This API is used by the `Web3 JavaScript API <https://github.com/ethereum/wiki/wiki/JavaScript-API>`__.
- `Solidity Documentation <https://solidity.readthedocs.org/en/latest/>`__ - Solidity is the Ethereum developed Smart Contract language, which compiles to EVM (Ethereum Virtual Machine) opcodes.
- Testnets - Test networks help developers develop and test Ethereum code and network interactions without spending their own Ether on the main network. Test network options are listed below.

Connecting to Morden Testnet
================================================================================
Expand All @@ -24,40 +25,40 @@ Usage
TurboEthereum (C++)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

This is supported natively on 0.9.93 and above. Pass the ``--morden``
argument in when starting any of the clients. e.g.:
This is supported natively on 0.9.93 and above. Pass the ``--morden`` argument in when starting any of the clients. e.g.:

::
.. code:: Console
> eth --morden
> eth --morden
Or, for AlethZero

::
.. code:: Console
> alethzero --morden
> alethzero --morden
PyEthApp (Python client)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

PyEthApp supports the morden network from v1.0.5 onwards:

::
.. code:: Console
> pyethapp --profile morden run
> pyethapp --profile morden run
geth (Go client)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

::
.. code:: Console
> geth --testnet
> geth --testnet
Details
--------------------------------------------------------------------------------
All parameters are the same as Frontier except:

- Network Identity: **2**
- All parameters same as Frontier except:
- Network Name: **Morden**
- Network Identity: 2
- genesis.json (given below);
- Initial Account Nonce (``IAN``) is 2^20 (instead of 0 in all previous
networks).
Expand All @@ -71,15 +72,10 @@ Details
- Genesis state root:
``f3f4696bbf3b3b07775128eb7a3763279a394e382130f27c21e70233e04946a9``

Seed Nodes
Morden's genesis.json
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

- ``enode://e58d5e26b3b630496ec640f2530f3e7fa8a8c7dfe79d9e9c4aac80e3730132b869c852d3125204ab35bb1b1951f6f2d40996c1034fd8c5a69b383ee337f02ddc@92.51.165.126:30303``

genesis.json
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

.. code:: json
.. code:: JSON
{
"nonce": "0x00006d6f7264656e",
Expand All @@ -103,16 +99,14 @@ Getting Morden Testnet Ether
--------------------------------------------------------------------------------

Three ways to get Morden testnet ether:
You can mine testnet Ethereum
One way to get Ether is by using the `Ethereum wei
faucet <https://zerogox.com/ethereum/wei_faucet>`__. Just type in your
account address and enjoy some free ether.

- You can mine testnet Ethereum
- Use the `Ethereum wei faucet <https://zerogox.com/ethereum/wei_faucet>`__.

.. todo::
Finish Morden Testnet Section



Setting Up a Local Private Testnet
================================================================================
You either pre-generate or mine your own Ether on a private
Expand Down Expand Up @@ -173,16 +167,15 @@ This will enable RPC interface on your node. This is generally enabled by defaul

This dictates what APIs that are allowed to be accessed over RPC. By default, Geth enables the web3 interface over RPC.

**IMPORTANT: Please note that offering an API over the RPC/IPC interface will give everyone access to the API who can access this interface (e.g. ÐApp's). Be careful which API's you enable. By default geth enables all API's over the ipc interface and only the db,eth,net and web3 API's over the RPC interface.**

**IMPORTANT: Please note that offering an API over the RPC/IPC interface will give everyone access to the API who can access this interface (e.g. ÐApp's). Be careful which API's you enable. By default geth enables all API's over the IPC interface and only the db,eth,net and web3 API's over the RPC interface.**

``--rpcport "8080"``

Change 8000 to any port that is open on your network. The default for geth is 8080.

``--rpccorsdomain "http://chriseth.github.io/browser-solidity/"``

This dictates what URLs can connect to your node in order to perform RPC client tasks. Be very careful with this put a specific URL rather than the wildcard (*) which would allow any URL to connect to your RPC instance. Since this is a private chain that will not hold real Ether, I usually put a wildcard so I can use sites such as [Browser Solidity](http://chriseth.github.io/browser-solidity/) for my testing.
This dictates what URLs can connect to your node in order to perform RPC client tasks. Be very careful with this and type a specific URL rather than the wildcard (*) which would allow any URL to connect to your RPC instance.


``--datadir "/home/TestChain1"``
Expand All @@ -204,18 +197,21 @@ Here is an example of how these identities show up on the network.
Creating the geth Command
--------------------------------------------------------------------------------

After you have created your custom genesis block JSON file and created a directory for your chain to go into, type the following command into your console that has access to geth:
After you have created your custom genesis block JSON file and created a directory for your blockchain data, type the following command into your console that has access to geth:

.. code-block:: Console
geth --identity "MyNodeName" --genesis /path/to/CustomGenesis.json --rpc --rpcport "8080" --rpccorsdomain "*" --datadir "C:\chains\TestChain1" --port "30303" --nodiscover --rpcapi "db,eth,net,web3" --networkid 1999 console
You will need to start your geth instance with your custom chain command every time you want to access your custom chain. If you just type "geth" in your console, it will not remember all of the flags you have set. Different operating systems have ways to make this easier. Check out this page NEED LINK HERE for other geth console commands that may be applicable to your network set-up and situation.
**Note:** Please change the flags to match your custom settings.

You will need to start your geth instance with your custom chain command every time you want to access your custom chain. If you just type "geth" in your console, it will not remember all of the flags you have set.

Pre-Allocating Ether to Your Account
--------------------------------------------------------------------------------

A difficulty of "0x400" allows you to mine Ether very quickly on your private testnet chain. If you create your chain and start mining, you should have hundreds of Ether in a matter of minutes which is way more than enough to test transactions on your network. If you would still like to pre-allocate Ether to your account, you will need to:

1. Create a new Ethereum account after you create your private chain
2. Copy your new account address
3. Add the following command to your Custom_Genesis.json file:
Expand All @@ -228,16 +224,35 @@ A difficulty of "0x400" allows you to mine Ether very quickly on your private te
{ "balance": "20000000000000000000" }
}
Save your genesis file and re-run the command at the bottom of this guide to start your private chain in Geth. You are now in the Geth console. We want to assign an address as "primary" and check it's balance.
**Note:** Replace ``0x1fb891f92eb557f4d688463d0d7c560552263b5a`` with your account address.

Save your genesis file and re-run your private chain command. Once geth is fully loaded, close Geth.

We want to assign an address as "primary" and check it's balance.

Run the command ``geth account list`` in your console to see what account # your new address was assigned.

.. code-block:: Console
> geth account list
Account #0: {d1ade25ccd3d550a7eb532ac759cac7be09c2719}
Account #1: {da65665fc30803cb1fb7e6d86691e20b1826dee0}
Account #2: {e470b1a7d2c9c5c6f03bbaa8fa20db6d404a0c32}
Account #3: {f4dd5c3794f1fd0cdc0327a83aa472609c806e99}
Take note of which account # is the one that you pre-allocated Ether to.

.. code-block:: Console
> primary = eth.accounts[0];
This should return you your primary Ethereum address you created. If it does not, try settinfg primary to 1, 2, etc. until you find your address you created. Addrsses are assigned those array indexes in order of creation.
**Note:** Replace ``0`` with your account's number.
This console command should return your primary Ethereum address.

Type the following command:

.. code-block:: Console
> balance = web3.fromWei(eth.getBalance(primary), "ether");
This should return you ``20`` Ether in your account. The reason we had to put such a large number in the alloc section of your genesis file is because the "balance" field takes a number in wei which is the smallest sub-unit of Ether.
This should return you ``20`` Ether in your account. The reason we had to put such a large number in the alloc section of your genesis file is because the "balance" field takes a number in wei which is the smallest sub-unit of Ether.
1 change: 1 addition & 0 deletions source/ethereum-ecosystem/client-implementations.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ Javascript/Node `EthereumJS <http://ethereumjs.github.io/>`_
Python `Pyethereum <https://github.com/ethereum/pyethereum>`_
Java `ethereumJ <https://github.com/ethereum/ethereumj>`_
Haskell `ethereumH <https://github.com/blockapps/strato-p2p-client>`_
Rust `Parity <https://ethcore.io/parity.html>`__
=============== ===============

0 comments on commit e5af05c

Please sign in to comment.