distribworks / xk6-ethereum

K6 extension for ethereum protocols
MIT License
10 stars 8 forks source link

🚨 [Runtime error] Invalid memory address or nil pointer dereference #14

Open olivmath opened 2 months ago

olivmath commented 2 months ago

I WANT:

I simply want to send hundreds of tx to my local node.

I DO:

  1. install this extension
  2. write my script
  3. execute test
  4. get a error 😢

MY MACHINE:

STEPS

install this extension

>>> docker run --rm -e GOOS=darwin -u "$(id -u):$(id -g)" -v "${PWD}:/xk6" \
  grafana/xk6 build \
  --with github.com/distribworks/xk6-ethereum

WARNING: The requested image's platform (linux/amd64) does not match the detected host platform (linux/arm64/v8) and no specific platform was requested
fixuid: fixuid should only ever be used on development systems. DO NOT USE IN PRODUCTION
fixuid: updating user 'xk6' to UID '501'
fixuid: runtime GID '20' matches existing group 'dialout'; not changing GID
fixuid: recursively searching path /
fixuid: chown /home/xk6
2024/08/29 21:23:55 [INFO] Temporary folder: /tmp/buildenv_2024-08-29-2123.475806423
2024/08/29 21:23:55 [INFO] Initializing Go module
2024/08/29 21:23:55 [INFO] exec (timeout=10s): /usr/local/go/bin/go mod init k6 
go: creating new go.mod: module k6
2024/08/29 21:23:55 [INFO] Pinning versions
2024/08/29 21:23:55 [INFO] exec (timeout=0s): /usr/local/go/bin/go mod edit -require github.com/distribworks/xk6-ethereum@latest 
2024/08/29 21:23:55 [INFO] exec (timeout=0s): /usr/local/go/bin/go mod tidy -compat=1.17 
go: downloading github.com/distribworks/xk6-ethereum v1.0.4
go: downloading github.com/umbracle/ethgo v0.1.4-0.20230620065855-8aa9d5b509da
go: downloading github.com/grafana/sobek v0.0.0-20240607083612-4f0cd64f4e78
go: downloading go.k6.io/k6 v0.51.1-0.20240610082146-1f01a9bc2365
go: downloading github.com/stretchr/testify v1.9.0
go: downloading github.com/dop251/goja v0.0.0-20240516125602-ccbae20bcec2
go: downloading github.com/mitchellh/mapstructure v1.4.1
go: downloading golang.org/x/crypto v0.24.0
go: downloading github.com/valyala/fastjson v1.4.1
go: downloading github.com/umbracle/fastrlp v0.0.0-20220527094140-59d5dd30e722
go: downloading github.com/btcsuite/btcd v0.22.1
go: downloading github.com/btcsuite/btcutil v1.0.3-0.20201208143702-a53e38424cce
go: downloading github.com/tyler-smith/go-bip39 v1.1.0
go: downloading github.com/go-sourcemap/sourcemap v2.1.4+incompatible
go: downloading golang.org/x/text v0.16.0
go: downloading github.com/davecgh/go-spew v1.1.1
go: downloading github.com/pmezard/go-difflib v1.0.0
go: downloading gopkg.in/yaml.v3 v3.0.1
go: downloading github.com/dlclark/regexp2 v1.9.0
go: downloading github.com/google/pprof v0.0.0-20230728192033-2ba5b33183c6
go: downloading github.com/gorilla/websocket v1.5.1
go: downloading github.com/valyala/fasthttp v1.4.0
go: downloading github.com/google/gofuzz v1.2.0
go: downloading golang.org/x/sys v0.21.0
go: downloading github.com/btcsuite/btcd/chaincfg/chainhash v1.0.1
go: downloading github.com/serenize/snaker v0.0.0-20201027110005-a7ad2135616e
go: downloading github.com/mailru/easyjson v0.7.7
go: downloading github.com/mstoykov/atlas v0.0.0-20220811071828-388f114305dd
go: downloading gopkg.in/guregu/null.v3 v3.5.0
go: downloading golang.org/x/net v0.26.0
go: downloading github.com/klauspost/compress v1.17.7
go: downloading github.com/valyala/bytebufferpool v1.0.0
go: downloading github.com/sirupsen/logrus v1.9.3
go: downloading go.opentelemetry.io/otel/trace v1.24.0
go: downloading golang.org/x/time v0.5.0
go: downloading github.com/spf13/afero v1.9.5
go: downloading github.com/evanw/esbuild v0.21.2
go: downloading go.opentelemetry.io/otel v1.24.0
go: downloading github.com/onsi/ginkgo v1.16.5
go: downloading github.com/onsi/gomega v1.18.1
go: downloading github.com/josharian/intern v1.0.0
go: downloading go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.24.0
go: downloading go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.24.0
go: downloading go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.24.0
go: downloading go.opentelemetry.io/otel/sdk v1.24.0
go: downloading github.com/fatih/color v1.16.0
go: downloading google.golang.org/grpc v1.63.2
go: downloading github.com/go-logr/logr v1.4.1
go: downloading go.opentelemetry.io/otel/metric v1.24.0
go: downloading github.com/go-logr/stdr v1.2.2
go: downloading github.com/mattn/go-colorable v0.1.13
go: downloading github.com/mattn/go-isatty v0.0.20
go: downloading google.golang.org/protobuf v1.33.0
go: downloading go.opentelemetry.io/proto/otlp v1.1.0
go: downloading google.golang.org/genproto/googleapis/rpc v0.0.0-20240227224415-6ceb2ff114de
go: downloading github.com/cenkalti/backoff/v4 v4.2.1
go: downloading github.com/grpc-ecosystem/grpc-gateway/v2 v2.19.0
go: downloading google.golang.org/genproto/googleapis/api v0.0.0-20240227224415-6ceb2ff114de
go: downloading pgregory.net/rapid v0.5.5
go: downloading github.com/ory/dockertest v3.3.5+incompatible
go: downloading github.com/mstoykov/envconfig v1.5.0
go: downloading gopkg.in/yaml.v2 v2.4.0
go: downloading github.com/mccutchen/go-httpbin v1.1.2-0.20190116014521-c5cb2f4802fa
go: downloading github.com/andybalholm/brotli v1.1.0
go: downloading github.com/google/go-cmp v0.6.0
go: downloading go.uber.org/goleak v1.3.0
go: downloading github.com/cenkalti/backoff v2.2.1+incompatible
go: downloading github.com/pkg/errors v0.9.1
go: downloading github.com/Microsoft/go-winio v0.6.0
go: downloading google.golang.org/genproto v0.0.0-20240227224415-6ceb2ff114de
go: downloading github.com/docker/go-units v0.4.0
go: downloading github.com/opencontainers/runc v1.1.5
go: downloading github.com/Nvveen/Gotty v0.0.0-20120604004816-cd527374f1e5
go: downloading github.com/opencontainers/image-spec v1.0.2
go: downloading github.com/containerd/continuity v0.3.0
go: downloading github.com/Azure/go-ansiterm v0.0.0-20170929234023-d6e3b3328b78
go: downloading github.com/docker/go-connections v0.4.0
go: downloading golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d
go: downloading github.com/opencontainers/go-digest v1.0.0
go: downloading golang.org/x/sync v0.7.0
go: downloading golang.org/x/mod v0.17.0
go: finding module for package github.com/nxadm/tail
go: downloading github.com/nxadm/tail v1.4.11
go: found github.com/nxadm/tail in github.com/nxadm/tail v1.4.11
go: downloading gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7
go: downloading github.com/fsnotify/fsnotify v1.6.0
2024/08/29 21:24:21 [INFO] exec (timeout=0s): /usr/local/go/bin/go mod tidy -compat=1.17 
2024/08/29 21:24:22 [INFO] Writing main module: /tmp/buildenv_2024-08-29-2123.475806423/main.go
2024/08/29 21:24:22 [INFO] exec (timeout=0s): /usr/local/go/bin/go mod tidy -compat=1.17 
go: downloading golang.org/x/crypto/x509roots/fallback v0.0.0-20240604170348-d4e7c9cb6cb8
go: downloading golang.org/x/term v0.21.0
go: downloading github.com/grafana/xk6-dashboard v0.7.4
go: downloading github.com/grafana/xk6-output-prometheus-remote v0.4.0
go: downloading github.com/spf13/cobra v1.4.0
go: downloading github.com/spf13/pflag v1.0.5
go: downloading github.com/grafana/xk6-browser v1.5.2-0.20240607140836-ffcc1f5169ad
go: downloading github.com/grafana/xk6-redis v0.3.0
go: downloading github.com/grafana/xk6-webcrypto v0.4.0
go: downloading github.com/grafana/xk6-websockets v0.5.1
go: downloading github.com/influxdata/influxdb1-client v0.0.0-20190402204710-8ff2fc3824fc
go: downloading github.com/DataDog/datadog-go v0.0.0-20180330214955-e67964b4021a
go: downloading gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c
go: downloading github.com/Azure/go-ntlmssp v0.0.0-20221128193559-754e69321358
go: downloading github.com/Soontao/goHttpDigestClient v0.0.0-20170320082612-6d28bb1415c5
go: downloading github.com/nu7hatch/gouuid v0.0.0-20131221200532-179d4d0c4d8d
go: downloading github.com/redis/go-redis/v9 v9.0.5
go: downloading github.com/mstoykov/k6-taskqueue-lib v0.1.0
go: downloading github.com/jhump/protoreflect v1.15.6
go: downloading github.com/golang/protobuf v1.5.4
go: downloading github.com/PuerkitoBio/goquery v1.9.1
go: downloading github.com/tidwall/gjson v1.17.1
go: downloading github.com/google/uuid v1.6.0
go: downloading github.com/chromedp/cdproto v0.0.0-20221023212508-67ada9507fb2
go: downloading github.com/kr/pretty v0.3.1
go: downloading github.com/cespare/xxhash/v2 v2.2.0
go: downloading github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f
go: downloading github.com/bufbuild/protocompile v0.8.0
go: downloading github.com/grpc-ecosystem/go-grpc-middleware v1.4.0
go: downloading github.com/andybalholm/cascadia v1.3.2
go: downloading github.com/inconshreveable/mousetrap v1.0.0
go: downloading buf.build/gen/go/prometheus/prometheus/protocolbuffers/go v1.31.0-20230627135113-9a12bc2590d2.1
go: downloading github.com/prometheus/client_model v0.4.0
go: downloading github.com/prometheus/client_golang v1.16.0
go: downloading github.com/kr/text v0.2.0
go: downloading github.com/rogpeppe/go-internal v1.10.0
go: downloading github.com/chromedp/sysutil v1.0.0
go: downloading github.com/tidwall/match v1.1.1
go: downloading github.com/tidwall/pretty v1.2.1
go: downloading github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8
go: downloading github.com/r3labs/sse/v2 v2.10.0
go: downloading buf.build/gen/go/gogo/protobuf/protocolbuffers/go v1.31.0-20210810001428-4df00b267f94.1
go: downloading gopkg.in/cenkalti/backoff.v1 v1.1.0
go: downloading github.com/prometheus/common v0.42.0
go: downloading github.com/prometheus/procfs v0.10.1
go: downloading github.com/beorn7/perks v1.0.1
go: downloading github.com/matttproud/golang_protobuf_extensions v1.0.4
go: downloading github.com/onsi/gomega v1.20.2
go: downloading github.com/golang/snappy v0.0.4
go: downloading github.com/bsm/ginkgo/v2 v2.7.0
go: downloading github.com/bsm/gomega v1.26.0
2024/08/29 21:24:31 [INFO] Build environment ready
2024/08/29 21:24:31 [INFO] Building k6
2024/08/29 21:24:31 [INFO] exec (timeout=0s): /usr/local/go/bin/go mod tidy -compat=1.17 
2024/08/29 21:24:32 [INFO] exec (timeout=0s): /usr/local/go/bin/go build -o /xk6/k6 -ldflags=-w -s -trimpath 

2024/08/29 21:26:52 [INFO] Build complete: ./k6
2024/08/29 21:26:52 [INFO] Cleaning up temporary folder: /tmp/buildenv_2024-08-29-2123.475806423

xk6 has now produced a new k6 binary which may be different than the command on your system path!
Be sure to run './k6 run <SCRIPT_NAME>' from the '/xk6' directory.

>>> ls -lhs

total 93416
drwxr-xr-x   7 olivmath  staff   224B Aug 29 17:56 .
drwxr-xr-x   7 olivmath  staff   224B Aug 28 23:48 ..
drwxr-xr-x  10 olivmath  staff   320B Aug 28 12:09 docker-compose.yml
-rwxr-xr-x   1 olivmath  staff    46M Aug 29 18:26 k6
-rw-r--r--   1 olivmath  staff   1.1K Aug 29 18:37 script.js

write my script

import eth from "k6/x/ethereum";
import { sleep } from "k6";

const privateKey =
  "ac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80";
const from = "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266";
const to = "0x70997970C51812dc3A010C7d01b50e0d17dc79C8";
const amount = Number(0.1 * 1e18);
const rpcUrl = "http://localhost:8545";

const client = new eth.Client({
  url: rpcUrl,
  privateKey: privateKey,
});

export const options = {
  stages: [
    { duration: "10s", target: 20 },
    { duration: "10s", target: 10 },
    { duration: "10s", target: 0 },
  ],
};

export function setup() {
  return { nonce: client.getNonce(from) };
}

function sendTx(data) {
  console.log(`nonce => ${data.nonce}`);
  const gas = client.gasPrice();
  console.log(`gas price => ${gas}`);

  const bal = client.getBalance(from, client.blockNumber());
  console.log(`bal => ${bal}`);

  const tx = {
    to: to,
    value: amount,
    gas_price: gas,
    nonce: data.nonce,
  };

  const txh = client.sendRawTransaction(tx);
  console.log("tx hash => " + txh);
  data.nonce = data.nonce + 1;
}
export default function (data) {
  sendTx(data);
  sleep(1);
}

execute test

>>> ./k6 run script.js

          /\      |‾‾| /‾‾/   /‾‾/   
     /\  /  \     |  |/  /   /  /    
    /  \/    \    |     (   /   ‾‾\  
   /          \   |  |\  \ |  (‾)  | 
  / __________ \  |__| \__\ \_____/ .io

     execution: local
        script: script.js
        output: -

     scenarios: (100.00%) 1 scenario, 1 max VUs, 10m30s max duration (incl. graceful stop):
              * default: 1 iterations for each of 1 VUs (maxDuration: 10m0s, gracefulStop: 30s)

INFO[0000] nonce => 66                                   source=console
INFO[0000] gas price => 1000                             source=console
INFO[0000] bal => 10182407872996600000                   source=console
INFO[0000] tx hash => 0x4f4d6fe33e49b0b9c4105d65efeae06e06de8dc9248f01cca6b424352a567916  source=console
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x2c0 pc=0x3c18a21]
default   [--------------------------------------] 1 VUs  00m00.5s/10m0s  0/1 iters, 1 per VU
goroutine 41 [running]:
github.com/distribworks/xk6-ethereum.(*Client).pollForBlocks(0xc000338540)
        github.com/distribworks/xk6-ethereum@v1.0.4/ethereum.go:381 +0xba1
created by github.com/distribworks/xk6-ethereum.(*ModuleInstance).NewClient in goroutine 1
        github.com/distribworks/xk6-ethereum@v1.0.4/module.go:120 +0x35c
nmilo commented 2 weeks ago

Experiencing same issue, any thoughts? @vcastellm