Refactor CrossLink RPC (#2512)

* [README] Update required go version in README to 1.13.6

* [rpc] Refactor GetLastCrossLinks RPC
pull/2514/head
janet-harmony 5 years ago committed by GitHub
parent 696c138524
commit d10dc005a0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 4
      README.md
  2. 38
      core/types/crosslink.go
  3. 8
      hmy/api_backend.go
  4. 2
      internal/hmyapi/apiv1/backend.go
  5. 7
      internal/hmyapi/apiv1/blockchain.go
  6. 2
      internal/hmyapi/apiv2/backend.go
  7. 7
      internal/hmyapi/apiv2/blockchain.go
  8. 2
      internal/hmyapi/backend.go

@ -112,7 +112,7 @@ based library dependencies (`libbls` and `mcl`) setup correctly for you. You can
versions easily, an example: versions easily, an example:
``` ```
$ eval $(gimme 1.12.6) $ eval $(gimme 1.13.6)
``` ```
Note that changing the go version might mean that dependencies won't work out right when trying to Note that changing the go version might mean that dependencies won't work out right when trying to
@ -129,7 +129,7 @@ brew install openssl
## Dev Environment Setup ## Dev Environment Setup
The required go version is: **go1.12** The required go version is: **go1.13.6**
```bash ```bash
export GOPATH=$HOME/<path_of_your_choice> export GOPATH=$HOME/<path_of_your_choice>

@ -2,6 +2,7 @@ package types
import ( import (
"encoding/hex" "encoding/hex"
"encoding/json"
"math/big" "math/big"
"sort" "sort"
@ -24,16 +25,6 @@ type CrossLink struct {
EpochF *big.Int EpochF *big.Int
} }
// RPCCrossLink is used for returning data to RPC calls, converting byte arrays to hex encoded strings
type RPCCrossLink struct {
HashF common.Hash `json:"parentHash"`
BlockNumberF *big.Int `json:"blockNumber"`
SignatureF string `json:"signature"`
BitmapF string `json:"signatureBitmap"`
ShardIDF uint32 `json:"shardId"`
EpochF *big.Int `json:"epochNumber"`
}
// NewCrossLink returns a new cross link object // NewCrossLink returns a new cross link object
// epoch is the parentHeader's epoch // epoch is the parentHeader's epoch
func NewCrossLink(header *block.Header, epoch *big.Int) CrossLink { func NewCrossLink(header *block.Header, epoch *big.Int) CrossLink {
@ -80,16 +71,23 @@ func (cl CrossLink) Signature() [96]byte {
return cl.SignatureF return cl.SignatureF
} }
// ConvertForRPC converts CrossLink into RPCCrossLink // MarshalJSON ..
func (cl CrossLink) ConvertForRPC() *RPCCrossLink { func (cl CrossLink) MarshalJSON() ([]byte, error) {
return &RPCCrossLink{ return json.Marshal(struct {
HashF: cl.HashF, Hash common.Hash `json:"hash"`
BlockNumberF: cl.BlockNumberF, BlockNumber *big.Int `json:"block-number"`
SignatureF: hex.EncodeToString(cl.SignatureF[:]), Signature string `json:"signature"`
BitmapF: hex.EncodeToString(cl.BitmapF), Bitmap string `json:"signature-bitmap"`
ShardIDF: cl.ShardIDF, ShardID uint32 `json:"shard-id"`
EpochF: cl.EpochF, EpochNumber *big.Int `json:"epoch-number"`
} }{
cl.HashF,
cl.BlockNumberF,
hex.EncodeToString(cl.SignatureF[:]),
hex.EncodeToString(cl.BitmapF),
cl.ShardIDF,
cl.EpochF,
})
} }
// Serialize returns bytes of cross link rlp-encoded content // Serialize returns bytes of cross link rlp-encoded content

@ -581,15 +581,15 @@ func (b *APIBackend) GetCurrentBadBlocks() []core.BadBlock {
} }
// GetLastCrossLinks .. // GetLastCrossLinks ..
func (b *APIBackend) GetLastCrossLinks() ([]*types.RPCCrossLink, error) { func (b *APIBackend) GetLastCrossLinks() ([]*types.CrossLink, error) {
lastCrossLinks := []*types.RPCCrossLink{} crossLinks := []*types.CrossLink{}
for i := uint32(1); i < shard.Schedule.InstanceForEpoch(b.CurrentBlock().Epoch()).NumShards(); i++ { for i := uint32(1); i < shard.Schedule.InstanceForEpoch(b.CurrentBlock().Epoch()).NumShards(); i++ {
link, err := b.hmy.BlockChain().ReadShardLastCrossLink(i) link, err := b.hmy.BlockChain().ReadShardLastCrossLink(i)
if err != nil { if err != nil {
return nil, err return nil, err
} }
lastCrossLinks = append(lastCrossLinks, link.ConvertForRPC()) crossLinks = append(crossLinks, link)
} }
return lastCrossLinks, nil return crossLinks, nil
} }

@ -87,5 +87,5 @@ type Backend interface {
GetSuperCommittees() (*quorum.Transition, error) GetSuperCommittees() (*quorum.Transition, error)
GetTotalStakingSnapshot() *big.Int GetTotalStakingSnapshot() *big.Int
GetCurrentBadBlocks() []core.BadBlock GetCurrentBadBlocks() []core.BadBlock
GetLastCrossLinks() ([]*types.RPCCrossLink, error) GetLastCrossLinks() ([]*types.CrossLink, error)
} }

@ -848,6 +848,9 @@ func (s *PublicBlockChainAPI) GetStakingNetworkInfo(ctx context.Context) (*Staki
} }
// GetLastCrossLinks .. // GetLastCrossLinks ..
func (s *PublicBlockChainAPI) GetLastCrossLinks() ([]*types.RPCCrossLink, error) { func (s *PublicBlockChainAPI) GetLastCrossLinks() ([]*types.CrossLink, error) {
return s.b.GetLastCrossLinks() if s.b.GetShardID() == shard.BeaconChainShardID {
return s.b.GetLastCrossLinks()
}
return nil, errNotBeaconChainShard
} }

@ -87,5 +87,5 @@ type Backend interface {
GetSuperCommittees() (*quorum.Transition, error) GetSuperCommittees() (*quorum.Transition, error)
GetTotalStakingSnapshot() *big.Int GetTotalStakingSnapshot() *big.Int
GetCurrentBadBlocks() []core.BadBlock GetCurrentBadBlocks() []core.BadBlock
GetLastCrossLinks() ([]*types.RPCCrossLink, error) GetLastCrossLinks() ([]*types.CrossLink, error)
} }

@ -813,6 +813,9 @@ func (s *PublicBlockChainAPI) GetStakingNetworkInfo(ctx context.Context) (*Staki
} }
// GetLastCrossLinks .. // GetLastCrossLinks ..
func (s *PublicBlockChainAPI) GetLastCrossLinks() ([]*types.RPCCrossLink, error) { func (s *PublicBlockChainAPI) GetLastCrossLinks() ([]*types.CrossLink, error) {
return s.b.GetLastCrossLinks() if s.b.GetShardID() == shard.BeaconChainShardID {
return s.b.GetLastCrossLinks()
}
return nil, errNotBeaconChainShard
} }

@ -89,7 +89,7 @@ type Backend interface {
GetSuperCommittees() (*quorum.Transition, error) GetSuperCommittees() (*quorum.Transition, error)
GetTotalStakingSnapshot() *big.Int GetTotalStakingSnapshot() *big.Int
GetCurrentBadBlocks() []core.BadBlock GetCurrentBadBlocks() []core.BadBlock
GetLastCrossLinks() ([]*types.RPCCrossLink, error) GetLastCrossLinks() ([]*types.CrossLink, error)
} }
// GetAPIs returns all the APIs. // GetAPIs returns all the APIs.

Loading…
Cancel
Save