You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
110 lines
3.1 KiB
110 lines
3.1 KiB
4 years ago
|
package rpc
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"math/big"
|
||
|
|
||
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
||
8 months ago
|
"github.com/woop-chain/woop/eth/rpc"
|
||
|
"github.com/woop-chain/woop/wiki"
|
||
4 years ago
|
)
|
||
|
|
||
8 months ago
|
// PublicWoopService provides an API to access Woop related information.
|
||
4 years ago
|
// It offers only methods that operate on public data that is freely available to anyone.
|
||
8 months ago
|
type PublicWoopService struct {
|
||
|
wiki *wiki.Woop
|
||
4 years ago
|
version Version
|
||
|
}
|
||
|
|
||
8 months ago
|
// NewPublicWoopAPI creates a new API for the RPC interface
|
||
|
func NewPublicWoopAPI(wiki *wiki.Woop, version Version) rpc.API {
|
||
4 years ago
|
return rpc.API{
|
||
|
Namespace: version.Namespace(),
|
||
|
Version: APIVersion,
|
||
8 months ago
|
Service: &PublicWoopService{wiki, version},
|
||
4 years ago
|
Public: true,
|
||
|
}
|
||
|
}
|
||
|
|
||
8 months ago
|
// ProtocolVersion returns the current Woop protocol version this node supports
|
||
4 years ago
|
// Note that the return type is an interface to account for the different versions
|
||
8 months ago
|
func (s *PublicWoopService) ProtocolVersion(
|
||
4 years ago
|
ctx context.Context,
|
||
|
) (interface{}, error) {
|
||
|
// Format response according to version
|
||
|
switch s.version {
|
||
4 years ago
|
case V1, Eth:
|
||
8 months ago
|
return hexutil.Uint(s.wiki.ProtocolVersion()), nil
|
||
4 years ago
|
case V2:
|
||
8 months ago
|
return s.wiki.ProtocolVersion(), nil
|
||
4 years ago
|
default:
|
||
|
return nil, ErrUnknownRPCVersion
|
||
|
}
|
||
|
}
|
||
|
|
||
2 years ago
|
// Syncing returns false in case the node is in sync with the network
|
||
2 years ago
|
// If it is syncing, it returns:
|
||
|
// starting block, current block, and network height
|
||
8 months ago
|
func (s *PublicWoopService) Syncing(
|
||
4 years ago
|
ctx context.Context,
|
||
|
) (interface{}, error) {
|
||
2 years ago
|
// difference = target - current
|
||
8 months ago
|
inSync, target, difference := s.wiki.NodeAPI.SyncStatus(s.wiki.ShardID)
|
||
2 years ago
|
if inSync {
|
||
|
return false, nil
|
||
|
}
|
||
2 years ago
|
return struct {
|
||
2 years ago
|
Start uint64 `json:"startingBlock"`
|
||
|
Current uint64 `json:"currentBlock"`
|
||
|
Target uint64 `json:"highestBlock"`
|
||
2 years ago
|
}{
|
||
2 years ago
|
// Start: 0, // TODO
|
||
|
Current: target - difference,
|
||
|
Target: target,
|
||
2 years ago
|
}, nil
|
||
4 years ago
|
}
|
||
|
|
||
|
// GasPrice returns a suggestion for a gas price.
|
||
|
// Note that the return type is an interface to account for the different versions
|
||
8 months ago
|
func (s *PublicWoopService) GasPrice(ctx context.Context) (interface{}, error) {
|
||
|
price, err := s.wiki.SuggestPrice(ctx)
|
||
2 years ago
|
if err != nil || price.Cmp(big.NewInt(100e9)) < 0 {
|
||
|
price = big.NewInt(100e9)
|
||
3 years ago
|
}
|
||
4 years ago
|
// Format response according to version
|
||
|
switch s.version {
|
||
4 years ago
|
case V1, Eth:
|
||
3 years ago
|
return (*hexutil.Big)(price), nil
|
||
4 years ago
|
case V2:
|
||
3 years ago
|
return price.Uint64(), nil
|
||
4 years ago
|
default:
|
||
|
return nil, ErrUnknownRPCVersion
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// GetNodeMetadata produces a NodeMetadata record, data is from the answering RPC node
|
||
8 months ago
|
func (s *PublicWoopService) GetNodeMetadata(
|
||
4 years ago
|
ctx context.Context,
|
||
|
) (StructuredResponse, error) {
|
||
|
// Response output is the same for all versions
|
||
8 months ago
|
return NewStructuredResponse(s.wiki.GetNodeMetadata())
|
||
4 years ago
|
}
|
||
|
|
||
|
// GetPeerInfo produces a NodePeerInfo record
|
||
8 months ago
|
func (s *PublicWoopService) GetPeerInfo(
|
||
4 years ago
|
ctx context.Context,
|
||
|
) (StructuredResponse, error) {
|
||
|
// Response output is the same for all versions
|
||
8 months ago
|
return NewStructuredResponse(s.wiki.GetPeerInfo())
|
||
4 years ago
|
}
|
||
3 years ago
|
|
||
8 months ago
|
// GetNumPendingCrossLinks returns length of wiki.BlockChain.ReadPendingCrossLinks()
|
||
|
func (s *PublicWoopService) GetNumPendingCrossLinks() (int, error) {
|
||
|
links, err := s.wiki.BlockChain.ReadPendingCrossLinks()
|
||
3 years ago
|
if err != nil {
|
||
|
return 0, err
|
||
|
}
|
||
|
|
||
|
return len(links), nil
|
||
|
}
|