Skip to content

Commit

Permalink
reversing cycle rights
Browse files Browse the repository at this point in the history
  • Loading branch information
DefinitelyNotAGoat committed Oct 18, 2020
1 parent 7583ffa commit bdff4cf
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 56 deletions.
68 changes: 14 additions & 54 deletions rpc/delegate.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,36 +139,21 @@ Function:
*/
type BakingRightsInput struct {
// The hash of block (height) of which you want to make the query.
Blockhash string
BlockHash string `validate:"required"`

// The block level of which you want to make the query.
Level int

// The cycle of which you want to make the query.
Cycle int

// The level URL parameter.
Level int

// The delegate public key hash of which you want to make the query.
Delegate string

// The max priotity of which you want to make the query.
MaxPriority int
}

func (b *BakingRightsInput) validate() error {
if b.Blockhash == "" && b.Cycle == 0 {
return errors.New("invalid input: missing key cycle or blockhash")
} else if b.Blockhash != "" && b.Cycle != 0 {
return errors.New("invalid input: cannot have both cycle and blockhash")
}

err := validator.New().Struct(b)
if err != nil {
return errors.Wrap(err, "invalid input")
}

return nil
}

/*
EndorsingRightsInput is the input for the goTezos.EndorsingRights function.
Expand All @@ -177,33 +162,18 @@ Function:
*/
type EndorsingRightsInput struct {
// The hash of block (height) of which you want to make the query.
Blockhash string
BlockHash string `validate:"required"`

// The block level of which you want to make the query.
Level int

// The cycle of which you want to make the query.
Cycle int

// The level URL parameter.
Level int

// The delegate public key hash of which you want to make the query.
Delegate string
}

func (e *EndorsingRightsInput) validate() error {
if e.Blockhash == "" && e.Cycle == 0 {
return errors.New("invalid input: missing key cycle or blockhash")
} else if e.Blockhash != "" && e.Cycle != 0 {
return errors.New("invalid input: cannot have both cycle and blockhash")
}

err := validator.New().Struct(e)
if err != nil {
return errors.Wrap(err, "invalid input")
}

return nil
}

/*
DelegatesInput is the input for the goTezos.Delegates function.
Expand Down Expand Up @@ -503,17 +473,12 @@ Link:
https://tezos.gitlab.io/api/rpc.html#get-block-id-helpers-baking-rights
*/
func (c *Client) BakingRights(input BakingRightsInput) (*BakingRights, error) {
err := input.validate()
if err != nil {
return &BakingRights{}, errors.Wrapf(err, "could not get delegations for delegate '%s'", input.Delegate)
}

input.Blockhash, err = c.extractBlockHash(input.Cycle, input.Blockhash)
err := validator.New().Struct(input)
if err != nil {
return &BakingRights{}, errors.Wrapf(err, "could not get delegations for delegate '%s'", input.Delegate)
return &BakingRights{}, errors.Wrap(err, "invalid input")
}

resp, err := c.get(fmt.Sprintf("/chains/%s/blocks/%s/helpers/baking_rights", c.chain, input.Blockhash), input.contructRPCOptions()...)
resp, err := c.get(fmt.Sprintf("/chains/%s/blocks/%s/helpers/baking_rights", c.chain, input.BlockHash), input.contructRPCOptions()...)
if err != nil {
return &BakingRights{}, errors.Wrapf(err, "could not get baking rights")
}
Expand Down Expand Up @@ -578,17 +543,12 @@ Link:
https://tezos.gitlab.io/api/rpc.html#get-block-id-helpers-endorsing-rights
*/
func (c *Client) EndorsingRights(input EndorsingRightsInput) (*EndorsingRights, error) {
err := input.validate()
if err != nil {
return &EndorsingRights{}, errors.Wrapf(err, "could not get delegations for delegate '%s'", input.Delegate)
}

input.Blockhash, err = c.extractBlockHash(input.Cycle, input.Blockhash)
err := validator.New().Struct(input)
if err != nil {
return &EndorsingRights{}, errors.Wrapf(err, "could not get delegations for delegate '%s'", input.Delegate)
return &EndorsingRights{}, errors.Wrap(err, "invalid input")
}

resp, err := c.get(fmt.Sprintf("/chains/%s/blocks/%s/helpers/endorsing_rights", c.chain, input.Blockhash), input.contructRPCOptions()...)
resp, err := c.get(fmt.Sprintf("/chains/%s/blocks/%s/helpers/endorsing_rights", c.chain, input.BlockHash), input.contructRPCOptions()...)
if err != nil {
return &EndorsingRights{}, errors.Wrap(err, "could not get endorsing rights")
}
Expand Down
4 changes: 2 additions & 2 deletions rpc/delegate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ func Test_BakingRights(t *testing.T) {
assert.Nil(t, err)

bakingRights, err := rpc.BakingRights(BakingRightsInput{
Blockhash: mockBlockHash,
BlockHash: mockBlockHash,
})
checkErr(t, tt.wantErr, tt.containsErr, err)

Expand Down Expand Up @@ -494,7 +494,7 @@ func Test_EndorsingRights(t *testing.T) {
assert.Nil(t, err)

endorsingRights, err := rpc.EndorsingRights(EndorsingRightsInput{
Blockhash: mockBlockHash,
BlockHash: mockBlockHash,
})
checkErr(t, tt.wantErr, tt.containsErr, err)

Expand Down

0 comments on commit bdff4cf

Please sign in to comment.