Skip to content

Commit

Permalink
test(mev-boost): delegation revocation
Browse files Browse the repository at this point in the history
  • Loading branch information
namn-grg committed Oct 8, 2024
1 parent a920bdd commit 0b1225f
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 1 deletion.
2 changes: 1 addition & 1 deletion mev-boost/server/constraints.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ type Delegation struct {

// Ref: https://docs.boltprotocol.xyz/api/builder#revoke
type SignedRevocation struct {
Message Delegation `json:"message"`
Message Revocation `json:"message"`
Signature phase0.BLSSignature `json:"signature"`
}

Expand Down
24 changes: 24 additions & 0 deletions mev-boost/server/mock_relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ func (m *mockRelay) getRouter() http.Handler {
r.HandleFunc(pathGetHeader, m.handleGetHeader).Methods(http.MethodGet)
r.HandleFunc(pathGetHeaderWithProofs, m.handleGetHeaderWithProofs).Methods(http.MethodGet)
r.HandleFunc(pathSubmitConstraint, m.handleSubmitConstraint).Methods(http.MethodPost)
r.HandleFunc(pathDelegate, m.handleDelegate).Methods(http.MethodPost)
r.HandleFunc(pathRevoke, m.handleRevoke).Methods(http.MethodPost)
r.HandleFunc(pathGetPayload, m.handleGetPayload).Methods(http.MethodPost)

return m.newTestMiddleware(r)
Expand Down Expand Up @@ -172,6 +174,28 @@ func (m *mockRelay) defaultHandleRegisterValidator(w http.ResponseWriter, req *h
w.WriteHeader(http.StatusOK)
}

func (m *mockRelay) handleDelegate(w http.ResponseWriter, req *http.Request) {
payload := SignedDelegation{}
if err := DecodeJSON(req.Body, &payload); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}

w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
}

func (m *mockRelay) handleRevoke(w http.ResponseWriter, req *http.Request) {
payload := SignedRevocation{}
if err := DecodeJSON(req.Body, &payload); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}

w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
}

func (m *mockRelay) handleSubmitConstraint(w http.ResponseWriter, req *http.Request) {
m.mu.Lock()
defer m.mu.Unlock()
Expand Down
55 changes: 55 additions & 0 deletions mev-boost/server/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,61 @@ func TestRegisterValidator(t *testing.T) {
})
}

func TestDelegate(t *testing.T) {
path := pathDelegate
delegate := SignedDelegation{
Message: Delegation{
ValidatorPubkey: _HexToPubkey("0xa695ad325dfc7e1191fbc9f186f58eff42a634029731b18380ff89bf42c464a42cb8ca55b200f051f57f1e1893c68759"),
DelegateePubkey: _HexToPubkey("0xb8ba260170b9cda2ad54c321d9a8d77e4ca34517106f587eb5ec184bf78f8a0ce4fb55658301b0dc6b129d10adf62391"),
},
Signature: _HexToSignature("0x8790321eacadd5b869838bc01db2338b0bd88a802d768bff8ddbe12aeff67ebc003af8ecc3bafedfef98d2946e869974075006f22367f77c58ca1f1ba20f0d90bf323d243063db16c631ce4ff89bc4f3f239e0879cc4eb492b9906a16fab6f16"),
}
payload := delegate

t.Run("Normal function", func(t *testing.T) {
backend := newTestBackend(t, 1, time.Second)
rr := backend.request(t, http.MethodPost, path, payload)
require.Equal(t, http.StatusOK, rr.Code)
require.Equal(t, 1, backend.relays[0].GetRequestCount(path))
})
}

func TestRevoke(t *testing.T) {
path := pathRevoke
revoke := SignedRevocation{
Message: Revocation{
ValidatorPubkey: _HexToPubkey("0xa695ad325dfc7e1191fbc9f186f58eff42a634029731b18380ff89bf42c464a42cb8ca55b200f051f57f1e1893c68759"),
DelegateePubkey: _HexToPubkey("0xb8ba260170b9cda2ad54c321d9a8d77e4ca34517106f587eb5ec184bf78f8a0ce4fb55658301b0dc6b129d10adf62391"),
},
Signature: _HexToSignature("0x8790321eacadd5b869838bc01db2338b0bd88a802d768bff8ddbe12aeff67ebc003af8ecc3bafedfef98d2946e869974075006f22367f77c58ca1f1ba20f0d90bf323d243063db16c631ce4ff89bc4f3f239e0879cc4eb492b9906a16fab6f16"),
}
payload := revoke

t.Run("Normal function", func(t *testing.T) {
backend := newTestBackend(t, 1, time.Second)
rr := backend.request(t, http.MethodPost, path, payload)
require.Equal(t, http.StatusOK, rr.Code)
require.Equal(t, 1, backend.relays[0].GetRequestCount(path))
})
}

func TestParseSignedDelegation(t *testing.T) {
jsonStr := `{
"message": {
"validator_pubkey": "0xa695ad325dfc7e1191fbc9f186f58eff42a634029731b18380ff89bf42c464a42cb8ca55b200f051f57f1e1893c68759",
"delegatee_pubkey": "0xb8ba260170b9cda2ad54c321d9a8d77e4ca34517106f587eb5ec184bf78f8a0ce4fb55658301b0dc6b129d10adf62391"
},
"signature": "0x8790321eacadd5b869838bc01db2338b0bd88a802d768bff8ddbe12aeff67ebc003af8ecc3bafedfef98d2946e869974075006f22367f77c58ca1f1ba20f0d90bf323d243063db16c631ce4ff89bc4f3f239e0879cc4eb492b9906a16fab6f16"
}`

delegation := SignedDelegation{}
err := json.Unmarshal([]byte(jsonStr), &delegation)
require.NoError(t, err)
require.Equal(t, phase0.BLSPubKey(_HexToBytes("0xa695ad325dfc7e1191fbc9f186f58eff42a634029731b18380ff89bf42c464a42cb8ca55b200f051f57f1e1893c68759")), delegation.Message.ValidatorPubkey)
require.Equal(t, phase0.BLSPubKey(_HexToBytes("0xb8ba260170b9cda2ad54c321d9a8d77e4ca34517106f587eb5ec184bf78f8a0ce4fb55658301b0dc6b129d10adf62391")), delegation.Message.DelegateePubkey)
require.Equal(t, phase0.BLSSignature(_HexToBytes("0x8790321eacadd5b869838bc01db2338b0bd88a802d768bff8ddbe12aeff67ebc003af8ecc3bafedfef98d2946e869974075006f22367f77c58ca1f1ba20f0d90bf323d243063db16c631ce4ff89bc4f3f239e0879cc4eb492b9906a16fab6f16")), delegation.Signature)
}

func TestParseConstraints(t *testing.T) {
jsonStr := `[
{
Expand Down

0 comments on commit 0b1225f

Please sign in to comment.