Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change block response parsing approach #111

Merged
merged 1 commit into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions rpc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,9 @@ type ClientInformational interface {

// GetLatestEntity returns latest AddressableEntity from the network.
GetLatestEntity(ctx context.Context, entityIdentifier EntityIdentifier) (StateGetEntity, error)
// GetEntityByHash returns an AddressableEntity by block hash from the network.
// GetEntityByBlockHash returns an AddressableEntity by block hash from the network.
GetEntityByBlockHash(ctx context.Context, entityIdentifier EntityIdentifier, hash string) (StateGetEntity, error)
// GetEntityByHeight returns an AddressableEntity by block height from the network.
// GetEntityByBlockHeight returns an AddressableEntity by block height from the network.
GetEntityByBlockHeight(ctx context.Context, entityIdentifier EntityIdentifier, height uint64) (StateGetEntity, error)

// GetLatestBlock returns the latest types.Block from the network.
Expand Down
15 changes: 15 additions & 0 deletions rpc/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,21 @@ func (b ChainGetBlockResult) GetRawJSON() json.RawMessage {
return b.rawJSON
}

func (v *ChainGetBlockResult) UnmarshalJSON(data []byte) error {
var res chainGetBlockResultV1Compatible
if err := json.Unmarshal(data, &res); err != nil {
return err
}

blockResult, err := newChainGetBlockResultFromV1Compatible(res, data)
if err != nil {
return err
}

*v = blockResult
return nil
}

type chainGetBlockResultV1Compatible struct {
APIVersion string `json:"api_version"`
BlockWithSignatures *types.BlockWithSignatures `json:"block_with_signatures"`
Expand Down
36 changes: 9 additions & 27 deletions rpc/rpc_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -400,54 +400,36 @@ func (c *client) GetEraInfoByBlockHash(ctx context.Context, hash string) (ChainG
}

func (c *client) GetLatestBlock(ctx context.Context) (ChainGetBlockResult, error) {
var result chainGetBlockResultV1Compatible
var result ChainGetBlockResult

resp, err := c.processRequest(ctx, MethodGetBlock, nil, &result)
_, err := c.processRequest(ctx, MethodGetBlock, nil, &result)
if err != nil {
return ChainGetBlockResult{}, err
}

blockResult, err := newChainGetBlockResultFromV1Compatible(result, resp.Result)
if err != nil {
return ChainGetBlockResult{}, err
}

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

func (c *client) GetBlockByHash(ctx context.Context, hash string) (ChainGetBlockResult, error) {
var result chainGetBlockResultV1Compatible
var result ChainGetBlockResult

resp, err := c.processRequest(ctx, MethodGetBlock, NewParamBlockByHash(hash), &result)
_, err := c.processRequest(ctx, MethodGetBlock, NewParamBlockByHash(hash), &result)
if err != nil {
return ChainGetBlockResult{}, err
}

blockResult, err := newChainGetBlockResultFromV1Compatible(result, resp.Result)
if err != nil {
return ChainGetBlockResult{}, err
}

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

func (c *client) GetBlockByHeight(ctx context.Context, height uint64) (ChainGetBlockResult, error) {
var result chainGetBlockResultV1Compatible
var result ChainGetBlockResult

resp, err := c.processRequest(ctx, MethodGetBlock, NewParamBlockByHeight(height), &result)
_, err := c.processRequest(ctx, MethodGetBlock, NewParamBlockByHeight(height), &result)
if err != nil {
return ChainGetBlockResult{}, err
}

blockResult, err := newChainGetBlockResultFromV1Compatible(result, resp.Result)
if err != nil {
return ChainGetBlockResult{}, err
}

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

func (c *client) GetLatestBlockTransfers(ctx context.Context) (ChainGetBlockTransfersResult, error) {
Expand Down
2 changes: 1 addition & 1 deletion types/unbonding_purse.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type UnbondingPurse struct {
// Bonding purse
BondingPurse key.URef `json:"bonding_purse"`
// Era ID in which this unbonding request was created.
EraOfCreation uint32 `json:"era_of_creation"`
EraOfCreation uint64 `json:"era_of_creation"`
// Unbonder public key.
UnbonderPublicKey keypair.PublicKey `json:"unbonder_public_key"`
// The original validator's public key.
Expand Down
Loading