github.com/linapex/ethereum-dpos-chinese@v0.0.0-20190316121959-b78b3a4a1ece/mobile/types.go (about)

     1  
     2  //<developer>
     3  //    <name>linapex 曹一峰</name>
     4  //    <email>linapex@163.com</email>
     5  //    <wx>superexc</wx>
     6  //    <qqgroup>128148617</qqgroup>
     7  //    <url>https://jsq.ink</url>
     8  //    <role>pku engineer</role>
     9  //    <date>2019-03-16 12:09:43</date>
    10  //</624342654510960640>
    11  
    12  
    13  //包含core/types包中的所有包装器。
    14  
    15  package geth
    16  
    17  import (
    18  	"encoding/json"
    19  	"errors"
    20  	"fmt"
    21  
    22  	"github.com/ethereum/go-ethereum/common"
    23  	"github.com/ethereum/go-ethereum/core/types"
    24  	"github.com/ethereum/go-ethereum/rlp"
    25  )
    26  
    27  
    28  
    29  //nonce是一个64位散列,它证明(与mix散列组合在一起)
    30  //对一个块进行了足够的计算。
    31  type Nonce struct {
    32  	nonce types.BlockNonce
    33  }
    34  
    35  //getbytes检索块nonce的字节表示形式。
    36  func (n *Nonce) GetBytes() []byte {
    37  	return n.nonce[:]
    38  }
    39  
    40  //gethex检索块nonce的十六进制字符串表示形式。
    41  func (n *Nonce) GetHex() string {
    42  	return fmt.Sprintf("0x%x", n.nonce[:])
    43  }
    44  
    45  //Bloom表示256位Bloom筛选器。
    46  type Bloom struct {
    47  	bloom types.Bloom
    48  }
    49  
    50  //GetBytes检索Bloom筛选器的字节表示形式。
    51  func (b *Bloom) GetBytes() []byte {
    52  	return b.bloom[:]
    53  }
    54  
    55  //GetHex检索Bloom筛选器的十六进制字符串表示形式。
    56  func (b *Bloom) GetHex() string {
    57  	return fmt.Sprintf("0x%x", b.bloom[:])
    58  }
    59  
    60  //header表示以太坊区块链中的区块头。
    61  type Header struct {
    62  	header *types.Header
    63  }
    64  
    65  //newheaderfromrlp解析来自rlp数据转储的头。
    66  func NewHeaderFromRLP(data []byte) (*Header, error) {
    67  	h := &Header{
    68  		header: new(types.Header),
    69  	}
    70  	if err := rlp.DecodeBytes(common.CopyBytes(data), h.header); err != nil {
    71  		return nil, err
    72  	}
    73  	return h, nil
    74  }
    75  
    76  //encoderlp将头编码为rlp数据转储。
    77  func (h *Header) EncodeRLP() ([]byte, error) {
    78  	return rlp.EncodeToBytes(h.header)
    79  }
    80  
    81  //NewHeaderFromJSON解析JSON数据转储中的头。
    82  func NewHeaderFromJSON(data string) (*Header, error) {
    83  	h := &Header{
    84  		header: new(types.Header),
    85  	}
    86  	if err := json.Unmarshal([]byte(data), h.header); err != nil {
    87  		return nil, err
    88  	}
    89  	return h, nil
    90  }
    91  
    92  //encodejson将头编码到json数据转储中。
    93  func (h *Header) EncodeJSON() (string, error) {
    94  	data, err := json.Marshal(h.header)
    95  	return string(data), err
    96  }
    97  
    98  func (h *Header) GetParentHash() *Hash   { return &Hash{h.header.ParentHash} }
    99  func (h *Header) GetUncleHash() *Hash    { return &Hash{h.header.UncleHash} }
   100  func (h *Header) GetCoinbase() *Address  { return &Address{h.header.Coinbase} }
   101  func (h *Header) GetRoot() *Hash         { return &Hash{h.header.Root} }
   102  func (h *Header) GetTxHash() *Hash       { return &Hash{h.header.TxHash} }
   103  func (h *Header) GetReceiptHash() *Hash  { return &Hash{h.header.ReceiptHash} }
   104  func (h *Header) GetBloom() *Bloom       { return &Bloom{h.header.Bloom} }
   105  func (h *Header) GetDifficulty() *BigInt { return &BigInt{h.header.Difficulty} }
   106  func (h *Header) GetNumber() int64       { return h.header.Number.Int64() }
   107  func (h *Header) GetGasLimit() int64     { return int64(h.header.GasLimit) }
   108  func (h *Header) GetGasUsed() int64      { return int64(h.header.GasUsed) }
   109  func (h *Header) GetTime() int64         { return h.header.Time.Int64() }
   110  func (h *Header) GetExtra() []byte       { return h.header.Extra }
   111  func (h *Header) GetMixDigest() *Hash    { return &Hash{h.header.MixDigest} }
   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表示一个头段。
   116  type Headers struct{ headers []*types.Header }
   117  
   118  //SIZE返回切片中的头数。
   119  func (h *Headers) Size() int {
   120  	return len(h.headers)
   121  }
   122  
   123  //get返回切片中给定索引处的头。
   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  //块表示以太坊区块链中的整个块。
   132  type Block struct {
   133  	block *types.Block
   134  }
   135  
   136  //newblockfromrlp解析来自rlp数据转储的块。
   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将块编码为rlp数据转储。
   148  func (b *Block) EncodeRLP() ([]byte, error) {
   149  	return rlp.EncodeToBytes(b.block)
   150  }
   151  
   152  //NewblockFromJSON解析来自JSON数据转储的块。
   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将块编码为json数据转储。
   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) GetUncleHash() *Hash    { return &Hash{b.block.UncleHash()} }
   171  func (b *Block) GetCoinbase() *Address  { return &Address{b.block.Coinbase()} }
   172  func (b *Block) GetRoot() *Hash         { return &Hash{b.block.Root()} }
   173  func (b *Block) GetTxHash() *Hash       { return &Hash{b.block.TxHash()} }
   174  func (b *Block) GetReceiptHash() *Hash  { return &Hash{b.block.ReceiptHash()} }
   175  func (b *Block) GetBloom() *Bloom       { return &Bloom{b.block.Bloom()} }
   176  func (b *Block) GetDifficulty() *BigInt { return &BigInt{b.block.Difficulty()} }
   177  func (b *Block) GetNumber() int64       { return b.block.Number().Int64() }
   178  func (b *Block) GetGasLimit() int64     { return int64(b.block.GasLimit()) }
   179  func (b *Block) GetGasUsed() int64      { return int64(b.block.GasUsed()) }
   180  func (b *Block) GetTime() int64         { return b.block.Time().Int64() }
   181  func (b *Block) GetExtra() []byte       { return b.block.Extra() }
   182  func (b *Block) GetMixDigest() *Hash    { return &Hash{b.block.MixDigest()} }
   183  func (b *Block) GetNonce() int64        { return int64(b.block.Nonce()) }
   184  
   185  func (b *Block) GetHash() *Hash        { return &Hash{b.block.Hash()} }
   186  func (b *Block) GetHashNoNonce() *Hash { return &Hash{b.block.HashNoNonce()} }
   187  
   188  func (b *Block) GetHeader() *Header             { return &Header{b.block.Header()} }
   189  func (b *Block) GetUncles() *Headers            { return &Headers{b.block.Uncles()} }
   190  func (b *Block) GetTransactions() *Transactions { return &Transactions{b.block.Transactions()} }
   191  func (b *Block) GetTransaction(hash *Hash) *Transaction {
   192  	return &Transaction{b.block.Transaction(hash.hash)}
   193  }
   194  
   195  //事务表示单个以太坊事务。
   196  type Transaction struct {
   197  	tx *types.Transaction
   198  }
   199  
   200  //newTransaction创建具有给定属性的新事务。
   201  func NewTransaction(nonce int64, to *Address, amount *BigInt, gasLimit int64, gasPrice *BigInt, data []byte) *Transaction {
   202  	return &Transaction{types.NewTransaction(types.Binary, uint64(nonce), to.address, amount.bigint, uint64(gasLimit), gasPrice.bigint, common.CopyBytes(data))}
   203  }
   204  
   205  //newTransactionFromRLP解析来自RLP数据转储的事务。
   206  func NewTransactionFromRLP(data []byte) (*Transaction, error) {
   207  	tx := &Transaction{
   208  		tx: new(types.Transaction),
   209  	}
   210  	if err := rlp.DecodeBytes(common.CopyBytes(data), tx.tx); err != nil {
   211  		return nil, err
   212  	}
   213  	return tx, nil
   214  }
   215  
   216  //encoderlp将事务编码为rlp数据转储。
   217  func (tx *Transaction) EncodeRLP() ([]byte, error) {
   218  	return rlp.EncodeToBytes(tx.tx)
   219  }
   220  
   221  //NewTransactionFromJSON解析来自JSON数据转储的事务。
   222  func NewTransactionFromJSON(data string) (*Transaction, error) {
   223  	tx := &Transaction{
   224  		tx: new(types.Transaction),
   225  	}
   226  	if err := json.Unmarshal([]byte(data), tx.tx); err != nil {
   227  		return nil, err
   228  	}
   229  	return tx, nil
   230  }
   231  
   232  //encodejson将事务编码为json数据转储。
   233  func (tx *Transaction) EncodeJSON() (string, error) {
   234  	data, err := json.Marshal(tx.tx)
   235  	return string(data), err
   236  }
   237  
   238  func (tx *Transaction) GetData() []byte      { return tx.tx.Data() }
   239  func (tx *Transaction) GetGas() int64        { return int64(tx.tx.Gas()) }
   240  func (tx *Transaction) GetGasPrice() *BigInt { return &BigInt{tx.tx.GasPrice()} }
   241  func (tx *Transaction) GetValue() *BigInt    { return &BigInt{tx.tx.Value()} }
   242  func (tx *Transaction) GetNonce() int64      { return int64(tx.tx.Nonce()) }
   243  
   244  func (tx *Transaction) GetHash() *Hash   { return &Hash{tx.tx.Hash()} }
   245  func (tx *Transaction) GetCost() *BigInt { return &BigInt{tx.tx.Cost()} }
   246  
   247  //已弃用:GetSighash无法知道要使用哪个签名者。
   248  func (tx *Transaction) GetSigHash() *Hash { return &Hash{types.HomesteadSigner{}.Hash(tx.tx)} }
   249  
   250  //已弃用:使用ethereumclient.TransactionSender
   251  func (tx *Transaction) GetFrom(chainID *BigInt) (address *Address, _ error) {
   252  	var signer types.Signer = types.HomesteadSigner{}
   253  	if chainID != nil {
   254  		signer = types.NewEIP155Signer(chainID.bigint)
   255  	}
   256  	from, err := types.Sender(signer, tx.tx)
   257  	return &Address{from}, err
   258  }
   259  
   260  func (tx *Transaction) GetTo() *Address {
   261  	if to := tx.tx.To(); to != nil {
   262  		return &Address{*to}
   263  	}
   264  	return nil
   265  }
   266  
   267  func (tx *Transaction) WithSignature(sig []byte, chainID *BigInt) (signedTx *Transaction, _ error) {
   268  	var signer types.Signer = types.HomesteadSigner{}
   269  	if chainID != nil {
   270  		signer = types.NewEIP155Signer(chainID.bigint)
   271  	}
   272  	rawTx, err := tx.tx.WithSignature(signer, common.CopyBytes(sig))
   273  	return &Transaction{rawTx}, err
   274  }
   275  
   276  //事务表示事务的一部分。
   277  type Transactions struct{ txs types.Transactions }
   278  
   279  //SIZE返回切片中的事务数。
   280  func (txs *Transactions) Size() int {
   281  	return len(txs.txs)
   282  }
   283  
   284  //get从切片返回给定索引处的事务。
   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  
   292  //收据表示交易的结果。
   293  type Receipt struct {
   294  	receipt *types.Receipt
   295  }
   296  
   297  //newReceiptFromRLP解析来自RLP数据转储的事务收据。
   298  func NewReceiptFromRLP(data []byte) (*Receipt, error) {
   299  	r := &Receipt{
   300  		receipt: new(types.Receipt),
   301  	}
   302  	if err := rlp.DecodeBytes(common.CopyBytes(data), r.receipt); err != nil {
   303  		return nil, err
   304  	}
   305  	return r, nil
   306  }
   307  
   308  //encoderlp将事务收据编码为rlp数据转储。
   309  func (r *Receipt) EncodeRLP() ([]byte, error) {
   310  	return rlp.EncodeToBytes(r.receipt)
   311  }
   312  
   313  //NewReceiptFromJSON解析来自JSON数据转储的事务回执。
   314  func NewReceiptFromJSON(data string) (*Receipt, error) {
   315  	r := &Receipt{
   316  		receipt: new(types.Receipt),
   317  	}
   318  	if err := json.Unmarshal([]byte(data), r.receipt); err != nil {
   319  		return nil, err
   320  	}
   321  	return r, nil
   322  }
   323  
   324  //encodejson将事务收据编码为json数据转储。
   325  func (r *Receipt) EncodeJSON() (string, error) {
   326  	data, err := rlp.EncodeToBytes(r.receipt)
   327  	return string(data), err
   328  }
   329  
   330  func (r *Receipt) GetStatus() int               { return int(r.receipt.Status) }
   331  func (r *Receipt) GetPostState() []byte         { return r.receipt.PostState }
   332  func (r *Receipt) GetCumulativeGasUsed() int64  { return int64(r.receipt.CumulativeGasUsed) }
   333  func (r *Receipt) GetBloom() *Bloom             { return &Bloom{r.receipt.Bloom} }
   334  func (r *Receipt) GetLogs() *Logs               { return &Logs{r.receipt.Logs} }
   335  func (r *Receipt) GetTxHash() *Hash             { return &Hash{r.receipt.TxHash} }
   336  func (r *Receipt) GetContractAddress() *Address { return &Address{r.receipt.ContractAddress} }
   337  func (r *Receipt) GetGasUsed() int64            { return int64(r.receipt.GasUsed) }
   338