Skip to content

Commit

Permalink
Prefetch the node identity
Browse files Browse the repository at this point in the history
Prefetch the node identity and use it for uptime updates
  • Loading branch information
muhamadazmy committed Oct 17, 2022
1 parent 110ecd2 commit b552f4f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 27 deletions.
22 changes: 11 additions & 11 deletions cmds/modules/noded/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,16 @@ func action(cli *cli.Context) error {
}
}()

log.Info().Uint32("twin", twin).Msg("node has been registered")
log.Debug().Msg("start message bus")
identityd := stubs.NewIdentityManagerStub(redis)
sk := ed25519.PrivateKey(identityd.PrivateKey(ctx))
id, err := substrate.NewIdentityFromEd25519Key(sk)
log.Info().Str("address", id.Address()).Msg("node address")
if err != nil {
return err
}

// uptime update
go func() {
defer log.Info().Msg("uptime reporting exited permanently")
Expand All @@ -253,7 +263,7 @@ func action(cli *cli.Context) error {
}
}()

err = uptime(ctx, redis)
err = uptime(ctx, id)
return err
}

Expand All @@ -270,16 +280,6 @@ func action(cli *cli.Context) error {
}
}()

log.Info().Uint32("twin", twin).Msg("node has been registered")
log.Debug().Msg("start message bus")
identityd := stubs.NewIdentityManagerStub(redis)
sk := ed25519.PrivateKey(identityd.PrivateKey(ctx))
id, err := substrate.NewIdentityFromEd25519Key(sk)
log.Info().Str("address", id.Address()).Msg("node address")
if err != nil {
return err
}

return runMsgBus(ctx, sub, id)
}

Expand Down
17 changes: 1 addition & 16 deletions cmds/modules/noded/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,26 @@ package noded

import (
"context"
"crypto/ed25519"
"time"

"github.com/centrifuge/go-substrate-rpc-client/v4/types"
"github.com/pkg/errors"
"github.com/rs/zerolog/log"
"github.com/shirou/gopsutil/host"
"github.com/threefoldtech/substrate-client"
"github.com/threefoldtech/zbus"
"github.com/threefoldtech/zos/pkg/environment"
"github.com/threefoldtech/zos/pkg/stubs"
)

const (
reportUptimeEvery = 40 * time.Minute
)

func uptime(ctx context.Context, cl zbus.Client) error {
var (
mgr = stubs.NewIdentityManagerStub(cl)
)

func uptime(ctx context.Context, id substrate.Identity) error {
subMgr, err := environment.GetSubstrate()
if err != nil {
return err
}

busCtx, cancel := context.WithTimeout(ctx, 10*time.Second)
defer cancel()
sk := ed25519.PrivateKey(mgr.PrivateKey(busCtx))
id, err := substrate.NewIdentityFromEd25519Key(sk)
if err != nil {
return err
}

update := func(uptime uint64) (types.Hash, error) {
sub, err := subMgr.Substrate()
if err != nil {
Expand Down

0 comments on commit b552f4f

Please sign in to comment.