The core protocol of WoopChain
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.
woop/api/service/explorer/service.go

201 lines
6.3 KiB

package explorer
import (
"context"
"encoding/json"
"fmt"
"net"
"net/http"
"strconv"
"time"
"github.com/ethereum/go-ethereum/rpc"
"github.com/gorilla/mux"
msg_pb "github.com/harmony-one/harmony/api/proto/message"
"github.com/harmony-one/harmony/api/service/syncing"
"github.com/harmony-one/harmony/consensus/reward"
"github.com/harmony-one/harmony/core"
"github.com/harmony-one/harmony/internal/utils"
"github.com/harmony-one/harmony/numeric"
"github.com/harmony-one/harmony/p2p"
)
// Constants for explorer service.
const (
explorerPortDifference = 4000
defaultPageSize = "1000"
maxAddresses = 100000
totalSupply = 12600000000
)
// HTTPError is an HTTP error.
type HTTPError struct {
Code int
Msg string
}
// Service is the struct for explorer service.
type Service struct {
router *mux.Router
IP string
Port string
Storage *Storage
server *http.Server
messageChan chan *msg_pb.Message
stateSync *syncing.StateSync
blockchain *core.BlockChain
}
// New returns explorer service.
func New(selfPeer *p2p.Peer, ss *syncing.StateSync, bc *core.BlockChain) *Service {
return &Service{IP: selfPeer.IP, Port: selfPeer.Port, stateSync: ss, blockchain: bc}
# This is a combination of 16 commits. # This is the 1st commit message: Fix travis # This is the commit message #2: [cleanup] remove is_genesis flag Signed-off-by: Leo Chen <leo@harmony.one> # This is the commit message #3: [nodetype] add nodetype to replace is_explorer Signed-off-by: Leo Chen <leo@harmony.one> # This is the commit message #4: fix beacon sync channel blocking issue # This is the commit message #5: use lastMileMux to protect lastMileBlock queue to avoid potential blocking; use sleep instead of ticker # This is the commit message #6: Fix the beacon committee check # This is the commit message #7: [release] add release action to go_executable_build.sh Signed-off-by: Leo Chen <leo@harmony.one> # This is the commit message #8: [node.sh] add -d option -d download only Signed-off-by: Leo Chen <leo@harmony.one> # This is the commit message #9: [node.sh] add -T node_type option -T node_type support node type (validator/explorer) Signed-off-by: Leo Chen <leo@harmony.one> # This is the commit message #10: [node.sh] backward compatible with older harmony binary Signed-off-by: Leo Chen <leo@harmony.one> # This is the commit message #11: [node.sh] support -i shard_id option -i shard_id specify shard_id, this is applicable only to explorer node Signed-off-by: Leo Chen <leo@harmony.one> # This is the commit message #12: Revisited api # This is the commit message #13: Fix rpc integration # This is the commit message #14: address some minor issues in comments and code # This is the commit message #15: addressed comments on others' buckets # This is the commit message #16: Add Global Access to OS Temp Directory Variable and Move DHT Files Into Temp Directory
5 years ago
}
// StartService starts explorer service.
func (s *Service) StartService() {
utils.Logger().Info().Msg("Starting explorer service.")
s.Init()
s.server = s.Run()
}
// StopService shutdowns explorer service.
func (s *Service) StopService() {
utils.Logger().Info().Msg("Shutting down explorer service.")
if err := s.server.Shutdown(context.Background()); err != nil {
utils.Logger().Error().Err(err).Msg("Error when shutting down explorer server")
} else {
utils.Logger().Info().Msg("Shutting down explorer server successfully")
}
}
6 years ago
// GetExplorerPort returns the port serving explorer dashboard. This port is explorerPortDifference less than the node port.
func GetExplorerPort(nodePort string) string {
if port, err := strconv.Atoi(nodePort); err == nil {
return fmt.Sprintf("%d", port-explorerPortDifference)
}
utils.Logger().Error().Msg("error on parsing.")
return ""
}
6 years ago
// Init is to initialize for ExplorerService.
func (s *Service) Init() {
s.Storage = GetStorageInstance(s.IP, s.Port)
}
// Run is to run serving explorer.
func (s *Service) Run() *http.Server {
// Init address.
addr := net.JoinHostPort("", GetExplorerPort(s.Port))
s.router = mux.NewRouter()
// Set up router for addresses.
// Fetch addresses request, accepts parameter size: how much addresses to read,
// parameter prefix: from which address prefix start
s.router.Path("/addresses").Queries("size", "{[0-9]*?}", "prefix", "{[a-zA-Z0-9]*?}").HandlerFunc(s.GetAddresses).Methods("GET")
s.router.Path("/addresses").HandlerFunc(s.GetAddresses)
6 years ago
// Set up router for node count.
s.router.Path("/circulating-supply").Queries().HandlerFunc(s.GetCirculatingSupply).Methods("GET")
s.router.Path("/circulating-supply").HandlerFunc(s.GetCirculatingSupply)
6 years ago
// Set up router for node count.
s.router.Path("/total-supply").Queries().HandlerFunc(s.GetTotalSupply).Methods("GET")
s.router.Path("/total-supply").HandlerFunc(s.GetTotalSupply)
// Set up router for node health check.
s.router.Path("/node-sync").Queries().HandlerFunc(s.GetNodeSync).Methods("GET")
s.router.Path("/node-sync").HandlerFunc(s.GetNodeSync)
// Do serving now.
utils.Logger().Info().Str("port", GetExplorerPort(s.Port)).Msg("[Explorer] Server started.")
Merge master to t3 0523 (#3085) * fix signers share computation to use only non-harmony nodes (#3008) * Add LastEpochInCommittee at prestaking epoch too (#3014) * write validator stats at the end of prestaking epoch (#3021) * write validator stats at the end of prestaking epoch * also need to write block reward accumulator * [build] enable static build by default (#2885) scripts/go_executable_build.sh -s (static build, default) scripts/go_executable_build.sh -S (non-static build) Signed-off-by: Leo Chen <leo@harmony.one> * Code formatting fix based on `staticcheck` (#3023) * fix some static check issues * more fixes according to staticcheck * add nil check on getHeader * Add more log info * [rpc] Revert change to v2 GetBalance RPC (#3037) * Cherrypick crosslink related fixes from t3 to master. (#3045) * Optimize crosslink verification logic and add more cache (#3032) * make crosslink broadcast smarter and more efficient (#3036) * adjust catch up speed to be a sane number * Fix crosslink broadcast condition (#3041) * Tx pool error report fix & local build fix (#3046) * [tx_list] Add transaction after you can fetch tx cost * [tx_pool] Add error sink reports for removed tx * Correct log levels for known transactions * Remove redundant hash to `enqueue` and `add` signatures * Update tests for signature changes * [build] Fix local build for MacOs * [tx_pool] Correct typo & logging level for known tx * [config] Create Testnet config * [node.sh] update testnet network config * [config] correct testnet config * [main] Enable beacon sync for explorer nodes (#3059) * [apr] should not use snapshot from last epoch, use current epoch snapshot (#3051) * use snapshot total delegation instead of current total delegation * should not use snapshot from last epoch, use current epoch snapshot (#3049) Co-authored-by: Rongjian Lan <rongjian.lan@gmail.com> Co-authored-by: Rongjian Lan <rongjian.lan@gmail.com> * cherrypick log level change * [rpc] Add active status back to Validator information [validator] Fix typo for ValidatorRPCEnhanced [Makefile] Change make exe to build non-static by default * [network] Added caching to node server (#3048) * [network] Added caching to node server * [network] renamed two singleflight.Group * [network] added two error handling * [network] added forget logic for single flight * [network] changed single flight to lru cache solution * [network] adjusted the cache size * [network] changed cache size to 10000 * Transaction api improvements (#3057) * [node] Add option to broadcast invalid tx (on by default) * update main args to take in broadcast invalid tx * update node.sh for new broadcast invalid tx option * [rpc] Do not submit transaction if tx has been finalized * Support legacy behavior and not return errors on tx submission * [main] make broadcasting invalid tx default false * [node] Fix formatting * Add timeouts for the explorer HTTP service * [test] State trans unit test (#3043) * completing create-validator tests generalizing create validator tests, started with edit validator tests one round completed before major refactor * [test] finished coding test TestCheckDuplicateFields * [test] finished debug TestCheckDuplicateFields * [test] Added TestDescription_EnsureLength * [test] added test in validator_test.go TestUpdateDescription * [test] Added test TestComputed_String * [test] refactored TestValidatorSanityCheck and added some message in error message returned from sanity check * [test] removed test index for debugging * [test] Added test TestValidatorWrapper_SanityCheck. Added value check for TestMarshalUnmarshalValidator. Fixed rlp usage defect in UnmarshalValidator * [test] added test TestVerifyBLSKeys * [test] added test TestContainsHarmonyBLSKeys * [test] added test TestCreateValidatorFromNewMsg * [test] fixed some tests errors * [test] added test TestUpdateValidatorFromEditMsg, last editted the test file, finished testing validator_test.go * [staking] added numeric.Dec.Copy method * [staking] added CommissionRates.Copy * [staking] add nil case handle to copy method and related test case * [test] added nil case for commissionRates.Copy test * [staking] finished CreateValidator.Copy and related test case * [staking] added EditValidator.Copy method and related test case * [test] added zero test cases for Copy method * [staking] implemented Delegate.Copy and related unit test case * [staking] added Undelegate.Copy and CollectReward.Copy method. Implemented corresponding test cases * [test] added two more simple unit tests * [test] solving the merge conflict with deep copy fix * [test] added schedule for sharding * [test] refactored double_sign_test.go * [test] comment out all test cases for now. * [test] added record Copy method for testing * [test] added the first test case in TestVerify * [test] finished test TestVerify * [test] Added test TestVerify, TestApplySlashRate, TestSetDifference * [test] half through TestDelegatorSlashApply * [test] fix the problem cause by unsafe usage of big.Int * [staking] added the copy methods to validatorWrapper * [test] added test cases for copy method for staking/validatorWrapper * [test] added test case TestApply * [test] added test case TestRate * [test] fix the golint errors * [test] removed commented out unit test code * [test] remote the empty line in imports * [test] moved copy methods to stake_testing module * [test] removed usage of interface stateDB * [test] removed empty lines in imports * [test] fixed golint package name issues * [test] removed a todo when writing code * [test] moved record.Copy to staking/slash/test * [test] add some changes * [test] added prototypes in staketest module * [test] fix the golint issue in staketest * [test] make prototype more beautiful * [test] refactored test TestCheckDuplicateFields * [test] add createValidator test data * [test] added positive test case for VerifyAndCreateValidatorFromMsg * [test] added create validator test * [test] added the positive test case for TestVerifyAndEditValidatorFromMsg * [test] added the tests case TestVerifyAndEditValidatorFromMsg * [test] fix one of the error is not triggered as expected * [test] more changes in test * [test] fix the positive test data for TestVerifyAndDelegateFromMsg * [test] Fixed two comment golint errors * [test] added delegate tests * [test] added a make function. WIP * [test] added undelegate positive test * [test] added negative test cases for undelegate * [test] added positive test. not passed. Need to add assert method in test suites * [test] added equal function to check Validator Equality * [test] added equals for staketest * [test] replaced deep equal with new equal methods: * [test] removed unused codes * [test] Finishing touch * [test] fix comment golint issue * [test] removed dead code in staking_verifier_test.go Co-authored-by: Ganesha Upadhyaya <ganeshrvce@gmail.com> * changed the sync port to dns port in flag * [network] Limit client sync connection (#3071) * [network] limit client connected peers * Changed signature and added tests * Set secure permissions on generated blskey pass file in node.sh * Fix spellin gtypo in node.sh * fix latest param handling in api v1 * added comment for the isGreaterThanLatestBlockNum check * fix a nil pointer crash due to previous revert Signed-off-by: Leo Chen <leo@harmony.one> * fix nil pointer crash Signed-off-by: Leo Chen <leo@harmony.one> * fix nil pointer of block Signed-off-by: Leo Chen <leo@harmony.one> * [rpc] add viewID and epoch to RPCMarshalBlock (#3009) * [rpc] add viewID and epoch to RPCMarshalBlock * add apt-get update * Do not rollback current block on failed block sync (#3101) Co-authored-by: Ganesha Upadhyaya <ganeshrvce@gmail.com> Co-authored-by: Rongjian Lan <rongjian.lan@gmail.com> Co-authored-by: Jacky Wang <jackyw.se@gmail.com> Co-authored-by: Janet Liang <56005637+janet-harmony@users.noreply.github.com> Co-authored-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> Co-authored-by: Janet Liang <janet@harmony.one> Co-authored-by: Sebastian Johnsson <sebastian.johnsson@gmail.com> Co-authored-by: hsiung <hsiung@pinterest.com> Co-authored-by: Dennis Won <jhwon0820@gmail.com> Co-authored-by: Yishuang Chen <34232522+ivorytowerdds@users.noreply.github.com>
5 years ago
server := &http.Server{
Addr: addr,
Handler: s.router,
ReadTimeout: 5 * time.Second,
WriteTimeout: 10 * time.Second,
IdleTimeout: 120 * time.Second,
}
go func() {
defer func() { utils.Logger().Debug().Msg("[Explorer] Server closed.") }()
if err := server.ListenAndServe(); err != nil {
utils.Logger().Warn().Err(err).Msg("[Explorer] Server error.")
}
}()
return server
}
// GetAddresses serves end-point /addresses, returns size of addresses from address with prefix.
func (s *Service) GetAddresses(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
sizeStr := r.FormValue("size")
prefix := r.FormValue("prefix")
if sizeStr == "" {
sizeStr = defaultPageSize
}
data := &Data{}
defer func() {
if err := json.NewEncoder(w).Encode(data.Addresses); err != nil {
utils.Logger().Warn().Err(err).Msg("cannot JSON-encode addresses")
}
}()
size, err := strconv.Atoi(sizeStr)
if err != nil || size > maxAddresses {
w.WriteHeader(http.StatusBadRequest)
return
}
data.Addresses, err = s.Storage.GetAddresses(size, prefix)
5 years ago
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
utils.Logger().Warn().Err(err).Msg("wasn't able to fetch addresses from storage")
return
5 years ago
}
}
// GetCirculatingSupply serves /circulating-supply end-point.
func (s *Service) GetCirculatingSupply(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
timestamp := time.Now().Unix()
circulatingSupply := reward.PercentageForTimeStamp(timestamp).Mul(numeric.NewDec(totalSupply))
if err := json.NewEncoder(w).Encode(circulatingSupply); err != nil {
utils.Logger().Warn().Msg("cannot JSON-encode circulating supply")
w.WriteHeader(http.StatusInternalServerError)
}
6 years ago
}
// GetTotalSupply serves /total-supply end-point.
func (s *Service) GetTotalSupply(w http.ResponseWriter, r *http.Request) {
6 years ago
w.Header().Set("Content-Type", "application/json")
if err := json.NewEncoder(w).Encode(totalSupply); err != nil {
utils.Logger().Warn().Msg("cannot JSON-encode total supply")
w.WriteHeader(http.StatusInternalServerError)
}
}
// GetNodeSync returns status code 500 if node is not in sync
func (s *Service) GetNodeSync(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
sync := !s.stateSync.IsOutOfSync(s.blockchain, false)
if !sync {
w.WriteHeader(http.StatusTeapot)
}
if err := json.NewEncoder(w).Encode(sync); err != nil {
utils.Logger().Warn().Msg("cannot JSON-encode total supply")
w.WriteHeader(http.StatusInternalServerError)
}
}
# This is a combination of 16 commits. # This is the 1st commit message: Fix travis # This is the commit message #2: [cleanup] remove is_genesis flag Signed-off-by: Leo Chen <leo@harmony.one> # This is the commit message #3: [nodetype] add nodetype to replace is_explorer Signed-off-by: Leo Chen <leo@harmony.one> # This is the commit message #4: fix beacon sync channel blocking issue # This is the commit message #5: use lastMileMux to protect lastMileBlock queue to avoid potential blocking; use sleep instead of ticker # This is the commit message #6: Fix the beacon committee check # This is the commit message #7: [release] add release action to go_executable_build.sh Signed-off-by: Leo Chen <leo@harmony.one> # This is the commit message #8: [node.sh] add -d option -d download only Signed-off-by: Leo Chen <leo@harmony.one> # This is the commit message #9: [node.sh] add -T node_type option -T node_type support node type (validator/explorer) Signed-off-by: Leo Chen <leo@harmony.one> # This is the commit message #10: [node.sh] backward compatible with older harmony binary Signed-off-by: Leo Chen <leo@harmony.one> # This is the commit message #11: [node.sh] support -i shard_id option -i shard_id specify shard_id, this is applicable only to explorer node Signed-off-by: Leo Chen <leo@harmony.one> # This is the commit message #12: Revisited api # This is the commit message #13: Fix rpc integration # This is the commit message #14: address some minor issues in comments and code # This is the commit message #15: addressed comments on others' buckets # This is the commit message #16: Add Global Access to OS Temp Directory Variable and Move DHT Files Into Temp Directory
5 years ago
// NotifyService notify service.
func (s *Service) NotifyService(params map[string]interface{}) {}
// SetMessageChan sets up message channel to service.
func (s *Service) SetMessageChan(messageChan chan *msg_pb.Message) {
s.messageChan = messageChan
}
// APIs for the services.
func (s *Service) APIs() []rpc.API {
return nil
}