Skip to content

Commit

Permalink
More UTs and fix regression tests (#116)
Browse files Browse the repository at this point in the history
* ut: client

* ut: config

* ut: rpc tests modification

* ut: sync package

* fix: lint

* fix: regression tests

* fix: add ctx to reorg detector start function

* fix: comments
  • Loading branch information
goran-ethernal committed Aug 28, 2024
1 parent 9b683fc commit 51f4b0b
Show file tree
Hide file tree
Showing 13 changed files with 403 additions and 40 deletions.
21 changes: 21 additions & 0 deletions .github/actions/monitor-cdk-verified-batches/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
name: monitor-cdk-verified-batches
description: Check that batches are being verified in a CDK environment

inputs:
verified_batches_target:
description: The minimum number of batches to be verified
required: false
default: '30'
timeout:
description: The script timeout in seconds
required: false
default: '600' # 10 minutes

runs:
using: "composite"
steps:
- name: Check that batches are being verified
working-directory: .github/actions/monitor-cdk-verified-batches
shell: bash
run: ./batch_verification_monitor.sh ${{ inputs.verified_batches_target }} ${{ inputs.timeout }}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/bin/bash

# This script monitors the verification progress of zkEVM batches.
# Usage: ./batch_verification_monitor <verified_batches_target> <timeout>

# The number of batches to be verified.
verified_batches_target="$1"

# The script timeout (in seconds).
timeout="$2"

start_time=$(date +%s)
end_time=$((start_time + timeout))

rpc_url="$(kurtosis port print cdk-v1 cdk-erigon-node-001 http-rpc)"
while true; do
verified_batches="$(cast to-dec "$(cast rpc --rpc-url "$rpc_url" zkevm_verifiedBatchNumber | sed 's/"//g')")"
echo "[$(date '+%Y-%m-%d %H:%M:%S')] Verified Batches: $verified_batches"

current_time=$(date +%s)
if (( current_time > end_time )); then
echo "[$(date '+%Y-%m-%d %H:%M:%S')] ❌ Exiting... Timeout reached!"
exit 1
fi

if (( verified_batches > verified_batches_target )); then
echo "[$(date '+%Y-%m-%d %H:%M:%S')] ✅ Exiting... $verified_batches batches were verified!"
exit 0
fi

sleep 10
done
8 changes: 6 additions & 2 deletions .github/workflows/regression-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
uses: actions/checkout@v4
with:
repository: 0xPolygon/kurtosis-cdk
ref: feat/cdk-erigon-zkevm
ref: main
path: kurtosis-cdk

- name: Install Kurtosis CDK tools
Expand All @@ -39,7 +39,11 @@ jobs:
working-directory: ./kurtosis-cdk
run: kurtosis run --enclave cdk-v1 --args-file params.yml --image-download always .

- name: Set executable permissions for the script
working-directory: ./cdk-data-availability
run: sudo chmod +x .github/actions/monitor-cdk-verified-batches/batch_verification_monitor.sh

- name: Monitor verified batches
working-directory: ./kurtosis-cdk
working-directory: ./cdk-data-availability
shell: bash
run: .github/actions/monitor-cdk-verified-batches/batch_verification_monitor.sh 19 600
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ dist

test/gethData
test/coverage.out
coverage.out
99 changes: 99 additions & 0 deletions client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,3 +318,102 @@ func TestClient_ListOffChainData(t *testing.T) {
})
}
}

func TestClient_SignSequenceBanana(t *testing.T) {
t.Parallel()

tests := []struct {
name string
ssb types.SignedSequenceBanana
result string
signature []byte
statusCode int
err error
}{
{
name: "successfully signed banana sequence",
ssb: types.SignedSequenceBanana{
Sequence: types.SequenceBanana{},
Signature: []byte("signature00"),
},
result: fmt.Sprintf(`{"result":"%s"}`, hex.EncodeToString([]byte("signature11"))),
signature: []byte("signature11"),
},
{
name: "error returned by rpc server",
ssb: types.SignedSequenceBanana{
Sequence: types.SequenceBanana{},
Signature: []byte("signature00"),
},
result: `{"error":{"code":123,"message":"test error"}}`,
err: errors.New("123 test error"),
},
{
name: "invalid signature returned by rpc server",
ssb: types.SignedSequenceBanana{
Sequence: types.SequenceBanana{},
Signature: []byte("signature00"),
},
result: `{"result":"invalid-signature"}`,
},
{
name: "unsuccessful status code returned by rpc server",
ssb: types.SignedSequenceBanana{
Sequence: types.SequenceBanana{},
Signature: []byte("signature00"),
},
statusCode: http.StatusInternalServerError,
err: errors.New("invalid status code, expected: 200, found: 500"),
},
}
for _, tt := range tests {
tt := tt

t.Run(tt.name, func(t *testing.T) {
t.Parallel()

srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var request rpc.Request
require.NoError(t, json.NewDecoder(r.Body).Decode(&request))
require.Equal(t, "datacom_signSequenceBanana", request.Method)

var params []types.SignedSequenceBanana
require.NoError(t, json.Unmarshal(request.Params, &params))
require.Equal(t, tt.ssb, params[0])

if tt.statusCode > 0 {
w.WriteHeader(tt.statusCode)
}

_, err := fmt.Fprint(w, tt.result)
require.NoError(t, err)
}))
defer srv.Close()

client := New(srv.URL)

result, err := client.SignSequenceBanana(context.Background(), tt.ssb)
if tt.err != nil {
require.Error(t, err)
require.EqualError(t, tt.err, err.Error())
} else {
require.NoError(t, err)
require.Equal(t, tt.signature, result)
}
})
}
}

func TestClient_Factory_New(t *testing.T) {
t.Parallel()

url := "http://example.com"
f := NewFactory()

c := f.New(url)
require.NotNil(t, c)

client, ok := c.(*client)
require.True(t, ok)
require.Equal(t, url, client.url)
}
2 changes: 1 addition & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ func start(cliCtx *cli.Context) error {
log.Fatal(err)
}

if err = detector.Start(); err != nil {
if err = detector.Start(cliCtx.Context); err != nil {
log.Fatal(err)
}

Expand Down
61 changes: 61 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import (
)

func Test_Defaults(t *testing.T) {
t.Parallel()

tcs := []struct {
path string
expectedValue interface{}
Expand Down Expand Up @@ -49,13 +51,17 @@ func Test_Defaults(t *testing.T) {
for _, tc := range tcs {
tc := tc
t.Run(tc.path, func(t *testing.T) {
t.Parallel()

actual := getValueFromStruct(tc.path, cfg)
require.Equal(t, tc.expectedValue, actual)
})
}
}

func Test_ConfigFileNotFound(t *testing.T) {
t.Parallel()

flags := flag.FlagSet{}
flags.String("cfg", "/fictitious-file/foo.cfg", "")

Expand All @@ -65,6 +71,8 @@ func Test_ConfigFileNotFound(t *testing.T) {
}

func Test_ConfigFileOverride(t *testing.T) {
t.Parallel()

tempDir := t.TempDir()
overrides := filepath.Join(tempDir, "overrides.toml")
f, err := os.Create(overrides)
Expand All @@ -81,6 +89,59 @@ func Test_ConfigFileOverride(t *testing.T) {
require.Equal(t, "0xDEADBEEF", cfg.L1.PolygonValidiumAddress)
}

func Test_NewKeyFromKeystore(t *testing.T) {
t.Parallel()

t.Run("valid keystore file", func(t *testing.T) {
t.Parallel()

cfg := types.KeystoreFileConfig{
Path: "../test/config/test-member.keystore",
Password: "testonly",
}

key, err := NewKeyFromKeystore(cfg)
require.NoError(t, err)
require.NotNil(t, key)
})

t.Run("no path and password", func(t *testing.T) {
t.Parallel()

cfg := types.KeystoreFileConfig{}

key, err := NewKeyFromKeystore(cfg)
require.NoError(t, err)
require.Nil(t, key)
})

t.Run("invalid keystore file", func(t *testing.T) {
t.Parallel()

cfg := types.KeystoreFileConfig{
Path: "non-existent.keystore",
Password: "testonly",
}

key, err := NewKeyFromKeystore(cfg)
require.ErrorContains(t, err, "no such file or directory")
require.Nil(t, key)
})

t.Run("invalid password", func(t *testing.T) {
t.Parallel()

cfg := types.KeystoreFileConfig{
Path: "../test/config/test-member.keystore",
Password: "invalid",
}

key, err := NewKeyFromKeystore(cfg)
require.ErrorContains(t, err, "could not decrypt key with given password")
require.Nil(t, key)
})
}

func getValueFromStruct(path string, object interface{}) interface{} {
keySlice := strings.Split(path, ".")
v := reflect.ValueOf(object)
Expand Down
3 changes: 1 addition & 2 deletions rpc/client_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package rpc

import (
"context"
"encoding/json"
"errors"
"fmt"
Expand Down Expand Up @@ -55,7 +54,7 @@ func Test_JSONRPCCallWithContext(t *testing.T) {
}))
defer svr.Close()

got, err := JSONRPCCallWithContext(context.Background(), svr.URL, "test")
got, err := JSONRPCCall(svr.URL, "test")
if tt.err != nil {
require.Error(t, err)
require.EqualError(t, tt.err, err.Error())
Expand Down
15 changes: 0 additions & 15 deletions rpc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,21 +245,6 @@ func handleError(w http.ResponseWriter, err error) {
}
}

// RPCErrorResponse formats error to be returned through RPC
func RPCErrorResponse(code int, message string, err error) (interface{}, Error) {
return RPCErrorResponseWithData(code, message, nil, err)
}

// RPCErrorResponseWithData formats error to be returned through RPC
func RPCErrorResponseWithData(code int, message string, data *[]byte, err error) (interface{}, Error) {
if err != nil {
log.Errorf("%v: %v", message, err.Error())
} else {
log.Error(message)
}
return nil, NewRPCErrorWithData(code, message, data)
}

func combinedLog(r *http.Request, start time.Time, httpStatus, dataLen int) {
log.Infof("%s - - %s \"%s %s %s\" %d %d \"%s\" \"%s\"",
r.RemoteAddr,
Expand Down
2 changes: 2 additions & 0 deletions sequencer/tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ func (st *Tracker) trackAddrChanges(ctx context.Context) {
if ctx.Err() != nil && ctx.Err() != context.DeadlineExceeded {
log.Warnf("context cancelled: %v", ctx.Err())
}
return
case <-st.stop:
return
}
Expand Down Expand Up @@ -227,6 +228,7 @@ func (st *Tracker) trackUrlChanges(ctx context.Context) {
if ctx.Err() != nil && ctx.Err() != context.DeadlineExceeded {
log.Warnf("context cancelled: %v", ctx.Err())
}
return
case <-st.stop:
return
}
Expand Down
Loading

0 comments on commit 51f4b0b

Please sign in to comment.