github.com/cheng762/PlatON-Go@v1.8.17-0.20190529111256-7deff2d7be26/mobile/types.go (about) 1 // Copyright 2016 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 // Contains all the wrappers from the core/types package. 18 19 package geth 20 21 import ( 22 "encoding/json" 23 "errors" 24 "fmt" 25 26 "github.com/PlatONnetwork/PlatON-Go/common" 27 "github.com/PlatONnetwork/PlatON-Go/core/types" 28 "github.com/PlatONnetwork/PlatON-Go/rlp" 29 whisper "github.com/PlatONnetwork/PlatON-Go/whisper/whisperv6" 30 ) 31 32 // A Nonce is a 64-bit hash which proves (combined with the mix-hash) that 33 // a sufficient amount of computation has been carried out on a block. 34 type Nonce struct { 35 nonce types.BlockNonce 36 } 37 38 // GetBytes retrieves the byte representation of the block nonce. 39 func (n *Nonce) GetBytes() []byte { 40 return n.nonce[:] 41 } 42 43 // GetHex retrieves the hex string representation of the block nonce. 44 func (n *Nonce) GetHex() string { 45 return fmt.Sprintf("0x%x", n.nonce[:]) 46 } 47 48 // Bloom represents a 256 bit bloom filter. 49 type Bloom struct { 50 bloom types.Bloom 51 } 52 53 // GetBytes retrieves the byte representation of the bloom filter. 54 func (b *Bloom) GetBytes() []byte { 55 return b.bloom[:] 56 } 57 58 // GetHex retrieves the hex string representation of the bloom filter. 59 func (b *Bloom) GetHex() string { 60 return fmt.Sprintf("0x%x", b.bloom[:]) 61 } 62 63 // Header represents a block header in the Ethereum blockchain. 64 type Header struct { 65 header *types.Header 66 } 67 68 // NewHeaderFromRLP parses a header from an RLP data dump. 69 func NewHeaderFromRLP(data []byte) (*Header, error) { 70 h := &Header{ 71 header: new(types.Header), 72 } 73 if err := rlp.DecodeBytes(common.CopyBytes(data), h.header); err != nil { 74 return nil, err 75 } 76 return h, nil 77 } 78 79 // EncodeRLP encodes a header into an RLP data dump. 80 func (h *Header) EncodeRLP() ([]byte, error) { 81 return rlp.EncodeToBytes(h.header) 82 } 83 84 // NewHeaderFromJSON parses a header from a JSON data dump. 85 func NewHeaderFromJSON(data string) (*Header, error) { 86 h := &Header{ 87 header: new(types.Header), 88 } 89 if err := json.Unmarshal([]byte(data), h.header); err != nil { 90 return nil, err 91 } 92 return h, nil 93 } 94 95 // EncodeJSON encodes a header into a JSON data dump. 96 func (h *Header) EncodeJSON() (string, error) { 97 data, err := json.Marshal(h.header) 98 return string(data), err 99 } 100 101 func (h *Header) GetParentHash() *Hash { return &Hash{h.header.ParentHash} } 102 func (h *Header) GetUncleHash() *Hash { return &Hash{h.header.UncleHash} } 103 func (h *Header) GetCoinbase() *Address { return &Address{h.header.Coinbase} } 104 func (h *Header) GetRoot() *Hash { return &Hash{h.header.Root} } 105 func (h *Header) GetTxHash() *Hash { return &Hash{h.header.TxHash} } 106 func (h *Header) GetReceiptHash() *Hash { return &Hash{h.header.ReceiptHash} } 107 func (h *Header) GetBloom() *Bloom { return &Bloom{h.header.Bloom} } 108 func (h *Header) GetNumber() int64 { return h.header.Number.Int64() } 109 func (h *Header) GetGasLimit() int64 { return int64(h.header.GasLimit) } 110 func (h *Header) GetGasUsed() int64 { return int64(h.header.GasUsed) } 111 func (h *Header) GetTime() int64 { return h.header.Time.Int64() } 112 func (h *Header) GetExtra() []byte { return h.header.Extra } 113 func (h *Header) GetMixDigest() *Hash { return &Hash{h.header.MixDigest} } 114 func (h *Header) GetNonce() *Nonce { return &Nonce{h.header.Nonce} } 115 func (h *Header) GetHash() *Hash { return &Hash{h.header.Hash()} } 116 117 // Headers represents a slice of headers. 118 type Headers struct{ headers []*types.Header } 119 120 // Size returns the number of headers in the slice. 121 func (h *Headers) Size() int { 122 return len(h.headers) 123 } 124 125 // Get returns the header at the given index from the slice. 126 func (h *Headers) Get(index int) (header *Header, _ error) { 127 if index < 0 || index >= len(h.headers) { 128 return nil, errors.New("index out of bounds") 129 } 130 return &Header{h.headers[index]}, nil 131 } 132 133 // Block represents an entire block in the Ethereum blockchain. 134 type Block struct { 135 block *types.Block 136 } 137 138 // NewBlockFromRLP parses a block from an RLP data dump. 139 func NewBlockFromRLP(data []byte) (*Block, error) { 140 b := &Block{ 141 block: new(types.Block), 142 } 143 if err := rlp.DecodeBytes(common.CopyBytes(data), b.block); err != nil { 144 return nil, err 145 } 146 return b, nil 147 } 148 149 // EncodeRLP encodes a block into an RLP data dump. 150 func (b *Block) EncodeRLP() ([]byte, error) { 151 return rlp.EncodeToBytes(b.block) 152 } 153 154 // NewBlockFromJSON parses a block from a JSON data dump. 155 func NewBlockFromJSON(data string) (*Block, error) { 156 b := &Block{ 157 block: new(types.Block), 158 } 159 if err := json.Unmarshal([]byte(data), b.block); err != nil { 160 return nil, err 161 } 162 return b, nil 163 } 164 165 // EncodeJSON encodes a block into a JSON data dump. 166 func (b *Block) EncodeJSON() (string, error) { 167 data, err := json.Marshal(b.block) 168 return string(data), err 169 } 170 171 func (b *Block) GetParentHash() *Hash { return &Hash{b.block.ParentHash()} } 172 func (b *Block) GetUncleHash() *Hash { return &Hash{b.block.UncleHash()} } 173 func (b *Block) GetCoinbase() *Address { return &Address{b.block.Coinbase()} } 174 func (b *Block) GetRoot() *Hash { return &Hash{b.block.Root()} } 175 func (b *Block) GetTxHash() *Hash { return &Hash{b.block.TxHash()} } 176 func (b *Block) GetReceiptHash() *Hash { return &Hash{b.block.ReceiptHash()} } 177 func (b *Block) GetBloom() *Bloom { return &Bloom{b.block.Bloom()} } 178 func (b *Block) GetNumber() int64 { return b.block.Number().Int64() } 179 func (b *Block) GetGasLimit() int64 { return int64(b.block.GasLimit()) } 180 func (b *Block) GetGasUsed() int64 { return int64(b.block.GasUsed()) } 181 func (b *Block) GetTime() int64 { return b.block.Time().Int64() } 182 func (b *Block) GetExtra() []byte { return b.block.Extra() } 183 func (b *Block) GetMixDigest() *Hash { return &Hash{b.block.MixDigest()} } 184 func (b *Block) GetNonce() int64 { return int64(b.block.Nonce()) } 185 func (b *Block) GetHash() *Hash { return &Hash{b.block.Hash()} } 186 func (b *Block) GetHeader() *Header { return &Header{b.block.Header()} } 187 func (b *Block) GetUncles() *Headers { return &Headers{b.block.Uncles()} } 188 func (b *Block) GetTransactions() *Transactions { return &Transactions{b.block.Transactions()} } 189 func (b *Block) GetTransaction(hash *Hash) *Transaction { 190 return &Transaction{b.block.Transaction(hash.hash)} 191 } 192 193 // Transaction represents a single Ethereum transaction. 194 type Transaction struct { 195 tx *types.Transaction 196 } 197 198 // NewTransaction creates a new transaction with the given properties. 199 func NewTransaction(nonce int64, to *Address, amount *BigInt, gasLimit int64, gasPrice *BigInt, data []byte) *Transaction { 200 return &Transaction{types.NewTransaction(uint64(nonce), to.address, amount.bigint, uint64(gasLimit), gasPrice.bigint, common.CopyBytes(data))} 201 } 202 203 // NewTransactionFromRLP parses a transaction from an RLP data dump. 204 func NewTransactionFromRLP(data []byte) (*Transaction, error) { 205 tx := &Transaction{ 206 tx: new(types.Transaction), 207 } 208 if err := rlp.DecodeBytes(common.CopyBytes(data), tx.tx); err != nil { 209 return nil, err 210 } 211 return tx, nil 212 } 213 214 // EncodeRLP encodes a transaction into an RLP data dump. 215 func (tx *Transaction) EncodeRLP() ([]byte, error) { 216 return rlp.EncodeToBytes(tx.tx) 217 } 218 219 // NewTransactionFromJSON parses a transaction from a JSON data dump. 220 func NewTransactionFromJSON(data string) (*Transaction, error) { 221 tx := &Transaction{ 222 tx: new(types.Transaction), 223 } 224 if err := json.Unmarshal([]byte(data), tx.tx); err != nil { 225 return nil, err 226 } 227 return tx, nil 228 } 229 230 // EncodeJSON encodes a transaction into a JSON data dump. 231 func (tx *Transaction) EncodeJSON() (string, error) { 232 data, err := json.Marshal(tx.tx) 233 return string(data), err 234 } 235 236 func (tx *Transaction) GetData() []byte { return tx.tx.Data() } 237 func (tx *Transaction) GetGas() int64 { return int64(tx.tx.Gas()) } 238 func (tx *Transaction) GetGasPrice() *BigInt { return &BigInt{tx.tx.GasPrice()} } 239 func (tx *Transaction) GetValue() *BigInt { return &BigInt{tx.tx.Value()} } 240 func (tx *Transaction) GetNonce() int64 { return int64(tx.tx.Nonce()) } 241 242 func (tx *Transaction) GetHash() *Hash { return &Hash{tx.tx.Hash()} } 243 func (tx *Transaction) GetCost() *BigInt { return &BigInt{tx.tx.Cost()} } 244 245 // Deprecated: GetSigHash cannot know which signer to use. 246 func (tx *Transaction) GetSigHash() *Hash { return &Hash{types.HomesteadSigner{}.Hash(tx.tx)} } 247 248 // Deprecated: use EthereumClient.TransactionSender 249 func (tx *Transaction) GetFrom(chainID *BigInt) (address *Address, _ error) { 250 var signer types.Signer = types.HomesteadSigner{} 251 if chainID != nil { 252 signer = types.NewEIP155Signer(chainID.bigint) 253 } 254 from, err := types.Sender(signer, tx.tx) 255 return &Address{from}, err 256 } 257 258 func (tx *Transaction) GetTo() *Address { 259 if to := tx.tx.To(); to != nil { 260 return &Address{*to} 261 } 262 return nil 263 } 264 265 func (tx *Transaction) WithSignature(sig []byte, chainID *BigInt) (signedTx *Transaction, _ error) { 266 var signer types.Signer = types.HomesteadSigner{} 267 if chainID != nil { 268 signer = types.NewEIP155Signer(chainID.bigint) 269 } 270 rawTx, err := tx.tx.WithSignature(signer, common.CopyBytes(sig)) 271 return &Transaction{rawTx}, err 272 } 273 274 // Transactions represents a slice of transactions. 275 type Transactions struct{ txs types.Transactions } 276 277 // Size returns the number of transactions in the slice. 278 func (txs *Transactions) Size() int { 279 return len(txs.txs) 280 } 281 282 // Get returns the transaction at the given index from the slice. 283 func (txs *Transactions) Get(index int) (tx *Transaction, _ error) { 284 if index < 0 || index >= len(txs.txs) { 285 return nil, errors.New("index out of bounds") 286 } 287 return &Transaction{txs.txs[index]}, nil 288 } 289 290 // Receipt represents the results of a transaction. 291 type Receipt struct { 292 receipt *types.Receipt 293 } 294 295 // NewReceiptFromRLP parses a transaction receipt from an RLP data dump. 296 func NewReceiptFromRLP(data []byte) (*Receipt, error) { 297 r := &Receipt{ 298 receipt: new(types.Receipt), 299 } 300 if err := rlp.DecodeBytes(common.CopyBytes(data), r.receipt); err != nil { 301 return nil, err 302 } 303 return r, nil 304 } 305 306 // EncodeRLP encodes a transaction receipt into an RLP data dump. 307 func (r *Receipt) EncodeRLP() ([]byte, error) { 308 return rlp.EncodeToBytes(r.receipt) 309 } 310 311 // NewReceiptFromJSON parses a transaction receipt from a JSON data dump. 312 func NewReceiptFromJSON(data string) (*Receipt, error) { 313 r := &Receipt{ 314 receipt: new(types.Receipt), 315 } 316 if err := json.Unmarshal([]byte(data), r.receipt); err != nil { 317 return nil, err 318 } 319 return r, nil 320 } 321 322 // EncodeJSON encodes a transaction receipt into a JSON data dump. 323 func (r *Receipt) EncodeJSON() (string, error) { 324 data, err := rlp.EncodeToBytes(r.receipt) 325 return string(data), err 326 } 327 328 func (r *Receipt) GetStatus() int { return int(r.receipt.Status) } 329 func (r *Receipt) GetPostState() []byte { return r.receipt.PostState } 330 func (r *Receipt) GetCumulativeGasUsed() int64 { return int64(r.receipt.CumulativeGasUsed) } 331 func (r *Receipt) GetBloom() *Bloom { return &Bloom{r.receipt.Bloom} } 332 func (r *Receipt) GetLogs() *Logs { return &Logs{r.receipt.Logs} } 333 func (r *Receipt) GetTxHash() *Hash { return &Hash{r.receipt.TxHash} } 334 func (r *Receipt) GetContractAddress() *Address { return &Address{r.receipt.ContractAddress} } 335 func (r *Receipt) GetGasUsed() int64 { return int64(r.receipt.GasUsed) } 336 337 // Info represents a diagnostic information about the whisper node. 338 type Info struct { 339 info *whisper.Info 340 } 341 342 // NewMessage represents a new whisper message that is posted through the RPC. 343 type NewMessage struct { 344 newMessage *whisper.NewMessage 345 } 346 347 func NewNewMessage() *NewMessage { 348 nm := &NewMessage{ 349 newMessage: new(whisper.NewMessage), 350 } 351 return nm 352 } 353 354 func (nm *NewMessage) GetSymKeyID() string { return nm.newMessage.SymKeyID } 355 func (nm *NewMessage) SetSymKeyID(symKeyID string) { nm.newMessage.SymKeyID = symKeyID } 356 func (nm *NewMessage) GetPublicKey() []byte { return nm.newMessage.PublicKey } 357 func (nm *NewMessage) SetPublicKey(publicKey []byte) { 358 nm.newMessage.PublicKey = common.CopyBytes(publicKey) 359 } 360 func (nm *NewMessage) GetSig() string { return nm.newMessage.Sig } 361 func (nm *NewMessage) SetSig(sig string) { nm.newMessage.Sig = sig } 362 func (nm *NewMessage) GetTTL() int64 { return int64(nm.newMessage.TTL) } 363 func (nm *NewMessage) SetTTL(ttl int64) { nm.newMessage.TTL = uint32(ttl) } 364 func (nm *NewMessage) GetPayload() []byte { return nm.newMessage.Payload } 365 func (nm *NewMessage) SetPayload(payload []byte) { nm.newMessage.Payload = common.CopyBytes(payload) } 366 func (nm *NewMessage) GetPowTime() int64 { return int64(nm.newMessage.PowTime) } 367 func (nm *NewMessage) SetPowTime(powTime int64) { nm.newMessage.PowTime = uint32(powTime) } 368 func (nm *NewMessage) GetPowTarget() float64 { return nm.newMessage.PowTarget } 369 func (nm *NewMessage) SetPowTarget(powTarget float64) { nm.newMessage.PowTarget = powTarget } 370 func (nm *NewMessage) GetTargetPeer() string { return nm.newMessage.TargetPeer } 371 func (nm *NewMessage) SetTargetPeer(targetPeer string) { nm.newMessage.TargetPeer = targetPeer } 372 func (nm *NewMessage) GetTopic() []byte { return nm.newMessage.Topic[:] } 373 func (nm *NewMessage) SetTopic(topic []byte) { nm.newMessage.Topic = whisper.BytesToTopic(topic) } 374 375 // Message represents a whisper message. 376 type Message struct { 377 message *whisper.Message 378 } 379 380 func (m *Message) GetSig() []byte { return m.message.Sig } 381 func (m *Message) GetTTL() int64 { return int64(m.message.TTL) } 382 func (m *Message) GetTimestamp() int64 { return int64(m.message.Timestamp) } 383 func (m *Message) GetPayload() []byte { return m.message.Payload } 384 func (m *Message) GetPoW() float64 { return m.message.PoW } 385 func (m *Message) GetHash() []byte { return m.message.Hash } 386 func (m *Message) GetDst() []byte { return m.message.Dst } 387 388 // Messages represents an array of messages. 389 type Messages struct { 390 messages []*whisper.Message 391 } 392 393 // Size returns the number of messages in the slice. 394 func (m *Messages) Size() int { 395 return len(m.messages) 396 } 397 398 // Get returns the message at the given index from the slice. 399 func (m *Messages) Get(index int) (message *Message, _ error) { 400 if index < 0 || index >= len(m.messages) { 401 return nil, errors.New("index out of bounds") 402 } 403 return &Message{m.messages[index]}, nil 404 } 405 406 // Criteria holds various filter options for inbound messages. 407 type Criteria struct { 408 criteria *whisper.Criteria 409 } 410 411 func NewCriteria(topic []byte) *Criteria { 412 c := &Criteria{ 413 criteria: new(whisper.Criteria), 414 } 415 encodedTopic := whisper.BytesToTopic(topic) 416 c.criteria.Topics = []whisper.TopicType{encodedTopic} 417 return c 418 } 419 420 func (c *Criteria) GetSymKeyID() string { return c.criteria.SymKeyID } 421 func (c *Criteria) SetSymKeyID(symKeyID string) { c.criteria.SymKeyID = symKeyID } 422 func (c *Criteria) GetPrivateKeyID() string { return c.criteria.PrivateKeyID } 423 func (c *Criteria) SetPrivateKeyID(privateKeyID string) { c.criteria.PrivateKeyID = privateKeyID } 424 func (c *Criteria) GetSig() []byte { return c.criteria.Sig } 425 func (c *Criteria) SetSig(sig []byte) { c.criteria.Sig = common.CopyBytes(sig) } 426 func (c *Criteria) GetMinPow() float64 { return c.criteria.MinPow } 427 func (c *Criteria) SetMinPow(pow float64) { c.criteria.MinPow = pow }