github.com/kisexp/xdchain@v0.0.0-20211206025815-490d6b732aa7/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 "context" 21 "encoding/json" 22 "fmt" 23 "math" 24 "strings" 25 26 "github.com/kisexp/xdchain/common" 27 "github.com/kisexp/xdchain/common/hexutil" 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 // Error wraps RPC errors, which contain an error code in addition to the message. 39 type Error interface { 40 Error() string // returns the message 41 ErrorCode() int // returns the code 42 } 43 44 // A DataError contains some data in addition to the error message. 45 type DataError interface { 46 Error() string // returns the message 47 ErrorData() interface{} // returns the error data 48 } 49 50 // ServerCodec implements reading, parsing and writing RPC messages for the server side of 51 // a RPC session. Implementations must be go-routine safe since the codec can be called in 52 // multiple go-routines concurrently. 53 // Quorum: 54 // As ServerCodec is used in both client and server implementation, we extend it with the interfaces 55 // securityContextConfigurer & SecurityContextResolver to hold authorization-related information 56 // which is then used by rpc/handler to enforce the security 57 type ServerCodec interface { 58 readBatch() (msgs []*jsonrpcMessage, isBatch bool, err error) 59 close() 60 jsonWriter 61 securityContextConfigurer 62 SecurityContextResolver 63 } 64 65 // jsonWriter can write JSON messages to its underlying connection. 66 // Implementations must be safe for concurrent use. 67 type jsonWriter interface { 68 writeJSON(context.Context, interface{}) error 69 // Closed returns a channel which is closed when the connection is closed. 70 closed() <-chan interface{} 71 // RemoteAddr returns the peer address of the connection. 72 remoteAddr() string 73 } 74 75 type BlockNumber int64 76 77 const ( 78 PendingBlockNumber = BlockNumber(-2) 79 LatestBlockNumber = BlockNumber(-1) 80 EarliestBlockNumber = BlockNumber(0) 81 ) 82 83 // UnmarshalJSON parses the given JSON fragment into a BlockNumber. It supports: 84 // - "latest", "earliest" or "pending" as string arguments 85 // - the block number 86 // Returned errors: 87 // - an invalid block number error when the given argument isn't a known strings 88 // - an out of range error when the given block number is either too little or too large 89 func (bn *BlockNumber) UnmarshalJSON(data []byte) error { 90 input := strings.TrimSpace(string(data)) 91 if len(input) >= 2 && input[0] == '"' && input[len(input)-1] == '"' { 92 input = input[1 : len(input)-1] 93 } 94 95 switch input { 96 case "earliest": 97 *bn = EarliestBlockNumber 98 return nil 99 case "latest": 100 *bn = LatestBlockNumber 101 return nil 102 case "pending": 103 *bn = PendingBlockNumber 104 return nil 105 } 106 107 blckNum, err := hexutil.DecodeUint64(input) 108 if err != nil { 109 return err 110 } 111 if blckNum > math.MaxInt64 { 112 return fmt.Errorf("block number larger than int64") 113 } 114 *bn = BlockNumber(blckNum) 115 return nil 116 } 117 118 func (bn BlockNumber) Int64() int64 { 119 return (int64)(bn) 120 } 121 122 type BlockNumberOrHash struct { 123 BlockNumber *BlockNumber `json:"blockNumber,omitempty"` 124 BlockHash *common.Hash `json:"blockHash,omitempty"` 125 RequireCanonical bool `json:"requireCanonical,omitempty"` 126 } 127 128 func (bnh *BlockNumberOrHash) UnmarshalJSON(data []byte) error { 129 type erased BlockNumberOrHash 130 e := erased{} 131 err := json.Unmarshal(data, &e) 132 if err == nil { 133 if e.BlockNumber != nil && e.BlockHash != nil { 134 return fmt.Errorf("cannot specify both BlockHash and BlockNumber, choose one or the other") 135 } 136 bnh.BlockNumber = e.BlockNumber 137 bnh.BlockHash = e.BlockHash 138 bnh.RequireCanonical = e.RequireCanonical 139 return nil 140 } 141 var input string 142 err = json.Unmarshal(data, &input) 143 if err != nil { 144 return err 145 } 146 switch input { 147 case "earliest": 148 bn := EarliestBlockNumber 149 bnh.BlockNumber = &bn 150 return nil 151 case "latest": 152 bn := LatestBlockNumber 153 bnh.BlockNumber = &bn 154 return nil 155 case "pending": 156 bn := PendingBlockNumber 157 bnh.BlockNumber = &bn 158 return nil 159 default: 160 if len(input) == 66 { 161 hash := common.Hash{} 162 err := hash.UnmarshalText([]byte(input)) 163 if err != nil { 164 return err 165 } 166 bnh.BlockHash = &hash 167 return nil 168 } else { 169 blckNum, err := hexutil.DecodeUint64(input) 170 if err != nil { 171 return err 172 } 173 if blckNum > math.MaxInt64 { 174 return fmt.Errorf("blocknumber too high") 175 } 176 bn := BlockNumber(blckNum) 177 bnh.BlockNumber = &bn 178 return nil 179 } 180 } 181 } 182 183 func (bnh *BlockNumberOrHash) Number() (BlockNumber, bool) { 184 if bnh.BlockNumber != nil { 185 return *bnh.BlockNumber, true 186 } 187 return BlockNumber(0), false 188 } 189 190 func (bnh *BlockNumberOrHash) Hash() (common.Hash, bool) { 191 if bnh.BlockHash != nil { 192 return *bnh.BlockHash, true 193 } 194 return common.Hash{}, false 195 } 196 197 func BlockNumberOrHashWithNumber(blockNr BlockNumber) BlockNumberOrHash { 198 return BlockNumberOrHash{ 199 BlockNumber: &blockNr, 200 BlockHash: nil, 201 RequireCanonical: false, 202 } 203 } 204 205 func BlockNumberOrHashWithHash(hash common.Hash, canonical bool) BlockNumberOrHash { 206 return BlockNumberOrHash{ 207 BlockNumber: nil, 208 BlockHash: &hash, 209 RequireCanonical: canonical, 210 } 211 }