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:
```
$ 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
@ -129,7 +129,7 @@ brew install openssl
## Dev Environment Setup
The required go version is: **go1.12**
The required go version is: **go1.13.6**
```bash
export GOPATH=$HOME/<path_of_your_choice>

@ -2,6 +2,7 @@ package types
import (
"encoding/hex"
"encoding/json"
"math/big"
"sort"
@ -24,16 +25,6 @@ type CrossLink struct {
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
// epoch is the parentHeader's epoch
func NewCrossLink(header *block.Header, epoch *big.Int) CrossLink {
@ -80,16 +71,23 @@ func (cl CrossLink) Signature() [96]byte {
return cl.SignatureF
}
// ConvertForRPC converts CrossLink into RPCCrossLink
func (cl CrossLink) ConvertForRPC() *RPCCrossLink {
return &RPCCrossLink{
HashF: cl.HashF,
BlockNumberF: cl.BlockNumberF,
SignatureF: hex.EncodeToString(cl.SignatureF[:]),
BitmapF: hex.EncodeToString(cl.BitmapF),
ShardIDF: cl.ShardIDF,
EpochF: cl.EpochF,
}
// MarshalJSON ..
func (cl CrossLink) MarshalJSON() ([]byte, error) {
return json.Marshal(struct {
Hash common.Hash `json:"hash"`
BlockNumber *big.Int `json:"block-number"`
Signature string `json:"signature"`
Bitmap string `json:"signature-bitmap"`
ShardID uint32 `json:"shard-id"`
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

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

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

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

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

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

Loading…
Cancel
Save