github.com/gochain-io/gochain@v2.2.26+incompatible/rpc/types.go (about) 1 // Copyright 2015 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum 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 go-ethereum 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 go-ethereum 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/gochain-io/gochain/common/hexutil" 27 ) 28 29 // API describes the set of methods offered over the RPC interface 30 type API struct { 31 Namespace string // namespace under which the rpc methods of Service are exposed 32 Version string // api version for DApp's 33 Service interface{} // receiver instance which holds the methods 34 Public bool // indication if the methods must be considered safe for public use 35 } 36 37 // callback is a method callback which was registered in the server 38 type callback struct { 39 rcvr reflect.Value // receiver of method 40 method reflect.Method // callback 41 argTypes []reflect.Type // input argument types 42 hasCtx bool // method's first argument is a context (not included in argTypes) 43 errPos int // err return idx, of -1 when method cannot return error 44 isSubscribe bool // indication if the callback is a subscription 45 } 46 47 // service represents a registered object 48 type service struct { 49 name string // name for service 50 typ reflect.Type // receiver type 51 callbacks callbacks // registered handlers 52 subscriptions subscriptions // available subscriptions/notifications 53 } 54 55 // serverRequest is an incoming request 56 type serverRequest struct { 57 id interface{} 58 svcname string 59 callb *callback 60 args []reflect.Value 61 isUnsubscribe bool 62 err Error 63 } 64 65 type serviceRegistry map[string]*service // collection of services 66 type callbacks map[string]*callback // collection of RPC callbacks 67 type subscriptions map[string]*callback // collection of subscription callbacks 68 69 // Server represents a RPC server 70 type Server struct { 71 services serviceRegistry 72 73 run int32 74 codecsMu sync.Mutex 75 codecs map[ServerCodec]struct{} 76 } 77 78 // rpcRequest represents a raw incoming RPC request 79 type rpcRequest struct { 80 service string 81 method string 82 id interface{} 83 isPubSub bool 84 params interface{} 85 err Error // invalid batch element 86 } 87 88 // Error wraps RPC errors, which contain an error code in addition to the message. 89 type Error interface { 90 Error() string // returns the message 91 ErrorCode() int // returns the code 92 } 93 94 // ServerCodec implements reading, parsing and writing RPC messages for the server side of 95 // a RPC session. Implementations must be go-routine safe since the codec can be called in 96 // multiple go-routines concurrently. 97 type ServerCodec interface { 98 // Read next request 99 ReadRequestHeaders() ([]rpcRequest, bool, Error) 100 // Parse request argument to the given types 101 ParseRequestArguments(argTypes []reflect.Type, params interface{}) ([]reflect.Value, Error) 102 // Assemble success response, expects response id and payload 103 CreateResponse(id interface{}, reply interface{}) interface{} 104 // Assemble error response, expects response id and error 105 CreateErrorResponse(id interface{}, err Error) interface{} 106 // Assemble error response with extra information about the error through info 107 CreateErrorResponseWithInfo(id interface{}, err Error, info interface{}) interface{} 108 // Create notification response 109 CreateNotification(id, namespace string, event interface{}) interface{} 110 // Write msg to client. 111 Write(msg interface{}) error 112 // Close underlying data stream 113 Close() 114 // Closed when underlying connection is closed 115 Closed() <-chan interface{} 116 } 117 118 type BlockNumber int64 119 120 const ( 121 PendingBlockNumber = BlockNumber(-2) 122 LatestBlockNumber = BlockNumber(-1) 123 EarliestBlockNumber = BlockNumber(0) 124 ) 125 126 // UnmarshalJSON parses the given JSON fragment into a BlockNumber. It supports: 127 // - "latest", "earliest" or "pending" as string arguments 128 // - the block number 129 // Returned errors: 130 // - an invalid block number error when the given argument isn't a known strings 131 // - an out of range error when the given block number is either too little or too large 132 func (bn *BlockNumber) UnmarshalJSON(data []byte) error { 133 input := strings.TrimSpace(string(data)) 134 if len(input) >= 2 && input[0] == '"' && input[len(input)-1] == '"' { 135 input = input[1 : len(input)-1] 136 } 137 138 switch input { 139 case "earliest": 140 *bn = EarliestBlockNumber 141 return nil 142 case "latest": 143 *bn = LatestBlockNumber 144 return nil 145 case "pending": 146 *bn = PendingBlockNumber 147 return nil 148 } 149 150 blckNum, err := hexutil.DecodeUint64(input) 151 if err != nil { 152 return err 153 } 154 if blckNum > math.MaxInt64 { 155 return fmt.Errorf("Blocknumber too high") 156 } 157 158 *bn = BlockNumber(blckNum) 159 return nil 160 } 161 162 func (bn BlockNumber) Int64() int64 { 163 return (int64)(bn) 164 }