Skip to content

Commit

Permalink
fix(devnet): rsk genesis with EOAs
Browse files Browse the repository at this point in the history
  • Loading branch information
jurajpiar committed Mar 4, 2024
1 parent e30e549 commit c57afb0
Show file tree
Hide file tree
Showing 10 changed files with 121 additions and 77 deletions.
66 changes: 52 additions & 14 deletions bedrock-devnet/devnet/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ def main():
devnet_config_template_path = pjoin(deploy_config_dir, 'regtest-template.json')
ops_chain_ops = pjoin(monorepo_dir, 'op-chain-ops')
sdk_dir = pjoin(monorepo_dir, 'packages', 'sdk')
genesis_dir=pjoin(devnet_dir, 'genesis')

paths = Bunch(
mono_repo_dir=monorepo_dir,
Expand All @@ -80,8 +81,9 @@ def main():
ops_bedrock_dir=ops_bedrock_dir,
ops_chain_ops=ops_chain_ops,
sdk_dir=sdk_dir,
genesis_l1_path=pjoin(devnet_dir, 'genesis-l1.json'),
genesis_l2_path=pjoin(devnet_dir, 'genesis-l2.json'),
genesis_l1_path=pjoin(genesis_dir, 'l1.json'),
genesis_rsk_path=pjoin(genesis_dir, 'rsk-dev.json'),
genesis_l2_path=pjoin(genesis_dir, 'l2.json'),
allocs_path=pjoin(devnet_dir, 'allocs-l1.json'),
addresses_json_path=pjoin(devnet_dir, 'addresses.json'),
sdk_addresses_json_path=pjoin(devnet_dir, 'sdk-addresses.json'),
Expand All @@ -93,7 +95,7 @@ def main():
devnet_test(paths)
return

os.makedirs(devnet_dir, exist_ok=True)
os.makedirs(genesis_dir, exist_ok=True)

if args.allocs:
generate_allocs(paths)
Expand Down Expand Up @@ -224,7 +226,7 @@ def merge_rsk_genesis(generated_genesis, paths: Bunch):

return {**default_genesis, **generated_genesis}

def format_genesis_for_rsk(genesis_json, paths):
def format_genesis_for_rsk(genesis_json):
log.info('Fromatting generated genesis file for use in RSK')
base_genesis_keys = {
'coinbase',
Expand Down Expand Up @@ -254,9 +256,9 @@ def format_genesis_for_rsk(genesis_json, paths):
valid_alloc = {}
alloc = allocs[key]
if 'balance' in alloc:
valid_alloc['balance'] = alloc['balance'][2:]
valid_alloc['balance'] = str(int(alloc['balance'], 16))
if 'nonce' in alloc:
valid_alloc['nonce'] = alloc['nonce'][2:]
valid_alloc['nonce'] = str(int(alloc['nonce'], 16))
if 'code' in alloc:
valid_alloc['contract'] = {
'code': alloc['code'][2:]
Expand Down Expand Up @@ -295,16 +297,13 @@ def generate_allocs(paths: Bunch):
if err:
raise Exception(f"Exception occurred in child process: {err}")

# need this to get the latest state root. However I am not sure that this is reliable enough. Maybe getting it from tx would be better?
res = getLatestBlock('127.0.0.1:8545')
response = json.loads(res)
log.info(f'latest block response: {response}')
state_root = response['result']['stateRoot']
latest_block = json.loads(getLatestBlock('127.0.0.1:8545'))['result']
log.info(f'latest block: {latest_block}')
state_root = latest_block['stateRoot']
log.info(f'state_root: {state_root}')

rsk_allocs = {"root": state_root, "accounts": {}}
contracts = read_json(paths.addresses_json_path)
# TODO: also do all EOA accounts
for contract in contracts.keys():
account = contracts[contract].lower()
extDumpState('127.0.0.1:8545', account)
Expand All @@ -314,6 +313,20 @@ def generate_allocs(paths: Bunch):
copy_from_docker(f'l1_deployer:/var/lib/rsk/{filename}', paths.devnet_dir, paths)
dump = retrieve_dump_for(account, paths)
rsk_allocs = merge_alloc(rsk_allocs, account, dump[account])

accounts = json.loads(eth_accounts('127.0.0.1:8545'))['result']
log.info(f'l1 accounts: {accounts}')

for account in accounts:
balance = json.loads(eth_getBalance(account, '127.0.0.1:8545'))['result']
nonce = json.loads(eth_getTransactionCount(account, '127.0.0.1:8545'))['result']
account_data = dict(
balance=str(int(balance, 16)),
nonce=int(nonce, 16)
)
log.info(f'{account} data: {account_data}')
rsk_allocs = merge_alloc(rsk_allocs, account, account_data)

log.info(f'Writing allocs to {paths.allocs_path}')
write_json(paths.allocs_path, rsk_allocs)
finally:
Expand Down Expand Up @@ -350,12 +363,12 @@ def devnet_deploy(paths):
exit(1)

write_json(
pjoin(paths.devnet_dir, 'rsk-dev.json'),
pjoin(paths.genesis_rsk_path),
format_genesis_for_rsk(
merge_rsk_genesis(
read_json(paths.genesis_l1_path),
paths
), paths
)
)
)

Expand All @@ -382,6 +395,9 @@ def devnet_deploy(paths):
rollup_config = read_json(paths.rollup_config_path)
addresses = read_json(paths.addresses_json_path)

log.debug('I am sleepy')
time.sleep(10) # TODO: there seem to be some kind of strange racing condition event where the l2 genesis file is not fully written/closed by the previous process before starting the l2 node

log.info('Bringing up L2.')
run_command(['docker', 'compose', 'up', '-d', 'l2'], cwd=paths.ops_bedrock_dir, env={
'PWD': paths.ops_bedrock_dir
Expand Down Expand Up @@ -420,6 +436,28 @@ def eth_accounts(url):
conn.close()
return data

def eth_getBalance(account, url):
log.info(f'Fetch balance for {account}')
conn = http.client.HTTPConnection(url)
headers = {'Content-type': 'application/json'}
body = f'{{"id":2, "jsonrpc":"2.0", "method": "eth_getBalance", "params":["{account}"]}}'
conn.request('POST', '/', body, headers)
response = conn.getresponse()
data = response.read().decode()
conn.close()
return data

def eth_getTransactionCount(account, url):
log.info(f'Fetch TX count for {account}')
conn = http.client.HTTPConnection(url)
headers = {'Content-type': 'application/json'}
body = f'{{"id":2, "jsonrpc":"2.0", "method": "eth_getTransactionCount", "params":["{account}", "latest"]}}'
conn.request('POST', '/', body, headers)
response = conn.getresponse()
data = response.read().decode()
conn.close()
return data


def getLatestBlock(url):
log.info(f'Fetch getBlockByNumber {url}')
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ require (
rsc.io/tmplfunc v0.0.3 // indirect
)

replace github.com/ethereum/go-ethereum => ../op-geth
replace github.com/ethereum/go-ethereum => ./op-geth

//replace github.com/ethereum-optimism/superchain-registry/superchain => ../superchain-registry/superchain
//replace github.com/ethereum/go-ethereum v1.13.5 => ../go-ethereum
2 changes: 1 addition & 1 deletion op-challenger/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ DISPUTE_GAME_FACTORY=$(jq -r .DisputeGameFactoryProxy .devnet/addresses.json)
--agree-with-proposed-output=true \
--datadir temp/challenger-data \
--cannon-rollup-config .devnet/rollup.json \
--cannon-l2-genesis .devnet/genesis-l2.json \
--cannon-l2-genesis .devnet/genesis/l2.json \
--cannon-bin ./cannon/bin/cannon \
--cannon-server ./op-program/bin/op-program \
--cannon-prestate ./op-program/bin/prestate.json \
Expand Down
2 changes: 1 addition & 1 deletion ops-bedrock/Dockerfile.l1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM jurajpiar/rskj:5.4.0
FROM jurajpiar/rskj:5.4.0.3

COPY entrypoint-l1.sh /entrypoint.sh

Expand Down
7 changes: 5 additions & 2 deletions ops-bedrock/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,16 @@ services:
- "8546:8546"
- "7060:5050"
volumes:
# - "${PWD}/../.devnet/l1_data:/var/lib/rsk/.rsk"
- "${PWD}/../.devnet/l1_data:/var/lib/rsk/.rsk"
- "${PWD}/../.devnet/genesis:/etc/rsk/genesis"
- "${PWD}/rskj.logback.xml:/etc/rsk/logback.xml"
- "${PWD}/rskj.node.conf:/etc/rsk/node.conf"
- "${PWD}/../.devnet/logs:/var/lib/rsk/logs"

l2:
depends_on:
- op_geth_builder
- l1
build:
context: .
dockerfile: Dockerfile.l2
Expand All @@ -65,7 +68,7 @@ services:
- "8060:6060"
volumes:
- "l2_data:/db"
- "${PWD}/../.devnet/genesis-l2.json:/genesis.json"
- "${PWD}/../.devnet/genesis/l2.json:/genesis.json"
- "${PWD}/test-jwt-secret.txt:/config/test-jwt-secret.txt"
entrypoint: # pass the L2 specific flags by overriding the entry-point and adding extra arguments
- "/bin/sh"
Expand Down
110 changes: 56 additions & 54 deletions ops-bedrock/rskj.logback.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,69 +56,71 @@
</appender>

<!-- Loggers -->
<logger name="execute" level="ERROR"/>
<logger name="blockvalidator" level="ERROR"/>
<logger name="blockexecutor" level="ERROR"/>
<logger name="general" level="ALL"/>
<logger name="gaspricetracker" level="ERROR"/>
<logger name="web3" level="ERROR"/>
<logger name="repository" level="ERROR"/>
<logger name="VM" level="ALL"/>
<logger name="blockqueue" level="ERROR"/>
<logger name="io.netty" level="ERROR"/>
<logger name="block" level="ERROR"/>
<logger name="minerserver" level="ERROR"/>
<logger name="txbuilderex" level="ERROR"/>
<logger name="pendingstate" level="ERROR"/>
<logger name="hsqldb.db" level="ERROR"/>
<logger name="TCK-Test" level="ERROR"/>
<logger name="db" level="ALL"/>
<logger name="net" level="ERROR"/>
<logger name="start" level="ALL"/>
<logger name="cli" level="ALL"/>
<logger name="txs" level="ERROR"/>
<logger name="execute" level="INFO"/>
<logger name="blockvalidator" level="INFO"/>
<logger name="blockexecutor" level="INFO"/>
<logger name="general" level="INFO"/>
<logger name="gaspricetracker" level="INFO"/>
<logger name="web3" level="INFO"/>
<logger name="repository" level="INFO"/>
<logger name="VM" level="ERROR"/>
<logger name="blockqueue" level="INFO"/>
<logger name="io.netty" level="INFO"/>
<logger name="block" level="INFO"/>
<logger name="minerserver" level="INFO"/>
<logger name="txbuilderex" level="INFO"/>
<logger name="pendingstate" level="INFO"/>
<logger name="hsqldb.db" level="INFO"/>
<logger name="TCK-Test" level="INFO"/>
<logger name="db" level="INFO"/>
<logger name="net" level="INFO"/>
<logger name="start" level="INFO"/>
<logger name="cli" level="INFO"/>
<logger name="txs" level="INFO"/>
<logger name="gas" level="ERROR"/>
<logger name="main" level="ALL"/>
<logger name="trie" level="ERROR"/>
<logger name="peermonitor" level="ERROR"/>
<logger name="bridge" level="ERROR"/>
<logger name="rlp" level="ERROR"/>
<logger name="messagehandler" level="ERROR"/>
<logger name="sync" level="ERROR"/>
<logger name="BtcToRskClient" level="ERROR"/>
<logger name="ui" level="ERROR"/>
<logger name="java.nio" level="ERROR"/>
<logger name="org.eclipse.jetty" level="ERROR"/>
<logger name="wire" level="ERROR"/>
<logger name="BridgeSupport" level="ERROR"/>
<logger name="jsonrpc" level="ALL"/>
<logger name="wallet" level="ERROR"/>
<logger name="blockchain" level="ERROR"/>
<logger name="blockprocessor" level="ERROR"/>
<logger name="asyncblockprocessor" level="ERROR"/>
<logger name="state" level="ERROR"/>
<logger name="org.bitcoinj" level="ERROR"/>
<logger name="metrics" level="ERROR"/>
<logger name="messageProcess" level="ERROR"/>
<logger name="co.rsk.db.migration.OrchidToUnitrieMigrator" level="ERROR"/>
<logger name="co.rsk.db.migration.MissingOrchidStorageKeysProvider" level="ERROR"/>
<logger name="blooms" level="ERROR"/>
<logger name="triestore" level="ERROR" />
<logger name="btcBlockStore" level="ERROR" />
<logger name="secp256k1" level="ERROR" />
<logger name="altbn128" level="ERROR" />
<logger name="main" level="INFO"/>
<logger name="trie" level="INFO"/>
<logger name="peermonitor" level="INFO"/>
<logger name="bridge" level="INFO"/>
<logger name="rlp" level="INFO"/>
<logger name="messagehandler" level="INFO"/>
<logger name="sync" level="INFO"/>
<logger name="BtcToRskClient" level="INFO"/>
<logger name="ui" level="INFO"/>
<logger name="java.nio" level="INFO"/>
<logger name="org.eclipse.jetty" level="INFO"/>
<logger name="wire" level="INFO"/>
<logger name="BridgeSupport" level="INFO"/>
<logger name="jsonrpc" level="INFO"/>
<logger name="wallet" level="INFO"/>
<logger name="blockchain" level="INFO"/>
<logger name="blockprocessor" level="INFO"/>
<logger name="asyncblockprocessor" level="INFO"/>
<logger name="state" level="INFO"/>
<logger name="org.bitcoinj" level="INFO"/>
<logger name="metrics" level="INFO"/>
<logger name="messageProcess" level="INFO"/>
<logger name="co.rsk.db.migration.OrchidToUnitrieMigrator" level="INFO"/>
<logger name="co.rsk.db.migration.MissingOrchidStorageKeysProvider" level="INFO"/>
<logger name="blooms" level="INFO"/>
<logger name="triestore" level="INFO" />
<logger name="btcBlockStore" level="INFO" />
<logger name="secp256k1" level="INFO" />
<logger name="altbn128" level="INFO" />
<logger name="co.rsk.net.discovery.PeerExplorer" level="INFO" />
<logger name="co.rsk.net.discovery.NodeChallengeManager" level="INFO" />

<!-- Use ERROR or upper levels for production environments. -->
<logger name="com.googlecode.jsonrpc4j" level="ERROR" />
<!-- Use INFO or upper levels for production environments. -->
<logger name="com.googlecode.jsonrpc4j" level="INFO" />

<!-- Logger for CLI tools -->
<logger name="clitool" level="ALL" additivity="false" >
<logger name="clitool" level="DEBUG" additivity="false" >
<appender-ref ref="STDOUT_CLI_TOOL"/>
<appender-ref ref="FILE-AUDIT"/>
</logger>

<!-- Root logger -->
<root level="WARN">
<root level="DEBUG">
<appender-ref ref="stdout"/>
<appender-ref ref="FILE-AUDIT"/>
</root>
Expand Down
2 changes: 1 addition & 1 deletion ops-bedrock/rskj.node.conf
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,4 @@ rpc {
}
}
}

genesis = "/etc/rsk/genesis/rsk-dev.json"
2 changes: 1 addition & 1 deletion ops-bedrock/rskj_deployer.node.conf
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ miner {
client.enabled = true
server.enabled = true
client.autoMine = false
client.delayBetweenBlocks = 3 second
client.delayBetweenBlocks = 0.4 second
coinbase.secret = "thisisasecret"
minGasPrice = 1
}
Expand Down
2 changes: 1 addition & 1 deletion ops/docker/op-stack-go/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ FROM --platform=$BUILDPLATFORM golang:1.21.3-alpine3.18 as builder
RUN apk add --no-cache make gcc musl-dev linux-headers git jq bash

# We copy the go.mod/sum first, so the `go mod download` does not have to re-run if dependencies do not change.
COPY ./op-geth /app/../op-geth
COPY ./op-geth /app/op-geth
COPY ./go.mod /app/go.mod
COPY ./go.sum /app/go.sum

Expand Down
3 changes: 2 additions & 1 deletion packages/sdk/hardhat.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const config: HardhatUserConfig = {
],
},
regtest: {
url: process.env.L1_RPC || 'http://localhost:4444',
url: process.env.L1_RPC || 'http://localhost:8545',
accounts: [
'c85ef7d79691fe79573b1a7064c19c1a9819ebdbd1faaab1a8ec92344438aaf4',
'082f57b8084286a079aeb9f2d0e17e565ced44a2cb9ce4844e6d4b9d89f3f595'
Expand Down Expand Up @@ -76,6 +76,7 @@ const config: HardhatUserConfig = {
],
hivenet: ['../contracts-bedrock/deployments/hivenet'],
devnetL1: ['../contracts-bedrock/deployments/devnetL1'],
regtest: ['../contracts-bedrock/deployments/regtest'],
goerli: [
'../contracts-bedrock/deployments/goerli',
'../contracts/deployments/goerli',
Expand Down

0 comments on commit c57afb0

Please sign in to comment.