diff --git a/.github/workflows/infrstructure.yml b/.github/workflows/infrstructure.yml index 300cf3733..7091bc664 100644 --- a/.github/workflows/infrstructure.yml +++ b/.github/workflows/infrstructure.yml @@ -14,6 +14,7 @@ on: options: - devnet - testnet + - stressnet default: 'devnet' init: description: 'Initialize' diff --git a/infrastructure/nomad/playbooks/deploy.yml b/infrastructure/nomad/playbooks/deploy.yml index d2216d251..d14ef99ff 100644 --- a/infrastructure/nomad/playbooks/deploy.yml +++ b/infrastructure/nomad/playbooks/deploy.yml @@ -229,6 +229,7 @@ label: "{{ item.name }}" vars: job: "{{ item }}" + profile: "{{ profile }}" when: build_templates is not defined or build_templates - name: Create "version.txt" diff --git a/infrastructure/nomad/playbooks/templates/jobs/datadog-agent.nomad.j2 b/infrastructure/nomad/playbooks/templates/jobs/datadog-agent.nomad.j2 index 0cf4be0f7..841c845a1 100644 --- a/infrastructure/nomad/playbooks/templates/jobs/datadog-agent.nomad.j2 +++ b/infrastructure/nomad/playbooks/templates/jobs/datadog-agent.nomad.j2 @@ -94,7 +94,27 @@ job "{{ job.name }}" { {{- range nomadService "mev-commit-provider-node1" }} {{- if contains "metrics" .Tags }} - openmetrics_endpoint: https://{{ .Address }}:{{ .Port }}/metrics - service: "mev-commit-provider-node1" + service: {{ .Name }} + metrics: + - mev_commit* + - go* + - libp2p* + {{- end }} + {{- end }} + {{- range nomadService "mev-commit-provider-node2" }} + {{- if contains "metrics" .Tags }} + - openmetrics_endpoint: https://{{ .Address }}:{{ .Port }}/metrics + service: {{ .Name }} + metrics: + - mev_commit* + - go* + - libp2p* + {{- end }} + {{- end }} + {{- range nomadService "mev-commit-provider-node3" }} + {{- if contains "metrics" .Tags }} + - openmetrics_endpoint: https://{{ .Address }}:{{ .Port }}/metrics + service: {{ .Name }} metrics: - mev_commit* - go* @@ -105,7 +125,47 @@ job "{{ job.name }}" { {{- range nomadService "mev-commit-bidder-node1" }} {{- if contains "metrics" .Tags }} - openmetrics_endpoint: https://{{ .Address }}:{{ .Port }}/metrics - service: "mev-commit-bidder-node1" + service: {{ .Name }} + metrics: + - mev_commit* + - go* + - libp2p* + {{- end }} + {{- end }} + {{- range nomadService "mev-commit-bidder-node2" }} + {{- if contains "metrics" .Tags }} + - openmetrics_endpoint: https://{{ .Address }}:{{ .Port }}/metrics + service: {{ .Name }} + metrics: + - mev_commit* + - go* + - libp2p* + {{- end }} + {{- end }} + {{- range nomadService "mev-commit-bidder-node3" }} + {{- if contains "metrics" .Tags }} + - openmetrics_endpoint: https://{{ .Address }}:{{ .Port }}/metrics + service: {{ .Name }} + metrics: + - mev_commit* + - go* + - libp2p* + {{- end }} + {{- end }} + {{- range nomadService "mev-commit-bidder-node4" }} + {{- if contains "metrics" .Tags }} + - openmetrics_endpoint: https://{{ .Address }}:{{ .Port }}/metrics + service: {{ .Name }} + metrics: + - mev_commit* + - go* + - libp2p* + {{- end }} + {{- end }} + {{- range nomadService "mev-commit-bidder-node5" }} + {{- if contains "metrics" .Tags }} + - openmetrics_endpoint: https://{{ .Address }}:{{ .Port }}/metrics + service: {{ .Name }} metrics: - mev_commit* - go* @@ -196,7 +256,7 @@ job "{{ job.name }}" { {{- range nomadService "mev-commit-provider-emulator-node1" }} {{- if contains "metrics" .Tags }} - openmetrics_endpoint: http://{{ .Address }}:{{ .Port }}/metrics - service: "mev-commit-provider-emulator-node1" + service: {{ .Name }} metrics: - mev_commit* {{- end }} @@ -205,7 +265,24 @@ job "{{ job.name }}" { {{- range nomadService "mev-commit-bidder-emulator-node1" }} {{- if contains "metrics" .Tags }} - openmetrics_endpoint: http://{{ .Address }}:{{ .Port }}/metrics - service: "mev-commit-bidder-emulator-node1" + service: {{ .Name }} + metrics: + - mev_commit* + {{- end }} + {{- end }} + {{- range nomadService "mev-commit-provider-emulator-nodes" }} + {{- if contains "metrics" .Tags }} + - openmetrics_endpoint: http://{{ .Address }}:{{ .Port }}/metrics + service: {{ .Name }} + metrics: + - mev_commit* + {{- end }} + {{- end }} + + {{- range nomadService "mev-commit-bidder-emulator-nodes" }} + {{- if contains "metrics" .Tags }} + - openmetrics_endpoint: http://{{ .Address }}:{{ .Port }}/metrics + service: {{ .Name }} metrics: - mev_commit* {{- end }} diff --git a/infrastructure/nomad/playbooks/templates/jobs/mev-commit-emulator.nomad.j2 b/infrastructure/nomad/playbooks/templates/jobs/mev-commit-emulator.nomad.j2 index 53cb63b73..7282c0eb4 100644 --- a/infrastructure/nomad/playbooks/templates/jobs/mev-commit-emulator.nomad.j2 +++ b/infrastructure/nomad/playbooks/templates/jobs/mev-commit-emulator.nomad.j2 @@ -49,8 +49,10 @@ job "{{ job.name }}" { if job.get('env') and job.env.get('log-tags') else 'service:' + job.name + '-{{ env "NOMAD_ALLOC_INDEX" }}' }}" + {%- raw %} - {{- range nomadService "{% endraw %}{{ job.target_name }}{% raw %}" }} + {{- $idx := add (env "NOMAD_ALLOC_INDEX" | parseInt) 1 }} + {{- range nomadService (printf "%s%d" "{% endraw %}{{ job.target_name }}{% raw %}" $idx) }} {{- if contains "rpc" .Tags }} EMULATOR_IP_PORT="{{ .Address }}:{{ .Port }}" {{- end }} diff --git a/infrastructure/nomad/playbooks/templates/jobs/mev-commit-funder.nomad.j2 b/infrastructure/nomad/playbooks/templates/jobs/mev-commit-funder.nomad.j2 index f4d994e07..e1227d5bb 100644 --- a/infrastructure/nomad/playbooks/templates/jobs/mev-commit-funder.nomad.j2 +++ b/infrastructure/nomad/playbooks/templates/jobs/mev-commit-funder.nomad.j2 @@ -43,7 +43,8 @@ job "{{ job.name }}" { {{ end }} {{- end }} - {{- range nomadService "{% endraw %}{{ job.target_name }}{% raw %}" }} + {{- $idx := add (env "NOMAD_ALLOC_INDEX" | parseInt) 1 }} + {{- range nomadService (printf "%s%d" "{% endraw %}{{ job.target_name }}{% raw %}" $idx) }} {{- if contains "http" .Tags }} START_TIME=$(date +%s) TIMEOUT=60 diff --git a/infrastructure/nomad/playbooks/templates/jobs/mev-commit-geth.nomad.j2 b/infrastructure/nomad/playbooks/templates/jobs/mev-commit-geth.nomad.j2 index ecc23bc29..c10914e2d 100644 --- a/infrastructure/nomad/playbooks/templates/jobs/mev-commit-geth.nomad.j2 +++ b/infrastructure/nomad/playbooks/templates/jobs/mev-commit-geth.nomad.j2 @@ -36,6 +36,12 @@ job "{{ job.name }}" { } {% endfor %} + {% if profile == 'stressnet' %} + resources { + memory = 16384 + } + {% endif %} + artifact { source = "https://primev-infrastructure-artifacts.s3.us-west-2.amazonaws.com/mev-commit-geth_{{ version }}_Linux_x86_64.tar.gz" } diff --git a/infrastructure/nomad/playbooks/templates/jobs/mev-commit-oracle.nomad.j2 b/infrastructure/nomad/playbooks/templates/jobs/mev-commit-oracle.nomad.j2 index 3e4c86138..9c744137f 100644 --- a/infrastructure/nomad/playbooks/templates/jobs/mev-commit-oracle.nomad.j2 +++ b/infrastructure/nomad/playbooks/templates/jobs/mev-commit-oracle.nomad.j2 @@ -47,6 +47,12 @@ job "{{ job.name }}" { sidecar = true } + {% if profile == 'stressnet' %} + resources { + memory = 4096 + } + {% endif %} + template { data = <<-EOH {%- raw %} @@ -119,6 +125,12 @@ job "{{ job.name }}" { source = "https://primev-infrastructure-artifacts.s3.us-west-2.amazonaws.com/contracts_{{ version }}.tar.gz" } + {% if profile == 'stressnet' %} + resources { + memory = 1024 + } + {% endif %} + template { data = <<-EOH KEYSTOREGEN_LOG_FMT="{{ job.env.get('log-format', 'json') }}" @@ -144,7 +156,7 @@ job "{{ job.name }}" { MEV_ORACLE_HTTP_PORT="{{ .Port }}" {{- end }} {{- end }} - {{- range nomadService "mev-commit-geth-bootnode1" }} + {{- range nomadService "mev-commit-geth-member-node" }} {{- if contains "http" .Tags }} MEV_ORACLE_SETTLEMENT_RPC_URL_HTTP="http://{{ .Address }}:{{ .Port }}" {{- end }} @@ -171,16 +183,41 @@ job "{{ job.name }}" { data = <<-EOH #!/usr/bin/env bash {%- raw %} + ADDRESSES = "" {{- range nomadService "mev-commit-provider-node1" }} {{ if contains "http" .Tags }} TOPOLOGY=$(curl https://{{ .Address}}:{{ .Port }}/v1/debug/topology) PROVIDER_ETHEREUM_ADDRESS=$(echo ${TOPOLOGY} | jq -r '.topology.self["Ethereum Address"]') - export MEV_ORACLE_OVERRIDE_WINNERS=${PROVIDER_ETHEREUM_ADDRESS} + if [ -z "${ADDRESSES}" ]; then + ADDRESSES="${PROVIDER_ETHEREUM_ADDRESS}" + else + ADDRESSES="${ADDRESSES},${PROVIDER_ETHEREUM_ADDRESS}" + fi {{ end }} {{- end }} - {% endraw %} + {{- range nomadService "mev-commit-provider-node2" }} + {{ if contains "http" .Tags }} + TOPOLOGY=$(curl https://{{ .Address}}:{{ .Port }}/v1/debug/topology) + PROVIDER_ETHEREUM_ADDRESS=$(echo ${TOPOLOGY} | jq -r '.topology.self["Ethereum Address"]') + if [ -z "${ADDRESSES}" ]; then + ADDRESSES="${PROVIDER_ETHEREUM_ADDRESS}" + else + ADDRESSES="${ADDRESSES},${PROVIDER_ETHEREUM_ADDRESS}" + fi + {{ end }} + {{- end }} + {{- range nomadService "mev-commit-provider-node3" }} + {{ if contains "http" .Tags }} + TOPOLOGY=$(curl https://{{ .Address}}:{{ .Port }}/v1/debug/topology) + PROVIDER_ETHEREUM_ADDRESS=$(echo ${TOPOLOGY} | jq -r '.topology.self["Ethereum Address"]') + if [ -z "${ADDRESSES}" ]; then + ADDRESSES="${PROVIDER_ETHEREUM_ADDRESS}" + else + ADDRESSES="${ADDRESSES},${PROVIDER_ETHEREUM_ADDRESS}" + fi + {{ end }} + {{- end }} - {%- raw %} {{- range nomadService "datadog-agent-logs-collector" }} {{ if contains "tcp" .Tags }} exec > >(nc {{ .Address }} {{ .Port }}) 2>&1 @@ -188,6 +225,11 @@ job "{{ job.name }}" { {{- end }} {% endraw %} + if [ ! -z "${ADDRESSES}" ]; then + echo "starting mev-oracle with override winners ${ADDRESSES}" + export MEV_ORACLE_OVERRIDE_WINNERS=${ADDRESSES} + fi + if [ ! -d "${MEV_ORACLE_KEYSTORE_PATH}" ]; then mkdir -p "${MEV_ORACLE_KEYSTORE_PATH}" > /dev/null 2>&1 mv local/contracts/deployer_keystore/* "${MEV_ORACLE_KEYSTORE_PATH}/" diff --git a/infrastructure/nomad/playbooks/templates/jobs/mev-commit.nomad.j2 b/infrastructure/nomad/playbooks/templates/jobs/mev-commit.nomad.j2 index 9fd2c9a13..8b69fd0f4 100644 --- a/infrastructure/nomad/playbooks/templates/jobs/mev-commit.nomad.j2 +++ b/infrastructure/nomad/playbooks/templates/jobs/mev-commit.nomad.j2 @@ -44,6 +44,12 @@ job "{{ job.name }}" { source = "https://primev-infrastructure-artifacts.s3.us-west-2.amazonaws.com/keystore-generator_{{ version }}_Linux_x86_64.tar.gz" } + {% if profile == 'stressnet' %} + resources { + memory = 1024 + } + {% endif %} + template { data = <<-EOH KEYSTOREGEN_LOG_FMT="{{ job.env.get('log-format', 'json') }}" diff --git a/infrastructure/nomad/playbooks/vars.yml b/infrastructure/nomad/playbooks/vars.yml index 3973364e3..804fbc65b 100644 --- a/infrastructure/nomad/playbooks/vars.yml +++ b/infrastructure/nomad/playbooks/vars.yml @@ -55,6 +55,29 @@ profiles: - "mev-commit-oracle" - "datadog-agent-metrics-collector" - "mev-commit-faucet" + stressnet: + job_names: + - "datadog-agent-logs-collector" + - "mev-commit-geth-bootnode1" + - "mev-commit-geth-signer-node1" + - "mev-commit-geth-member-node" + - "deploy-contracts" + - "mev-commit-bootnode1" + - "mev-commit-provider-node1" + - "mev-commit-provider-node2" + - "mev-commit-provider-node3" + - "mev-commit-provider-node-funders" + - "mev-commit-provider-emulator-nodes" + - "mev-commit-oracle" + - "mev-commit-bidder-node1" + - "mev-commit-bidder-node2" + - "mev-commit-bidder-node3" + - "mev-commit-bidder-node4" + - "mev-commit-bidder-node5" + - "mev-commit-bidder-node-funders" + - "mev-commit-bidder-emulator-nodes" + - "datadog-agent-metrics-collector" + - "mev-commit-faucet" artifacts: - name: keystore-generator @@ -174,12 +197,56 @@ jobs: - metrics: to: 13523 http: - static: 13533 + static: 13623 + to: 13523 + p2p: + to: 13522 + rpc: + static: 13624 + to: 13524 + env: + type: provider + nat_address: "{{ ansible_facts['default_ipv4']['address'] }}" + tls_crt_file: "{{ tls_crt_file }}" + tls_key_file: "{{ tls_key_file }}" + + - name: mev-commit-provider-node2 + template: mev-commit.nomad.j2 + count: 1 + secrets: + keystore: provider1 + ports: + - metrics: + to: 13523 + http: + static: 13633 to: 13523 p2p: to: 13522 rpc: - static: 13534 + static: 13634 + to: 13524 + env: + type: provider + nat_address: "{{ ansible_facts['default_ipv4']['address'] }}" + tls_crt_file: "{{ tls_crt_file }}" + tls_key_file: "{{ tls_key_file }}" + + - name: mev-commit-provider-node3 + template: mev-commit.nomad.j2 + count: 1 + secrets: + keystore: provider1 + ports: + - metrics: + to: 13523 + http: + static: 13643 + to: 13523 + p2p: + to: 13522 + rpc: + static: 13644 to: 13524 env: type: provider @@ -191,7 +258,31 @@ jobs: template: mev-commit-funder.nomad.j2 count: 1 target_type: provider - target_name: mev-commit-provider-node1 + target_name: mev-commit-provider-node + + - name: mev-commit-provider-emulator-node1 + template: mev-commit-emulator.nomad.j2 + count: 1 + target_type: provider + target_name: mev-commit-provider-node + ports: + - metrics: + to: 8080 + + - name: mev-commit-provider-node-funders + template: mev-commit-funder.nomad.j2 + count: 3 + target_type: provider + target_name: mev-commit-provider-node + + - name: mev-commit-provider-emulator-nodes + template: mev-commit-emulator.nomad.j2 + count: 3 + target_type: provider + target_name: mev-commit-provider-node + ports: + - metrics: + to: 8080 - name: mev-commit-oracle template: mev-commit-oracle.nomad.j2 @@ -217,12 +308,96 @@ jobs: - metrics: to: 13523 http: - static: 13532 + static: 13723 + to: 13523 + p2p: + to: 13522 + rpc: + static: 13724 + to: 13524 + env: + type: bidder + tls_crt_file: "{{ tls_crt_file }}" + tls_key_file: "{{ tls_key_file }}" + + - name: mev-commit-bidder-node2 + template: mev-commit.nomad.j2 + count: 1 + secrets: + keystore: bidder1 + ports: + - metrics: + to: 13523 + http: + static: 13733 + to: 13523 + p2p: + to: 13522 + rpc: + static: 13734 + to: 13524 + env: + type: bidder + tls_crt_file: "{{ tls_crt_file }}" + tls_key_file: "{{ tls_key_file }}" + + - name: mev-commit-bidder-node3 + template: mev-commit.nomad.j2 + count: 1 + secrets: + keystore: bidder1 + ports: + - metrics: + to: 13523 + http: + static: 13743 to: 13523 p2p: to: 13522 rpc: - static: 13544 + static: 13744 + to: 13524 + env: + type: bidder + tls_crt_file: "{{ tls_crt_file }}" + tls_key_file: "{{ tls_key_file }}" + + - name: mev-commit-bidder-node4 + template: mev-commit.nomad.j2 + count: 1 + secrets: + keystore: bidder1 + ports: + - metrics: + to: 13523 + http: + static: 13753 + to: 13523 + p2p: + to: 13522 + rpc: + static: 13754 + to: 13524 + env: + type: bidder + tls_crt_file: "{{ tls_crt_file }}" + tls_key_file: "{{ tls_key_file }}" + + - name: mev-commit-bidder-node5 + template: mev-commit.nomad.j2 + count: 1 + secrets: + keystore: bidder1 + ports: + - metrics: + to: 13523 + http: + static: 13763 + to: 13523 + p2p: + to: 13522 + rpc: + static: 13764 to: 13524 env: type: bidder @@ -233,22 +408,30 @@ jobs: template: mev-commit-funder.nomad.j2 count: 1 target_type: bidder - target_name: mev-commit-bidder-node1 + target_name: mev-commit-bidder-node - - name: mev-commit-provider-emulator-node1 + - name: mev-commit-bidder-emulator-node1 template: mev-commit-emulator.nomad.j2 count: 1 - target_type: provider - target_name: mev-commit-provider-node1 + target_type: bidder + target_name: mev-commit-bidder-node ports: - metrics: to: 8080 + env: + l1_rpc_url: https://eth-sepolia.g.alchemy.com/v2/ui86u7ZUgpn2e-WHFbHQHk2s-6Odjggx - - name: mev-commit-bidder-emulator-node1 + - name: mev-commit-bidder-node-funders + template: mev-commit-funder.nomad.j2 + count: 5 + target_type: bidder + target_name: mev-commit-bidder-node + + - name: mev-commit-bidder-emulator-nodes template: mev-commit-emulator.nomad.j2 - count: 1 + count: 5 target_type: bidder - target_name: mev-commit-bidder-node1 + target_name: mev-commit-bidder-node ports: - metrics: to: 8080 diff --git a/p2p/integrationtest/real-bidder/main.go b/p2p/integrationtest/real-bidder/main.go index 9b7c3690f..912893917 100644 --- a/p2p/integrationtest/real-bidder/main.go +++ b/p2p/integrationtest/real-bidder/main.go @@ -12,7 +12,9 @@ import ( "math/rand" "net/http" "os" + "slices" "strconv" + "strings" "sync" "time" @@ -57,6 +59,11 @@ var ( 8080, "The port to serve the HTTP metrics endpoint on", ) + bidWorkers = flag.Int( + "bid-workers", + 3, + "Number of workers to send bids", + ) ) var ( @@ -143,11 +150,25 @@ func main() { wg.Add(1) go func() { - ticker := time.NewTicker(2 * time.Second) + ticker := time.NewTicker(10 * time.Minute) defer ticker.Stop() + minDepositResp, err := bidderClient.GetMinDeposit(context.Background(), &pb.EmptyMessage{}) + if err != nil { + logger.Error("failed to get min deposit", "err", err) + return + } + + minDeposit, set := big.NewInt(0).SetString(minDepositResp.Amount, 10) + if !set { + logger.Error("failed to parse min deposit amount") + return + } + + minDepositAmt := new(big.Int).Mul(minDeposit, big.NewInt(10)) + for { - err = checkOrDeposit(bidderClient, logger) + err = checkOrDeposit(bidderClient, logger, minDepositAmt) if err != nil { logger.Error("failed to check or stake", "err", err) } @@ -160,7 +181,10 @@ func main() { txns []string } - newBlockChan := make(chan blockWithTxns, 1) + blockChans := make([]chan blockWithTxns, *bidWorkers) + for i := 0; i < *bidWorkers; i++ { + blockChans[i] = make(chan blockWithTxns, 1) + } wg.Add(1) go func(logger *slog.Logger) { @@ -179,54 +203,58 @@ func main() { } currentBlkNum = blkNum - newBlockChan <- blockWithTxns{ - blockNum: blkNum, - txns: block, + for _, ch := range blockChans { + ch <- blockWithTxns{ + blockNum: blkNum, + txns: slices.Clone(block), + } } } }(logger) - wg.Add(1) - go func(logger *slog.Logger) { - defer wg.Done() - ticker := time.NewTicker(200 * time.Millisecond) - currentBlock := blockWithTxns{} - for { - select { - case block := <-newBlockChan: - currentBlock = block - case <-ticker.C: + for i := 0; i < *bidWorkers; i++ { + wg.Add(1) + go func(logger *slog.Logger, newBlockChan <-chan blockWithTxns) { + defer wg.Done() + ticker := time.NewTicker(100 * time.Millisecond) + currentBlock := blockWithTxns{} + for { + select { + case block := <-newBlockChan: + currentBlock = block + case <-ticker.C: + } + + if len(currentBlock.txns) == 0 { + continue + } + + bundleLen := rand.Intn(10) + bundleStart := rand.Intn(len(currentBlock.txns) - 1) + bundleEnd := bundleStart + bundleLen + if bundleEnd > len(currentBlock.txns) { + bundleEnd = len(currentBlock.txns) - 1 + } + + min := 5000 + max := 10000 + startTimeDiff := rand.Intn(max-min+1) + min + endTimeDiff := rand.Intn(max-min+1) + min + err = sendBid( + bidderClient, + logger, + rpcClient, + currentBlock.txns[bundleStart:bundleEnd], + currentBlock.blockNum, + (time.Now().UnixMilli())-int64(startTimeDiff), + (time.Now().UnixMilli())+int64(endTimeDiff), + ) + if err != nil { + logger.Error("failed to send bid", "err", err) + } } - - if len(currentBlock.txns) == 0 { - continue - } - - bundleLen := rand.Intn(10) - bundleStart := rand.Intn(len(currentBlock.txns) - 1) - bundleEnd := bundleStart + bundleLen - if bundleEnd > len(currentBlock.txns) { - bundleEnd = len(currentBlock.txns) - 1 - } - - min := 5000 - max := 10000 - startTimeDiff := rand.Intn(max-min+1) + min - endTimeDiff := rand.Intn(max-min+1) + min - err = sendBid( - bidderClient, - logger, - rpcClient, - currentBlock.txns[bundleStart:bundleEnd], - currentBlock.blockNum, - (time.Now().UnixMilli())-int64(startTimeDiff), - (time.Now().UnixMilli())+int64(endTimeDiff), - ) - if err != nil { - logger.Error("failed to send bid", "err", err) - } - } - }(logger) + }(logger, blockChans[i]) + } wg.Wait() } @@ -244,7 +272,7 @@ func RetreivedBlock(rpcClient *ethclient.Client) ([]string, int64, error) { blockTxns := []string{} txns := fullBlock.Transactions() for _, txn := range txns { - blockTxns = append(blockTxns, txn.Hash().Hex()[2:]) + blockTxns = append(blockTxns, strings.TrimPrefix(txn.Hash().Hex(), "0x")) } return blockTxns, int64(blkNum), nil @@ -253,6 +281,7 @@ func RetreivedBlock(rpcClient *ethclient.Client) ([]string, int64, error) { func checkOrDeposit( bidderClient pb.BidderClient, logger *slog.Logger, + minDeposit *big.Int, ) error { deposit, err := bidderClient.GetDeposit(context.Background(), &pb.GetDepositRequest{}) if err != nil { @@ -260,47 +289,38 @@ func checkOrDeposit( return err } - logger.Info("initial deposit", "amount", deposit.Amount, "window", deposit.WindowNumber.Value) - - minDeposit, err := bidderClient.GetMinDeposit(context.Background(), &pb.EmptyMessage{}) - if err != nil { - logger.Error("failed to get min deposit", "err", err) - return err - } - - depositAmt, set := big.NewInt(0).SetString(deposit.Amount, 10) - if !set { - logger.Error("failed to parse deposit amount") - return errors.New("failed to parse deposit amount") - } - - minDepositAmt, set := big.NewInt(0).SetString(minDeposit.Amount, 10) - if !set { - logger.Error("failed to parse min deposit amount") - return errors.New("failed to parse min deposit amount") - } - - if depositAmt.Cmp(minDepositAmt) > 0 { - logger.Error("bidder already has balance") - return nil - } - - topup := big.NewInt(0).Mul(minDepositAmt, big.NewInt(10)) + for i := deposit.WindowNumber.Value; i < deposit.WindowNumber.Value+64; i++ { + if _, ok := deposits[i]; !ok { + deposit, err = bidderClient.GetDeposit(context.Background(), &pb.GetDepositRequest{ + WindowNumber: wrapperspb.UInt64(i), + }) + if err != nil { + logger.Error("failed to get deposit", "err", err) + return err + } + depositAmount, set := big.NewInt(0).SetString(deposit.Amount, 10) + if !set { + logger.Error("failed to parse deposit amount") + return errors.New("failed to parse deposit amount") + } - deposit, err = bidderClient.Deposit(context.Background(), &pb.DepositRequest{ - Amount: topup.String(), - }) - if err != nil { - logger.Error("failed to deposit", "err", err) - return err + if depositAmount.Cmp(minDeposit) < 0 { + newDeposit, err := bidderClient.Deposit(context.Background(), &pb.DepositRequest{ + Amount: new(big.Int).Sub(minDeposit, depositAmount).String(), + WindowNumber: wrapperspb.UInt64(i), + }) + if err != nil { + logger.Error("failed to deposit", "err", err) + return err + } + logger.Info("deposit", "amount", newDeposit.Amount, "window", newDeposit.WindowNumber.Value) + deposits[newDeposit.WindowNumber.Value] = struct{}{} + } + } } - logger.Info("deposit after topup", "amount", topup.String(), "window", deposit.WindowNumber.Value) - - deposits[deposit.WindowNumber.Value] = struct{}{} - for window := range deposits { - if window < deposit.WindowNumber.Value-2 { + if window < deposit.WindowNumber.Value-3 { resp, err := bidderClient.Withdraw(context.Background(), &pb.WithdrawRequest{ WindowNumber: wrapperspb.UInt64(window), }) diff --git a/p2p/pkg/p2p/libp2p/libp2p.go b/p2p/pkg/p2p/libp2p/libp2p.go index 53da4d077..184e88d84 100644 --- a/p2p/pkg/p2p/libp2p/libp2p.go +++ b/p2p/pkg/p2p/libp2p/libp2p.go @@ -224,6 +224,8 @@ func (s *Service) handleConnectReq(streamlibp2p network.Stream) { peerID := streamlibp2p.Conn().RemotePeer() stream := newStream(streamlibp2p, nil, nil) + defer stream.Close() + peer, err := s.hsSvc.Handle(s.baseCtx, stream, peerID) if err != nil { s.logger.Error("error handling handshake", "err", err) @@ -426,6 +428,8 @@ func (s *Service) Connect(ctx context.Context, info []byte) (p2p.Peer, error) { } stream := newStream(streamlibp2p, nil, nil) + defer stream.Close() + p, err := s.hsSvc.Handshake(ctx, addrInfo.ID, stream) if err != nil { _ = s.host.Network().ClosePeer(addrInfo.ID) diff --git a/p2p/pkg/preconfirmation/tracker/tracker.go b/p2p/pkg/preconfirmation/tracker/tracker.go index d5ab7f688..649fffa63 100644 --- a/p2p/pkg/preconfirmation/tracker/tracker.go +++ b/p2p/pkg/preconfirmation/tracker/tracker.go @@ -203,13 +203,13 @@ func (t *Tracker) handleNewL1Block( // of bidder or provider should open the commitment as 1 of the txns would // fail. This delay is to ensure this. t.winners[newL1Block.BlockNumber.Int64()] = newL1Block - pastBlock, ok := t.winners[newL1Block.BlockNumber.Int64()-1] + pastBlock, ok := t.winners[newL1Block.BlockNumber.Int64()-2] if !ok { return nil } blockToProcess = pastBlock.BlockNumber for k := range t.winners { - if k < blockToProcess.Int64() { + if k < blockToProcess.Int64()-1 { delete(t.winners, k) } } diff --git a/p2p/pkg/preconfirmation/tracker/tracker_test.go b/p2p/pkg/preconfirmation/tracker/tracker_test.go index 4060b731f..986560c57 100644 --- a/p2p/pkg/preconfirmation/tracker/tracker_test.go +++ b/p2p/pkg/preconfirmation/tracker/tracker_test.go @@ -238,6 +238,11 @@ func TestTracker(t *testing.T) { Winner: winnerProvider, Window: big.NewInt(1), }) + publishNewWinner(evtMgr, &btABI, blocktracker.BlocktrackerNewL1Block{ + BlockNumber: big.NewInt(7), + Winner: winnerProvider, + Window: big.NewInt(1), + }) opened = []*store.EncryptedPreConfirmationWithDecrypted{ commitments[8], diff --git a/p2p/pkg/rpc/bidder/service_test.go b/p2p/pkg/rpc/bidder/service_test.go index 857d5bee9..710c2e4cc 100644 --- a/p2p/pkg/rpc/bidder/service_test.go +++ b/p2p/pkg/rpc/bidder/service_test.go @@ -185,7 +185,7 @@ func startServer(t *testing.T) bidderapiv1.BidderClient { if err := baseServer.Serve(lis); err != nil { // Ignore "use of closed network connection" error if opErr, ok := err.(*net.OpError); !ok || !errors.Is(opErr.Err, net.ErrClosed) { - t.Errorf("error serving server: %v", err) + t.Logf("server stopped err: %v", err) } } }() diff --git a/p2p/pkg/rpc/provider/service_test.go b/p2p/pkg/rpc/provider/service_test.go index ca82d3d5c..bbe004115 100644 --- a/p2p/pkg/rpc/provider/service_test.go +++ b/p2p/pkg/rpc/provider/service_test.go @@ -97,7 +97,7 @@ func startServer(t *testing.T) (providerapiv1.ProviderClient, *providerapi.Servi if err := baseServer.Serve(lis); err != nil { // Ignore "use of closed network connection" error if opErr, ok := err.(*net.OpError); !ok || !errors.Is(opErr.Err, net.ErrClosed) { - t.Errorf("error serving server: %v", err) + t.Logf("server stopped err: %v", err) } } }() diff --git a/x/contracts/txmonitor/txmonitor.go b/x/contracts/txmonitor/txmonitor.go index 58046bd16..8f4b7fbc3 100644 --- a/x/contracts/txmonitor/txmonitor.go +++ b/x/contracts/txmonitor/txmonitor.go @@ -22,6 +22,7 @@ var ( var ( ErrTxnCancelled = errors.New("transaction was cancelled") + ErrTxnFailed = errors.New("transaction failed") ErrMonitorClosed = errors.New("monitor was closed") ) @@ -222,13 +223,12 @@ func (m *Monitor) Sent(ctx context.Context, tx *types.Transaction) { r := <-res status := "success" if r.Err != nil { - m.logger.Error("transaction failed", "err", r.Err) status = fmt.Sprintf("failed: %v", r.Err) } if err := m.saver.Update(context.Background(), tx.Hash(), status); err != nil { m.logger.Error("failed to update transaction", "err", err) } - m.logger.Debug("transaction status", + m.logger.Info("transaction status", "txHash", tx.Hash(), "status", status, "receipt", r.Receipt, @@ -353,13 +353,28 @@ func (m *Monitor) check(ctx context.Context, newBlock uint64, lastNonce uint64) m.notify(nonce, txHashes[start+i], Result{nil, ErrTxnCancelled}) continue } + m.logger.Error("failed to get receipt", "error", r.Err, "txHash", txHashes[start+i]) + continue + } + if r.Receipt.Status != types.ReceiptStatusSuccessful { tt, err := m.helper.TraceTransaction(ctx, txHashes[start+i]) if err != nil { - m.logger.Error("retrieving transaction trace failed", "error", err) + m.logger.Error( + "retrieving transaction trace failed", + "error", err, + "txHash", txHashes[start+i], + ) + } else { + m.logger.Error("transaction failed", + "transaction_trace", tt, + "txHash", txHashes[start+i], + ) } m.logger.Error("failed to get receipt", "error", r.Err, "transaction_trace", tt) + m.notify(nonce, txHashes[start+i], Result{r.Receipt, ErrTxnFailed}) continue } + m.notify(nonce, txHashes[start+i], Result{r.Receipt, nil}) } } diff --git a/x/keysigner/keystoresigner.go b/x/keysigner/keystoresigner.go index 6c69bee5d..2f68d6cf0 100644 --- a/x/keysigner/keystoresigner.go +++ b/x/keysigner/keystoresigner.go @@ -36,6 +36,10 @@ func NewKeystoreSigner(path, password string) (*KeystoreSigner, error) { account = ksAccounts[0] } + if err := keystore.Unlock(account, password); err != nil { + return nil, err + } + return &KeystoreSigner{ keystore: keystore, password: password, @@ -73,10 +77,6 @@ func (kss *KeystoreSigner) String() string { } func (kss *KeystoreSigner) GetAuth(chainID *big.Int) (*bind.TransactOpts, error) { - if err := kss.keystore.Unlock(kss.account, kss.password); err != nil { - return nil, err - } - return bind.NewKeyStoreTransactorWithChainID(kss.keystore, kss.account, chainID) }