github.com/annchain/OG@v0.0.9/og/protocol/ogmessage/archive/token_tx.go (about) 1 // Copyright © 2019 Annchain Authors <EMAIL ADDRESS> 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 package archive 15 16 import ( 17 "fmt" 18 "github.com/annchain/OG/arefactor/og/types" 19 "github.com/annchain/OG/common/byteutil" 20 "math/rand" 21 "strings" 22 "time" 23 24 "github.com/annchain/OG/common" 25 "github.com/annchain/OG/common/hexutil" 26 "github.com/annchain/OG/common/math" 27 "github.com/annchain/OG/types/msg" 28 ) 29 30 //go:generate msgp never generate automaticly 31 const ( 32 ActionTxActionIPO uint8 = iota 33 ActionTxActionDestroy 34 ActionTxActionSPO 35 ActionRequestDomainName 36 ) 37 38 type ActionData interface { 39 msg.MsgpMember 40 String() string 41 } 42 43 //msgp:tuple InitialOffering 44 type PublicOffering struct { 45 TokenId int32 `json:"token_id"` //for Secondary Public Issues 46 Value *math.BigInt `json:"value"` 47 //To Address //when publish a token ,to equals from 48 EnableSPO bool `json:"enable_spo"` //if enableSPO is false , no Secondary Public Issues. 49 TokenName string `json:"token_name"` 50 } 51 52 func NewPublicOffering() *PublicOffering { 53 return &PublicOffering{ 54 Value: math.NewBigInt(0), 55 } 56 } 57 58 //msgp:tuple RequestDomain 59 type RequestDomain struct { 60 DomainName string 61 } 62 63 //msgp:tuple ActionTx 64 type ActionTx struct { 65 TxBase 66 Action uint8 67 From *common.Address 68 ActionData ActionData 69 confirm time.Time 70 } 71 72 func (p PublicOffering) String() string { 73 return fmt.Sprintf("tokenid %d,value %v, EnableSPO %v", p.TokenId, p.Value, p.EnableSPO) 74 } 75 76 func (r RequestDomain) String() string { 77 return r.DomainName 78 } 79 80 func (t *ActionTx) GetConfirm() time.Duration { 81 return time.Since(t.confirm) 82 } 83 84 func (t *ActionTx) Setconfirm() { 85 t.confirm = time.Now() 86 } 87 88 func (t *ActionTx) String() string { 89 if t.GetSender() == nil { 90 return fmt.Sprintf("%s-[nil]-%d-ATX", t.TxBase.String(), t.AccountNonce) 91 } 92 return fmt.Sprintf("%s-[%.10s]-%d-ATX", t.TxBase.String(), t.Sender().String(), t.AccountNonce) 93 } 94 95 func SampleActionTx() *ActionTx { 96 //v, _ := math.NewBigIntFromString("-1234567890123456789012345678901234567890123456789012345678901234567890", 10) 97 from := common.HexToAddress("0x99") 98 return &ActionTx{TxBase: TxBase{ 99 Height: 12, 100 ParentsHash: types.Hashes{types.HexToHash("0xCCDD"), types.HexToHash("0xEEFF")}, 101 Type: TxBaseTypeNormal, 102 AccountNonce: 234, 103 }, 104 From: &from, 105 //To: common.HexToAddress("0x88"), 106 //Value: v, 107 } 108 } 109 110 func RandomActionTx() *ActionTx { 111 from := types.RandomAddress() 112 return &ActionTx{TxBase: TxBase{ 113 Hash: types.RandomHash(), 114 Height: uint64(rand.Int63n(1000)), 115 ParentsHash: types.Hashes{types.RandomHash(), types.RandomHash()}, 116 Type: TxBaseTypeNormal, 117 AccountNonce: uint64(rand.Int63n(50000)), 118 Weight: uint64(rand.Int31n(2000)), 119 }, 120 From: &from, 121 //To: common.RandomAddress(), 122 //Value: math.NewBigInt(rand.Int63()), 123 } 124 } 125 126 func (t *ActionTx) GetPublicOffering() *PublicOffering { 127 if t.Action == ActionTxActionIPO || t.Action == ActionTxActionSPO || t.Action == ActionTxActionDestroy { 128 v, ok := t.ActionData.(*PublicOffering) 129 if ok { 130 return v 131 } 132 } 133 return nil 134 } 135 136 func (t *ActionTx) GetDomainName() *RequestDomain { 137 if t.Action == ActionRequestDomainName { 138 v, ok := t.ActionData.(*RequestDomain) 139 if ok { 140 return v 141 } 142 } 143 return nil 144 } 145 146 func (t *ActionTx) CheckActionIsValid() bool { 147 switch t.Action { 148 case ActionTxActionIPO: 149 case ActionTxActionSPO: 150 case ActionTxActionDestroy: 151 case ActionRequestDomainName: 152 default: 153 return false 154 } 155 return true 156 } 157 158 func (t *ActionTx) SignatureTargets() []byte { 159 // log.WithField("tx", t).Tracef("SignatureTargets: %s", t.Dump()) 160 161 w := byteutil.NewBinaryWriter() 162 163 w.Write(t.AccountNonce, t.Action) 164 if !CanRecoverPubFromSig { 165 w.Write(t.From.Bytes) 166 } 167 //types.PanicIfError(binary.Write(&buf, binary.BigEndian, t.To.KeyBytes)) 168 if t.Action == ActionTxActionIPO || t.Action == ActionTxActionSPO || t.Action == ActionTxActionDestroy { 169 of := t.GetPublicOffering() 170 w.Write(of.Value.GetSigBytes(), of.EnableSPO) 171 if t.Action == ActionTxActionIPO { 172 w.Write([]byte(of.TokenName)) 173 } else { 174 w.Write(of.TokenId) 175 } 176 177 } else if t.Action == ActionRequestDomainName { 178 r := t.GetDomainName() 179 w.Write(r.DomainName) 180 } 181 return w.Bytes() 182 } 183 184 func (t *ActionTx) Sender() common.Address { 185 return *t.From 186 } 187 188 func (tc *ActionTx) GetSender() *common.Address { 189 return tc.From 190 } 191 192 func (t *ActionTx) GetOfferValue() *math.BigInt { 193 return t.GetPublicOffering().Value 194 } 195 196 func (t *ActionTx) Compare(tx Txi) bool { 197 switch tx := tx.(type) { 198 case *ActionTx: 199 if t.GetHash().Cmp(tx.GetHash()) == 0 { 200 return true 201 } 202 return false 203 default: 204 return false 205 } 206 } 207 208 func (t *ActionTx) GetBase() *TxBase { 209 return &t.TxBase 210 } 211 212 func (t *ActionTx) Dump() string { 213 var phashes []string 214 for _, p := range t.ParentsHash { 215 phashes = append(phashes, p.Hex()) 216 } 217 return fmt.Sprintf("hash %s, pHash:[%s], from : %s \n nonce : %d , signatute : %s, pubkey: %s ,"+ 218 "height: %d , mined Nonce: %v, type: %v, weight: %d, action %d, actionData %s", t.Hash.Hex(), 219 strings.Join(phashes, " ,"), t.From.Hex(), 220 t.AccountNonce, hexutil.Encode(t.Signature), hexutil.Encode(t.PublicKey), 221 t.Height, t.MineNonce, t.Type, t.Weight, t.Action, t.ActionData) 222 } 223 func (t *ActionTx) RawActionTx() *RawActionTx { 224 if t == nil { 225 return nil 226 } 227 rawTx := &RawActionTx{ 228 TxBase: t.TxBase, 229 Action: t.Action, 230 ActionData: t.ActionData, 231 } 232 return rawTx 233 } 234 235 type ActionTxs []*ActionTx 236 237 func (t ActionTxs) String() string { 238 var strs []string 239 for _, v := range t { 240 strs = append(strs, v.String()) 241 } 242 return strings.Join(strs, ", ") 243 } 244 245 func (t ActionTxs) ToRawTxs() RawActionTxs { 246 if len(t) == 0 { 247 return nil 248 } 249 var rawTxs RawActionTxs 250 for _, v := range t { 251 rasTx := v.RawActionTx() 252 rawTxs = append(rawTxs, rasTx) 253 } 254 return rawTxs 255 } 256 257 func (c *ActionTx) RawTxi() RawTxi { 258 return c.RawActionTx() 259 } 260 261 func (c *ActionTx) SetSender(addr common.Address) { 262 c.From = &addr 263 } 264 265 func (r *ActionTxs) Len() int { 266 if r == nil { 267 return 0 268 } 269 return len(*r) 270 }