Skip to content

Commit

Permalink
support contracts in node registrar
Browse files Browse the repository at this point in the history
  • Loading branch information
Eslam-Nawara committed Feb 9, 2025
1 parent 54484cd commit bf52c85
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 97 deletions.
108 changes: 12 additions & 96 deletions node-registrar/pkg/db/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ type Account struct {
PublicKey string `gorm:"type:text;not null;unique" json:"public_key"`
// Relations | likely we need to use OnDelete:RESTRICT (Prevent Twin deletion if farms exist)
// @swagger:ignore
Farms []Farm `gorm:"foreignKey:TwinID;references:TwinID;constraint:OnDelete:RESTRICT"`
Farms []Farm `gorm:"foreignKey:TwinID;references:TwinID;constraint:OnDelete:RESTRICT"`
Contracts []Contract `json:"contracts" gorm:"foreignKey:ContractID;references:ContractID;constraint:OnDelete:CASCADE"`
}

type Farm struct {
Expand Down Expand Up @@ -47,12 +48,22 @@ type Node struct {
SerialNumber string

UptimeReports []UptimeReport `json:"uptime" gorm:"foreignKey:NodeID;references:NodeID;constraint:OnDelete:CASCADE"`
Contracts []Contract `json:"contracts" gorm:"foreignKey:ContractID;references:ContractID;constraint:OnDelete:CASCADE"`

CreatedAt time.Time
UpdatedAt time.Time
Approved bool
}

type Contract struct {
ContractID uint64 `json:"contract_id" gorm:"primaryKey;autoIncrement"`
// gorm:"size:40;not null;unique;check:farm_name <> ''" json:"farm_name"
State string `json:"state" gorm:"size:40;not null;check:state<> ''"`
TwinID uint64 `json:"twin_id"`
ContractType string `json:"contract_type"`
// SolutionProviderID types.OptionU64 `json:"solution_provider_id"`
}

type UptimeReport struct {
ID uint64 `gorm:"primaryKey;autoIncrement"`
NodeID uint64 `gorm:"index" json:"node_id"`
Expand All @@ -73,115 +84,20 @@ type Interface struct {
IPs string `json:"ips"`
}

/* // Value implements the Valuer interface for storing Interface in the database
func (i Interface) Value() (driver.Value, error) {
bytes, err := json.Marshal(i)
if err != nil {
return nil, fmt.Errorf("failed to marshal Interface: %w", err)
}
return string(bytes), nil
}
// Scan implements the Scanner interface for retrieving Interface from the database
func (i *Interface) Scan(value any) error {
bytes, ok := value.([]byte)
if !ok {
return fmt.Errorf("invalid data type for Interface: %T", value)
}
if err := json.Unmarshal(bytes, i); err != nil {
return fmt.Errorf("failed to unmarshal Interface: %w", err)
}
return nil
} */

type Resources struct {
HRU uint64 `json:"hru"`
SRU uint64 `json:"sru"`
CRU uint64 `json:"cru"`
MRU uint64 `json:"mru"`
}

/* // Value implements the Valuer interface for storing Resources in the database
func (r Resources) Value() (driver.Value, error) {
bytes, err := json.Marshal(r)
if err != nil {
return nil, fmt.Errorf("failed to marshal resources: %w", err)
}
return string(bytes), nil
}
// Scan implements the Scanner interface for retrieving Resources from the database
func (r *Resources) Scan(value any) error {
bytes, ok := value.([]byte)
if !ok {
return fmt.Errorf("invalid data type for resources: %T", value)
}
if err := json.Unmarshal(bytes, r); err != nil {
return fmt.Errorf("failed to unmarshal resources: %w", err)
}
return nil
} */

type Location struct {
Country string `json:"country" gorm:"not null"`
City string `json:"city" gorm:"not null"`
Longitude string `json:"longitude" gorm:"not null"`
Latitude string `json:"latitude" gorm:"not null"`
}

/* // Value implements the Valuer interface for storing Location in the database
func (l Location) Value() (driver.Value, error) {
bytes, err := json.Marshal(l)
if err != nil {
return nil, fmt.Errorf("failed to marshal Location: %w", err)
}
return string(bytes), nil
}
// Scan implements the Scanner interface for retrieving Location from the database
func (l *Location) Scan(value any) error {
bytes, ok := value.([]byte)
if !ok {
return fmt.Errorf("invalid data type for Location: %T", value)
}
if err := json.Unmarshal(bytes, l); err != nil {
return fmt.Errorf("failed to unmarshal Location: %w", err)
}
return nil
} */

// type PublicConfig struct {
// PublicIPV4 string `json:"public_ip_v4"`
// PublicIPV6 string `json:"public_ip_v6"`
// Domain string `json:"domain"`
// }
//
// // Value implements the Valuer interface for storing PublicConfig in the database
//
// func (c PublicConfig) Value() (driver.Value, error) {
// bytes, err := json.Marshal(c)
// if err != nil {
// return nil, fmt.Errorf("failed to marshal PublicConfig: %w", err)
// }
// return string(bytes), nil
// }
//
// // Scan implements the Scanner interface for retrieving PublicConfig from the database
//
// func (c *PublicConfig) Scan(value any) error {
// bytes, ok := value.([]byte)
// if !ok {
// return fmt.Errorf("invalid data type for PublicConfig: %T", value)
// }
//
// if err := json.Unmarshal(bytes, c); err != nil {
// return fmt.Errorf("failed to unmarshal PublicConfig: %w", err)
// }
// return nil
// }
type NodeFilter struct {
NodeID *uint64 `form:"node_id"`
FarmID *uint64 `form:"farm_id"`
Expand Down
18 changes: 18 additions & 0 deletions node-registrar/pkg/server/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -753,6 +753,24 @@ func (s *Server) getZOSVersionHandler(c *gin.Context) {
c.JSON(http.StatusOK, version)
}

func (s *Server) getContractHandler(c *gin.Context) {
}

func (s *Server) listContractsHandler(c *gin.Context) {
}

func (s *Server) updateContractHandler(c *gin.Context) {
}

func (s *Server) setContractConsumptionHandler(c *gin.Context) {
}

func (s *Server) createContractHandler(c *gin.Context) {
}

func (s *Server) cancelContractHandler(c *gin.Context) {
}

// Helper function to validate public key format
func isValidPublicKey(publicKeyBase64 string) bool {
publicKeyBytes, err := base64.StdEncoding.DecodeString(publicKeyBase64)
Expand Down
12 changes: 11 additions & 1 deletion node-registrar/pkg/server/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,21 @@ func (s *Server) SetupRoutes() {
accountRoutes.Use(s.AuthMiddleware())
accountRoutes.PATCH("/:twin_id", s.updateAccountHandler)

// Contracts routes
contractsRoutes := v1.Group("contracts")
contractsRoutes.GET("/:contract_id", s.getContractHandler)
contractsRoutes.GET("/", s.listContractsHandler)
// protected by account key
contractsRoutes.Use(s.AuthMiddleware())
contractsRoutes.POST("/", s.createContractHandler)
contractsRoutes.PATCH("/:contract_id", s.updateContractHandler)
contractsRoutes.POST("/:contract_id/consumption", s.setContractConsumptionHandler)
contractsRoutes.DELETE("/:contract_id", s.cancelContractHandler)

// zOS Version endpoints
zosRoutes := v1.Group("/zos")
zosRoutes.GET("/version", s.getZOSVersionHandler)
// protected by admin key
zosRoutes.Use(s.AuthMiddleware())
zosRoutes.PUT("/version", s.setZOSVersionHandler)

}

0 comments on commit bf52c85

Please sign in to comment.