Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: run functional test with cached rpc response in ci #64

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
43 changes: 42 additions & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,49 @@ jobs:
echo "localstack ready"
- name: Integration Tests
run: make integration
functional:
runs-on: ubuntu-22.04
strategy:
matrix:
go-version:
- "1.21"
- "1.20"
- "1.19"
- "1.18"
test:
- internal/blockchain/integration_test/ethereum_integration_test.go
- internal/blockchain/integration_test/bsc_integration_test.go
steps:
- name: Checkout
uses: actions/checkout@v4
- name: "Setup environment"
uses: ./.github/workflows/setup
with:
go-version: ${{ matrix.go-version }}
- name: Run localstack
shell: bash
run: |
docker compose -f docker-compose-testing.yml up -d --force-recreate
echo "Waiting for localstack to be ready..."
for i in $(seq 1 999); do
if [ "$(curl -s http://localhost:4566/_localstack/health | jq -r '.services.s3')" = "available" ]; then
break
fi
sleep 0.3
if [ $(($i % 30)) -eq 0 ]; then
echo .
fi
done
echo "localstack ready"
- name: Functional Test
run: |
nohup go run tools/rpc_replayer/main.go &
cp tools/rpc_replayer/config/eth-mainnet.yml config/chainstorage/ethereum/mainnet/secrets.yml
cp tools/rpc_replayer/config/eth-goerli.yml config/chainstorage/ethereum/goerli/secrets.yml
cp tools/rpc_replayer/config/bsc-mainnet.yml config/chainstorage/bsc/mainnet/secrets.yml
make functional TARGET=${{ matrix.test }}
ci-passed:
runs-on: ubuntu-22.04
needs: [build, test, integration]
needs: [build, test, integration, functional]
steps:
- run: echo "All tests passed"
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

# Auto-generated config file for local execution
/config/**/**/**/secrets.yml
/tools/rpc_replayer/secrets.yml

# Build output
/bin/
Expand Down
27 changes: 27 additions & 0 deletions tools/rpc_replayer/config/bsc-mainnet.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
chain:
client:
master:
endpoint_group: |
{
"endpoints": [
{
"name": "replayer",
"url": "http://bsc-mainnet.local.gd:7090",
"weight": 1
}
],
"sticky_session": {
"header_hash": "x-session-hash"
}
}
slave:
endpoint_group: |
{
"endpoints": [
{
"name": "replayer",
"url": "http://bsc-mainnet.local.gd:7090",
"weight": 1
}
]
}
27 changes: 27 additions & 0 deletions tools/rpc_replayer/config/eth-goerli.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
chain:
client:
master:
endpoint_group: |
{
"endpoints": [
{
"name": "replayer",
"url": "http://eth-goerli.local.gd:7090",
"weight": 1
}
],
"sticky_session": {
"header_hash": "x-session-hash"
}
}
slave:
endpoint_group: |
{
"endpoints": [
{
"name": "replayer",
"url": "http://eth-goerli.local.gd:7090",
"weight": 1
}
]
}
27 changes: 27 additions & 0 deletions tools/rpc_replayer/config/eth-mainnet.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
chain:
client:
master:
endpoint_group: |
{
"endpoints": [
{
"name": "replayer",
"url": "http://eth-mainnet.local.gd:7090",
"weight": 1
}
],
"sticky_session": {
"header_hash": "x-session-hash"
}
}
slave:
endpoint_group: |
{
"endpoints": [
{
"name": "replayer",
"url": "http://eth-mainnet.local.gd:7090",
"weight": 1
}
]
}
137 changes: 137 additions & 0 deletions tools/rpc_replayer/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
package main
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you update README and document how to re-generate the fixtures?


import (
"bytes"
"crypto/sha256"
"encoding/json"
"errors"
"fmt"
"io"
"io/fs"
"log"
"net/http"
"os"
"path"
"regexp"
"strings"

"github.com/spf13/viper"
)

const (
baseFolder string = "tools/rpc_replayer"
)

var v *viper.Viper
var hostMatcher *regexp.Regexp

func init() {
hostMatcher = regexp.MustCompile(`^(.+)\.local\.gd(\:\d+)$`)
v = viper.New()
v.SetConfigType("yaml")
v.AutomaticEnv()
v.AllowEmptyEnv(true)
v.SetEnvPrefix("RPC_REPLAYER")
v.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
f, err := os.Open(path.Join(baseFolder, "secrets.yml"))
if err != nil && !errors.Is(err, os.ErrNotExist) {
panic(err)
}
if err == nil {
err = v.ReadConfig(f)
if err != nil {
panic(err)
}
}
}

func handleRpcRequest(w http.ResponseWriter, r *http.Request) {
g := hostMatcher.FindAllStringSubmatch(r.Host, -1)
if len(g) != 1 {
http.Error(w, "Invalid host", http.StatusBadRequest)
return
}
chain := g[0][1]
body, err := io.ReadAll(r.Body)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
fmt.Printf("Requesting %v with %v\n", chain, string(body))
var jsonBody interface{}
err = json.Unmarshal(body, &jsonBody)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
batchRequest := true
if v, ok := jsonBody.(map[string]interface{}); ok {
jsonBody = []interface{}{v}
batchRequest = false
}
var responseBody []interface{}
for _, r := range jsonBody.([]interface{}) {
req := r.(map[string]interface{})
params, err := json.Marshal(req["params"])
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
cachePath := path.Join(
baseFolder, "seed", chain,
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
baseFolder, "seed", chain,
baseFolder, "fixtures", chain,

req["method"].(string),
fmt.Sprintf("%x.json", sha256.Sum256(params)))
fmt.Printf("%+v, cache path: %v\n", jsonBody, cachePath)
cache, err := os.ReadFile(cachePath)
if err == nil {
_, _ = w.Write(cache)
Copy link
Contributor

Choose a reason for hiding this comment

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

do not ignore error

return
}
if !errors.Is(err, fs.ErrNotExist) {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
err = os.MkdirAll(path.Dir(cachePath), 0o755)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
rpc := v.GetString(chain)
if rpc == "" {
http.Error(w, fmt.Sprintf("RPC url not configured for %v", chain), http.StatusInternalServerError)
return
}
response, err := http.Post(rpc, "application/json", bytes.NewReader(body))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
result, err := io.ReadAll(response.Body)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
_ = os.WriteFile(cachePath, result, 0o644)
Copy link
Contributor

Choose a reason for hiding this comment

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

do not ignore error

responseBody = append(responseBody, json.RawMessage(result))
}

w.WriteHeader(http.StatusOK)
w.Header().Add("Content-Type", "application/json")
var resultBody []byte
if batchRequest {
resultBody, err = json.Marshal(responseBody)
} else {
resultBody, err = json.Marshal(responseBody[0])
}
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
_, _ = w.Write(resultBody)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

can you add an empty line at the end?

image


func main() {
mux := http.NewServeMux()
mux.HandleFunc("/", handleRpcRequest)
log.Fatal(http.ListenAndServe(":7090", mux))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"jsonrpc":"2.0","id":0,"result":[{"result":{"from":"0x38d9860af557e1cbf622c6513dc6876ffbf5a965","gas":"0x4b9f2","gasUsed":"0x366b4","to":"0x10ed43c718714eb63d5aa57b78b54704e256024e","input":"0x5c11d7950000000000000000000000000000000000000000000005ede20f01a45980000000000000000000000000000000000000000000000000000f6f412887b653a92500000000000000000000000000000000000000000000000000000000000000a000000000000000000000000038d9860af557e1cbf622c6513dc6876ffbf5a965000000000000000000000000000000000000000000000000000000006186a1b400000000000000000000000000000000000000000000000000000000000000030000000000000000000000009d12cc56d133fc5c60e9385b7a92f35a682da0bd000000000000000000000000bb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c000000000000000000000000e9e7cea3dedca5984780bafc599bd69add087d56","calls":[{"from":"0x10ed43c718714eb63d5aa57b78b54704e256024e","gas":"0x49c95","gasUsed":"0xba6f","to":"0x9d12cc56d133fc5c60e9385b7a92f35a682da0bd","input":"0x23b872dd00000000000000000000000038d9860af557e1cbf622c6513dc6876ffbf5a965000000000000000000000000204c71820301bedf4a43ecd0c2ae299f04710afd0000000000000000000000000000000000000000000005ede20f01a459800000","output":"0x0000000000000000000000000000000000000000000000000000000000000001","calls":[{"from":"0x9d12cc56d133fc5c60e9385b7a92f35a682da0bd","gas":"0x47c23","gasUsed":"0x4ba","to":"0x9d12cc56d133fc5c60e9385b7a92f35a682da0bd","input":"0x70a082310000000000000000000000009d12cc56d133fc5c60e9385b7a92f35a682da0bd","output":"0x000000000000000000000000000000000000000000002285a7fb475286222b40","type":"STATICCALL"},{"from":"0x9d12cc56d133fc5c60e9385b7a92f35a682da0bd","gas":"0x46471","gasUsed":"0x4ba","to":"0x9d12cc56d133fc5c60e9385b7a92f35a682da0bd","input":"0x70a08231000000000000000000000000204c71820301bedf4a43ecd0c2ae299f04710afd","output":"0x00000000000000000000000000000000000000000013720754bd8da87d9c1b6f","type":"STATICCALL"}],"value":"0x0","type":"CALL"},{"from":"0x10ed43c718714eb63d5aa57b78b54704e256024e","gas":"0x3dde5","gasUsed":"0x4cf","to":"0xe9e7cea3dedca5984780bafc599bd69add087d56","input":"0x70a0823100000000000000000000000038d9860af557e1cbf622c6513dc6876ffbf5a965","output":"0x0000000000000000000000000000000000000000000000000000000000000000","type":"STATICCALL"},{"from":"0x10ed43c718714eb63d5aa57b78b54704e256024e","gas":"0x3ce8e","gasUsed":"0xbb1","to":"0x204c71820301bedf4a43ecd0c2ae299f04710afd","input":"0x0902f1ac","output":"0x00000000000000000000000000000000000000000013720754bd8da87d9c1b6f00000000000000000000000000000000000000000000001a1f946cdd10543abe0000000000000000000000000000000000000000000000000000000061869d0b","type":"STATICCALL"},{"from":"0x10ed43c718714eb63d5aa57b78b54704e256024e","gas":"0x3bc70","gasUsed":"0x4ba","to":"0x9d12cc56d133fc5c60e9385b7a92f35a682da0bd","input":"0x70a08231000000000000000000000000204c71820301bedf4a43ecd0c2ae299f04710afd","output":"0x00000000000000000000000000000000000000000013779a241d945256dc1b6f","type":"STATICCALL"},{"from":"0x10ed43c718714eb63d5aa57b78b54704e256024e","gas":"0x3aa94","gasUsed":"0xfada","to":"0x204c71820301bedf4a43ecd0c2ae299f04710afd","input":"0x022c0d9f00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000775e1126eb7d2c200000000000000000000000058f876857a02d6762e0101bb5c46a8c1ed44dc1600000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000000","calls":[{"from":"0x204c71820301bedf4a43ecd0c2ae299f04710afd","gas":"0x36c32","gasUsed":"0x3b3a","to":"0xbb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c","input":"0xa9059cbb00000000000000000000000058f876857a02d6762e0101bb5c46a8c1ed44dc160000000000000000000000000000000000000000000000000775e1126eb7d2c2","output":"0x0000000000000000000000000000000000000000000000000000000000000001","value":"0x0","type":"CALL"},{"from":"0x204c71820301bedf4a43ecd0c2ae299f04710afd","gas":"0x32aad","gasUsed":"0x4ba","to":"0x9d12cc56d133fc5c60e9385b7a92f35a682da0bd","input":"0x70a08231000000000000000000000000204c71820301bedf4a43ecd0c2ae299f04710afd","output":"0x00000000000000000000000000000000000000000013779a241d945256dc1b6f","type":"STATICCALL"},{"from":"0x204c71820301bedf4a43ecd0c2ae299f04710afd","gas":"0x31fab","gasUsed":"0x4d2","to":"0xbb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c","input":"0x70a08231000000000000000000000000204c71820301bedf4a43ecd0c2ae299f04710afd","output":"0x00000000000000000000000000000000000000000000001a181e8bcaa19c67fc","type":"STATICCALL"}],"value":"0x0","type":"CALL"},{"from":"0x10ed43c718714eb63d5aa57b78b54704e256024e","gas":"0x2a9c8","gasUsed":"0xbb1","to":"0x58f876857a02d6762e0101bb5c46a8c1ed44dc16","input":"0x0902f1ac","output":"0x000000000000000000000000000000000000000000005485989d56f37a42ecfc000000000000000000000000000000000000000000c9dc02864b74d70536ce680000000000000000000000000000000000000000000000000000000061869d0b","type":"STATICCALL"},{"from":"0x10ed43c718714eb63d5aa57b78b54704e256024e","gas":"0x297aa","gasUsed":"0x4d2","to":"0xbb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c","input":"0x70a0823100000000000000000000000058f876857a02d6762e0101bb5c46a8c1ed44dc16","output":"0x000000000000000000000000000000000000000000005485a0133805e8fabfbe","type":"STATICCALL"},{"from":"0x10ed43c718714eb63d5aa57b78b54704e256024e","gas":"0x28852","gasUsed":"0x132d4","to":"0x58f876857a02d6762e0101bb5c46a8c1ed44dc16","input":"0x022c0d9f0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000011c5dc8c9e5451859100000000000000000000000038d9860af557e1cbf622c6513dc6876ffbf5a96500000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000000","calls":[{"from":"0x58f876857a02d6762e0101bb5c46a8c1ed44dc16","gas":"0x24e79","gasUsed":"0x731f","to":"0xe9e7cea3dedca5984780bafc599bd69add087d56","input":"0xa9059cbb00000000000000000000000038d9860af557e1cbf622c6513dc6876ffbf5a965000000000000000000000000000000000000000000000011c5dc8c9e54518591","output":"0x0000000000000000000000000000000000000000000000000000000000000001","value":"0x0","type":"CALL"},{"from":"0x58f876857a02d6762e0101bb5c46a8c1ed44dc16","gas":"0x1d5ef","gasUsed":"0x4d2","to":"0xbb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c","input":"0x70a0823100000000000000000000000058f876857a02d6762e0101bb5c46a8c1ed44dc16","output":"0x000000000000000000000000000000000000000000005485a0133805e8fabfbe","type":"STATICCALL"},{"from":"0x58f876857a02d6762e0101bb5c46a8c1ed44dc16","gas":"0x1cad5","gasUsed":"0x4cf","to":"0xe9e7cea3dedca5984780bafc599bd69add087d56","input":"0x70a0823100000000000000000000000058f876857a02d6762e0101bb5c46a8c1ed44dc16","output":"0x000000000000000000000000000000000000000000c9dbf0c06ee838b0e548d7","type":"STATICCALL"}],"value":"0x0","type":"CALL"},{"from":"0x10ed43c718714eb63d5aa57b78b54704e256024e","gas":"0x1539c","gasUsed":"0x4cf","to":"0xe9e7cea3dedca5984780bafc599bd69add087d56","input":"0x70a0823100000000000000000000000038d9860af557e1cbf622c6513dc6876ffbf5a965","output":"0x000000000000000000000000000000000000000000000011c5dc8c9e54518591","type":"STATICCALL"}],"value":"0x0","type":"CALL"}},{"result":{"from":"0x29a97c6effb8a411dabc6adeefaa84f5067c8bbe","gas":"0x7fffffffffffac47","gasUsed":"0x96b6","to":"0x0000000000000000000000000000000000001000","input":"0xf340fa0100000000000000000000000029a97c6effb8a411dabc6adeefaa84f5067c8bbe","value":"0x4f30a30c0b2000","type":"CALL"}}]}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"jsonrpc":"2.0","id":0,"result":{"hash":"0xb94ccd031e76c41214e924ef21bc5aec4c1264272f6fa1c15a03ed8f29433ca5","parentHash":"0x7641e9a1220486bdd8554fa4b24fc1afb6a0e0e9cba6d356da7d91b942595173","sha3Uncles":"0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347","miner":"0x29a97c6effb8a411dabc6adeefaa84f5067c8bbe","stateRoot":"0x4c48c93f5c4a61698e29a6a59e907f01c6085643c2a610f50839831d35ad5244","transactionsRoot":"0x8ab4f30177282819611263d5632a5b30eab9a8a9b98629d2ab70847433a1abdd","receiptsRoot":"0x39da63c347d67388f6c78530c4e40517138b43f2034b0c1fe8d7701cf9fdda6a","logsBloom":"0x00200210000000102000000080000000000000000000000000000000000000000000000000000000010000000000000000000000000000000020000000200000000000000000040000000008000000202010000000000000800400000000080000001020000200400100000000000000000000808000000000200010200000001000000000000000000080080000000000240400000000080000004000000800020000000000000000000400020040000100000000000000008000000000000000400002020000020000000000001000000000000000001000000000000080000010000000000000010000000000000000000000000000000000000000000000","difficulty":"0x2","number":"0xbd8381","gasLimit":"0x5c4e609","gasUsed":"0x3a9b2","timestamp":"0x61869d0e","extraData":"0xd883010103846765746888676f312e31362e39856c696e7578000000fc3ca6b71b10d399072823b07603fd5d4dad7ea3d43effe92b71604d7b64c1c6390ce3bc4ff2e5d186ab64d89188317b7495e9546708ec8d06195f977b79c59970fbf70301","mixHash":"0x0000000000000000000000000000000000000000000000000000000000000000","nonce":"0x0000000000000000","size":"0x48c","totalDifficulty":"0x1790e2a","transactions":[{"blockHash":"0xb94ccd031e76c41214e924ef21bc5aec4c1264272f6fa1c15a03ed8f29433ca5","blockNumber":"0xbd8381","from":"0x38d9860af557e1cbf622c6513dc6876ffbf5a965","gas":"0x5158e","gasPrice":"0x174876e800","hash":"0x33dd44fb8b489f7effe4a434829543e2f5eaaf24a8bac425acac4907af69b1f5","input":"0x5c11d7950000000000000000000000000000000000000000000005ede20f01a45980000000000000000000000000000000000000000000000000000f6f412887b653a92500000000000000000000000000000000000000000000000000000000000000a000000000000000000000000038d9860af557e1cbf622c6513dc6876ffbf5a965000000000000000000000000000000000000000000000000000000006186a1b400000000000000000000000000000000000000000000000000000000000000030000000000000000000000009d12cc56d133fc5c60e9385b7a92f35a682da0bd000000000000000000000000bb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c000000000000000000000000e9e7cea3dedca5984780bafc599bd69add087d56","nonce":"0x8","to":"0x10ed43c718714eb63d5aa57b78b54704e256024e","transactionIndex":"0x0","value":"0x0","type":"0x0","v":"0x93","r":"0x34fa1d5eec13b9d9fb6d56b7dd3ff557d1ee5a9e0ef08aca986cb7dade1431bc","s":"0xdd924bc2a6e49befe280fb0c5de43fb860a53d78d7cdec7be52763ac137e002"},{"blockHash":"0xb94ccd031e76c41214e924ef21bc5aec4c1264272f6fa1c15a03ed8f29433ca5","blockNumber":"0xbd8381","from":"0x29a97c6effb8a411dabc6adeefaa84f5067c8bbe","gas":"0x7fffffffffffffff","gasPrice":"0x0","hash":"0xd3035674621c53539a92f5700a386fdc262cf6a5b49bf876f459a71ee481fb0f","input":"0xf340fa0100000000000000000000000029a97c6effb8a411dabc6adeefaa84f5067c8bbe","nonce":"0x3e2f2","to":"0x0000000000000000000000000000000000001000","transactionIndex":"0x1","value":"0x4f30a30c0b2000","type":"0x0","v":"0x94","r":"0x3075b66d836035f797f6b8790debaac85b06d37e12c574f2b7f395b95b9e80e0","s":"0x37dcafa26575e9f129f2de7fa656b1f0bc794aa568d9c7c2ca5664226d9f1321"}],"uncles":[]}}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"jsonrpc":"2.0","id":0,"result":null}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"jsonrpc":"2.0","id":0,"result":null}
Loading