|
| 1 | +package gnosis |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/hex" |
| 6 | + "encoding/json" |
| 7 | + "io" |
| 8 | + "net/http" |
| 9 | + "net/http/httptest" |
| 10 | + "os" |
| 11 | + "strings" |
| 12 | + "testing" |
| 13 | + |
| 14 | + "github.com/ethereum/go-ethereum/common" |
| 15 | + "github.com/ethereum/go-ethereum/core/types" |
| 16 | + validatorRegistryBindings "github.com/shutter-network/gnosh-contracts/gnoshcontracts/validatorregistry" |
| 17 | + "gotest.tools/assert" |
| 18 | + |
| 19 | + "github.com/shutter-network/rolling-shutter/rolling-shutter/keyperimpl/gnosis/database" |
| 20 | + "github.com/shutter-network/rolling-shutter/rolling-shutter/medley/beaconapiclient" |
| 21 | + "github.com/shutter-network/rolling-shutter/rolling-shutter/medley/testsetup" |
| 22 | +) |
| 23 | + |
| 24 | +func TestAggregateValidationWithData(t *testing.T) { |
| 25 | + if testing.Short() { |
| 26 | + t.Skip("skipping integration test") |
| 27 | + } |
| 28 | + ctx := context.Background() |
| 29 | + url := mockBeaconClientWithJSONData(t) |
| 30 | + cl, err := beaconapiclient.New(url) |
| 31 | + assert.NilError(t, err) |
| 32 | + dbpool, dbclose := testsetup.NewTestDBPool(ctx, t, database.Definition) |
| 33 | + t.Cleanup(dbclose) |
| 34 | + vs := ValidatorSyncer{ |
| 35 | + BeaconAPIClient: cl, |
| 36 | + DBPool: dbpool, |
| 37 | + ChainID: 10200, |
| 38 | + } |
| 39 | + |
| 40 | + msg := readMsg(t) |
| 41 | + |
| 42 | + message, err := hex.DecodeString(msg["message"][2:]) |
| 43 | + assert.NilError(t, err) |
| 44 | + |
| 45 | + signature, err := hex.DecodeString(msg["signature"][2:]) |
| 46 | + assert.NilError(t, err) |
| 47 | + |
| 48 | + events := []*validatorRegistryBindings.ValidatorregistryUpdated{{ |
| 49 | + Signature: signature, |
| 50 | + Message: message, |
| 51 | + Raw: types.Log{ |
| 52 | + Address: common.HexToAddress("0xa9289A3Dd14FEBe10611119bE81E5d35eAaC3084"), |
| 53 | + }, |
| 54 | + }} |
| 55 | + |
| 56 | + finalEvents, err := vs.filterEvents(ctx, events) |
| 57 | + assert.NilError(t, err) |
| 58 | + |
| 59 | + assert.DeepEqual(t, len(finalEvents), 1) |
| 60 | +} |
| 61 | + |
| 62 | +func mockBeaconClientWithJSONData(t *testing.T) string { |
| 63 | + t.Helper() |
| 64 | + jsonFile, err := os.Open("../../../testdata/validatorInfo.json") |
| 65 | + assert.NilError(t, err) |
| 66 | + defer jsonFile.Close() |
| 67 | + |
| 68 | + byteValue, _ := io.ReadAll(jsonFile) |
| 69 | + var result map[string]string |
| 70 | + json.Unmarshal([]byte(byteValue), &result) |
| 71 | + |
| 72 | + return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 73 | + parts := strings.Split(r.URL.Path, "/") |
| 74 | + |
| 75 | + x := beaconapiclient.GetValidatorByIndexResponse{ |
| 76 | + Finalized: true, |
| 77 | + Data: beaconapiclient.ValidatorData{ |
| 78 | + Validator: beaconapiclient.Validator{ |
| 79 | + PubkeyHex: result[parts[len(parts)-1]], |
| 80 | + }, |
| 81 | + }, |
| 82 | + } |
| 83 | + res, err := json.Marshal(x) |
| 84 | + assert.NilError(t, err) |
| 85 | + w.WriteHeader(http.StatusOK) |
| 86 | + _, err = w.Write(res) |
| 87 | + assert.NilError(t, err) |
| 88 | + })).URL |
| 89 | +} |
| 90 | + |
| 91 | +func readMsg(t *testing.T) map[string]string { |
| 92 | + t.Helper() |
| 93 | + jsonFile, err := os.Open("../../../testdata/signedRegistrations.json") |
| 94 | + assert.NilError(t, err) |
| 95 | + defer jsonFile.Close() |
| 96 | + |
| 97 | + byteValue, _ := io.ReadAll(jsonFile) |
| 98 | + var result map[string]string |
| 99 | + json.Unmarshal([]byte(byteValue), &result) |
| 100 | + return result |
| 101 | +} |
0 commit comments