types.go 12 KB

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