github.com/aquanetwork/aquachain@v1.7.8/rpc/types.go (about) 1 // Copyright 2015 The aquachain Authors 2 // This file is part of the aquachain library. 3 // 4 // The aquachain 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 aquachain 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 aquachain 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 "gitlab.com/aquachain/aquachain/common/hexutil" 27 "gopkg.in/fatih/set.v0" 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 *set.Set 77 } 78 79 // rpcRequest represents a raw incoming RPC request 80 type rpcRequest struct { 81 service string 82 method string 83 id interface{} 84 isPubSub bool 85 params interface{} 86 err Error // invalid batch element 87 } 88 89 // Error wraps RPC errors, which contain an error code in addition to the message. 90 type Error interface { 91 Error() string // returns the message 92 ErrorCode() int // returns the code 93 } 94 95 // ServerCodec implements reading, parsing and writing RPC messages for the server side of 96 // a RPC session. Implementations must be go-routine safe since the codec can be called in 97 // multiple go-routines concurrently. 98 type ServerCodec interface { 99 // Read next request 100 ReadRequestHeaders() ([]rpcRequest, bool, Error) 101 // Parse request argument to the given types 102 ParseRequestArguments(argTypes []reflect.Type, params interface{}) ([]reflect.Value, Error) 103 // Assemble success response, expects response id and payload 104 CreateResponse(id interface{}, reply interface{}) interface{} 105 // Assemble error response, expects response id and error 106 CreateErrorResponse(id interface{}, err Error) interface{} 107 // Assemble error response with extra information about the error through info 108 CreateErrorResponseWithInfo(id interface{}, err Error, info interface{}) interface{} 109 // Create notification response 110 CreateNotification(id, namespace string, event interface{}) interface{} 111 // Write msg to client. 112 Write(msg interface{}) error 113 // Close underlying data stream 114 Close() 115 // Closed when underlying connection is closed 116 Closed() <-chan interface{} 117 } 118 119 type BlockNumber int64 120 121 const ( 122 PendingBlockNumber = BlockNumber(-2) 123 LatestBlockNumber = BlockNumber(-1) 124 EarliestBlockNumber = BlockNumber(0) 125 ) 126 127 // UnmarshalJSON parses the given JSON fragment into a BlockNumber. It supports: 128 // - "latest", "earliest" or "pending" as string arguments 129 // - the block number 130 // Returned errors: 131 // - an invalid block number error when the given argument isn't a known strings 132 // - an out of range error when the given block number is either too little or too large 133 func (bn *BlockNumber) UnmarshalJSON(data []byte) error { 134 input := strings.TrimSpace(string(data)) 135 if len(input) >= 2 && input[0] == '"' && input[len(input)-1] == '"' { 136 input = input[1 : len(input)-1] 137 } 138 139 switch input { 140 case "earliest": 141 *bn = EarliestBlockNumber 142 return nil 143 case "latest": 144 *bn = LatestBlockNumber 145 return nil 146 case "pending": 147 *bn = PendingBlockNumber 148 return nil 149 } 150 151 blckNum, err := hexutil.DecodeUint64(input) 152 if err != nil { 153 return err 154 } 155 if blckNum > math.MaxInt64 { 156 return fmt.Errorf("Blocknumber too high") 157 } 158 159 *bn = BlockNumber(blckNum) 160 return nil 161 } 162 163 func (bn BlockNumber) Int64() int64 { 164 return (int64)(bn) 165 }