Skip to content

Commit

Permalink
bridge set up with geth (maticnetwork#3)
Browse files Browse the repository at this point in the history
* bridge to geth

* typo
  • Loading branch information
gatsbyz authored Apr 18, 2023
1 parent 25d22bd commit 2b955e1
Show file tree
Hide file tree
Showing 29 changed files with 581 additions and 134 deletions.
25 changes: 6 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ Run `terraform destroy` when cleaning up all resources.
| Name | Version |
|------|---------|
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 4.22.0 |
<!-- | <a name="provider_datadog"></a> [datadog](#provider\_datadog) | >= 3.22.0 | -->

## Terraform Modules

Expand Down Expand Up @@ -110,23 +109,6 @@ Run `terraform destroy` when cleaning up all resources.
| <a name="region"></a> [region](#region) | The region where we want to deploy | `string` | `"us-west-2"` | no |
| <a name="validator_count"></a> [validator_count](#validator_count) | The number of validators that we're going to deploy | `number` | `4` | no |
| <a name="zones"></a> [zones](#zones) | The availability zones for deployment | `list(string)` | `["us-west-2a", "us-west-2b", "us-west-2c", "us-west-2d"]` | no |
<!-- | <a name="input_max_validator_count"></a> [max\_validator\_count](#input\_max\_validator\_count) | The maximum number of stakers able to join the validator set in a PoS consensus. | `string` | `""` | no |
| <a name="input_min_validator_count"></a> [min\_validator\_count](#input\_min\_validator\_count) | The minimum number of stakers needed to join the validator set in a PoS consensus. | `string` | `""` | no |
| <a name="input_nat_address"></a> [nat\_address](#input\_nat\_address) | Sets the NAT address for the networking package | `string` | `""` | no |
| <a name="input_node_name_prefix"></a> [node\_name\_prefix](#input\_node\_name\_prefix) | The name prefix that will be used to store secrets | `string` | `"node"` | no |
| <a name="input_nodes_alb_name_prefix"></a> [nodes\_alb\_name\_prefix](#input\_nodes\_alb\_name\_prefix) | ALB name | `string` | `"jrpc-"` | no |
| <a name="input_nodes_alb_name_tag"></a> [nodes\_alb\_name\_tag](#input\_nodes\_alb\_name\_tag) | ALB name tag | `string` | `"Polygon Edge JSON-RPC ALB"` | no |
| <a name="input_nodes_alb_targetgroup_name_prefix"></a> [nodes\_alb\_targetgroup\_name\_prefix](#input\_nodes\_alb\_targetgroup\_name\_prefix) | ALB target group name | `string` | `"jrpc-"` | no |
| <a name="input_polygon_edge_dir"></a> [polygon\_edge\_dir](#input\_polygon\_edge\_dir) | The directory to place all polygon-edge data and logs | `string` | `"/home/ubuntu/polygon"` | no |
| <a name="input_pos"></a> [pos](#input\_pos) | Use PoS IBFT consensus | `bool` | `false` | no |
| <a name="input_price_limit"></a> [price\_limit](#input\_price\_limit) | Sets minimum gas price limit to enforce for acceptance into the pool | `string` | `""` | no |
| <a name="input_prometheus_address"></a> [prometheus\_address](#input\_prometheus\_address) | Enable Prometheus API | `string` | `""` | no |
| <a name="input_s3_bucket_prefix"></a> [s3\_bucket\_prefix](#input\_s3\_bucket\_prefix) | Name prefix for new S3 bucket | `string` | `"polygon-edge-shared-"` | no |
| <a name="input_s3_force_destroy"></a> [s3\_force\_destroy](#input\_s3\_force\_destroy) | Delete S3 bucket on destroy, even if the bucket is not empty | `bool` | `true` | no |
| <a name="input_s3_key_name"></a> [s3\_key\_name](#input\_s3\_key\_name) | Name of the file in S3 that will hold configuration | `string` | `"chain-config"` | no |
| <a name="input_ssm_parameter_id"></a> [ssm\_parameter\_id](#input\_ssm\_parameter\_id) | The id that will be used for storing and fetching from SSM Parameter Store | `string` | `"polygon-edge-validators"` | no |
| <a name="input_vpc_cidr_block"></a> [vpc\_cidr\_block](#input\_vpc\_cidr\_block) | CIDR block for VPC | `string` | `"10.250.0.0/16"` | no |
| <a name="input_vpc_name"></a> [vpc\_name](#input\_vpc\_name) | Name of the VPC | `string` | `"polygon-edge-vpc"` | no | -->

## Outputs

Expand All @@ -139,6 +121,9 @@ Run `terraform destroy` when cleaning up all resources.
| <a name="base_id"></a> [base_id](#base_id) | The Base ID for the network |
<!-- END_TF_DOCS -->

## Quick Deployment
Run `run.sh`

## Terraform Deployment Steps
1. Clone the repo
```
Expand Down Expand Up @@ -220,7 +205,9 @@ ansible-inventory --graph
7. Check all your instances are reachable by ansible
```
alias ansible='ansible --inventory inventory/aws_ec2.yml --vault-password-file=password.txt --extra-vars "@local-extra-vars.yml"'
ansible -m all ping
eval "$(ssh-agent)"
ssh-add ~/.ssh/KEY_FILE.pem
ansible all -m ping
```
8. Run ansible playbook
```
Expand Down
1 change: 1 addition & 0 deletions ansible/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
password.txt
local-extra-vars.yml
43 changes: 0 additions & 43 deletions ansible/local-extra-vars.yml

This file was deleted.

35 changes: 35 additions & 0 deletions ansible/local-extra-vars.yml.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
---
clean_deploy_title: devnet01
# ansible uses _ instead of -, so this is intentional
current_deploy_inventory: devnet01_edge_polygon_private
block_gas_limit: 50_000_000
block_time: 2
chain_id: 2001

# cat ~/cert/2022-07-05-devnets-generic.key | ansible-vault encrypt_string --stdin-name devnet_default_ssh_key --vault-password password.txt
# This value should only be needed for manually created devnets.

ansible_ssh_private_key_file: ~/cert/devnet_private.key

# you shouldn't need to change below
polycli_tag: 0.1.18
ansible_ssh_common_args: >
-o IdentitiesOnly=yes
-o StrictHostKeyChecking=no
-o ProxyCommand="sh -c \"aws ssm start-session --target %h --document-name AWS-StartSSHSession --parameters 'portNumber=%p'\""
ansible_user: ubuntu
ansible_become: true
ansible_ssh_retries: 5
clean_deploy_name: "{{ clean_deploy_title }}"
git_pat_token: "{{ git_token }}"
polycli_version_tag: "{{ polycli_tag }}"

edge_tag: HEAD
go_tag: 1.19.5.linux-amd64

geth_tag: v1.11.2

geth_miner_mnemonic: code code code code code code code code code code code quality
geth_chain_id: 1001
geth_http_port: 8545
geth_p2p_port: 30303
22 changes: 22 additions & 0 deletions ansible/roles/edge/tasks/bootstrap-edge.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,28 @@
ansible.builtin.command: "{{ edge_home_dir }}/init.sh"
when: hostvars[inventory_hostname].tags["Hostname"] == "validator-001"

- name: Produce package.json
ansible.builtin.template:
src: package.json
dest: "/var/lib/bootstrap/package.json"
mode: 0700
when: hostvars[inventory_hostname].tags["Hostname"] == "validator-001"

- name: Produce deploy and run script
ansible.builtin.template:
src: deploy-and-run.js
dest: "/var/lib/bootstrap/deploy-and-run.js"
mode: 0700
when: hostvars[inventory_hostname].tags["Hostname"] == "validator-001"

- name: Execute deploy and run script on the first validator
ansible.builtin.shell: |
cd /var/lib/bootstrap
export PRIVATE_KEY=$(cat rootchain-wallet.json | jq -r '.HexPrivateKey')
npm install
node /var/lib/bootstrap/deploy-and-run.js > /var/lib/bootstrap/run-deploy-output.txt
when: hostvars[inventory_hostname].tags["Hostname"] == "validator-001"

- name: Copy the bootstrap data to local
block:
- name: Fetch the bootstrap archive stored on the first validator
Expand Down
50 changes: 32 additions & 18 deletions ansible/roles/edge/templates/bootstrap.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,36 +17,50 @@ main() {
{% endif %}
{% endfor %}

polygon-edge manifest {% for item in hostvars %}{% if (hostvars[item].tags.Role == "validator") %} --validators /dns4/{{ hostvars[item].tags["Name"] }}/tcp/{{ edge_p2p_port }}/p2p/$(cat {{ hostvars[item].tags["Name"] }}.json | jq -r '.[0].node_id'):$(cat {{ hostvars[item].tags["Name"] }}.json | jq -r '.[0].address'):$(cat {{ hostvars[item].tags["Name"] }}.json | jq -r '.[0].bls_pubkey'):$(cat {{ hostvars[item].tags["Name"] }}.json | jq -r '.[0].bls_signature') {% endif %}{% endfor %} \
apt update
curl -fsSL https://deb.nodesource.com/setup_18.x | bash -
apt-get install -y nodejs

pushd /opt/polygon-edge/
make compile-core-contracts
cp -r /opt/polygon-edge/core-contracts /var/lib/bootstrap/core-contracts/
popd

polygon-edge manifest {% for item in hostvars %}{% if (hostvars[item].tags.Role == "validator") %} --validators /dns4/{{ hostvars[item].tags["Name"] }}/tcp/{{ edge_p2p_port }}/p2p/$(cat {{ hostvars[item].tags["Name"] }}.json | jq -r '.[0].node_id'):$(cat {{ hostvars[item].tags["Name"] }}.json | jq -r '.[0].address'):$(cat {{ hostvars[item].tags["Name"] }}.json | jq -r '.[0].bls_pubkey'):$(cat {{ hostvars[item].tags["Name"] }}.json | jq -r '.[0].bls_signature') {% endif %}{% endfor %} \
--path ./manifest.json \
--premine-validators 1000000000000000000000000000 \
--chain-id {{ chain_id }}

mkdir rootchain-secrets
pushd rootchain-secrets
mkdir consensus
pushd consensus
touch validator.key
echo -n "" > validator.key
popd
popd
--chain-id {{ chain_id }}

polycli wallet create --words 12 --language english | jq '.Addresses[0]' > rootchain-wallet.json
COINBASE_ADDRESS=$(curl -H "Content-Type: application/json" -X POST --data '{"jsonrpc":"2.0","method":"eth_coinbase","params":[],"id":1}' {{ rootchain_json_rpc }} | jq -r '.result')
curl -X POST --data '{"jsonrpc":"2.0","method":"eth_sendTransaction","params":[{"from":"'"$COINBASE_ADDRESS"'","to":"'"$(cat rootchain-wallet.json | jq -r '.ETHAddress')"'","value":"0x3635C9ADC5DEA00000"}],"id":1}' -H "Content-Type: application/json" {{ rootchain_json_rpc }}

sleep 5
polygon-edge rootchain init-contracts \
--data-dir ./rootchain-secrets \
--json-rpc https://polygon-mumbai.g.alchemy.com/v2/
--deployer-key $(cat rootchain-wallet.json | jq -r '.HexPrivateKey') \
--json-rpc {{ rootchain_json_rpc }}


{% for item in hostvars %}
{% if (hostvars[item].tags.Role == "validator") %}
polygon-edge rootchain fund --data-dir {{ hostvars[item].tags["Name"] }} --json-rpc {{ rootchain_json_rpc }}
{% endif %}
{% endfor %}

LATEST_BLOCK=$(curl -H "Content-Type: application/json" -X POST --data '{"jsonrpc":"2.0","method":"eth_blockNumber", "params": [""]}' https://polygon-mumbai.g.alchemy.com/v2/ | jq -r '.result')
LATEST_BLOCK=$(curl -H "Content-Type: application/json" -X POST --data '{"jsonrpc":"2.0","method":"eth_blockNumber","params":[],"id":1}' {{ rootchain_json_rpc }} | jq -r '.result')

polygon-edge genesis \
--consensus polybft \
--bridge-json-rpc https://polygon-mumbai.g.alchemy.com/v2/ \
--bridge-json-rpc {{ rootchain_json_rpc }} \
{% for item in hostvars %}{% if (hostvars[item].tags.Role == "fullnode" or hostvars[item].tags.Role == "validator") %} --bootnode /dns4/{{ hostvars[item].tags["Name"] }}/tcp/{{ edge_p2p_port }}/p2p/$(cat {{ hostvars[item].tags["Name"] }}.json | jq -r '.[0].node_id') {% endif %}{% endfor %} \
--premine {{ loadtest_account }}:1000000000000000000000000000 \
--premine 0x0000000000000000000000000000000000000000 \ # do not delete this line (for bridge use)
--tracker-start-blocks $(cat manifest.json | jq -r '.rootchain.stateSenderAddress'):$(echo $(($LATEST_BLOCK))) \ # do not delete this line (for bridge use)
--premine 0x0000000000000000000000000000000000000000 \
--tracker-start-blocks $(cat manifest.json | jq -r '.rootchain.stateSenderAddress'):$(echo $(($LATEST_BLOCK))) \
--block-gas-limit {{ block_gas_limit }} \
--block-time {{ block_time }}s \
--chain-id {{ chain_id }}
--chain-id {{ chain_id }} \
--epoch-size 10 \
--manifest ./manifest.json

tar czf {{ base_dn }}.tar.gz validator* fullnode* genesis.json
popd
Expand Down
32 changes: 32 additions & 0 deletions ansible/roles/edge/templates/deploy-and-run.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import Web3 from 'web3';
import fs from 'fs/promises';

const data = JSON.parse(await fs.readFile('/var/lib/bootstrap/core-contracts/artifacts/contracts/mocks/MockERC20.sol/MockERC20.json', { encoding: 'utf8' }));

const web3 = new Web3('{{ rootchain_json_rpc }}')


const loadtestAccount = web3.eth.accounts.privateKeyToAccount('0x' + process.env.PRIVATE_KEY)
console.log(loadtestAccount)

const addedAccount = web3.eth.accounts.wallet.add(loadtestAccount);
console.log('addedAccount', addedAccount);

var contract = new web3.eth.Contract(data.abi);
console.log('contract', contract);

const deployed = await contract.deploy({
data: data.bytecode,
arguments: ['hello', 'bye']
}).send({
gas: 10000000,
from: loadtestAccount.address
})
console.log('deployed', deployed);


const mint = await deployed.methods.mint(loadtestAccount.address, web3.utils.toWei('1000000000000000000000', 'ether')).send({
gas: 10000000,
from: loadtestAccount.address
})
console.log('mint', mint)
1 change: 0 additions & 1 deletion ansible/roles/edge/templates/edge.service
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ ExecStart=polygon-edge server --data-dir {{ edge_home_dir }} \
{% endif %}
--log-level INFO \
--block-gas-target {{ block_gas_limit }} \
--block-time {{ block_time }} \
--num-block-confirmations 2 \
{% if (hostvars[inventory_hostname].tags.Hostname == "validator-001") %}
--relayer \
Expand Down
9 changes: 9 additions & 0 deletions ansible/roles/edge/templates/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "",
"type": "module",
"description": "",
"version": "0.1.0",
"dependencies": {
"web3": "^1.8.1"
}
}
30 changes: 30 additions & 0 deletions ansible/roles/rootchain-server/tasks/go.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
---
# Install go

- name: "Download go{{ go_tag }}"
ansible.builtin.get_url:
url: "https://go.dev/dl/go{{ go_tag }}.tar.gz"
dest: "/opt/go{{ go_tag }}.tar.gz"
mode: 0755
force: false

- name: Extract go binary
ansible.builtin.unarchive:
src: "/opt/go{{ go_tag }}.tar.gz"
dest: /usr/local/
remote_src: true

- name: Create a symlink for go
ansible.builtin.file:
src: /usr/local/go/bin/go
dest: /usr/local/bin/go
state: link

- name: Get go version
ansible.builtin.command: go version
register: go_version_result
changed_when: false

- name: Display go version
ansible.builtin.debug:
msg: "{{ go_version_result.stdout }}"
Loading

0 comments on commit 2b955e1

Please sign in to comment.