Skip to content

Commit

Permalink
update accounts and farms to filter and update with options
Browse files Browse the repository at this point in the history
  • Loading branch information
Eslam-Nawara committed Feb 19, 2025
1 parent 6128abb commit 45455c2
Show file tree
Hide file tree
Showing 3 changed files with 288 additions and 75 deletions.
144 changes: 90 additions & 54 deletions node-registrar/client/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,26 +26,26 @@ func (c RegistrarClient) GetAccountByPK(pk []byte) (account Account, err error)
return c.getAccountByPK(pk)
}

func (c RegistrarClient) UpdateAccount(relays []string, rmbEncKey string) (err error) {
return c.updateAccount(relays, rmbEncKey)
func (c RegistrarClient) UpdateAccount(opts ...UpdateAccountOpts) (err error) {
return c.updateAccount(opts)
}

func (c RegistrarClient) EnsureAccount(pk []byte, relays []string, rmbEncKey string) (account Account, err error) {
return c.ensureAccount(pk, relays, rmbEncKey)
}

func (c *RegistrarClient) createAccount(relays []string, rmbEncKey string) (result Account, err error) {
func (c *RegistrarClient) createAccount(relays []string, rmbEncKey string) (account Account, err error) {
url, err := url.JoinPath(c.baseURL, "accounts")
if err != nil {
return result, errors.Wrap(err, "failed to construct registrar url")
return account, errors.Wrap(err, "failed to construct registrar url")
}

publicKeyBase64 := base64.StdEncoding.EncodeToString(c.keyPair.publicKey)

timestamp := time.Now().Unix()
signature := c.signRequest(timestamp)

account := map[string]any{
data := map[string]any{
"public_key": publicKeyBase64,
"signature": signature,
"timestamp": timestamp,
Expand All @@ -54,25 +54,25 @@ func (c *RegistrarClient) createAccount(relays []string, rmbEncKey string) (resu
}

var body bytes.Buffer
err = json.NewEncoder(&body).Encode(account)
err = json.NewEncoder(&body).Encode(data)
if err != nil {
return result, errors.Wrap(err, "failed to parse request body")
return account, errors.Wrap(err, "failed to parse request body")
}

resp, err := c.httpClient.Post(url, "application/json", &body)
if err != nil {
return result, errors.Wrap(err, "failed to send request to the registrar")
return account, errors.Wrap(err, "failed to send request to the registrar")
}

if resp.StatusCode != http.StatusCreated {
err = parseResponseError(resp.Body)
return result, errors.Wrapf(err, "failed to create account with status %s", resp.Status)
return account, errors.Wrapf(err, "failed to create account with status %s", resp.Status)
}
defer resp.Body.Close()

err = json.NewDecoder(resp.Body).Decode(&result)
err = json.NewDecoder(resp.Body).Decode(&account)

c.twinID = result.TwinID
c.twinID = account.TwinID
return
}

Expand Down Expand Up @@ -114,24 +114,57 @@ func (c RegistrarClient) getAccount(id uint64) (account Account, err error) {
return
}

func (c RegistrarClient) updateAccount(relays []string, rmbEncKey string) (err error) {
url, err := url.JoinPath(c.baseURL, "accounts", fmt.Sprint(c.twinID))
func (c RegistrarClient) getAccountByPK(pk []byte) (account Account, err error) {
url, err := url.JoinPath(c.baseURL, "accounts")
if err != nil {
return errors.Wrap(err, "failed to construct registrar url")
return account, errors.Wrap(err, "failed to construct registrar url")
}

publicKeyBase64 := base64.StdEncoding.EncodeToString(pk)

req, err := http.NewRequest("GET", url, nil)
if err != nil {
return account, err
}

q := req.URL.Query()
q.Add("public_key", publicKeyBase64)
req.URL.RawQuery = q.Encode()

resp, err := c.httpClient.Do(req)
if err != nil {
return account, err
}

acc := map[string]any{}
if resp == nil {
return account, errors.New("no response received")
}
defer resp.Body.Close()

if len(relays) != 0 {
acc["relays"] = relays
if resp.StatusCode == http.StatusNotFound {
return account, ErrorAccountNotFround
}

if len(rmbEncKey) != 0 {
acc["rmb_enc_key"] = rmbEncKey
if resp.StatusCode != http.StatusOK {
err = parseResponseError(resp.Body)
return account, errors.Wrapf(err, "failed to get account by public_key with status code %s", resp.Status)
}

err = json.NewDecoder(resp.Body).Decode(&account)

return account, err
}

func (c RegistrarClient) updateAccount(opts []UpdateAccountOpts) (err error) {
url, err := url.JoinPath(c.baseURL, "accounts", fmt.Sprint(c.twinID))
if err != nil {
return errors.Wrap(err, "failed to construct registrar url")
}

var body bytes.Buffer
err = json.NewEncoder(&body).Encode(acc)
data := parseUpdateAccountOpts(opts)

err = json.NewEncoder(&body).Encode(data)
if err != nil {
return errors.Wrap(err, "failed to parse request body")
}
Expand Down Expand Up @@ -162,45 +195,25 @@ func (c RegistrarClient) updateAccount(relays []string, rmbEncKey string) (err e
return
}

func (c RegistrarClient) getAccountByPK(pk []byte) (account Account, err error) {
url, err := url.JoinPath(c.baseURL, "accounts")
if err != nil {
return account, errors.Wrap(err, "failed to construct registrar url")
}

publicKeyBase64 := base64.StdEncoding.EncodeToString(pk)

req, err := http.NewRequest("GET", url, nil)
if err != nil {
return account, err
}

q := req.URL.Query()
q.Add("public_key", publicKeyBase64)
req.URL.RawQuery = q.Encode()

resp, err := c.httpClient.Do(req)
if err != nil {
return account, err
}
type accountCfg struct {
relays []string
rmbEncKey string
}

if resp == nil {
return account, errors.New("no response received")
}
defer resp.Body.Close()
type (
UpdateAccountOpts func(*accountCfg)
)

if resp.StatusCode == http.StatusNotFound {
return account, ErrorAccountNotFround
func UpdateAccountWithRelays(relays []string) UpdateAccountOpts {
return func(n *accountCfg) {
n.relays = relays
}
}

if resp.StatusCode != http.StatusOK {
err = parseResponseError(resp.Body)
return account, errors.Wrapf(err, "failed to get account by public_key with status code %s", resp.Status)
func UpdateAccountWithRMBEncKey(rmbEncKey string) UpdateAccountOpts {
return func(n *accountCfg) {
n.rmbEncKey = rmbEncKey
}

err = json.NewDecoder(resp.Body).Decode(&account)

return account, err
}

func (c RegistrarClient) ensureAccount(pk []byte, relays []string, rmbEncKey string) (account Account, err error) {
Expand All @@ -227,3 +240,26 @@ func (c *RegistrarClient) ensureTwinID() error {
c.twinID = twin.TwinID
return nil
}

func parseUpdateAccountOpts(opts []UpdateAccountOpts) map[string]any {
cfg := accountCfg{
rmbEncKey: "",
relays: []string{},
}

for _, opt := range opts {
opt(&cfg)
}

data := map[string]any{}

if len(cfg.relays) != 0 {
data["relays"] = cfg.relays
}

if len(cfg.rmbEncKey) != 0 {
data["rmb_enc_key"] = cfg.rmbEncKey
}

return data
}
32 changes: 18 additions & 14 deletions node-registrar/client/farm.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,10 @@ func (c RegistrarClient) createFarm(farmName string, twinID uint64, dedicated bo
return farmID, errors.Wrap(err, "failed to construct registrar url")
}

data := map[string]any{
"farm_name": farmName,
"twin_id": twinID,
"dedicated": dedicated,
data := Farm{
FarmName: farmName,
TwinID: twinID,
Dedicated: dedicated,
}

var body bytes.Buffer
Expand Down Expand Up @@ -129,12 +129,15 @@ func (c RegistrarClient) createFarm(farmName string, twinID uint64, dedicated bo
return farmID, fmt.Errorf("failed to create farm with status code %s", resp.Status)
}

var result uint64
result := struct {
FarmID uint64 `json:"farm_id"`
}{}

if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
return farmID, errors.Wrap(err, "failed to decode response body")
}

return result, nil
return result.FarmID, nil
}

func (c RegistrarClient) updateFarm(farmID uint64, opts []UpdateFarmOpts) (err error) {
Expand All @@ -144,7 +147,7 @@ func (c RegistrarClient) updateFarm(farmID uint64, opts []UpdateFarmOpts) (err e
}

var body bytes.Buffer
data := parseUpdateFarmOpts(opts...)
data := parseUpdateFarmOpts(opts)

err = json.NewEncoder(&body).Encode(data)
if err != nil {
Expand Down Expand Up @@ -215,16 +218,17 @@ func (c RegistrarClient) listFarms(opts ...ListFarmOpts) (farms []Farm, err erro

data := parseListFarmOpts(opts)

var body bytes.Buffer
err = json.NewEncoder(&body).Encode(data)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return farms, errors.Wrap(err, "failed to encode request body")
return farms, errors.Wrap(err, "failed to construct http request to the registrar")
}

req, err := http.NewRequest("GET", url, &body)
if err != nil {
return farms, errors.Wrap(err, "failed to construct http request to the registrar")
q := req.URL.Query()

for key, val := range data {
q.Add(key, fmt.Sprint(val))
}
req.URL.RawQuery = q.Encode()

resp, err := c.httpClient.Do(req)
if err != nil {
Expand Down Expand Up @@ -282,7 +286,7 @@ func parseListFarmOpts(opts []ListFarmOpts) map[string]any {
return data
}

func parseUpdateFarmOpts(opts ...UpdateFarmOpts) map[string]any {
func parseUpdateFarmOpts(opts []UpdateFarmOpts) map[string]any {
cfg := farmCfg{
farmName: "",
dedicated: false,
Expand Down
Loading

0 comments on commit 45455c2

Please sign in to comment.