github.com/arieschain/arieschain@v0.0.0-20191023063405-37c074544356/mobile/types.go (about)

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