Skip to content
This repository has been archived by the owner on Nov 30, 2021. It is now read-only.

Commit

Permalink
merge develop
Browse files Browse the repository at this point in the history
  • Loading branch information
fedekunze committed Nov 17, 2020
2 parents d3e4df2 + ab951e2 commit 189d6c7
Show file tree
Hide file tree
Showing 31 changed files with 817 additions and 189 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/release-sims.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
- name: install runsim
run: |
export GO111MODULE="on" && go get github.com/cosmos/tools/cmd/runsim@v1.0.0
- uses: actions/cache@v2.1.2
- uses: actions/cache@v2.1.3
with:
path: ~/go/bin
key: ${{ runner.os }}-go-runsim-binary
Expand All @@ -33,7 +33,7 @@ jobs:
needs: install-runsim
steps:
- uses: actions/checkout@v2
- uses: actions/cache@v2.1.2
- uses: actions/cache@v2.1.3
with:
path: ~/go/bin
key: ${{ runner.os }}-go-runsim-binary
Expand Down
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,13 @@ corresponding Ethereum API namespace:

* (evm) [\#588](https://github.com/cosmos/ethermint/pull/588) The EVM transaction CLI has been removed in favor of the JSON-RPC.

### Improvements

* (deps) [\#594](https://github.com/cosmos/ethermint/pull/594) Bump go-ethereum version to [v1.9.24](https://github.com/ethereum/go-ethereum/releases/tag/v1.9.24)

### Bug Fixes

* (ante) [\#597](https://github.com/cosmos/ethermint/pull/597) Fix incorrect fee check on `AnteHandler`.
* (evm) [\#583](https://github.com/cosmos/ethermint/pull/583) Fixes incorrect resetting of tx count and block bloom during `BeginBlock`, as well as gas consumption.
* (crypto) [\#577](https://github.com/cosmos/ethermint/pull/577) Fix `BIP44HDPath` that did not prepend `m/` to the path. This now uses the `DefaultBaseDerivationPath` variable from go-ethereum to ensure addresses are consistent.

Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,9 @@ clean:
docker-build:
docker build -t ${DOCKER_IMAGE}:${DOCKER_TAG} .
docker tag ${DOCKER_IMAGE}:${DOCKER_TAG} ${DOCKER_IMAGE}:latest
docker tag ${DOCKER_IMAGE}:${DOCKER_TAG} ${DOCKER_IMAGE}:${COMMIT_HASH}
# docker tag ${DOCKER_IMAGE}:${DOCKER_TAG} ${DOCKER_IMAGE}:${COMMIT_HASH}
# update old container
docker rm ethermint
docker rm ethermint || true
# create a new container from the latest image
docker create --name ethermint -t -i cosmos/ethermint:latest ethermint
# move the binaries to the ./build directory
Expand Down
11 changes: 6 additions & 5 deletions app/ante/eth.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,20 +98,21 @@ func (emfd EthMempoolFeeDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simula

evmDenom := emfd.evmKeeper.GetParams(ctx).EvmDenom

// fee = GP * GL
// fee = gas price * gas limit
fee := sdk.NewInt64DecCoin(evmDenom, msgEthTx.Fee().Int64())

minGasPrices := ctx.MinGasPrices()
minFees := minGasPrices.AmountOf(evmDenom).MulInt64(int64(msgEthTx.Data.GasLimit))

// check that fee provided is greater than the minimum
// NOTE: we only check if aphotons are present in min gas prices. It is up to the
// check that fee provided is greater than the minimum defined by the validator node
// NOTE: we only check if the evm denom tokens are present in min gas prices. It is up to the
// sender if they want to send additional fees in other denominations.
var hasEnoughFees bool
if fee.Amount.GTE(minGasPrices.AmountOf(evmDenom)) {
if fee.Amount.GTE(minFees) {
hasEnoughFees = true
}

// reject transaction if minimum gas price is positive and the transaction does not
// reject transaction if minimum gas price is not zero and the transaction does not
// meet the minimum fee
if !ctx.MinGasPrices().IsZero() && !hasEnoughFees {
return ctx, sdkerrors.Wrap(
Expand Down
1 change: 1 addition & 0 deletions docs/basics/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ This repository contains reference documentation on the basic concepts of Etherm
3. [Lifecycle of a transaction](./transactions.md)
4. [Photon](./photon.md)
5. [JSON-RPC Server](./json_rpc.md)
6. [Hard Spoon](./hard_spoon.md)

After reading the basics, head on to the [Core Reference](../core/README.md) for more advanced material.
2 changes: 1 addition & 1 deletion docs/basics/accounts.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ curl -X GET "<NODE_IP>/auth/accounts/eth1f8rqrfwut7ngkxwth0gt99h0lxnxsp09ngvzwl"
The Cosmos SDK Keyring output (i.e `ethermintd keys`) only supports addresses and public keys in Bech32 format.
:::

To retrieve the Ethereum hex address using Web3, use the JSON-RPC `eth_accounts` endpoint:
To retrieve the Ethereum hex address using Web3, use the JSON-RPC [`eth_accounts`](./json_rpc.md#eth-accounts) endpoint:

```bash
# query against a local node
Expand Down
81 changes: 73 additions & 8 deletions docs/basics/gas.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,88 @@ order: 3

Learn about the differences between `Gas` and `Fees` in Ethereum and Cosmos. {synopsis}

## Introduction to `Gas` in the SDK
## Pre-requisite Readings

<!-- TODO: -->
- [Cosmos SDK Gas](https://docs.cosmos.network/master/basics/gas-fees.html) {prereq}
- [Ethereum Gas](https://ethereum.org/en/developers/docs/gas/) {prereq}

The concept of Gas represents the amount of computational effort required to execute specific operations on the state machine.

Gas was created on Ethereum to disallow the EVM (Ethereum Virtual Machine) from running infinite
loops by allocating a small amount of monetary value into the system. A unit of gas, usually in a
form as a fraction of the native coin, is consumed for every operation on the EVM and requires a
user to pay for these operations. These operations consist in state transitions such as sending a
transaction or calling a contract.

Exactly like Ethereum, Cosmos utilizes the concept of gas and this is how Cosmos tracks the resource
usage of operations during execution. Operations on Cosmos are represented as read or writes done to the chain's store.

In Cosmos, a fee is calculated and charged to the user during a message execution. This fee is
calculated from the sum of all gas consumed in an message execution:

```
fee = gas * gas price
```

In both networks, gas is used to make sure that operations do not require an excess amount of
computational power to complete and as a way to deter bad-acting users from spamming the network.

## Cosmos SDK `Gas`

In the Cosmos SDK, gas is tracked in the main `GasMeter` and the `BlockGasMeter`:

- `GasMeter`: keeps track of the gas consumed during executions that lead to state transitions. It is reset on every transaction execution.
- `BlockGasMeter`: keeps track of the gas consumed in a block and enforces that the gas does not go over a predefined limit. This limit is defined in the Tendermint consensus parameters and can be changed via governance parameter change proposals.

More information regarding gas in Cosmos SDK can be found [here](https://docs.cosmos.network/master/basics/gas-fees.html).

## Matching EVM Gas consumption

<!-- TODO: -->
Ethermint is an EVM-compatible chain that supports Ethereum Web3 tooling. For this reason, gas
consumption must be equitable in order to accurately calculate the state transition hashes and exact
the behaviour that would be seen on the main Ethereum network (main net).

In Cosmos, there are types of operations that are not triggered by transactions that can also result in state transitions. Concrete examples are the `BeginBlock` and `EndBlock` operations and the `AnteHandler` checks, which might also read and write to the store before running the state transition from a transaction.

### `BeginBlock` and `EndBlock`

These operations are defined by the Tendermint Core's Application Blockchain Interface (ABCI) and are defined by each Cosmos SDK module. As their name suggest, they are executed at the beginning and at the end of each block processing respectively (i.e pre and post transaction execution). Since these operations are not reflected on Ethereum, to match the the gas consumption we reset the main `GasMeter` to 0 on Ethermint's EVM module.

### `AnteHandler`

The Cosmos SDK [`AnteHandler`](https://docs.cosmos.network/master/basics/gas-fees.html#antehandler)
performs basic checks prior to transaction execution. These checks are usually signature
verification, transaction field validation, transaction fees, etc.

Because the gas calculated in Ethermint is done by the `IntrinsicGas` method from go-ethereum, a
special `AnteHandler` that is customized for EVM transaction fee verification is required. This
allows Ethermint to generate the expected gas costs for operations done in the network and scale the
gas costs as it would in the Ethereum network.

## Gas Refunds

## Gas refunds
In Ethereum, gas can be specified prior to execution and the remaining gas will be refunded back to
the user if any gas is left over - should fail with out of gas if not enough gas was provided. In
Ethermint, the concept of gas refunds does not exist and the fees paid is not refunded in part back
to the user. The fees exacted on a transaction will be collected by the validator and no refunds are
issued. Thus, it is extremely important to use the correct gas.

<!-- TODO: -->
To prevent overspending on fees, providing the `--gas-adjustment` flag for a cosmos transactions
will determine the fees automatically. Also the `eth_estimateGas` rpc call can be used to manually
get the correct gas costs for a transaction.

## AnteHandler
## 0 Fee Transactions

The `AnteHandler` is a special `handler` that is run for every transaction during `CheckTx` and `DeliverTx`, before the `handler` of each `message` in the transaction. `AnteHandler`s have a different signature than `handler`s
In Cosmos, a minimum gas price is not enforced by the `AnteHandler` as the `min-gas-prices` is
checked against the local node/validator. In other words, the minimum fees accepted are determined
by the validators of the network, and each validator can specify a different value for their fees.
This potentially allows end users to submit 0 fee transactions if there is at least one single
validator that is willing to include transactions with `0` gas price in their blocks proposed.

<!-- TODO: -->
For this same reason, in Ethermint it is possible to send transactions with `0` fees for transaction
types other than the ones defined by the `evm` module. EVM module transactions cannot have `0` fees
as gas is required inherently by Ethereum. This check is done by the evm transactions
`ValidateBasic` function as well as on the custom `AnteHandler` defined by Ethermint.

## Next {hide}

Expand Down
17 changes: 17 additions & 0 deletions docs/basics/hard_spoon.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!--
order: 6
-->

# Hard Spoon

Learn about Ethermint's Hard Spoon functionality. {synopsis}

## Hard Spoon on Ethermint

A [hard spoon](https://blog.cosmos.network/introducing-the-hard-spoon-4a9288d3f0df) is the migration of the snapshot of a target blockchain's state into a new chain. The state of a network can be imported (or "scooped") onto a separate chain to accurately replicate the account balances and other information from the state.

Ethermint's Hard Spoon tool (currently under specification) is targeted to import the state of the Ethereum mainnet to replicate the state for the given accounts. This will allow anyone on the Ethereum network to import their contracts into Ethermint.

## Next {hide}

Learn about the [encoding](./../core/encoding.md) formats used on Ethermint {hide}
2 changes: 1 addition & 1 deletion docs/basics/json_rpc.md
Original file line number Diff line number Diff line change
Expand Up @@ -895,4 +895,4 @@ curl -X POST --data '{"jsonrpc":"2.0","method":"personal_ecRecover","params":["0

## Next {hide}

Learn about the [encoding](./../core/encoding.md) formats used on Ethermint {hide}
Learn about the Ethermint [Hard Spoon](./hard_spoon.md) functionality {hide}
4 changes: 2 additions & 2 deletions docs/basics/transactions.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ responsible for performing preliminary message execution business logic such as
signature verification, etc. This is particular to Cosmos SDK routed transactions. Ethereum routed
transactions will bypass this as the EVM handles the same business logic.

Ethereum routed transactions coming from a web3 source are expected to be RLP encoded, however all
Ethereum routed transactions coming from a Web3 source are expected to be [RLP](./../core/encoding.md#rlp) encoded, however all
internal interaction between Ethermint and Tendermint will utilize one of the supported encoding
formats: Protobuf, Amino or Hybrid of the previous two.
formats: [Protobuf](./../core/encoding.md#protocol-buffers) and [Amino](./../core/encoding.md#amino).

## Transaction formats

Expand Down
1 change: 1 addition & 0 deletions docs/core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ parent:
This repository contains reference documentation on the core concepts of Ethermint.

1. [Encoding](./encoding.md)
2. [Pending State](./pending_state.md)

After reading the core concepts, head on to the [guides](../guides/README.md) to learn how to use Ethereum tooling with Ethermint.
69 changes: 19 additions & 50 deletions docs/core/encoding.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,70 +4,39 @@ order: 1

# Encoding

The `codec` is used everywhere in the Cosmos SDK to encode and decode structs and interfaces. The specific codec used in the Cosmos SDK is called `go-amino`. {synopsis}
Learn about the encoding formats used on Ethermint. {synopsis}

## Pre-requisite Readings

- [Cosmos SDK Encoding](https://docs.cosmos.network/master/core/encoding.html) {prereq}
- [Ethereum RLP](https://eth.wiki/en/fundamentals/rlp) {prereq}

## Encoding Formats

The Cosmos SDK utilizes two binary wire encoding protocols, [Amino](https://github.com/tendermint/go-amino/)
and [Protocol Buffers](https://developers.google.com/protocol-buffers), where Amino
is an object encoding specification. It is a subset of Proto3 with an extension for
interface support. See the [Proto3 spec](https://developers.google.com/protocol-buffers/docs/proto3)
for more information on Proto3, which Amino is largely compatible with (but not with Proto2).
### Protocol Buffers

Due to Amino having significant performance drawbacks, being reflection-based, and
not having any meaningful cross-language/client support, Protocol Buffers, specifically
[gogoprotobuf](https://github.com/gogo/protobuf/), is being used in place of Amino.
Note, this process of using Protocol Buffers over Amino is still an ongoing process.

Binary wire encoding of types in the Cosmos SDK can be broken down into two main
categories, client encoding and store encoding. Client encoding mainly revolves
around transaction processing and signing, whereas store encoding revolves around
types used in state-machine transitions and what is ultimately stored in the Merkle
tree.

For store encoding, protobuf definitions can exist for any type and will typically
have an Amino-based "intermediary" type. Specifically, the protobuf-based type
definition is used for serialization and persistence, whereas the Amino-based type
is used for business logic in the state-machine where they may converted back-n-forth.
Note, the Amino-based types may slowly be phased-out in the future so developers
should take note to use the protobuf message definitions where possible.

In the `codec` package, there exists two core interfaces, `Marshaler` and `ProtoMarshaler`,
where the former encapsulates the current Amino interface except it operates on
types implementing the latter instead of generic `interface{}` types.

In addition, there exists three implementations of `Marshaler`. The first being
`AminoCodec`, where both binary and JSON serialization is handled via Amino. The
second being `ProtoCodec`, where both binary and JSON serialization is handled
via Protobuf. Finally, `HybridCodec`, a codec that utilizes Protobuf for binary
serialization and Amino for JSON serialization. The `HybridCodec` is typically
the codec that used in majority in situations as it's easier to use for client
and state serialization.

This means that modules may use Amino or Protobuf encoding but the types must
implement `ProtoMarshaler`. If modules wish to avoid implementing this interface
for their types, they may use an Amino codec directly.
The Cosmos [Stargate](https://stargate.cosmos.network/) release introduces
[protobuf](https://developers.google.com/protocol-buffers) as the main encoding format for both
client and state serialization. All the EVM module structs that are used for state and clients
(transaction messages, genesis, query services, etc) will be implemented as protocol buffer messages.

### Amino

Every module uses an Amino codec to serialize types and interfaces. This codec typically
has types and interfaces registered in that module's domain only (e.g. messages),
but there are exceptions like `x/gov`. Each module exposes a `RegisterLegacyAminoCodec` function
that allows a user to provide a codec and have all the types registered. An application
will call this method for each necessary module.

### Protobuf
The Cosmos SDK also supports the legacy Amino encoding format for backwards compatibility with
previous versions, specially for client encoding. Ethermint will not support Amino in the EVM module
once the migration to SDK `v0.40` is finalized.

<!-- TODO: -->
### RLP

## RLP
Recursive Length Prefix ([RLP](https://eth.wiki/en/fundamentals/rlp)), is an encoding/decoding algorithm that serializes a message and
allows for quick reconstruction of encoded data. Ethermint uses RLP to encode/decode Ethereum
messages for JSON-RPC handling to conform messages to the proper Ethereum format. This allows
messages to be encoded and decoded in the exact format as Ethereum's.

<!-- TODO: -->
Each message type defined on the EVM module define the `EncodeRLP` and `DecodeRLP` methods which
implement the `rlp.Encoder` and `rlp.Decoder` interfaces respectively. The RLP encode method is used
to sign bytes and transactions in `RLPSignBytes` and `Sign`.

## Next {hide}

Learn how to deploy a Solidity smart contract on Ethermint using [Truffle](./../guides/truffle.md) {hide}
Learn how [pending state](./pending_state.md) is handled on Ethermint. {hide}
49 changes: 49 additions & 0 deletions docs/core/pending_state.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<!--
order: 2
-->

# Pending State

Learn how Ethermint handles pending state queries. {synopsis}

## Pre-requisite Readings

- [Tendermint Mempool](https://docs.tendermint.com/master/tendermint-core/mempool.htm) {prereq}

## Ethermint vs Ethereum

In Ethereum, pending blocks are generated as they are queued for production by miners. These pending
blocks include pending transactions that are picked out by miners, based on the highest reward paid
in gas. This mechanism exists as block finality is not possible on the Ethereum network. Blocks are
committed with probabilistic finality, which means that transactions and blocks become less likely
to become reverted as more time (and blocks) passes.

Ethermint is designed quite differently on this front as there is no concept of a "pending state".
Ethermint uses [Tendermint Core](https://docs.tendermint.com/) BFT consensus which provides instant
finality for transaction. For this reason, Etheremint does not require a pending state mechanism, as
all (if not most) of the transactions will be committed to the next block (avg. block time on Cosmos chains is ~8s). However, this causes a
few hiccups in terms of the Ethereum Web3-compatible queries that can be made to pending state.

Another significant difference with Ethereum, is that blocks are produced by validators or block producers, who include transactions from their local mempool into blocks in a
first-in-first-out (FIFO) fashion. Transactions on Ethermint cannot be ordered or cherry picked out from the Tendermint node [mempool](https://docs.tendermint.com/master/tendermint-core/mempool.html#transaction-ordering).

## Pending State Queries

Ethermint will make queries which will account for any unconfirmed transactions present in a node's
transaction mempool. A pending state query made will be subjective and the query will be made on the
target node's mempool. Thus, the pending state will not be the same for the same query to two
different nodes.

### RPC Calls on Pending Transactions

- [`eth_getBalance`](./../basics/json_rpc.md#eth_getbalance)
- [`eth_getTransactionCount`](./../basics/json_rpc.md#eth-gettransactioncount)
- [`eth_getBlockTransactionCountByNumber`](./../basics/json_rpc.md#eth-getblocktransactioncountbynumber)
- [`eth_getBlockByNumber`](./../basics/json_rpc.md#eth-getblockbynumber)
- [`eth_getTransactionByHash`](./../basics/json_rpc.md#eth-gettransactionbyhash)
- [`eth_getTransactionByBlockNumberAndIndex`](./../basics/json_rpc.html#eth-gettransactionbyblockhashandindex)
- [`eth_sendTransaction`](./../basics/json_rpc.md#eth-sendtransaction)

## Next {hide}

Learn how to deploy a Solidity smart contract on Ethermint using [Truffle](./../guides/truffle.md) {hide}
1 change: 1 addition & 0 deletions docs/intro/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ This folder contains introduction material for Ethermint.

1. [Overview](./overview.md)
1. [Architecture](./architecture.md)
1. [Use Cases](./use_cases.md)
1. [Resources](./resources.md)

After reading the introduction material, head over to the [basics](../basics/README.md) to learn more.
Expand Down
Loading

0 comments on commit 189d6c7

Please sign in to comment.