Skip to content

Commit fdd3a5d

Browse files
integration data test
1 parent 1e9ae44 commit fdd3a5d

File tree

4 files changed

+106
-3
lines changed

4 files changed

+106
-3
lines changed
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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+
}

rolling-shutter/medley/validatorregistry/validatorregistry.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ func (m *AggregateRegistrationMessage) Marshal() []byte {
3333
b = binary.BigEndian.AppendUint64(b, m.ChainID)
3434
b = append(b, m.ValidatorRegistryAddress.Bytes()...)
3535
b = binary.BigEndian.AppendUint64(b, m.ValidatorIndex)
36-
b = binary.BigEndian.AppendUint32(b, m.Nonce)
3736
b = binary.BigEndian.AppendUint32(b, m.Count)
37+
b = binary.BigEndian.AppendUint32(b, m.Nonce)
3838
if m.IsRegistration {
3939
b = append(b, 1)
4040
} else {
@@ -53,8 +53,8 @@ func (m *AggregateRegistrationMessage) Unmarshal(b []byte) error {
5353
m.ChainID = binary.BigEndian.Uint64(b[1:9])
5454
m.ValidatorRegistryAddress = common.BytesToAddress(b[9:29])
5555
m.ValidatorIndex = binary.BigEndian.Uint64(b[29:37])
56-
m.Nonce = binary.BigEndian.Uint32(b[37:41])
57-
m.Count = binary.BigEndian.Uint32(b[41:45])
56+
m.Count = binary.BigEndian.Uint32(b[37:41])
57+
m.Nonce = binary.BigEndian.Uint32(b[41:45])
5858
switch b[45] {
5959
case 0:
6060
m.IsRegistration = false

testdata/signedRegistrations.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"message":"0x0100000000000027d8a9289a3dd14febe10611119be81e5d35eaac30840000000000000000000001f40000000001","signature":"0x941698fb21c716ac4b9eae341c2eb63cbafb1206e1382986130ca06a3879a5f6a111a582f95233517abef65aac9e4a1912ea81b5b7584635ea3773969c9af747f6cc5d5c1e2aa628b6779fa56c02f6147a151ca46677b53bc03cbba1a9a079ed"}

testdata/validatorInfo.json

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)