Skip to content

Commit

Permalink
Add flag to watch for proposer builds (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
ferranbt authored Jul 31, 2024
1 parent e84d83c commit eb08709
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,6 @@ Options:
- `--continue` (bool): Whether to restart the chain from a previous run if the output folder is not empty. It defaults to `false`.
- `--use-bin-path` (bool): Whether to use the binaries from the local path instead of downloading them. It defaults to `false`.
- `--genesis-delay` (int): The delay in seconds before the genesis block is created. It is used to account for the delay between the creation of the artifacts and the running of the services. It defaults to `5` seconds.
- `--watch-payloads` (bool): If enabled, it logs whenever a builder builds a valid block through the relay. It defaults to `false`.

Unless the `--continue` flag is set, the playground will delete the output directory and start a new chain from scratch on every run.
53 changes: 53 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import (
"encoding/json"
"fmt"
"html/template"
"io"
"math/big"
"net/http"
"os"
"os/exec"
"os/signal"
Expand Down Expand Up @@ -49,6 +51,7 @@ var continueFlag bool
var useBinPathFlag bool
var validateFlag bool
var genesisDelayFlag uint64
var watchPayloadsFlag bool

var rootCmd = &cobra.Command{
Use: "playground",
Expand Down Expand Up @@ -153,6 +156,7 @@ func main() {
rootCmd.Flags().BoolVar(&continueFlag, "continue", false, "")
rootCmd.Flags().BoolVar(&useBinPathFlag, "use-bin-path", false, "")
rootCmd.Flags().Uint64Var(&genesisDelayFlag, "genesis-delay", 5, "")
rootCmd.Flags().BoolVar(&watchPayloadsFlag, "watch-payloads", false, "")

downloadArtifactsCmd.Flags().BoolVar(&validateFlag, "validate", false, "")
validateCmd.Flags().Uint64Var(&numBlocksValidate, "num-blocks", 5, "")
Expand Down Expand Up @@ -207,6 +211,10 @@ func runIt() error {
return err
}

if watchPayloadsFlag {
go watchProposerPayloads()
}

sig := make(chan os.Signal, 1)
signal.Notify(sig, os.Interrupt)

Expand Down Expand Up @@ -781,3 +789,48 @@ func getHomeDir() (string, error) {

return customHomeDir, nil
}

func watchProposerPayloads() {
// This is not the most efficient solution since we are querying the endpoint for the full list of payloads
// every 2 seconds. It should be fine for the kind of workloads expected to run.

lastSlot := uint64(0)

for {
time.Sleep(2 * time.Second)

vals, err := getProposerPayloadDelivered()
if err != nil {
fmt.Println("Error getting proposer payloads:", err)
continue
}

for _, val := range vals {
if val.Slot <= lastSlot {
continue
}

fmt.Printf("Block Proposed: Slot: %d, Builder: %s, Block: %d\n", val.Slot, val.BuilderPubkey, val.BlockNumber)
lastSlot = val.Slot
}
}
}

func getProposerPayloadDelivered() ([]*mevRCommon.BidTraceV2JSON, error) {
resp, err := http.Get("http://localhost:5555/relay/v1/data/bidtraces/proposer_payload_delivered")
if err != nil {
return nil, err
}
defer resp.Body.Close()

data, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}

var payloadDeliveredList []*mevRCommon.BidTraceV2JSON
if err := json.Unmarshal(data, &payloadDeliveredList); err != nil {
return nil, err
}
return payloadDeliveredList, nil
}

0 comments on commit eb08709

Please sign in to comment.