From be2ac4d39665513a208edb23ede00918fa1a36db Mon Sep 17 00:00:00 2001 From: Ferran Borreguero Date: Wed, 31 Jul 2024 11:57:44 +0100 Subject: [PATCH] Add flag to watch for proposer builds --- README.md | 1 + main.go | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/README.md b/README.md index 01a56ff..5f67488 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/main.go b/main.go index 6940436..04a4861 100644 --- a/main.go +++ b/main.go @@ -8,7 +8,9 @@ import ( "encoding/json" "fmt" "html/template" + "io" "math/big" + "net/http" "os" "os/exec" "os/signal" @@ -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", @@ -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, "") @@ -207,6 +211,10 @@ func runIt() error { return err } + if watchPayloadsFlag { + go watchProposerPayloads() + } + sig := make(chan os.Signal, 1) signal.Notify(sig, os.Interrupt) @@ -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 +}