From e6e39c8cc86540a34ae4a63e9798ec544681c034 Mon Sep 17 00:00:00 2001 From: huangzhiran Date: Sun, 18 Feb 2024 17:14:48 +0800 Subject: [PATCH 1/3] feat(clients): move mock clients from config file to go source file --- clients/manager.go | 97 ++++++++++++++++++++++++++++++++++++--------- cmd/enode/env.go | 1 - cmd/enode/init.go | 1 - cmd/enode/main.go | 2 +- docker-compose.yaml | 3 -- enode.Dockerfile | 1 - 6 files changed, 80 insertions(+), 25 deletions(-) diff --git a/clients/manager.go b/clients/manager.go index 5a06b94b..a8f388a9 100644 --- a/clients/manager.go +++ b/clients/manager.go @@ -1,12 +1,7 @@ package clients import ( - "encoding/json" - "log/slog" - "os" "sync" - - "github.com/pkg/errors" ) type Client struct { @@ -18,7 +13,7 @@ type Client struct { var manager *Manager -func NewManager(confPath string) *Manager { +func NewManager() *Manager { if manager != nil { return manager } @@ -26,9 +21,7 @@ func NewManager(confPath string) *Manager { mux: sync.Mutex{}, pool: make(map[string]*Client), } - if err := m.syncFromLocal(confPath); err != nil { - slog.Error("failed to sync clients from local", "msg", err) - } + m.fillByMockClients() manager = m return manager } @@ -54,14 +47,83 @@ func (mgr *Manager) AddClient(c *Client) { // TODO syncFromContract func (mgr *Manager) syncFromContract() {} -func (mgr *Manager) syncFromLocal(path string) error { - content, err := os.ReadFile(path) - if err != nil { - panic(errors.Wrap(err, "failed to read local config")) - } - clients := make([]*Client, 0) - if err = json.Unmarshal(content, &clients); err != nil { - return errors.Wrap(err, "failed to parse local config") +func (mgr *Manager) fillByMockClients() { + clients := []*Client{ + { + ClientDID: "did:ethr:0x9d9250fb4e08ba7a858fe7196a6ba946c6083ff0", + Projects: []uint64{ + 1, + 2, + 3, + 4, + 5, + 6, + 7, + 8, + 9, + 10, + 11, + 12, + 13, + 14, + 15, + 16, + 17, + 18, + 19, + 20, + }, + }, + { + ClientDID: "did:key:z6MkeeChrUs1EoKkNNzoy9FwJJb9gNQ92UT8kcXZHMbwj67B", + Projects: []uint64{ + 1, + 2, + 3, + 4, + 5, + 6, + 7, + 8, + 9, + 10, + 11, + 12, + 13, + 14, + 15, + 16, + 17, + 18, + 19, + 20, + }, + }, + { + ClientDID: "did:example:d23dd687a7dc6787646f2eb98d0", + Projects: []uint64{ + 1, + 2, + 3, + 4, + 5, + 6, + 7, + 8, + 9, + 10, + 11, + 12, + 13, + 14, + 15, + 16, + 17, + 18, + 19, + 20, + }, + }, } for _, c := range clients { c.projects = make(map[uint64]struct{}) @@ -70,5 +132,4 @@ func (mgr *Manager) syncFromLocal(path string) error { } mgr.pool[c.ClientDID] = c } - return nil } diff --git a/cmd/enode/env.go b/cmd/enode/env.go index 3978cfb9..e03d621e 100644 --- a/cmd/enode/env.go +++ b/cmd/enode/env.go @@ -11,5 +11,4 @@ const ( DIDAuthServerEndpoint = "DIDAUTH_SERVER_ENDPOINT" OperatorPrivateKey = "OPERATOR_PRIVATE_KEY" OperatorPrivateKeyED25519 = "OPERATOR_PRIVATE_KEY_ED25519" - ClientsFilePath = "CLIENTS_FILE_PATH" ) diff --git a/cmd/enode/init.go b/cmd/enode/init.go index bfabd6d2..b3638d85 100644 --- a/cmd/enode/init.go +++ b/cmd/enode/init.go @@ -31,7 +31,6 @@ func initConfig() { viper.MustBindEnv(ProjectContractAddress) viper.MustBindEnv(IPFSEndpoint) viper.MustBindEnv(DIDAuthServerEndpoint) - viper.MustBindEnv(ClientsFilePath) viper.BindEnv(OperatorPrivateKey) viper.BindEnv(OperatorPrivateKeyED25519) diff --git a/cmd/enode/main.go b/cmd/enode/main.go index 6ff3f0d4..a5b4463d 100644 --- a/cmd/enode/main.go +++ b/cmd/enode/main.go @@ -24,7 +24,7 @@ func main() { log.Fatal(err) } - _ = clients.NewManager(viper.GetString(ClientsFilePath)) + _ = clients.NewManager() projectManager, err := project.NewManager(viper.GetString(ChainEndpoint), viper.GetString(ProjectContractAddress), viper.GetString(IPFSEndpoint)) if err != nil { diff --git a/docker-compose.yaml b/docker-compose.yaml index 8b2380ed..669284bb 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -13,9 +13,6 @@ services: environment: OPERATOR_PRIVATE_KEY: ${PRIVATE_KEY:-} OPERATOR_PRIVATE_KEY_ED25519: ${PRIVATE_KEY_ED25519:-} - CLIENTS_FILE_PATH: /data/test/clients/clients - volumes: - - .:/data znode: image: ghcr.io/machinefi/znode:latest diff --git a/enode.Dockerfile b/enode.Dockerfile index 9932e723..8119e24a 100644 --- a/enode.Dockerfile +++ b/enode.Dockerfile @@ -11,7 +11,6 @@ RUN cd ./cmd/enode && go build -o enode FROM golang:1.21 AS runtime COPY --from=builder /go/src/cmd/enode/enode /go/bin/enode -COPY --from=builder /go/src/test/clients/clients /go/bin/test/clients/clients EXPOSE 9000 WORKDIR /go/bin From a949a3f47421cb9bb07e38b9bd448f220202731e Mon Sep 17 00:00:00 2001 From: huangzhiran Date: Sun, 18 Feb 2024 17:18:57 +0800 Subject: [PATCH 2/3] del clients file --- test/clients/clients | 77 -------------------------------------------- 1 file changed, 77 deletions(-) delete mode 100644 test/clients/clients diff --git a/test/clients/clients b/test/clients/clients deleted file mode 100644 index 450360ae..00000000 --- a/test/clients/clients +++ /dev/null @@ -1,77 +0,0 @@ -[ - { - "clientDID": "did:ethr:0x9d9250fb4e08ba7a858fe7196a6ba946c6083ff0", - "projects": [ - 1, - 2, - 3, - 4, - 5, - 6, - 7, - 8, - 9, - 10, - 11, - 12, - 13, - 14, - 15, - 16, - 17, - 18, - 19, - 20 - ] - }, - { - "clientDID": "did:key:z6MkeeChrUs1EoKkNNzoy9FwJJb9gNQ92UT8kcXZHMbwj67B", - "projects": [ - 1, - 2, - 3, - 4, - 5, - 6, - 7, - 8, - 9, - 10, - 11, - 12, - 13, - 14, - 15, - 16, - 17, - 18, - 19, - 20 - ] - }, - { - "clientDID": "did:example:d23dd687a7dc6787646f2eb98d0", - "projects": [ - 1, - 2, - 3, - 4, - 5, - 6, - 7, - 8, - 9, - 10, - 11, - 12, - 13, - 14, - 15, - 16, - 17, - 18, - 19, - 20 - ] - } -] \ No newline at end of file From 35083a8ba70f8eea4260292cee1e09c9dedfbf23 Mon Sep 17 00:00:00 2001 From: sincos Date: Sun, 18 Feb 2024 18:08:08 +0800 Subject: [PATCH 3/3] chore(clients): embed mock clients config --- clients/clients | 77 +++++++++++++++++++++++++++++++++++++++++ clients/manager.go | 86 ++++++---------------------------------------- 2 files changed, 87 insertions(+), 76 deletions(-) create mode 100644 clients/clients diff --git a/clients/clients b/clients/clients new file mode 100644 index 00000000..450360ae --- /dev/null +++ b/clients/clients @@ -0,0 +1,77 @@ +[ + { + "clientDID": "did:ethr:0x9d9250fb4e08ba7a858fe7196a6ba946c6083ff0", + "projects": [ + 1, + 2, + 3, + 4, + 5, + 6, + 7, + 8, + 9, + 10, + 11, + 12, + 13, + 14, + 15, + 16, + 17, + 18, + 19, + 20 + ] + }, + { + "clientDID": "did:key:z6MkeeChrUs1EoKkNNzoy9FwJJb9gNQ92UT8kcXZHMbwj67B", + "projects": [ + 1, + 2, + 3, + 4, + 5, + 6, + 7, + 8, + 9, + 10, + 11, + 12, + 13, + 14, + 15, + 16, + 17, + 18, + 19, + 20 + ] + }, + { + "clientDID": "did:example:d23dd687a7dc6787646f2eb98d0", + "projects": [ + 1, + 2, + 3, + 4, + 5, + 6, + 7, + 8, + 9, + 10, + 11, + 12, + 13, + 14, + 15, + 16, + 17, + 18, + 19, + 20 + ] + } +] \ No newline at end of file diff --git a/clients/manager.go b/clients/manager.go index a8f388a9..84d0cb41 100644 --- a/clients/manager.go +++ b/clients/manager.go @@ -1,9 +1,16 @@ package clients import ( + _ "embed" // embed mock clients configuration + "encoding/json" "sync" ) +var ( + //go:embed clients + mockClientsConfig []byte +) + type Client struct { ClientDID string `json:"clientDID"` Projects []uint64 `json:"projects"` @@ -48,82 +55,9 @@ func (mgr *Manager) AddClient(c *Client) { func (mgr *Manager) syncFromContract() {} func (mgr *Manager) fillByMockClients() { - clients := []*Client{ - { - ClientDID: "did:ethr:0x9d9250fb4e08ba7a858fe7196a6ba946c6083ff0", - Projects: []uint64{ - 1, - 2, - 3, - 4, - 5, - 6, - 7, - 8, - 9, - 10, - 11, - 12, - 13, - 14, - 15, - 16, - 17, - 18, - 19, - 20, - }, - }, - { - ClientDID: "did:key:z6MkeeChrUs1EoKkNNzoy9FwJJb9gNQ92UT8kcXZHMbwj67B", - Projects: []uint64{ - 1, - 2, - 3, - 4, - 5, - 6, - 7, - 8, - 9, - 10, - 11, - 12, - 13, - 14, - 15, - 16, - 17, - 18, - 19, - 20, - }, - }, - { - ClientDID: "did:example:d23dd687a7dc6787646f2eb98d0", - Projects: []uint64{ - 1, - 2, - 3, - 4, - 5, - 6, - 7, - 8, - 9, - 10, - 11, - 12, - 13, - 14, - 15, - 16, - 17, - 18, - 19, - 20, - }, - }, + clients := make([]*Client, 0) + if err := json.Unmarshal(mockClientsConfig, &clients); err != nil { + panic(err) } for _, c := range clients { c.projects = make(map[uint64]struct{})