Skip to content

Commit

Permalink
rename all substrate gateways to registrar gateways
Browse files Browse the repository at this point in the history
  • Loading branch information
Eslam-Nawara committed Jan 30, 2025
1 parent 9befdc6 commit c56227c
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 36 deletions.
4 changes: 2 additions & 2 deletions cmds/modules/provisiond/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ type ContractEventHandler struct {
eventsConsumer *events.RedisConsumer
}

func NewContractEventHandler(node uint64, substrateGateway *stubs.RegistrarGatewayStub, engine provision.Engine, events *events.RedisConsumer) ContractEventHandler {
return ContractEventHandler{node: node, registrarGateway: substrateGateway, engine: engine, eventsConsumer: events}
func NewContractEventHandler(node uint64, registrarGateway *stubs.RegistrarGatewayStub, engine provision.Engine, events *events.RedisConsumer) ContractEventHandler {
return ContractEventHandler{node: node, registrarGateway: registrarGateway, engine: engine, eventsConsumer: events}
}

func (r *ContractEventHandler) current() (map[uint64]gridtypes.Deployment, error) {
Expand Down
8 changes: 4 additions & 4 deletions cmds/modules/provisiond/reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ type Reporter struct {

identity substrate.Identity
queue *dque.DQue
substrateGateway *zos4stubs.RegistrarGatewayStub
registrarGateway *zos4stubs.RegistrarGatewayStub
}

func reportBuilder() interface{} {
Expand Down Expand Up @@ -80,7 +80,7 @@ func NewReporter(metricsPath string, cl zbus.Client, root string) (*Reporter, er
return nil, errors.Wrap(err, "failed to setup report persisted queue")
}

substrateGateway := zos4stubs.NewRegistrarGatewayStub(cl)
registrarGateway := zos4stubs.NewRegistrarGatewayStub(cl)

rrd, err := rrd.NewRRDBolt(metricsPath, 5*time.Minute, 24*time.Hour)
if err != nil {
Expand All @@ -92,7 +92,7 @@ func NewReporter(metricsPath string, cl zbus.Client, root string) (*Reporter, er
rrd: rrd,
identity: id,
queue: queue,
substrateGateway: substrateGateway,
registrarGateway: registrarGateway,
}, nil
}

Expand All @@ -106,7 +106,7 @@ func (r *Reporter) pushOne() error {

log.Info().Int("len", len(report.Consumption)).Msgf("sending capacity report")

hash, err := r.substrateGateway.Report(context.Background(), report.Consumption)
hash, err := r.registrarGateway.Report(context.Background(), report.Consumption)
if err != nil {
return errors.Wrap(err, "failed to publish consumption report")
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/provision/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ func WithStartupOrder(t ...gridtypes.WorkloadType) EngineOption {
// WithAPIGateway sets the API Gateway. If set it will
// be used by the engine to fetch (and validate) the deployment contract
// then contract with be available on the deployment context
func WithAPIGateway(node uint64, substrateGateway *stubs.RegistrarGatewayStub) EngineOption {
return &withAPIGateway{node, substrateGateway}
func WithAPIGateway(node uint64, registrarGateway *stubs.RegistrarGatewayStub) EngineOption {
return &withAPIGateway{node, registrarGateway}
}

// WithRerunAll if set forces the engine to re-run all reservations
Expand Down Expand Up @@ -153,12 +153,12 @@ func (o *withAdminsKeyGetter) apply(e *NativeEngine) {

type withAPIGateway struct {
nodeID uint64
substrateGateway *stubs.RegistrarGatewayStub
registrarGateway *stubs.RegistrarGatewayStub
}

func (o *withAPIGateway) apply(e *NativeEngine) {
e.nodeID = o.nodeID
e.registrarGateway = o.substrateGateway
e.registrarGateway = o.registrarGateway
}

type withStartupOrder struct {
Expand Down
54 changes: 28 additions & 26 deletions pkg/registrar_gateway/registrar_gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,29 +77,29 @@ func (r *registrarGateway) CreateNode(node types.NodeRegistrationRequest) (uint6
return id, err
}

func (g *registrarGateway) CreateTwin(relay string, pk []byte) (uint64, error) {
url := fmt.Sprintf("%s/v1/accounts", g.baseURL)
func (r *registrarGateway) CreateTwin(relay string, pk []byte) (uint64, error) {
url := fmt.Sprintf("%s/v1/accounts", r.baseURL)
log.Debug().Str("url", url).Str("relay", relay).Str("pk", hex.EncodeToString(pk)).Msg("creating account")

g.mu.Lock()
defer g.mu.Unlock()
r.mu.Lock()
defer r.mu.Unlock()

id, err := g.createTwin(url, []string{relay}, pk)
g.twinID = id
id, err := r.createTwin(url, []string{relay}, pk)
r.twinID = id
return id, err
}

func (g *registrarGateway) EnsureAccount(twinID uint64, pk []byte) (twin types.Account, err error) {
url := fmt.Sprintf("%s/v1/accounts", g.baseURL)
func (r *registrarGateway) EnsureAccount(twinID uint64, pk []byte) (twin types.Account, err error) {
url := fmt.Sprintf("%s/v1/accounts", r.baseURL)
log.Debug().Str("url", url).Msg("ensure account")

g.mu.Lock()
defer g.mu.Unlock()
r.mu.Lock()
defer r.mu.Unlock()

return g.ensureAccount(twinID, url, pk)
return r.ensureAccount(twinID, url, pk)
}

func (g *registrarGateway) GetContract(id uint64) (result substrate.Contract, serr pkg.SubstrateError) {
func (r *registrarGateway) GetContract(id uint64) (result substrate.Contract, serr pkg.SubstrateError) {
// log.Trace().Str("method", "GetContract").Uint64("id", id).Msg("method called")
// contract, err := g.sub.GetContract(id)
//
Expand All @@ -110,7 +110,7 @@ func (g *registrarGateway) GetContract(id uint64) (result substrate.Contract, se
return
}

func (g *registrarGateway) GetContractIDByNameRegistration(name string) (result uint64, serr pkg.SubstrateError) {
func (r *registrarGateway) GetContractIDByNameRegistration(name string) (result uint64, serr pkg.SubstrateError) {
// log.Trace().Str("method", "GetContractIDByNameRegistration").Str("name", name).Msg("method called")
// contractID, err := g.sub.GetContractIDByNameRegistration(name)
//
Expand Down Expand Up @@ -139,13 +139,13 @@ func (r *registrarGateway) GetNodeByTwinID(twin uint64) (result uint64, err erro
return r.getNodeByTwinID(url, twin)
}

func (g *registrarGateway) GetNodeContracts(node uint32) ([]subTypes.U64, error) {
func (r *registrarGateway) GetNodeContracts(node uint32) ([]subTypes.U64, error) {
// log.Trace().Str("method", "GetNodeContracts").Uint32("node", node).Msg("method called")
// return g.sub.GetNodeContracts(node)
return []subTypes.U64{}, nil
}

func (g *registrarGateway) GetNodeRentContract(node uint32) (result uint64, serr pkg.SubstrateError) {
func (r *registrarGateway) GetNodeRentContract(node uint32) (result uint64, serr pkg.SubstrateError) {
// log.Trace().Str("method", "GetNodeRentContract").Uint32("node", node).Msg("method called")
// contractID, err := g.sub.GetNodeRentContract(node)
//
Expand All @@ -160,7 +160,7 @@ func (r *registrarGateway) GetNodes(farmID uint32) (nodeIDs []uint32, err error)
return r.getNodesInFarm(url, farmID)
}

func (g *registrarGateway) GetPowerTarget() (power substrate.NodePower, err error) {
func (r *registrarGateway) GetPowerTarget() (power substrate.NodePower, err error) {
// log.Trace().Str("method", "GetPowerTarget").Uint32("node id", uint32(g.nodeID)).Msg("method called")
// return g.sub.GetPowerTarget(uint32(g.nodeID))
return
Expand Down Expand Up @@ -203,7 +203,7 @@ func (r *registrarGateway) Report(consumptions []substrate.NruConsumption) (subT
return subTypes.Hash{}, nil
}

func (g *registrarGateway) SetContractConsumption(resources ...substrate.ContractResources) error {
func (r *registrarGateway) SetContractConsumption(resources ...substrate.ContractResources) error {
// contractIDs := make([]uint64, 0, len(resources))
// for _, v := range resources {
// contractIDs = append(contractIDs, uint64(v.ContractID))
Expand All @@ -215,7 +215,7 @@ func (g *registrarGateway) SetContractConsumption(resources ...substrate.Contrac
return nil
}

func (g *registrarGateway) SetNodePowerState(up bool) (hash subTypes.Hash, err error) {
func (r *registrarGateway) SetNodePowerState(up bool) (hash subTypes.Hash, err error) {
// log.Debug().Str("method", "SetNodePowerState").Bool("up", up).Msg("method called")
// g.mu.Lock()
// defer g.mu.Unlock()
Expand Down Expand Up @@ -245,7 +245,7 @@ func (r *registrarGateway) UpdateNodeUptimeV2(uptime uint64, timestampHint uint6
return r.updateNodeUptimeV2(r.twinID, url, uptime)
}

func (g *registrarGateway) GetTime() (time.Time, error) {
func (r *registrarGateway) GetTime() (time.Time, error) {
// log.Trace().Str("method", "Time").Msg("method called")
//
// return g.sub.Time()
Expand Down Expand Up @@ -305,9 +305,9 @@ func createChallenge(timestamp int64, publicKey string) string {
return hex.EncodeToString(hash[:])
}

func (g *registrarGateway) createTwin(url string, relayURL []string, pk []byte) (uint64, error) {
func (r *registrarGateway) createTwin(url string, relayURL []string, pk []byte) (uint64, error) {
publicKeyBase64 := base64.StdEncoding.EncodeToString(pk)
signature := getNodeSignature(pk, g.privKey)
signature := getNodeSignature(pk, r.privKey)

account := types.AccountCreationRequest{
PublicKey: publicKeyBase64,
Expand All @@ -325,7 +325,7 @@ func (g *registrarGateway) createTwin(url string, relayURL []string, pk []byte)
return 0, err
}

resp, err := g.httpClient.Post(url, "application/json", &body)
resp, err := r.httpClient.Post(url, "application/json", &body)
if err != nil {
return 0, err
}
Expand All @@ -341,20 +341,22 @@ func (g *registrarGateway) createTwin(url string, relayURL []string, pk []byte)
return twinID, err
}

func (g *registrarGateway) ensureAccount(twinID uint64, relay string, pk []byte) (types.Account, error) {
twin, err := g.GetTwin(twinID)
func (r *registrarGateway) ensureAccount(twinID uint64, relay string, pk []byte) (twin types.Account, err error) {
url := fmt.Sprintf("%s/v1/accounts/", r.baseURL)

twin, err = r.getTwin(url, twinID)
if err != nil {
if !errors.Is(err, ErrorRecordNotFound) {
return types.Account{}, err
}

// account not found, create the account
twinID, err = g.CreateTwin(relay, pk)
twinID, err = r.createTwin(url, []string{relay}, pk)
if err != nil {
return types.Account{}, err
}

twin, err = g.GetTwin(twinID)
twin, err = r.getTwin(url, twinID)
}

return twin, err
Expand Down

0 comments on commit c56227c

Please sign in to comment.