Skip to content

Commit

Permalink
Merge pull request #102 from make-software/CSDK/implement_info_get_re…
Browse files Browse the repository at this point in the history
…ward_rpc

Implement info get reward RPC
  • Loading branch information
ZhmakAS authored Jul 17, 2024
2 parents 52ba0e3 + fef2274 commit 803350e
Show file tree
Hide file tree
Showing 10 changed files with 228 additions and 7 deletions.
17 changes: 17 additions & 0 deletions rpc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,23 @@ type ClientInformational interface {

// GetChainspec returns the raw bytes of the chainspec.toml, accounts.toml and global_state.toml files as read at node startup.
GetChainspec(ctx context.Context) (InfoGetChainspecResult, error)

// GetLatestValidatorReward returns the latest reward for a given validator
GetLatestValidatorReward(ctx context.Context, validator keypair.PublicKey) (InfoGetRewardResult, error)
// GetValidatorRewardByEraID returns the reward for a given era and a validator
GetValidatorRewardByEraID(ctx context.Context, validator keypair.PublicKey, eraID uint64) (InfoGetRewardResult, error)
// GetValidatorRewardByBlockHash returns the reward for a given block hash and a validator
GetValidatorRewardByBlockHash(ctx context.Context, validator keypair.PublicKey, blockHash string) (InfoGetRewardResult, error)
// GetValidatorRewardByBlockHeight returns the reward for a given block height and a validator
GetValidatorRewardByBlockHeight(ctx context.Context, validator keypair.PublicKey, height uint64) (InfoGetRewardResult, error)
// GetLatestDelegatorReward returns the latest delegator reward for a given validator
GetLatestDelegatorReward(ctx context.Context, validator, delegator keypair.PublicKey) (InfoGetRewardResult, error)
// GetDelegatorRewardByEraID returns the delegator reward for a given era and a validator
GetDelegatorRewardByEraID(ctx context.Context, validator, delegator keypair.PublicKey, eraID uint64) (InfoGetRewardResult, error)
// GetDelegatorRewardByBlockHash returns the delegator reward for a given block hash and a validator
GetDelegatorRewardByBlockHash(ctx context.Context, validator, delegator keypair.PublicKey, blockHash string) (InfoGetRewardResult, error)
// GetDelegatorRewardByBlockHeight returns the delegator reward for a given block height and a validator
GetDelegatorRewardByBlockHeight(ctx context.Context, validator, delegator keypair.PublicKey, height uint64) (InfoGetRewardResult, error)
}

// ClientTransactional contains the description of account_put_deploy,
Expand Down
12 changes: 12 additions & 0 deletions rpc/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ const (
MethodGetValidatorChanges Method = "info_get_validator_changes"
MethodGetStateRootHash Method = "chain_get_state_root_hash"
MethodGetStatus Method = "info_get_status"
MethodGetReward Method = "info_get_reward"
MethodGetPeers Method = "info_get_peers"
MethodPutDeploy Method = "account_put_deploy"
MethodSpeculativeExec Method = "speculative_exec"
Expand Down Expand Up @@ -159,6 +160,11 @@ type GlobalStateIdentifier struct {
StateRoot *string `json:"StateRootHash,omitempty"`
}

type EraIdentifier struct {
Block *BlockIdentifier `json:"Block,omitempty"`
Era *uint64 `json:"Era,omitempty"`
}

type ParamBlockIdentifier struct {
BlockIdentifier *BlockIdentifier `json:"block_identifier"`
}
Expand Down Expand Up @@ -240,3 +246,9 @@ type QueryBalanceDetailsRequest struct {
PurseIdentifier PurseIdentifier `json:"purse_identifier"`
StateIdentifier *GlobalStateIdentifier `json:"state_identifier,omitempty"`
}

type InfoGetRewardRequest struct {
Validator keypair.PublicKey `json:"validator"`
Delegator *keypair.PublicKey `json:"delegator,omitempty"`
EraIdentifier *EraIdentifier `json:"era_identifier,omitempty"`
}
17 changes: 15 additions & 2 deletions rpc/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ type infoGetDeployResultV1Compatible struct {
BlockHash *key.Hash `json:"block_hash,omitempty"`
BlockHeight *uint64 `json:"block_height,omitempty"`

rawJSON json.RawMessage
_rawJSON json.RawMessage
}

func (v *InfoGetDeployResult) GetRawJSON() json.RawMessage {
Expand Down Expand Up @@ -216,7 +216,7 @@ type infoGetTransactionResultV1Compatible struct {
BlockHash *key.Hash `json:"block_hash,omitempty"`
BlockHeight *uint64 `json:"block_height,omitempty"`

rawJSON json.RawMessage
_rawJSON json.RawMessage
}

func newInfoGetTransactionResultFromV1Compatible(result infoGetTransactionResultV1Compatible, rawJSON json.RawMessage) (InfoGetTransactionResult, error) {
Expand Down Expand Up @@ -498,6 +498,19 @@ func (b QueryBalanceDetailsResult) GetRawJSON() json.RawMessage {
return b.rawJSON
}

type InfoGetRewardResult struct {
APIVersion string `json:"api_version"`
DelegationRate float32 `json:"delegation_rate"`
EraID uint32 `json:"era_id"`
RewardAmount clvalue.UInt512 `json:"reward_amount"`

rawJSON json.RawMessage
}

func (b InfoGetRewardResult) GetRawJSON() json.RawMessage {
return b.rawJSON
}

// BalanceHoldWithProof The block time at which the hold was created.
type BalanceHoldWithProof struct {
//Time types.BlockTime `json:"time"`
Expand Down
142 changes: 142 additions & 0 deletions rpc/rpc_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -759,6 +759,148 @@ func (c *client) GetChainspec(ctx context.Context) (InfoGetChainspecResult, erro
return result, nil
}

func (c *client) GetValidatorRewardByEraID(ctx context.Context, validator keypair.PublicKey, eraID uint64) (InfoGetRewardResult, error) {
var result InfoGetRewardResult

resp, err := c.processRequest(ctx, MethodGetReward, InfoGetRewardRequest{
Validator: validator,
EraIdentifier: &EraIdentifier{
Era: &eraID,
},
}, &result)
if err != nil {
return InfoGetRewardResult{}, err
}

result.rawJSON = resp.Result
return result, nil
}

func (c *client) GetValidatorRewardByBlockHash(ctx context.Context, validator keypair.PublicKey, blockHash string) (InfoGetRewardResult, error) {
var result InfoGetRewardResult

resp, err := c.processRequest(ctx, MethodGetReward, InfoGetRewardRequest{
Validator: validator,
EraIdentifier: &EraIdentifier{
Block: &BlockIdentifier{
Hash: &blockHash,
},
},
}, &result)
if err != nil {
return InfoGetRewardResult{}, err
}

result.rawJSON = resp.Result
return result, nil
}

func (c *client) GetValidatorRewardByBlockHeight(ctx context.Context, validator keypair.PublicKey, height uint64) (InfoGetRewardResult, error) {
var result InfoGetRewardResult

resp, err := c.processRequest(ctx, MethodGetReward, InfoGetRewardRequest{
Validator: validator,
EraIdentifier: &EraIdentifier{
Block: &BlockIdentifier{
Height: &height,
},
},
}, &result)
if err != nil {
return InfoGetRewardResult{}, err
}

result.rawJSON = resp.Result
return result, nil
}

func (c *client) GetDelegatorRewardByEraID(ctx context.Context, validator, delegator keypair.PublicKey, eraID uint64) (InfoGetRewardResult, error) {
var result InfoGetRewardResult

resp, err := c.processRequest(ctx, MethodGetReward, InfoGetRewardRequest{
Validator: validator,
Delegator: &delegator,
EraIdentifier: &EraIdentifier{
Era: &eraID,
},
}, &result)
if err != nil {
return InfoGetRewardResult{}, err
}

result.rawJSON = resp.Result
return result, nil
}

func (c *client) GetDelegatorRewardByBlockHash(ctx context.Context, validator, delegator keypair.PublicKey, blockHash string) (InfoGetRewardResult, error) {
var result InfoGetRewardResult

resp, err := c.processRequest(ctx, MethodGetReward, InfoGetRewardRequest{
Validator: validator,
Delegator: &delegator,
EraIdentifier: &EraIdentifier{
Block: &BlockIdentifier{
Hash: &blockHash,
},
},
}, &result)
if err != nil {
return InfoGetRewardResult{}, err
}

result.rawJSON = resp.Result
return result, nil
}

func (c *client) GetDelegatorRewardByBlockHeight(ctx context.Context, validator, delegator keypair.PublicKey, height uint64) (InfoGetRewardResult, error) {
var result InfoGetRewardResult

resp, err := c.processRequest(ctx, MethodGetReward, InfoGetRewardRequest{
Validator: validator,
Delegator: &delegator,
EraIdentifier: &EraIdentifier{
Block: &BlockIdentifier{
Height: &height,
},
},
}, &result)
if err != nil {
return InfoGetRewardResult{}, err
}

result.rawJSON = resp.Result
return result, nil
}

func (c *client) GetLatestValidatorReward(ctx context.Context, validator keypair.PublicKey) (InfoGetRewardResult, error) {
var result InfoGetRewardResult

resp, err := c.processRequest(ctx, MethodGetReward, InfoGetRewardRequest{
Validator: validator,
}, &result)
if err != nil {
return InfoGetRewardResult{}, err
}

result.rawJSON = resp.Result
return result, nil
}

func (c *client) GetLatestDelegatorReward(ctx context.Context, validator, delegator keypair.PublicKey) (InfoGetRewardResult, error) {
var result InfoGetRewardResult

resp, err := c.processRequest(ctx, MethodGetReward, InfoGetRewardRequest{
Validator: validator,
Delegator: &delegator,
}, &result)
if err != nil {
return InfoGetRewardResult{}, err
}

result.rawJSON = resp.Result
return result, nil
}

func (c *client) processRequest(ctx context.Context, method Method, params interface{}, result any) (RpcResponse, error) {
request := DefaultRpcRequest(method, params)
if reqID := GetReqIdCtx(ctx); reqID != "0" {
Expand Down
10 changes: 10 additions & 0 deletions tests/data/rpc_response/info_get_reward.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"jsonrpc": "2.0",
"id": "1",
"result": {
"api_version": "2.0.0",
"reward_amount": "62559062048560",
"era_id": 13,
"delegation_rate": 1
}
}
28 changes: 28 additions & 0 deletions tests/rpc/rpc_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,34 @@ func Test_DefaultClient_GetBlockLatest(t *testing.T) {
}
}

func Test_DefaultClient_GetReward(t *testing.T) {
tests := []struct {
filePath string
}{
{
filePath: "../data/rpc_response/info_get_reward.json",
},
}

pubKey, err := casper.NewPublicKey("0115394d1f395a87dfed4ab62bbfbc91b573bbb2bffb2c8ebb9c240c51d95bcc4d")
require.NoError(t, err)

for _, tt := range tests {
t.Run("GetValidatorRewardByEraID", func(t *testing.T) {
server := SetupServer(t, tt.filePath)
defer server.Close()
client := casper.NewRPCClient(casper.NewRPCHandler(server.URL, http.DefaultClient))
result, err := client.GetValidatorRewardByEraID(context.Background(), pubKey, 100)
require.NoError(t, err)
assert.NotEmpty(t, result.APIVersion)
assert.NotEmpty(t, result.EraID)
assert.NotEmpty(t, result.RewardAmount)
assert.Equal(t, result.RewardAmount.Value().Int64(), int64(62559062048560))
assert.NotEmpty(t, result.GetRawJSON())
})
}
}

func Test_DefaultClient_GetEntity(t *testing.T) {
server := SetupServer(t, "../data/rpc_response/state_get_entity_account_example.json")
defer server.Close()
Expand Down
2 changes: 1 addition & 1 deletion types/auction_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type PublicKeyAndBid struct {

// EraValidators contains validators and weights for an Era.
type EraValidators struct {
EraID uint32 `json:"era_id"`
EraID uint64 `json:"era_id"`
// List of the validator's weight in the Era
ValidatorWeights []ValidatorWeightAuction `json:"validator_weights"`
}
4 changes: 2 additions & 2 deletions types/bid.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ type Delegator struct {
// Credit is a bridge record pointing to a new `ValidatorBid` after the public key was changed.
type Credit struct {
// The era id the credit was created.
EraID uint32 `json:"era_id"`
EraID uint64 `json:"era_id"`
// Validator's public key.
ValidatorPublicKey keypair.PublicKey `json:"validator_public_key"`
// The credit amount.
Expand All @@ -103,7 +103,7 @@ type Credit struct {

// Bridge is a bridge record pointing to a new `ValidatorBid` after the public key was changed.
type Bridge struct {
EraID uint32 `json:"era_id"`
EraID uint64 `json:"era_id"`
// Previous validator public key associated with the bid."
OldValidatorPublicKey keypair.PublicKey `json:"old_validator_public_key"`
// New validator public key associated with the bid.
Expand Down
2 changes: 1 addition & 1 deletion types/era_summary.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ type EraSummary struct {
// The block hash
BlockHash key.Hash `json:"block_hash"`
// The Era Id
EraID uint32 `json:"era_id"`
EraID uint64 `json:"era_id"`
// The StoredValue containing era information.
StoredValue StoredValue `json:"stored_value"`
// Hex-encoded hash of the state root.
Expand Down
1 change: 0 additions & 1 deletion types/key/bid_addr.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,6 @@ func (h BidAddr) Bytes() []byte {

buf := new(bytes.Buffer)
binary.Write(buf, binary.LittleEndian, h.Credit.EraId)

return append(res, buf.Bytes()...)
default:
panic("Unexpected BidAddr type")
Expand Down

0 comments on commit 803350e

Please sign in to comment.