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