github.com/platonnetwork/platon-go@v0.7.6/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 "math/big" 26 27 "github.com/PlatONnetwork/PlatON-Go/common" 28 "github.com/PlatONnetwork/PlatON-Go/core/types" 29 "github.com/PlatONnetwork/PlatON-Go/rlp" 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) GetCoinbase() *Address { return &Address{h.header.Coinbase} } 103 func (h *Header) GetRoot() *Hash { return &Hash{h.header.Root} } 104 func (h *Header) GetTxHash() *Hash { return &Hash{h.header.TxHash} } 105 func (h *Header) GetReceiptHash() *Hash { return &Hash{h.header.ReceiptHash} } 106 func (h *Header) GetBloom() *Bloom { return &Bloom{h.header.Bloom} } 107 func (h *Header) GetNumber() int64 { return h.header.Number.Int64() } 108 func (h *Header) GetGasLimit() int64 { return int64(h.header.GasLimit) } 109 func (h *Header) GetGasUsed() int64 { return int64(h.header.GasUsed) } 110 func (h *Header) GetTime() int64 { return h.header.Time.Int64() } 111 func (h *Header) GetExtra() []byte { return h.header.Extra } 112 func (h *Header) GetNonce() *Nonce { return &Nonce{h.header.Nonce} } 113 func (h *Header) GetHash() *Hash { return &Hash{h.header.Hash()} } 114 115 // Headers represents a slice of headers. 116 type Headers struct{ headers []*types.Header } 117 118 // Size returns the number of headers in the slice. 119 func (h *Headers) Size() int { 120 return len(h.headers) 121 } 122 123 // Get returns the header at the given index from the slice. 124 func (h *Headers) Get(index int) (header *Header, _ error) { 125 if index < 0 || index >= len(h.headers) { 126 return nil, errors.New("index out of bounds") 127 } 128 return &Header{h.headers[index]}, nil 129 } 130 131 // Block represents an entire block in the Ethereum blockchain. 132 type Block struct { 133 block *types.Block 134 } 135 136 // NewBlockFromRLP parses a block from an RLP data dump. 137 func NewBlockFromRLP(data []byte) (*Block, error) { 138 b := &Block{ 139 block: new(types.Block), 140 } 141 if err := rlp.DecodeBytes(common.CopyBytes(data), b.block); err != nil { 142 return nil, err 143 } 144 return b, nil 145 } 146 147 // EncodeRLP encodes a block into an RLP data dump. 148 func (b *Block) EncodeRLP() ([]byte, error) { 149 return rlp.EncodeToBytes(b.block) 150 } 151 152 // NewBlockFromJSON parses a block from a JSON data dump. 153 func NewBlockFromJSON(data string) (*Block, error) { 154 b := &Block{ 155 block: new(types.Block), 156 } 157 if err := json.Unmarshal([]byte(data), b.block); err != nil { 158 return nil, err 159 } 160 return b, nil 161 } 162 163 // EncodeJSON encodes a block into a JSON data dump. 164 func (b *Block) EncodeJSON() (string, error) { 165 data, err := json.Marshal(b.block) 166 return string(data), err 167 } 168 169 func (b *Block) GetParentHash() *Hash { return &Hash{b.block.ParentHash()} } 170 func (b *Block) GetCoinbase() *Address { return &Address{b.block.Coinbase()} } 171 func (b *Block) GetRoot() *Hash { return &Hash{b.block.Root()} } 172 func (b *Block) GetTxHash() *Hash { return &Hash{b.block.TxHash()} } 173 func (b *Block) GetReceiptHash() *Hash { return &Hash{b.block.ReceiptHash()} } 174 func (b *Block) GetBloom() *Bloom { return &Bloom{b.block.Bloom()} } 175 func (b *Block) GetNumber() int64 { return b.block.Number().Int64() } 176 func (b *Block) GetGasLimit() int64 { return int64(b.block.GasLimit()) } 177 func (b *Block) GetGasUsed() int64 { return int64(b.block.GasUsed()) } 178 func (b *Block) GetTime() int64 { return b.block.Time().Int64() } 179 func (b *Block) GetExtra() []byte { return b.block.Extra() } 180 func (b *Block) GetNonce() []byte { return b.block.Nonce() } 181 func (b *Block) GetHash() *Hash { return &Hash{b.block.Hash()} } 182 func (b *Block) GetHeader() *Header { return &Header{b.block.Header()} } 183 func (b *Block) GetTransactions() *Transactions { return &Transactions{b.block.Transactions()} } 184 func (b *Block) GetTransaction(hash *Hash) *Transaction { 185 return &Transaction{b.block.Transaction(hash.hash)} 186 } 187 188 // Transaction represents a single Ethereum transaction. 189 type Transaction struct { 190 tx *types.Transaction 191 } 192 193 // NewTransaction creates a new transaction with the given properties. 194 func NewTransaction(nonce int64, to *Address, amount *BigInt, gasLimit int64, gasPrice *BigInt, data []byte) *Transaction { 195 return &Transaction{types.NewTransaction(uint64(nonce), to.address, amount.bigint, uint64(gasLimit), gasPrice.bigint, common.CopyBytes(data))} 196 } 197 198 // NewTransactionFromRLP parses a transaction from an RLP data dump. 199 func NewTransactionFromRLP(data []byte) (*Transaction, error) { 200 tx := &Transaction{ 201 tx: new(types.Transaction), 202 } 203 if err := rlp.DecodeBytes(common.CopyBytes(data), tx.tx); err != nil { 204 return nil, err 205 } 206 return tx, nil 207 } 208 209 // EncodeRLP encodes a transaction into an RLP data dump. 210 func (tx *Transaction) EncodeRLP() ([]byte, error) { 211 return rlp.EncodeToBytes(tx.tx) 212 } 213 214 // NewTransactionFromJSON parses a transaction from a JSON data dump. 215 func NewTransactionFromJSON(data string) (*Transaction, error) { 216 tx := &Transaction{ 217 tx: new(types.Transaction), 218 } 219 if err := json.Unmarshal([]byte(data), tx.tx); err != nil { 220 return nil, err 221 } 222 return tx, nil 223 } 224 225 // EncodeJSON encodes a transaction into a JSON data dump. 226 func (tx *Transaction) EncodeJSON() (string, error) { 227 data, err := json.Marshal(tx.tx) 228 return string(data), err 229 } 230 231 func (tx *Transaction) GetData() []byte { return tx.tx.Data() } 232 func (tx *Transaction) GetGas() int64 { return int64(tx.tx.Gas()) } 233 func (tx *Transaction) GetGasPrice() *BigInt { return &BigInt{tx.tx.GasPrice()} } 234 func (tx *Transaction) GetValue() *BigInt { return &BigInt{tx.tx.Value()} } 235 func (tx *Transaction) GetNonce() int64 { return int64(tx.tx.Nonce()) } 236 237 func (tx *Transaction) GetHash() *Hash { return &Hash{tx.tx.Hash()} } 238 func (tx *Transaction) GetCost() *BigInt { return &BigInt{tx.tx.Cost()} } 239 240 // Deprecated: GetSigHash cannot know which signer to use. 241 func (tx *Transaction) GetSigHash() *Hash { 242 return &Hash{types.NewEIP155Signer(new(big.Int)).Hash(tx.tx)} 243 } 244 245 // Deprecated: use EthereumClient.TransactionSender 246 func (tx *Transaction) GetFrom(chainID *BigInt) (address *Address, _ error) { 247 var signer types.Signer = types.NewEIP155Signer(new(big.Int)) 248 if chainID != nil { 249 signer = types.NewEIP155Signer(chainID.bigint) 250 } 251 from, err := types.Sender(signer, tx.tx) 252 return &Address{from}, err 253 } 254 255 func (tx *Transaction) GetTo() *Address { 256 if to := tx.tx.To(); to != nil { 257 return &Address{*to} 258 } 259 return nil 260 } 261 262 func (tx *Transaction) WithSignature(sig []byte, chainID *BigInt) (signedTx *Transaction, _ error) { 263 var signer types.Signer = types.NewEIP155Signer(new(big.Int)) 264 if chainID != nil { 265 signer = types.NewEIP155Signer(chainID.bigint) 266 } 267 rawTx, err := tx.tx.WithSignature(signer, common.CopyBytes(sig)) 268 return &Transaction{rawTx}, err 269 } 270 271 // Transactions represents a slice of transactions. 272 type Transactions struct{ txs types.Transactions } 273 274 // Size returns the number of transactions in the slice. 275 func (txs *Transactions) Size() int { 276 return len(txs.txs) 277 } 278 279 // Get returns the transaction at the given index from the slice. 280 func (txs *Transactions) Get(index int) (tx *Transaction, _ error) { 281 if index < 0 || index >= len(txs.txs) { 282 return nil, errors.New("index out of bounds") 283 } 284 return &Transaction{txs.txs[index]}, nil 285 } 286 287 // Receipt represents the results of a transaction. 288 type Receipt struct { 289 receipt *types.Receipt 290 } 291 292 // NewReceiptFromRLP parses a transaction receipt from an RLP data dump. 293 func NewReceiptFromRLP(data []byte) (*Receipt, error) { 294 r := &Receipt{ 295 receipt: new(types.Receipt), 296 } 297 if err := rlp.DecodeBytes(common.CopyBytes(data), r.receipt); err != nil { 298 return nil, err 299 } 300 return r, nil 301 } 302 303 // EncodeRLP encodes a transaction receipt into an RLP data dump. 304 func (r *Receipt) EncodeRLP() ([]byte, error) { 305 return rlp.EncodeToBytes(r.receipt) 306 } 307 308 // NewReceiptFromJSON parses a transaction receipt from a JSON data dump. 309 func NewReceiptFromJSON(data string) (*Receipt, error) { 310 r := &Receipt{ 311 receipt: new(types.Receipt), 312 } 313 if err := json.Unmarshal([]byte(data), r.receipt); err != nil { 314 return nil, err 315 } 316 return r, nil 317 } 318 319 // EncodeJSON encodes a transaction receipt into a JSON data dump. 320 func (r *Receipt) EncodeJSON() (string, error) { 321 data, err := rlp.EncodeToBytes(r.receipt) 322 return string(data), err 323 } 324 325 func (r *Receipt) GetStatus() int { return int(r.receipt.Status) } 326 func (r *Receipt) GetPostState() []byte { return r.receipt.PostState } 327 func (r *Receipt) GetCumulativeGasUsed() int64 { return int64(r.receipt.CumulativeGasUsed) } 328 func (r *Receipt) GetBloom() *Bloom { return &Bloom{r.receipt.Bloom} } 329 func (r *Receipt) GetLogs() *Logs { return &Logs{r.receipt.Logs} } 330 func (r *Receipt) GetTxHash() *Hash { return &Hash{r.receipt.TxHash} } 331 func (r *Receipt) GetContractAddress() *Address { return &Address{r.receipt.ContractAddress} } 332 func (r *Receipt) GetGasUsed() int64 { return int64(r.receipt.GasUsed) }