github.com/SmartMeshFoundation/Spectrum@v0.0.0-20220621030607-452a266fee1e/rpc/types.go (about) 1 // Copyright 2015 The Spectrum Authors 2 // This file is part of the Spectrum library. 3 // 4 // The Spectrum library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The Spectrum library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the Spectrum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package rpc 18 19 import ( 20 "fmt" 21 "math" 22 "reflect" 23 "strings" 24 "sync" 25 26 "github.com/SmartMeshFoundation/Spectrum/common/hexutil" 27 mapset "github.com/deckarep/golang-set" 28 ) 29 30 // API describes the set of methods offered over the RPC interface 31 type API struct { 32 Namespace string // namespace under which the rpc methods of Service are exposed 33 Version string // api version for DApp's 34 Service interface{} // receiver instance which holds the methods 35 Public bool // indication if the methods must be considered safe for public use 36 } 37 38 // callback is a method callback which was registered in the server 39 type callback struct { 40 rcvr reflect.Value // receiver of method 41 method reflect.Method // callback 42 argTypes []reflect.Type // input argument types 43 hasCtx bool // method's first argument is a context (not included in argTypes) 44 errPos int // err return idx, of -1 when method cannot return error 45 isSubscribe bool // indication if the callback is a subscription 46 } 47 48 // service represents a registered object 49 type service struct { 50 name string // name for service 51 typ reflect.Type // receiver type 52 callbacks callbacks // registered handlers 53 subscriptions subscriptions // available subscriptions/notifications 54 } 55 56 // serverRequest is an incoming request 57 type serverRequest struct { 58 id interface{} 59 svcname string 60 callb *callback 61 args []reflect.Value 62 isUnsubscribe bool 63 err Error 64 } 65 66 type serviceRegistry map[string]*service // collection of services 67 type callbacks map[string]*callback // collection of RPC callbacks 68 type subscriptions map[string]*callback // collection of subscription callbacks 69 70 // Server represents a RPC server 71 type Server struct { 72 services serviceRegistry 73 74 run int32 75 codecsMu sync.Mutex 76 codecs mapset.Set 77 //codecs *set.Set 78 } 79 80 // rpcRequest represents a raw incoming RPC request 81 type rpcRequest struct { 82 service string 83 method string 84 id interface{} 85 isPubSub bool 86 params interface{} 87 err Error // invalid batch element 88 } 89 90 // Error wraps RPC errors, which contain an error code in addition to the message. 91 type Error interface { 92 Error() string // returns the message 93 ErrorCode() int // returns the code 94 } 95 96 // ServerCodec implements reading, parsing and writing RPC messages for the server side of 97 // a RPC session. Implementations must be go-routine safe since the codec can be called in 98 // multiple go-routines concurrently. 99 type ServerCodec interface { 100 // Read next request 101 ReadRequestHeaders() ([]rpcRequest, bool, Error) 102 // Parse request argument to the given types 103 ParseRequestArguments(argTypes []reflect.Type, params interface{}) ([]reflect.Value, Error) 104 // Assemble success response, expects response id and payload 105 CreateResponse(id interface{}, reply interface{}) interface{} 106 // Assemble error response, expects response id and error 107 CreateErrorResponse(id interface{}, err Error) interface{} 108 // Assemble error response with extra information about the error through info 109 CreateErrorResponseWithInfo(id interface{}, err Error, info interface{}) interface{} 110 // Create notification response 111 CreateNotification(id, namespace string, event interface{}) interface{} 112 // Write msg to client. 113 Write(msg interface{}) error 114 // Close underlying data stream 115 Close() 116 // Closed when underlying connection is closed 117 Closed() <-chan interface{} 118 } 119 120 type BlockNumber int64 121 122 const ( 123 PendingBlockNumber = BlockNumber(-2) 124 LatestBlockNumber = BlockNumber(-1) 125 EarliestBlockNumber = BlockNumber(0) 126 ) 127 128 // UnmarshalJSON parses the given JSON fragment into a BlockNumber. It supports: 129 // - "latest", "earliest" or "pending" as string arguments 130 // - the block number 131 // Returned errors: 132 // - an invalid block number error when the given argument isn't a known strings 133 // - an out of range error when the given block number is either too little or too large 134 func (bn *BlockNumber) UnmarshalJSON(data []byte) error { 135 input := strings.TrimSpace(string(data)) 136 if len(input) >= 2 && input[0] == '"' && input[len(input)-1] == '"' { 137 input = input[1 : len(input)-1] 138 } 139 140 switch input { 141 case "earliest": 142 *bn = EarliestBlockNumber 143 return nil 144 case "latest": 145 *bn = LatestBlockNumber 146 return nil 147 case "pending": 148 *bn = PendingBlockNumber 149 return nil 150 } 151 152 blckNum, err := hexutil.DecodeUint64(input) 153 if err != nil { 154 return err 155 } 156 if blckNum > math.MaxInt64 { 157 return fmt.Errorf("Blocknumber too high") 158 } 159 160 *bn = BlockNumber(blckNum) 161 return nil 162 } 163 164 func (bn BlockNumber) Int64() int64 { 165 return (int64)(bn) 166 }