github.hscsec.cn/scroll-tech/go-ethereum@v1.9.7/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/ethereum/go-ethereum/common" 27 "github.com/ethereum/go-ethereum/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 // ServerCodec implements reading, parsing and writing RPC messages for the server side of 45 // a RPC session. Implementations must be go-routine safe since the codec can be called in 46 // multiple go-routines concurrently. 47 type ServerCodec interface { 48 Read() (msgs []*jsonrpcMessage, isBatch bool, err error) 49 Close() 50 jsonWriter 51 } 52 53 // jsonWriter can write JSON messages to its underlying connection. 54 // Implementations must be safe for concurrent use. 55 type jsonWriter interface { 56 Write(context.Context, interface{}) error 57 // Closed returns a channel which is closed when the connection is closed. 58 Closed() <-chan interface{} 59 // RemoteAddr returns the peer address of the connection. 60 RemoteAddr() string 61 } 62 63 type BlockNumber int64 64 65 const ( 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 } 94 95 blckNum, err := hexutil.DecodeUint64(input) 96 if err != nil { 97 return err 98 } 99 if blckNum > math.MaxInt64 { 100 return fmt.Errorf("Blocknumber too high") 101 } 102 103 *bn = BlockNumber(blckNum) 104 return nil 105 } 106 107 func (bn BlockNumber) Int64() int64 { 108 return (int64)(bn) 109 } 110 111 type BlockNumberOrHash struct { 112 BlockNumber *BlockNumber `json:"blockNumber,omitempty"` 113 BlockHash *common.Hash `json:"blockHash,omitempty"` 114 RequireCanonical bool `json:"requireCanonical,omitempty"` 115 } 116 117 func (bnh *BlockNumberOrHash) UnmarshalJSON(data []byte) error { 118 type erased BlockNumberOrHash 119 e := erased{} 120 err := json.Unmarshal(data, &e) 121 if err == nil { 122 if e.BlockNumber != nil && e.BlockHash != nil { 123 return fmt.Errorf("cannot specify both BlockHash and BlockNumber, choose one or the other") 124 } 125 bnh.BlockNumber = e.BlockNumber 126 bnh.BlockHash = e.BlockHash 127 bnh.RequireCanonical = e.RequireCanonical 128 return nil 129 } 130 var input string 131 err = json.Unmarshal(data, &input) 132 if err != nil { 133 return err 134 } 135 switch input { 136 case "earliest": 137 bn := EarliestBlockNumber 138 bnh.BlockNumber = &bn 139 return nil 140 case "latest": 141 bn := LatestBlockNumber 142 bnh.BlockNumber = &bn 143 return nil 144 case "pending": 145 bn := PendingBlockNumber 146 bnh.BlockNumber = &bn 147 return nil 148 default: 149 if len(input) == 66 { 150 hash := common.Hash{} 151 err := hash.UnmarshalText([]byte(input)) 152 if err != nil { 153 return err 154 } 155 bnh.BlockHash = &hash 156 return nil 157 } else { 158 blckNum, err := hexutil.DecodeUint64(input) 159 if err != nil { 160 return err 161 } 162 if blckNum > math.MaxInt64 { 163 return fmt.Errorf("blocknumber too high") 164 } 165 bn := BlockNumber(blckNum) 166 bnh.BlockNumber = &bn 167 return nil 168 } 169 } 170 } 171 172 func (bnh *BlockNumberOrHash) Number() (BlockNumber, bool) { 173 if bnh.BlockNumber != nil { 174 return *bnh.BlockNumber, true 175 } 176 return BlockNumber(0), false 177 } 178 179 func (bnh *BlockNumberOrHash) Hash() (common.Hash, bool) { 180 if bnh.BlockHash != nil { 181 return *bnh.BlockHash, true 182 } 183 return common.Hash{}, false 184 } 185 186 func BlockNumberOrHashWithNumber(blockNr BlockNumber) BlockNumberOrHash { 187 return BlockNumberOrHash{ 188 BlockNumber: &blockNr, 189 BlockHash: nil, 190 RequireCanonical: false, 191 } 192 } 193 194 func BlockNumberOrHashWithHash(hash common.Hash, canonical bool) BlockNumberOrHash { 195 return BlockNumberOrHash{ 196 BlockNumber: nil, 197 BlockHash: &hash, 198 RequireCanonical: canonical, 199 } 200 }