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

feat(vm): default vm handler #44

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all 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
10 changes: 2 additions & 8 deletions cmd/node/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"github.com/machinefi/w3bstream-mainnet/enums"
"github.com/machinefi/w3bstream-mainnet/vm"
"log"
"log/slog"
"net/http"
Expand All @@ -14,18 +15,11 @@ import (

"github.com/machinefi/w3bstream-mainnet/msg"
"github.com/machinefi/w3bstream-mainnet/msg/handler"
"github.com/machinefi/w3bstream-mainnet/vm"
)

func main() {
vmHandler := vm.NewHandler(
map[vm.Type]string{
vm.Risc0: viper.GetString(enums.EnvKeyRisc0ServerEndpoint),
vm.Halo2: viper.GetString(enums.EnvKeyHalo2ServerEndpoint),
},
)
msgHandler := handler.New(
vmHandler,
vm.DefaultHandler,
viper.GetString(enums.EnvKeyChainEndpoint),
viper.GetString(enums.EnvKeyOperatorPrivateKey),
viper.GetString(enums.EnvKeyProjectConfigPath),
Expand Down
7 changes: 7 additions & 0 deletions vm/type.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
package vm

import "github.com/machinefi/w3bstream-mainnet/enums"

type Type string

const (
Risc0 Type = "risc0"
Halo2 Type = "halo2"
)

var vmEndpointConfigEnvKeyMap = map[string]Type{
enums.EnvKeyRisc0ServerEndpoint: Risc0,
enums.EnvKeyHalo2ServerEndpoint: Halo2,
}
14 changes: 14 additions & 0 deletions vm/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package vm

import (
"context"
"github.com/spf13/viper"
"log/slog"

"github.com/machinefi/w3bstream-mainnet/msg"
Expand Down Expand Up @@ -40,3 +41,16 @@ func NewHandler(endpoints map[Type]string) *Handler {
instanceMgr: server.NewMgr(),
}
}

var DefaultHandler *Handler

func init() {
var endpoints = make(map[Type]string)
for key, typ := range vmEndpointConfigEnvKeyMap {
if ep := viper.GetString(key); ep != "" {
endpoints[typ] = ep
}
}

DefaultHandler = NewHandler(endpoints)
}
Comment on lines +45 to +56
Copy link
Collaborator

Choose a reason for hiding this comment

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

why we need it be initialized in init?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I tried to initialize a default vm handler, if the vm endpoint can be read from env. And then the vm.DefaultHandler will be used to initialize the msg.DefaultHandler.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

if some module don't need the default handler, they can create a hander by NewHander

Copy link
Member

Choose a reason for hiding this comment

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

i think default if unnecessary, just NewHandler is more clear