github.com/sixexorg/magnetic-ring@v0.0.0-20191119090307-31705a21e419/store/mainchain/validation/tx_handler.go (about) 1 package validation 2 3 import ( 4 "math/big" 5 6 "github.com/sixexorg/magnetic-ring/common" 7 "github.com/sixexorg/magnetic-ring/core/mainchain/types" 8 ) 9 10 type opCode byte 11 12 const ( 13 Account_box_add opCode = 0x01 14 Account_energy_add opCode = 0x02 15 Account_box_sub opCode = 0x03 16 Account_energy_sub opCode = 0x04 17 Account_energy_consume opCode = 0x05 18 Account_bonus_left opCode = 0x06 19 Account_nonce_add opCode = 0x07 20 AccountMaxLine opCode = 0x19 21 22 LeagueMinLine opCode = 0x20 23 League_create opCode = 0x21 24 //League_rename opCode = 0x22 25 League_frozen_add opCode = 0x23 26 League_frozen_sub opCode = 0x24 27 League_member_apply opCode = 0x25 28 League_member_remove opCode = 0x26 29 League_member_add opCode = 0x27 30 League_nonce_add opCode = 0x28 31 League_gas_destroy opCode = 0x29 32 League_raise opCode = 0x30 33 League_gas_sub opCode = 0x31 34 League_minbox opCode = 0x32 35 LeagueMaxLine opCode = 0x39 36 37 Account_bonus_fee opCode = 0xa0 38 ) 39 40 type OpLogs []*OpLog 41 42 type OpLog struct { 43 method opCode 44 data interface{} 45 } 46 type OPAddressAdress struct { 47 First common.Address 48 Second common.Address 49 } 50 type OPAddressUint64 struct { 51 Address common.Address 52 Num uint64 53 } 54 type OPAddressBigInt struct { 55 Address common.Address 56 Num *big.Int 57 } 58 type OPAddressSymbol struct { 59 Address common.Address 60 Symbol common.Symbol 61 } 62 63 type OPCreateLeague struct { 64 Creator common.Address 65 League common.Address 66 NodeId common.Address 67 FrozenBox *big.Int 68 Rate uint32 69 Minbox uint64 70 Private bool 71 TxHash common.Hash 72 Symbol common.Symbol 73 } 74 75 func CheckTxFee(tx *types.Transaction) bool { 76 switch tx.TxType { 77 case types.TransferBox, types.TransferEnergy: 78 l := len(tx.TxData.Froms.Tis) + len(tx.TxData.Tos.Tos) 79 limitFee := big.NewInt(0).SetUint64(types.GetFee(tx.TxType)) 80 limitFee.Mul(limitFee, big.NewInt(int64(l))) 81 return limitFee.Cmp(tx.TxData.Fee) != -1 82 case types.MSG: 83 //TODO other txType 84 } 85 return false 86 } 87 88 func HandleTransaction(tx *types.Transaction) []*OpLog { 89 oplogs := []*OpLog{} 90 re := false 91 oplogs, re = commonTxRoute(tx) 92 if re { 93 return oplogs 94 } 95 oplogs, re = systemTxRoute(tx) 96 if re { 97 return oplogs 98 } 99 oplogs, re = bonusTxRoute(tx) 100 if re { 101 return oplogs 102 } 103 return oplogs 104 } 105 106 func oplogPraseAddresAddress(first, second common.Address, method opCode) *OpLog { 107 op := &OpLog{ 108 method: method, 109 data: &OPAddressAdress{ 110 First: first, 111 Second: second, 112 }, 113 } 114 return op 115 116 } 117 func oplogPraseAddresUint64(address common.Address, num uint64, method opCode) *OpLog { 118 op := &OpLog{ 119 method: method, 120 data: &OPAddressUint64{ 121 Address: address, 122 Num: num, 123 }, 124 } 125 return op 126 } 127 func oplogPraseAddressSymbol(address common.Address, symbol common.Symbol, method opCode) *OpLog { 128 data := &OPAddressSymbol{ 129 Address: address, 130 Symbol: symbol, 131 } 132 return &OpLog{method, data} 133 } 134 func oplogPraseAddressBigInt(address common.Address, num *big.Int, method opCode) *OpLog { 135 data := &OPAddressBigInt{ 136 Address: address, 137 Num: big.NewInt(0).Set(num), 138 } 139 return &OpLog{method, data} 140 } 141 142 // for create league 143 func oplogPraseCreateLeague(creator, leagueId, nodeId common.Address, frozenBox *big.Int, minbox uint64, rate uint32, private bool, txHash common.Hash, symbol common.Symbol) *OpLog { 144 data := &OPCreateLeague{ 145 Creator: creator, 146 League: leagueId, 147 FrozenBox: frozenBox, 148 Rate: rate, 149 Minbox: minbox, 150 Private: private, 151 TxHash: txHash, 152 Symbol: symbol, 153 } 154 return &OpLog{League_create, data} 155 }