github.com/0xsequence/ethkit@v1.25.0/go-ethereum/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 "strconv" 25 "strings" 26 27 "github.com/0xsequence/ethkit/go-ethereum/common" 28 "github.com/0xsequence/ethkit/go-ethereum/common/hexutil" 29 ) 30 31 // API describes the set of methods offered over the RPC interface 32 type API struct { 33 Namespace string // namespace under which the rpc methods of Service are exposed 34 Version string // deprecated - this field is no longer used, but retained for compatibility 35 Service interface{} // receiver instance which holds the methods 36 Public bool // deprecated - this field is no longer used, but retained for compatibility 37 Authenticated bool // whether the api should only be available behind authentication. 38 } 39 40 // ServerCodec implements reading, parsing and writing RPC messages for the server side of 41 // a RPC session. Implementations must be go-routine safe since the codec can be called in 42 // multiple go-routines concurrently. 43 type ServerCodec interface { 44 peerInfo() PeerInfo 45 readBatch() (msgs []*jsonrpcMessage, isBatch bool, err error) 46 close() 47 48 jsonWriter 49 } 50 51 // jsonWriter can write JSON messages to its underlying connection. 52 // Implementations must be safe for concurrent use. 53 type jsonWriter interface { 54 writeJSON(context.Context, interface{}) error 55 // Closed returns a channel which is closed when the connection is closed. 56 closed() <-chan interface{} 57 // RemoteAddr returns the peer address of the connection. 58 remoteAddr() string 59 } 60 61 type BlockNumber int64 62 63 const ( 64 SafeBlockNumber = BlockNumber(-4) 65 FinalizedBlockNumber = BlockNumber(-3) 66 PendingBlockNumber = BlockNumber(-2) 67 LatestBlockNumber = BlockNumber(-1) 68 EarliestBlockNumber = BlockNumber(0) 69 ) 70 71 // UnmarshalJSON parses the given JSON fragment into a BlockNumber. It supports: 72 // - "latest", "earliest" or "pending" as string arguments 73 // - the block number 74 // Returned errors: 75 // - an invalid block number error when the given argument isn't a known strings 76 // - an out of range error when the given block number is either too little or too large 77 func (bn *BlockNumber) UnmarshalJSON(data []byte) error { 78 input := strings.TrimSpace(string(data)) 79 if len(input) >= 2 && input[0] == '"' && input[len(input)-1] == '"' { 80 input = input[1 : len(input)-1] 81 } 82 83 switch input { 84 case "earliest": 85 *bn = EarliestBlockNumber 86 return nil 87 case "latest": 88 *bn = LatestBlockNumber 89 return nil 90 case "pending": 91 *bn = PendingBlockNumber 92 return nil 93 case "finalized": 94 *bn = FinalizedBlockNumber 95 return nil 96 case "safe": 97 *bn = SafeBlockNumber 98 return nil 99 } 100 101 blckNum, err := hexutil.DecodeUint64(input) 102 if err != nil { 103 return err 104 } 105 if blckNum > math.MaxInt64 { 106 return fmt.Errorf("block number larger than int64") 107 } 108 *bn = BlockNumber(blckNum) 109 return nil 110 } 111 112 // MarshalText implements encoding.TextMarshaler. It marshals: 113 // - "latest", "earliest" or "pending" as strings 114 // - other numbers as hex 115 func (bn BlockNumber) MarshalText() ([]byte, error) { 116 switch bn { 117 case EarliestBlockNumber: 118 return []byte("earliest"), nil 119 case LatestBlockNumber: 120 return []byte("latest"), nil 121 case PendingBlockNumber: 122 return []byte("pending"), nil 123 case FinalizedBlockNumber: 124 return []byte("finalized"), nil 125 case SafeBlockNumber: 126 return []byte("safe"), nil 127 default: 128 return hexutil.Uint64(bn).MarshalText() 129 } 130 } 131 132 func (bn BlockNumber) Int64() int64 { 133 return (int64)(bn) 134 } 135 136 type BlockNumberOrHash struct { 137 BlockNumber *BlockNumber `json:"blockNumber,omitempty"` 138 BlockHash *common.Hash `json:"blockHash,omitempty"` 139 RequireCanonical bool `json:"requireCanonical,omitempty"` 140 } 141 142 func (bnh *BlockNumberOrHash) UnmarshalJSON(data []byte) error { 143 type erased BlockNumberOrHash 144 e := erased{} 145 err := json.Unmarshal(data, &e) 146 if err == nil { 147 if e.BlockNumber != nil && e.BlockHash != nil { 148 return fmt.Errorf("cannot specify both BlockHash and BlockNumber, choose one or the other") 149 } 150 bnh.BlockNumber = e.BlockNumber 151 bnh.BlockHash = e.BlockHash 152 bnh.RequireCanonical = e.RequireCanonical 153 return nil 154 } 155 var input string 156 err = json.Unmarshal(data, &input) 157 if err != nil { 158 return err 159 } 160 switch input { 161 case "earliest": 162 bn := EarliestBlockNumber 163 bnh.BlockNumber = &bn 164 return nil 165 case "latest": 166 bn := LatestBlockNumber 167 bnh.BlockNumber = &bn 168 return nil 169 case "pending": 170 bn := PendingBlockNumber 171 bnh.BlockNumber = &bn 172 return nil 173 case "finalized": 174 bn := FinalizedBlockNumber 175 bnh.BlockNumber = &bn 176 return nil 177 case "safe": 178 bn := SafeBlockNumber 179 bnh.BlockNumber = &bn 180 return nil 181 default: 182 if len(input) == 66 { 183 hash := common.Hash{} 184 err := hash.UnmarshalText([]byte(input)) 185 if err != nil { 186 return err 187 } 188 bnh.BlockHash = &hash 189 return nil 190 } else { 191 blckNum, err := hexutil.DecodeUint64(input) 192 if err != nil { 193 return err 194 } 195 if blckNum > math.MaxInt64 { 196 return fmt.Errorf("blocknumber too high") 197 } 198 bn := BlockNumber(blckNum) 199 bnh.BlockNumber = &bn 200 return nil 201 } 202 } 203 } 204 205 func (bnh *BlockNumberOrHash) Number() (BlockNumber, bool) { 206 if bnh.BlockNumber != nil { 207 return *bnh.BlockNumber, true 208 } 209 return BlockNumber(0), false 210 } 211 212 func (bnh *BlockNumberOrHash) String() string { 213 if bnh.BlockNumber != nil { 214 return strconv.Itoa(int(*bnh.BlockNumber)) 215 } 216 if bnh.BlockHash != nil { 217 return bnh.BlockHash.String() 218 } 219 return "nil" 220 } 221 222 func (bnh *BlockNumberOrHash) Hash() (common.Hash, bool) { 223 if bnh.BlockHash != nil { 224 return *bnh.BlockHash, true 225 } 226 return common.Hash{}, false 227 } 228 229 func BlockNumberOrHashWithNumber(blockNr BlockNumber) BlockNumberOrHash { 230 return BlockNumberOrHash{ 231 BlockNumber: &blockNr, 232 BlockHash: nil, 233 RequireCanonical: false, 234 } 235 } 236 237 func BlockNumberOrHashWithHash(hash common.Hash, canonical bool) BlockNumberOrHash { 238 return BlockNumberOrHash{ 239 BlockNumber: nil, 240 BlockHash: &hash, 241 RequireCanonical: canonical, 242 } 243 } 244 245 // DecimalOrHex unmarshals a non-negative decimal or hex parameter into a uint64. 246 type DecimalOrHex uint64 247 248 // UnmarshalJSON implements json.Unmarshaler. 249 func (dh *DecimalOrHex) UnmarshalJSON(data []byte) error { 250 input := strings.TrimSpace(string(data)) 251 if len(input) >= 2 && input[0] == '"' && input[len(input)-1] == '"' { 252 input = input[1 : len(input)-1] 253 } 254 255 value, err := strconv.ParseUint(input, 10, 64) 256 if err != nil { 257 value, err = hexutil.DecodeUint64(input) 258 } 259 if err != nil { 260 return err 261 } 262 *dh = DecimalOrHex(value) 263 return nil 264 }