github.com/turingchain2020/turingchain@v1.1.21/system/dapp/coins/types/types.go (about) 1 // Copyright Turing Corp. 2018 All Rights Reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package types 6 7 import ( 8 "reflect" 9 10 "github.com/turingchain2020/turingchain/types" 11 ) 12 13 const ( 14 // CoinsActionTransfer defines const number 15 CoinsActionTransfer = 1 16 // CoinsActionGenesis defines const coinsactiongenesis number 17 CoinsActionGenesis = 2 18 // CoinsActionWithdraw defines const number coinsactionwithdraw 19 CoinsActionWithdraw = 3 20 // CoinsActionTransferToExec defines const number coinsactiontransfertoExec 21 CoinsActionTransferToExec = 10 22 ) 23 24 var ( 25 // CoinsX defines a global string 26 CoinsX = "coins" 27 // ExecerCoins execer coins 28 ExecerCoins = []byte(CoinsX) 29 actionName = map[string]int32{ 30 "Transfer": CoinsActionTransfer, 31 "TransferToExec": CoinsActionTransferToExec, 32 "Withdraw": CoinsActionWithdraw, 33 "Genesis": CoinsActionGenesis, 34 } 35 logmap = make(map[int64]*types.LogInfo) 36 ) 37 38 func init() { 39 types.AllowUserExec = append(types.AllowUserExec, ExecerCoins) 40 types.RegFork(CoinsX, InitFork) 41 types.RegExec(CoinsX, InitExecutor) 42 } 43 44 // InitFork initials coins forks. 45 func InitFork(cfg *types.TuringchainConfig) { 46 cfg.RegisterDappFork(CoinsX, "Enable", 0) 47 } 48 49 // InitExecutor registers coins. 50 func InitExecutor(cfg *types.TuringchainConfig) { 51 types.RegistorExecutor(CoinsX, NewType(cfg)) 52 } 53 54 // CoinsType defines exec type 55 type CoinsType struct { 56 types.ExecTypeBase 57 } 58 59 // NewType new coinstype 60 func NewType(cfg *types.TuringchainConfig) *CoinsType { 61 c := &CoinsType{} 62 c.SetChild(c) 63 c.SetConfig(cfg) 64 return c 65 } 66 67 // GetPayload return payload 68 func (c *CoinsType) GetPayload() types.Message { 69 return &CoinsAction{} 70 } 71 72 // GetName return coins string 73 func (c *CoinsType) GetName() string { 74 return CoinsX 75 } 76 77 // GetLogMap return log for map 78 func (c *CoinsType) GetLogMap() map[int64]*types.LogInfo { 79 return logmap 80 } 81 82 // GetTypeMap return actionname for map 83 func (c *CoinsType) GetTypeMap() map[string]int32 { 84 return actionName 85 } 86 87 //DecodePayloadValue 为了性能考虑,coins 是最常用的合约,我们这里不用反射吗,做了特殊化的优化 88 func (c *CoinsType) DecodePayloadValue(tx *types.Transaction) (string, reflect.Value, error) { 89 name, value, err := c.decodePayloadValue(tx) 90 return name, value, err 91 } 92 93 func (c *CoinsType) decodePayloadValue(tx *types.Transaction) (string, reflect.Value, error) { 94 var action CoinsAction 95 if tx.GetPayload() == nil { 96 return "", reflect.ValueOf(nil), types.ErrActionNotSupport 97 } 98 err := types.Decode(tx.Payload, &action) 99 if err != nil { 100 return "", reflect.ValueOf(nil), err 101 } 102 var name string 103 var value types.Message 104 105 switch action.Ty { 106 case CoinsActionTransfer: 107 name = "Transfer" 108 value = action.GetTransfer() 109 case CoinsActionTransferToExec: 110 name = "TransferToExec" 111 value = action.GetTransferToExec() 112 case CoinsActionWithdraw: 113 name = "Withdraw" 114 value = action.GetWithdraw() 115 case CoinsActionGenesis: 116 name = "Genesis" 117 value = action.GetGenesis() 118 } 119 if value == nil { 120 return "", reflect.ValueOf(nil), types.ErrActionNotSupport 121 } 122 return name, reflect.ValueOf(value), nil 123 } 124 125 // RPC_Default_Process default process fo rpc 126 func (c *CoinsType) RPC_Default_Process(action string, msg interface{}) (*types.Transaction, error) { 127 var create *types.CreateTx 128 if _, ok := msg.(*types.CreateTx); !ok { 129 return nil, types.ErrInvalidParam 130 } 131 create = msg.(*types.CreateTx) 132 if create.IsToken { 133 return nil, types.ErrNotSupport 134 } 135 tx, err := c.AssertCreate(create) 136 if err != nil { 137 return nil, err 138 } 139 //to地址的问题,如果是主链交易,to地址就是直接是设置to 140 types := c.GetConfig() 141 if !types.IsPara() { 142 tx.To = create.To 143 } 144 return tx, err 145 } 146 147 // GetAssets return asset list 148 func (c *CoinsType) GetAssets(tx *types.Transaction) ([]*types.Asset, error) { 149 assets, err := c.ExecTypeBase.GetAssets(tx) 150 if err != nil || len(assets) == 0 { 151 return nil, err 152 } 153 154 types := c.GetConfig() 155 assets[0].Symbol = types.GetCoinSymbol() 156 157 if assets[0].Symbol == "trc" { 158 assets[0].Symbol = "TRC" 159 } 160 return assets, nil 161 }