types.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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. // Contains all the wrappers from the core/types package.
  17. package geth
  18. import (
  19. "encoding/json"
  20. "errors"
  21. "fmt"
  22. "github.com/ethereum/go-ethereum/common"
  23. "github.com/ethereum/go-ethereum/core/types"
  24. "github.com/ethereum/go-ethereum/rlp"
  25. )
  26. type jsonEncoder interface {
  27. EncodeJSON() (string, error)
  28. }
  29. // encodeOrError tries to encode the object into json.
  30. // If the encoding fails the resulting error is returned.
  31. func encodeOrError(encoder jsonEncoder) string {
  32. enc, err := encoder.EncodeJSON()
  33. if err != nil {
  34. return err.Error()
  35. }
  36. return enc
  37. }
  38. // A Nonce is a 64-bit hash which proves (combined with the mix-hash) that
  39. // a sufficient amount of computation has been carried out on a block.
  40. type Nonce struct {
  41. nonce types.BlockNonce
  42. }
  43. // GetBytes retrieves the byte representation of the block nonce.
  44. func (n *Nonce) GetBytes() []byte {
  45. return n.nonce[:]
  46. }
  47. // GetHex retrieves the hex string representation of the block nonce.
  48. func (n *Nonce) GetHex() string {
  49. return fmt.Sprintf("0x%x", n.nonce[:])
  50. }
  51. // String returns a printable representation of the nonce.
  52. func (n *Nonce) String() string {
  53. return n.GetHex()
  54. }
  55. // Bloom represents a 256 bit bloom filter.
  56. type Bloom struct {
  57. bloom types.Bloom
  58. }
  59. // GetBytes retrieves the byte representation of the bloom filter.
  60. func (b *Bloom) GetBytes() []byte {
  61. return b.bloom[:]
  62. }
  63. // GetHex retrieves the hex string representation of the bloom filter.
  64. func (b *Bloom) GetHex() string {
  65. return fmt.Sprintf("0x%x", b.bloom[:])
  66. }
  67. // String returns a printable representation of the bloom filter.
  68. func (b *Bloom) String() string {
  69. return b.GetHex()
  70. }
  71. // Header represents a block header in the Ethereum blockchain.
  72. type Header struct {
  73. header *types.Header
  74. }
  75. // NewHeaderFromRLP parses a header from an RLP data dump.
  76. func NewHeaderFromRLP(data []byte) (*Header, error) {
  77. h := &Header{
  78. header: new(types.Header),
  79. }
  80. if err := rlp.DecodeBytes(common.CopyBytes(data), h.header); err != nil {
  81. return nil, err
  82. }
  83. return h, nil
  84. }
  85. // EncodeRLP encodes a header into an RLP data dump.
  86. func (h *Header) EncodeRLP() ([]byte, error) {
  87. return rlp.EncodeToBytes(h.header)
  88. }
  89. // NewHeaderFromJSON parses a header from a JSON data dump.
  90. func NewHeaderFromJSON(data string) (*Header, error) {
  91. h := &Header{
  92. header: new(types.Header),
  93. }
  94. if err := json.Unmarshal([]byte(data), h.header); err != nil {
  95. return nil, err
  96. }
  97. return h, nil
  98. }
  99. // EncodeJSON encodes a header into a JSON data dump.
  100. func (h *Header) EncodeJSON() (string, error) {
  101. data, err := json.Marshal(h.header)
  102. return string(data), err
  103. }
  104. // String returns a printable representation of the header.
  105. func (h *Header) String() string {
  106. return encodeOrError(h)
  107. }
  108. func (h *Header) GetParentHash() *Hash { return &Hash{h.header.ParentHash} }
  109. func (h *Header) GetUncleHash() *Hash { return &Hash{h.header.UncleHash} }
  110. func (h *Header) GetCoinbase() *Address { return &Address{h.header.Coinbase} }
  111. func (h *Header) GetRoot() *Hash { return &Hash{h.header.Root} }
  112. func (h *Header) GetTxHash() *Hash { return &Hash{h.header.TxHash} }
  113. func (h *Header) GetReceiptHash() *Hash { return &Hash{h.header.ReceiptHash} }
  114. func (h *Header) GetBloom() *Bloom { return &Bloom{h.header.Bloom} }
  115. func (h *Header) GetDifficulty() *BigInt { return &BigInt{h.header.Difficulty} }
  116. func (h *Header) GetNumber() int64 { return h.header.Number.Int64() }
  117. func (h *Header) GetGasLimit() int64 { return int64(h.header.GasLimit) }
  118. func (h *Header) GetGasUsed() int64 { return int64(h.header.GasUsed) }
  119. func (h *Header) GetTime() int64 { return int64(h.header.Time) }
  120. func (h *Header) GetExtra() []byte { return h.header.Extra }
  121. func (h *Header) GetMixDigest() *Hash { return &Hash{h.header.MixDigest} }
  122. func (h *Header) GetNonce() *Nonce { return &Nonce{h.header.Nonce} }
  123. func (h *Header) GetHash() *Hash { return &Hash{h.header.Hash()} }
  124. // Headers represents a slice of headers.
  125. type Headers struct{ headers []*types.Header }
  126. // Size returns the number of headers in the slice.
  127. func (h *Headers) Size() int {
  128. return len(h.headers)
  129. }
  130. // Get returns the header at the given index from the slice.
  131. func (h *Headers) Get(index int) (header *Header, _ error) {
  132. if index < 0 || index >= len(h.headers) {
  133. return nil, errors.New("index out of bounds")
  134. }
  135. return &Header{h.headers[index]}, nil
  136. }
  137. // Block represents an entire block in the Ethereum blockchain.
  138. type Block struct {
  139. block *types.Block
  140. }
  141. // NewBlockFromRLP parses a block from an RLP data dump.
  142. func NewBlockFromRLP(data []byte) (*Block, error) {
  143. b := &Block{
  144. block: new(types.Block),
  145. }
  146. if err := rlp.DecodeBytes(common.CopyBytes(data), b.block); err != nil {
  147. return nil, err
  148. }
  149. return b, nil
  150. }
  151. // EncodeRLP encodes a block into an RLP data dump.
  152. func (b *Block) EncodeRLP() ([]byte, error) {
  153. return rlp.EncodeToBytes(b.block)
  154. }
  155. // NewBlockFromJSON parses a block from a JSON data dump.
  156. func NewBlockFromJSON(data string) (*Block, error) {
  157. b := &Block{
  158. block: new(types.Block),
  159. }
  160. if err := json.Unmarshal([]byte(data), b.block); err != nil {
  161. return nil, err
  162. }
  163. return b, nil
  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. // String returns a printable representation of the block.
  171. func (b *Block) String() string {
  172. return encodeOrError(b)
  173. }
  174. func (b *Block) GetParentHash() *Hash { return &Hash{b.block.ParentHash()} }
  175. func (b *Block) GetUncleHash() *Hash { return &Hash{b.block.UncleHash()} }
  176. func (b *Block) GetCoinbase() *Address { return &Address{b.block.Coinbase()} }
  177. func (b *Block) GetRoot() *Hash { return &Hash{b.block.Root()} }
  178. func (b *Block) GetTxHash() *Hash { return &Hash{b.block.TxHash()} }
  179. func (b *Block) GetReceiptHash() *Hash { return &Hash{b.block.ReceiptHash()} }
  180. func (b *Block) GetBloom() *Bloom { return &Bloom{b.block.Bloom()} }
  181. func (b *Block) GetDifficulty() *BigInt { return &BigInt{b.block.Difficulty()} }
  182. func (b *Block) GetNumber() int64 { return b.block.Number().Int64() }
  183. func (b *Block) GetGasLimit() int64 { return int64(b.block.GasLimit()) }
  184. func (b *Block) GetGasUsed() int64 { return int64(b.block.GasUsed()) }
  185. func (b *Block) GetTime() int64 { return int64(b.block.Time()) }
  186. func (b *Block) GetExtra() []byte { return b.block.Extra() }
  187. func (b *Block) GetMixDigest() *Hash { return &Hash{b.block.MixDigest()} }
  188. func (b *Block) GetNonce() int64 { return int64(b.block.Nonce()) }
  189. func (b *Block) GetHash() *Hash { return &Hash{b.block.Hash()} }
  190. func (b *Block) GetHeader() *Header { return &Header{b.block.Header()} }
  191. func (b *Block) GetUncles() *Headers { return &Headers{b.block.Uncles()} }
  192. func (b *Block) GetTransactions() *Transactions { return &Transactions{b.block.Transactions()} }
  193. func (b *Block) GetTransaction(hash *Hash) *Transaction {
  194. return &Transaction{b.block.Transaction(hash.hash)}
  195. }
  196. // Transaction represents a single Ethereum transaction.
  197. type Transaction struct {
  198. tx *types.Transaction
  199. }
  200. // NewContractCreation creates a new transaction for deploying a new contract with
  201. // the given properties.
  202. func NewContractCreation(nonce int64, amount *BigInt, gasLimit int64, gasPrice *BigInt, data []byte) *Transaction {
  203. return &Transaction{types.NewContractCreation(uint64(nonce), amount.bigint, uint64(gasLimit), gasPrice.bigint, common.CopyBytes(data))}
  204. }
  205. // NewTransaction creates a new transaction with the given properties. Contracts
  206. // can be created by transacting with a nil recipient.
  207. func NewTransaction(nonce int64, to *Address, amount *BigInt, gasLimit int64, gasPrice *BigInt, data []byte) *Transaction {
  208. if to == nil {
  209. return &Transaction{types.NewContractCreation(uint64(nonce), amount.bigint, uint64(gasLimit), gasPrice.bigint, common.CopyBytes(data))}
  210. }
  211. return &Transaction{types.NewTransaction(uint64(nonce), to.address, amount.bigint, uint64(gasLimit), gasPrice.bigint, common.CopyBytes(data))}
  212. }
  213. // NewTransactionFromRLP parses a transaction from an RLP data dump.
  214. func NewTransactionFromRLP(data []byte) (*Transaction, error) {
  215. tx := &Transaction{
  216. tx: new(types.Transaction),
  217. }
  218. if err := rlp.DecodeBytes(common.CopyBytes(data), tx.tx); err != nil {
  219. return nil, err
  220. }
  221. return tx, nil
  222. }
  223. // EncodeRLP encodes a transaction into an RLP data dump.
  224. func (tx *Transaction) EncodeRLP() ([]byte, error) {
  225. return rlp.EncodeToBytes(tx.tx)
  226. }
  227. // NewTransactionFromJSON parses a transaction from a JSON data dump.
  228. func NewTransactionFromJSON(data string) (*Transaction, error) {
  229. tx := &Transaction{
  230. tx: new(types.Transaction),
  231. }
  232. if err := json.Unmarshal([]byte(data), tx.tx); err != nil {
  233. return nil, err
  234. }
  235. return tx, nil
  236. }
  237. // EncodeJSON encodes a transaction into a JSON data dump.
  238. func (tx *Transaction) EncodeJSON() (string, error) {
  239. data, err := json.Marshal(tx.tx)
  240. return string(data), err
  241. }
  242. // String returns a printable representation of the transaction.
  243. func (tx *Transaction) String() string {
  244. return encodeOrError(tx)
  245. }
  246. func (tx *Transaction) GetData() []byte { return tx.tx.Data() }
  247. func (tx *Transaction) GetGas() int64 { return int64(tx.tx.Gas()) }
  248. func (tx *Transaction) GetGasPrice() *BigInt { return &BigInt{tx.tx.GasPrice()} }
  249. func (tx *Transaction) GetValue() *BigInt { return &BigInt{tx.tx.Value()} }
  250. func (tx *Transaction) GetNonce() int64 { return int64(tx.tx.Nonce()) }
  251. func (tx *Transaction) GetHash() *Hash { return &Hash{tx.tx.Hash()} }
  252. func (tx *Transaction) GetCost() *BigInt { return &BigInt{tx.tx.Cost()} }
  253. // Deprecated: GetSigHash cannot know which signer to use.
  254. func (tx *Transaction) GetSigHash() *Hash { return &Hash{types.HomesteadSigner{}.Hash(tx.tx)} }
  255. // Deprecated: use EthereumClient.TransactionSender
  256. func (tx *Transaction) GetFrom(chainID *BigInt) (address *Address, _ error) {
  257. var signer types.Signer = types.HomesteadSigner{}
  258. if chainID != nil {
  259. signer = types.NewEIP155Signer(chainID.bigint)
  260. }
  261. from, err := types.Sender(signer, tx.tx)
  262. return &Address{from}, err
  263. }
  264. func (tx *Transaction) GetTo() *Address {
  265. if to := tx.tx.To(); to != nil {
  266. return &Address{*to}
  267. }
  268. return nil
  269. }
  270. func (tx *Transaction) WithSignature(sig []byte, chainID *BigInt) (signedTx *Transaction, _ error) {
  271. var signer types.Signer = types.HomesteadSigner{}
  272. if chainID != nil {
  273. signer = types.NewEIP155Signer(chainID.bigint)
  274. }
  275. rawTx, err := tx.tx.WithSignature(signer, common.CopyBytes(sig))
  276. return &Transaction{rawTx}, err
  277. }
  278. // Transactions represents a slice of transactions.
  279. type Transactions struct{ txs types.Transactions }
  280. // Size returns the number of transactions in the slice.
  281. func (txs *Transactions) Size() int {
  282. return len(txs.txs)
  283. }
  284. // Get returns the transaction at the given index from the slice.
  285. func (txs *Transactions) Get(index int) (tx *Transaction, _ error) {
  286. if index < 0 || index >= len(txs.txs) {
  287. return nil, errors.New("index out of bounds")
  288. }
  289. return &Transaction{txs.txs[index]}, nil
  290. }
  291. // Receipt represents the results of a transaction.
  292. type Receipt struct {
  293. receipt *types.Receipt
  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. // EncodeRLP encodes a transaction receipt into an RLP data dump.
  306. func (r *Receipt) EncodeRLP() ([]byte, error) {
  307. return rlp.EncodeToBytes(r.receipt)
  308. }
  309. // NewReceiptFromJSON parses a transaction receipt from a JSON data dump.
  310. func NewReceiptFromJSON(data string) (*Receipt, error) {
  311. r := &Receipt{
  312. receipt: new(types.Receipt),
  313. }
  314. if err := json.Unmarshal([]byte(data), r.receipt); err != nil {
  315. return nil, err
  316. }
  317. return r, nil
  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. // String returns a printable representation of the receipt.
  325. func (r *Receipt) String() string {
  326. return encodeOrError(r)
  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) }