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

compose: add vouch to compose #1814

Merged
merged 7 commits into from
Feb 24, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -801,7 +801,10 @@ func createMockValidators(pubkeys []eth2p0.BLSPubKey) beaconmock.ValidatorSet {
Status: eth2v1.ValidatorStateActiveOngoing,
Validator: &eth2p0.Validator{
WithdrawalCredentials: []byte("12345678901234567890123456789012"),
EffectiveBalance: eth2p0.Gwei(31300000000),
PublicKey: pubkey,
ExitEpoch: 18446744073709551615,
WithdrawableEpoch: 18446744073709551615,
},
}
}
Expand Down
90 changes: 75 additions & 15 deletions testutil/beaconmock/headproducer.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,24 @@ import (
"fmt"
"math/rand"
"net/http"
"strconv"
"sync"
"time"

eth2v1 "github.com/attestantio/go-eth2-client/api/v1"
eth2p0 "github.com/attestantio/go-eth2-client/spec/phase0"
"github.com/gorilla/mux"
"github.com/r3labs/sse/v2"
)

const sseStreamID = "head_events"
"github.com/obolnetwork/charon/app/log"
"github.com/obolnetwork/charon/app/z"
)

func newHeadProducer() *headProducer {
server := sse.New()
server.CreateStream(sseStreamID)

return &headProducer{
server: server,
quit: make(chan struct{}),
server: sse.New(),
streamsByTopic: make(map[string][]string),
quit: make(chan struct{}),
}
}

Expand All @@ -49,8 +49,9 @@ type headProducer struct {
quit chan struct{}

// Mutable state
mu sync.Mutex
currentHead *eth2v1.HeadEvent
mu sync.Mutex
currentHead *eth2v1.HeadEvent
streamsByTopic map[string][]string
}

// Start starts the internal slot ticker that updates head.
Expand Down Expand Up @@ -95,20 +96,55 @@ func (p *headProducer) setCurrentHead(currentHead *eth2v1.HeadEvent) {
p.currentHead = currentHead
}

func (p *headProducer) getStreamIDs(topic string) []string {
p.mu.Lock()
defer p.mu.Unlock()

return p.streamsByTopic[topic]
}

func (p *headProducer) setStreamIDs(topic string, streamID string) {
p.mu.Lock()
defer p.mu.Unlock()

p.streamsByTopic[topic] = append(p.streamsByTopic[topic], streamID)
}

// updateHead updates current head based on provided slot.
func (p *headProducer) updateHead(slot eth2p0.Slot) {
currentHead := pseudoRandomHeadEvent(slot)
p.setCurrentHead(currentHead)

data, err := json.Marshal(currentHead)
currentBlock := &eth2v1.BlockEvent{
Slot: slot,
Block: currentHead.Block,
}

headData, err := json.Marshal(currentHead)
if err != nil {
panic(err) // This should never happen and this is test code sorry ;)
}

p.server.Publish(sseStreamID, &sse.Event{
Event: []byte("head"),
Data: data,
})
blockData, err := json.Marshal(currentBlock)
if err != nil {
panic(err) // This should never happen and this is test code sorry ;)
}

// Publish head events.
for _, streamID := range p.getStreamIDs("head") {
dB2510 marked this conversation as resolved.
Show resolved Hide resolved
p.server.Publish(streamID, &sse.Event{
Event: []byte("head"),
Data: headData,
})
}

// Publish block events.
for _, streamID := range p.getStreamIDs("block") {
p.server.Publish(streamID, &sse.Event{
Event: []byte("block"),
Data: blockData,
})
}
}

type getBlockRootResponseJSON struct {
Expand Down Expand Up @@ -173,9 +209,33 @@ func (p *headProducer) handleGetBlockRoot(w http.ResponseWriter, r *http.Request

// handleEvents is a http handler to handle "/eth/v1/events".
func (p *headProducer) handleEvents(w http.ResponseWriter, r *http.Request) {
Copy link
Contributor

Choose a reason for hiding this comment

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

should probably add a test for this headProducer, it is pretty complex...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done!

//nolint:gosec
streamID := strconv.Itoa(rand.Int())
p.server.CreateStream(streamID)

query := r.URL.Query()
query.Set("stream", sseStreamID) // Add sseStreamID for sse server to serve events on.
query.Set("stream", streamID) // Add sseStreamID for sse server to serve events on.
r.URL.RawQuery = query.Encode()

for _, topic := range query["topics"] {
dB2510 marked this conversation as resolved.
Show resolved Hide resolved
if topic != "head" && topic != "block" {
log.Warn(context.Background(), "Unknown topic requested", nil, z.Str("topic", topic))
dB2510 marked this conversation as resolved.
Show resolved Hide resolved
w.WriteHeader(http.StatusInternalServerError)
resp, err := json.Marshal(errorMsgJSON{
Code: 500,
Message: "unknown topic",
})
if err != nil {
panic(err) // This should never happen and this is test code sorry ;)
}
_, _ = w.Write(resp)

return
}

p.setStreamIDs(topic, streamID)
}

p.server.ServeHTTP(w, r)
}

Expand Down
22 changes: 22 additions & 0 deletions testutil/beaconmock/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,14 @@ import (

eth2client "github.com/attestantio/go-eth2-client"
eth2http "github.com/attestantio/go-eth2-client/http"
eth2spec "github.com/attestantio/go-eth2-client/spec"
"github.com/attestantio/go-eth2-client/spec/bellatrix"
"github.com/gorilla/mux"

"github.com/obolnetwork/charon/app/errors"
"github.com/obolnetwork/charon/app/log"
"github.com/obolnetwork/charon/app/z"
"github.com/obolnetwork/charon/testutil"
)

//go:embed static.json
Expand Down Expand Up @@ -100,6 +103,25 @@ func newHTTPServer(addr string, optionalHandlers map[string]http.HandlerFunc, ov
case <-r.Context().Done():
}
},
"/eth/v2/beacon/blocks/{block_id}": func(w http.ResponseWriter, r *http.Request) {
type signedBlockResponseJSON struct {
Version *eth2spec.DataVersion `json:"version"`
Data *bellatrix.SignedBeaconBlock `json:"data"`
}

version := eth2spec.DataVersionBellatrix
resp, err := json.Marshal(signedBlockResponseJSON{
Version: &version,
Data: testutil.RandomBellatrixSignedBeaconBlock(),
})
if err != nil {
panic(err) // This should never happen and this is test code sorry ;)
}

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

for path, handler := range optionalHandlers {
Expand Down
1 change: 1 addition & 0 deletions testutil/compose/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ const (
VCMock VCType = "mock"
VCTeku VCType = "teku"
VCLighthouse VCType = "lighthouse"
VCVouch VCType = "vouch"
)

// KeyGen defines a key generation process.
Expand Down
4 changes: 4 additions & 0 deletions testutil/compose/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ func Run(ctx context.Context, dir string, conf Config) (TmplData, error) {
// getVC returns the validator client template data for the provided type and index.
func getVC(typ VCType, nodeIdx int, numVals int, insecure bool) (TmplVC, error) {
vcByType := map[VCType]TmplVC{
VCVouch: {
Label: string(VCVouch),
Build: "vouch",
},
VCLighthouse: {
Label: string(VCLighthouse),
Build: "lighthouse",
Expand Down
9 changes: 9 additions & 0 deletions testutil/compose/static/vouch/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
FROM wealdtech/ethdo:1.25.3 as ethdo

FROM attestant/vouch:1.6.2

COPY --from=ethdo /app/ethdo /app/ethdo

RUN apt-get update && apt-get install -y curl jq wget

ENTRYPOINT ["/compose/vouch/run.sh"]
50 changes: 50 additions & 0 deletions testutil/compose/static/vouch/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#!/usr/bin/env bash

# Running vouch VC is split into three steps:
# 1. Converting keys into a format which vouch understands. This is what ethdo does.
# 2. Creating configuration for vouch (vouch.yml).
# 3. Actually running the vouch validator client.

BASE_DIR="/tmp/vouch"
KEYS_DIR="/tmp/vouch/keys"
ACCOUNT_PASSPHRASE="secret" # Hardcoded ethdo account passphrase

rm -rf /tmp/vouch || true
mkdir ${BASE_DIR}
mkdir ${KEYS_DIR}

cp /compose/vouch/vouch.yml ${BASE_DIR}

# Create an ethdo wallet within the keys folder.
wallet="validators"
/app/ethdo --base-dir="${KEYS_DIR}" wallet create --wallet ${wallet}

# Import keys into the ethdo wallet.
account=0
for f in /compose/"${NODE}"/validator_keys/keystore-*.json; do
accountName="account-${account}"
echo "Importing key ${f} into ethdo wallet: ${wallet}/${accountName}"

KEYSTORE_PASSPHRASE=$(cat "${f//json/txt}")
/app/ethdo \
--base-dir="${KEYS_DIR}" account import \
--account="${wallet}"/"${accountName}" \
--keystore="$f" \
--passphrase="$ACCOUNT_PASSPHRASE" \
--keystore-passphrase="$KEYSTORE_PASSPHRASE" \
--allow-weak-passphrases

# Increment account.
# shellcheck disable=SC2003
account=$(expr "$account" + 1)
done

# Log wallet info.
echo "Starting vouch validator client. Wallet info:"
/app/ethdo wallet info \
--wallet="${wallet}" \
--base-dir="${KEYS_DIR}" \
--verbose

# Now run vouch.
exec /app/vouch --base-dir=${BASE_DIR} --beacon-node-address="http://${NODE}:3600"
36 changes: 36 additions & 0 deletions testutil/compose/static/vouch/vouch.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# This is a sample config to run vouch VC. It is used by the run.sh script
# to generate a custom config for each vouch VC connected to a charon node.
# Refer: https://github.com/attestantio/vouch/blob/master/docs/configuration.md.

# The wallet account manager obtains account information from local wallets, and signs locally.
# It supports wallets created by ethdo.
accountmanager:
wallet:
locations: /tmp/vouch/keys
accounts: validators
passphrases: secret

# metrics is the module that logs metrics, in this case using prometheus. Note that vouch doesn't emit metrics if
# the following block is not provided.
metrics:
prometheus:
# listen-address is the address on which prometheus listens for metrics requests.
listen-address: 0.0.0.0:8081

# Allow sufficient time (10s) to block while fetching duties for DVT.
strategies:
beaconblockproposal:
timeout: 10s
blindedbeaconblockproposal:
timeout: 10s
attestationdata:
timeout: 10s
aggregateattestation:
timeout: 10s
synccommitteecontribution:
timeout: 10s

# blockrelay provides information about working with local execution clients and remote relays for block proposals.
# fallback-fee-recipient is required field for vouch if no execution configuration is provided.
blockrelay:
fallback-fee-recipient: '0x0000000000000000000000000000000000000001'
7 changes: 7 additions & 0 deletions testutil/random.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,13 @@ func RandomBellatrixBeaconBlock() *bellatrix.BeaconBlock {
}
}

func RandomBellatrixSignedBeaconBlock() *bellatrix.SignedBeaconBlock {
return &bellatrix.SignedBeaconBlock{
Message: RandomBellatrixBeaconBlock(),
Signature: RandomEth2Signature(),
}
}

func RandomBellatrixBeaconBlockBody() *bellatrix.BeaconBlockBody {
return &bellatrix.BeaconBlockBody{
RANDAOReveal: RandomEth2Signature(),
Expand Down