github.com/yinchengtsinghua/golang-Eos-dpos-Ethereum@v0.0.0-20190121132951-92cc4225ed8e/tests/transaction_test_util.go (about) 1 2 //此源码被清华学神尹成大魔王专业翻译分析并修改 3 //尹成QQ77025077 4 //尹成微信18510341407 5 //尹成所在QQ群721929980 6 //尹成邮箱 yinc13@mails.tsinghua.edu.cn 7 //尹成毕业于清华大学,微软区块链领域全球最有价值专家 8 //https://mvp.microsoft.com/zh-cn/PublicProfile/4033620 9 // 10 // 11 // 12 // 13 // 14 // 15 // 16 // 17 // 18 // 19 // 20 // 21 // 22 // 23 // 24 25 package tests 26 27 import ( 28 "bytes" 29 "errors" 30 "fmt" 31 "math/big" 32 33 "github.com/ethereum/go-ethereum/common" 34 "github.com/ethereum/go-ethereum/common/hexutil" 35 "github.com/ethereum/go-ethereum/common/math" 36 "github.com/ethereum/go-ethereum/core/types" 37 "github.com/ethereum/go-ethereum/params" 38 "github.com/ethereum/go-ethereum/rlp" 39 ) 40 41 // 42 type TransactionTest struct { 43 json ttJSON 44 } 45 46 type ttJSON struct { 47 BlockNumber math.HexOrDecimal64 `json:"blockNumber"` 48 RLP hexutil.Bytes `json:"rlp"` 49 Sender hexutil.Bytes `json:"sender"` 50 Transaction *ttTransaction `json:"transaction"` 51 } 52 53 // 54 55 type ttTransaction struct { 56 Data []byte `gencodec:"required"` 57 GasLimit uint64 `gencodec:"required"` 58 GasPrice *big.Int `gencodec:"required"` 59 Nonce uint64 `gencodec:"required"` 60 Value *big.Int `gencodec:"required"` 61 R *big.Int `gencodec:"required"` 62 S *big.Int `gencodec:"required"` 63 V *big.Int `gencodec:"required"` 64 To common.Address `gencodec:"required"` 65 } 66 67 type ttTransactionMarshaling struct { 68 Data hexutil.Bytes 69 GasLimit math.HexOrDecimal64 70 GasPrice *math.HexOrDecimal256 71 Nonce math.HexOrDecimal64 72 Value *math.HexOrDecimal256 73 R *math.HexOrDecimal256 74 S *math.HexOrDecimal256 75 V *math.HexOrDecimal256 76 } 77 78 func (tt *TransactionTest) Run(config *params.ChainConfig) error { 79 tx := new(types.Transaction) 80 if err := rlp.DecodeBytes(tt.json.RLP, tx); err != nil { 81 if tt.json.Transaction == nil { 82 return nil 83 } 84 return fmt.Errorf("RLP decoding failed: %v", err) 85 } 86 // 87 signer := types.MakeSigner(config, new(big.Int).SetUint64(uint64(tt.json.BlockNumber))) 88 sender, err := types.Sender(signer, tx) 89 if err != nil { 90 return err 91 } 92 if sender != common.BytesToAddress(tt.json.Sender) { 93 return fmt.Errorf("Sender mismatch: got %x, want %x", sender, tt.json.Sender) 94 } 95 // 96 err = tt.json.Transaction.verify(signer, tx) 97 if tt.json.Sender == nil && err == nil { 98 return errors.New("field validations succeeded but should fail") 99 } 100 if tt.json.Sender != nil && err != nil { 101 return fmt.Errorf("field validations failed after RLP decoding: %s", err) 102 } 103 return nil 104 } 105 106 func (tt *ttTransaction) verify(signer types.Signer, tx *types.Transaction) error { 107 if !bytes.Equal(tx.Data(), tt.Data) { 108 return fmt.Errorf("Tx input data mismatch: got %x want %x", tx.Data(), tt.Data) 109 } 110 if tx.Gas() != tt.GasLimit { 111 return fmt.Errorf("GasLimit mismatch: got %d, want %d", tx.Gas(), tt.GasLimit) 112 } 113 if tx.GasPrice().Cmp(tt.GasPrice) != 0 { 114 return fmt.Errorf("GasPrice mismatch: got %v, want %v", tx.GasPrice(), tt.GasPrice) 115 } 116 if tx.Nonce() != tt.Nonce { 117 return fmt.Errorf("Nonce mismatch: got %v, want %v", tx.Nonce(), tt.Nonce) 118 } 119 v, r, s := tx.RawSignatureValues() 120 if r.Cmp(tt.R) != 0 { 121 return fmt.Errorf("R mismatch: got %v, want %v", r, tt.R) 122 } 123 if s.Cmp(tt.S) != 0 { 124 return fmt.Errorf("S mismatch: got %v, want %v", s, tt.S) 125 } 126 if v.Cmp(tt.V) != 0 { 127 return fmt.Errorf("V mismatch: got %v, want %v", v, tt.V) 128 } 129 if tx.To() == nil { 130 if tt.To != (common.Address{}) { 131 return fmt.Errorf("To mismatch when recipient is nil (contract creation): %x", tt.To) 132 } 133 } else if *tx.To() != tt.To { 134 return fmt.Errorf("To mismatch: got %x, want %x", *tx.To(), tt.To) 135 } 136 if tx.Value().Cmp(tt.Value) != 0 { 137 return fmt.Errorf("Value mismatch: got %x, want %x", tx.Value(), tt.Value) 138 } 139 return nil 140 }