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

Plasma POC #1128

Merged
merged 176 commits into from
Sep 18, 2019
Merged

Plasma POC #1128

merged 176 commits into from
Sep 18, 2019

Conversation

T-Dnzt
Copy link

@T-Dnzt T-Dnzt commented Aug 2, 2019

Issues: #987, #989
Closes #987 #989

Overview

This PR introduces an early POC of a plasma implementation in the eWallet.
It covers 2 main features:

  • Deposit funds in the rootchain smart contract
  • Transfer funds on plasma from the primary hot wallet

And a smaller one: The updated blockchain_wallet.get_balances endpoint

Warning: This plasma integration is not meant to be used in production!

Implementation details

A new sub-app has been created eth_elixir_omg_adapter that takes care of the communication with the plasma watcher and contains some logic to format and encode transactions.

Some refactor was also done on the way to pass blockchain adapter attributes to the blockchain sub-app.

Deposit flow

An eth deposit is made by calling the deposit(bytes) or depositFrom(bytes) functions on the rootchain contract with a valid encoded tx_bytes:

  • Call endpoint blockchain_wallet.deposit_to_childchain with the following params:
{
  "token_id": "tok_ETH_01dft2tk077p5chqx1d05v3epz",
  "amount": 100,
  "address": "0x48b91d5f363892592bf836777dc73b54a10b72ae",
  "idempotency_token": "7831c0be5-15d1-4463-9ec2-02bc8ded7120"
}
  • The BlockchainWalletController performs some checks in deposit_to_childchain func and forwards the call to ChildchainTransactionGate.deposit/2
  • ChildchainTransactionGate.deposit/2 formats the given params and set the type to deposit then forward to BlockchainTransactionGate.create/3
  • BlockchainTransactionGate.create/3 builds the required transaction attrs, records the transaction in the DB, then forward the transaction to the blockchain by calling BlockchainHelper.call(:deposit_to_childchain, attrs) and finally starts the tracker to monitor the tx status on the blockchain.
  • EthBlockchain.Childchain.deposit/2 receives the deposit and checks if the given childchain is valid, then get the encoded transaction bytes by calling EthElixirOmgAdapter.Transaction.get_deposit_tx_bytes/3
  • Finally, if the deposit is ETH, the transaction is submitted to EthBlockchain.Transaction.deposit_eth/2 and to EthBlockchain.Transaction.deposit_erc20/2if it's an ERC20.

Note: For ERC20 deposits the funds must be approved (ERC20 spec) before they can be deposited to plasma. This is done in EthBlockchain.Transaction.deposit_erc20/2

Transfer flow:

Plasma transfers use the same endpoint as internal and ethereum transfer but require the blockchain_identifier param to be elixir_omg.
The flow is similar to ethereum transfer until the transaction is submitted to the blockchain sub-app where it's handled by EthBlockchain.Childchain.send/2.
The transfer logic is in EthElixirOmgAdapter.Transaction where the transaction is first created using the watcher's transaction.create API that returns a pre-formatted transaction that needs to be signed and submitted to the transaction.submit_typed API.

Usage

This PR can be tested using swagger but requires some setup. You will need to clone and run elixir-omg (master branch) with docker, then:

Go to the elixir-omg API spec, change the server to http://localhost:7434, then execute the call to /status.get.

Take the plasma rootchain contract address from the response (contract_addr) and replace it in ewallet/apps/eth_elixir_omg_adapter/config/dev.exs.

In an elixir console

iex -S mix
address = EWalletDB.BlockchainWallet.get_primary_hot_wallet("ethereum").address

private_key =  Keychain.Key.private_key_for_wallet(address)

funding_amount = trunc(:math.pow(10, 18) * 100)

{:ok, _addr} = Ethereumex.HttpClient.request("personal_importRawKey", [private_key, "password"], [])

{:ok, [default_faucet | _]} = Ethereumex.HttpClient.eth_accounts()

Ethereumex.HttpClient.eth_send_transaction(%{from: default_faucet, to: address, value: Utils.Helpers.Encoding.to_hex(funding_amount)})

Wait until the previous call succeed!

Then if you don't have an ERC20 token yet, deploy one using swagger's token.deploy_erc20 call.

Known problem

After making a transaction, you will see some errors in the elixir-omg log saying that the authority address needs to be unlocked. Copy the address and in a new terminal attach a geth console to the running node:

geth attach http://localhost:8545

Then unlock the account (the default passphrase is ThisIsATestnetPassphrase):

personal.unlockAccount("0xto_replace_with_authority_address", "ThisIsATestnetPassphrase", 0)

Copy link
Contributor

@unnawut unnawut left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Quick stuff first...

tools/local_geth_testnet/genesis.json Show resolved Hide resolved
apps/eth_geth_adapter/test/support/test_helper.exs Outdated Show resolved Hide resolved
apps/eth_elixir_omg_adapter/mix.exs Outdated Show resolved Hide resolved
@unnawut unnawut moved this from 1-Inbox to 3-In Progress in eWallet Sep 11, 2019
@mederic-p mederic-p moved this from 3-In Progress to 4-Review in eWallet Sep 11, 2019
Copy link
Author

@T-Dnzt T-Dnzt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved. Few small comments, also wondering if we should have one full happy path integration test for OmiseGO Network deposit (and one for transactions). Good job!

PS: I can't approve 'cause I opened the PR :)

apps/eth_blockchain/lib/eth_blockchain/childchain.ex Outdated Show resolved Hide resolved
apps/eth_blockchain/lib/eth_blockchain/childchain.ex Outdated Show resolved Hide resolved
apps/eth_blockchain/lib/eth_blockchain/childchain.ex Outdated Show resolved Hide resolved
apps/eth_blockchain/lib/eth_blockchain/childchain.ex Outdated Show resolved Hide resolved
transaction_index: Enum.at(success_body["outputs"], 1)["txindex"],
utxo_position: Enum.at(success_body["outputs"], 1)["utxo_pos"]
}
],
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👀

@mederic-p mederic-p merged commit 8a3e3bb into eth-blockchain Sep 18, 2019
eWallet automation moved this from 4-Review to 5-Done Sep 18, 2019
@mederic-p mederic-p deleted the plasma-poc branch September 18, 2019 06:28
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
eWallet
  
5-Done
Development

Successfully merging this pull request may close these issues.

None yet

3 participants