github.com/sixexorg/magnetic-ring@v0.0.0-20191119090307-31705a21e419/common/sink/complex.go (about) 1 package sink 2 3 import ( 4 "math/big" 5 6 "bytes" 7 8 "reflect" 9 10 "github.com/sixexorg/magnetic-ring/common" 11 "github.com/sixexorg/magnetic-ring/errors" 12 "github.com/sixexorg/magnetic-ring/rlp" 13 ) 14 15 const ( 16 TxIns byte = 0x01 17 TxOuts byte = 0x02 18 BigInt byte = 0x03 19 HashArray byte = 0x04 20 Bytes byte = 0x05 21 Addresses byte = 0x06 22 SigBuff byte = 0x07 23 SigPack byte = 0x08 24 Tmplt byte = 0x09 25 26 maxU16 = int(^uint16(0)) 27 ) 28 29 type ComplexType struct { 30 Size [2]byte 31 MType byte 32 Data []byte 33 } 34 35 func ZeroCopySourceRelease(source *ZeroCopySource, typ reflect.Type) (out interface{}, irregular, eof bool) { 36 eof = true 37 switch typ { 38 case common.BType_Bool: 39 out, irregular, eof = source.NextBool() 40 case common.BType_Uint8: 41 out, eof = source.NextUint8() 42 case common.BType_Uint16: 43 out, eof = source.NextUint16() 44 case common.BType_Uint32: 45 out, eof = source.NextUint32() 46 case common.BType_Uint64: 47 out, eof = source.NextUint64() 48 case common.BType_BigInt: 49 bg := &ComplexType{} 50 bg, eof = source.NextComplex() 51 out, _ = bg.ComplexToBigInt() 52 case common.BType_Hash: 53 out, eof = source.NextHash() 54 case common.BType_Address: 55 out, eof = source.NextAddress() 56 case common.BType_Symbol: 57 out, eof = source.NextSymbol() 58 case common.BType_TxIns: 59 bg := &ComplexType{} 60 bg, eof = source.NextComplex() 61 out, _ = bg.ComplexToTxIns() 62 case common.BType_TxOuts: 63 bg := &ComplexType{} 64 bg, eof = source.NextComplex() 65 out, _ = bg.ComplexToTxOuts() 66 case common.BType_TxHashArray: 67 bg := &ComplexType{} 68 bg, eof = source.NextComplex() 69 out, _ = bg.ComplexToHashArray() 70 case common.BType_SigBuf: 71 bg := &ComplexType{} 72 bg, eof = source.NextComplex() 73 out, _ = bg.ComplexToSigBuf() 74 case common.BType_Bytes: 75 bg := &ComplexType{} 76 bg, eof = source.NextComplex() 77 out, _ = bg.ComplexToBytes() 78 } 79 return 80 } 81 82 func ZeroCopySinkAppend(sk *ZeroCopySink, value reflect.Value) error { 83 switch value.Interface().(type) { 84 case bool: 85 sk.WriteBool(value.Interface().(bool)) 86 case uint8: 87 sk.WriteUint8(value.Interface().(uint8)) 88 case uint16: 89 sk.WriteUint16(value.Interface().(uint16)) 90 case uint32: 91 sk.WriteUint32(value.Interface().(uint32)) 92 case uint64: 93 sk.WriteUint64(value.Interface().(uint64)) 94 case *big.Int: 95 cpx, err := BigIntToComplex(value.Interface().(*big.Int)) 96 if err != nil { 97 return err 98 } 99 sk.WriteComplex(cpx) 100 case common.Hash: 101 sk.WriteHash(value.Interface().(common.Hash)) 102 case common.Address: 103 sk.WriteAddress(value.Interface().(common.Address)) 104 case common.Symbol: 105 sk.WriteSymbol(value.Interface().(common.Symbol)) 106 case *common.TxIns: 107 cpx, err := TxInsToComplex(value.Interface().(*common.TxIns)) 108 if err != nil { 109 return err 110 } 111 sk.WriteComplex(cpx) 112 case *common.TxOuts: 113 cpx, err := TxOutsToComplex(value.Interface().(*common.TxOuts)) 114 if err != nil { 115 return err 116 } 117 sk.WriteComplex(cpx) 118 case *common.HashArray: 119 cpx, err := HashArrayToComplex(value.Interface().(common.HashArray)) 120 if err != nil { 121 return err 122 } 123 sk.WriteComplex(cpx) 124 case *common.SigBuf: 125 cpx, err := SigBufToComplex(value.Interface().(common.SigBuf)) 126 if err != nil { 127 return err 128 } 129 sk.WriteComplex(cpx) 130 case []byte: 131 arr := value.Interface().([]byte) 132 cpx, err := BytesToComplex(arr) 133 if err != nil { 134 return err 135 } 136 sk.WriteComplex(cpx) 137 } 138 return nil 139 } 140 141 func DataCheck(data []byte) (size [2]byte, err error) { 142 l := len(data) 143 if l > int(^uint16(0)) { 144 err = errors.ERR_TX_DATA_OVERFLOW 145 146 } else { 147 u16 := common.Uint16ToBytes(uint16(l)) 148 size = [2]byte{u16[0], u16[1]} 149 } 150 return 151 } 152 func BigIntToComplex(int *big.Int) (*ComplexType, error) { 153 if int == nil { 154 int = big.NewInt(0) 155 } 156 size, err := DataCheck(int.Bytes()) 157 if err != nil { 158 return nil, err 159 } 160 ct := &ComplexType{ 161 Size: size, 162 MType: BigInt, 163 Data: int.Bytes(), 164 } 165 return ct, nil 166 } 167 168 func TxInsToComplex(tas *common.TxIns) (*ComplexType, error) { 169 buff := new(bytes.Buffer) 170 err := rlp.Encode(buff, tas) 171 if err != nil { 172 return nil, err 173 } 174 size, err := DataCheck(buff.Bytes()) 175 if err != nil { 176 return nil, err 177 } 178 ct := &ComplexType{ 179 Size: size, 180 MType: TxIns, 181 Data: buff.Bytes(), 182 } 183 return ct, nil 184 } 185 186 func TxOutsToComplex(tas *common.TxOuts) (*ComplexType, error) { 187 buff := new(bytes.Buffer) 188 err := rlp.Encode(buff, tas) 189 if err != nil { 190 return nil, err 191 } 192 size, err := DataCheck(buff.Bytes()) 193 if err != nil { 194 return nil, err 195 } 196 ct := &ComplexType{ 197 Size: size, 198 MType: TxOuts, 199 Data: buff.Bytes(), 200 } 201 return ct, nil 202 } 203 204 func SigPackToComplex(tas *common.SigPack) (*ComplexType, error) { 205 if tas == nil { 206 tas = &common.SigPack{ 207 make([]*common.SigMap, 0), 208 } 209 } 210 buff := new(bytes.Buffer) 211 err := rlp.Encode(buff, tas) 212 if err != nil { 213 return nil, err 214 } 215 size, err := DataCheck(buff.Bytes()) 216 if err != nil { 217 return nil, err 218 } 219 ct := &ComplexType{ 220 Size: size, 221 MType: SigPack, 222 Data: buff.Bytes(), 223 } 224 return ct, nil 225 } 226 227 func (cpx *ComplexType) ComplexToSigpack() (*common.SigPack, error) { 228 if cpx.MType != SigPack { 229 return nil, errors.ERR_SINK_TYPE_DIFF 230 } 231 var fs common.SigPack 232 err := rlp.Decode(bytes.NewReader(cpx.Data), &fs) 233 if err != nil { 234 return nil, err 235 } 236 return &fs, nil 237 } 238 239 func (cpx *ComplexType) ComplexToTxOuts() (*common.TxOuts, error) { 240 if cpx.MType != TxOuts { 241 return nil, errors.ERR_SINK_TYPE_DIFF 242 } 243 var fs common.TxOuts 244 err := rlp.Decode(bytes.NewReader(cpx.Data), &fs) 245 if err != nil { 246 return nil, err 247 } 248 return &fs, nil 249 } 250 251 func (cpx *ComplexType) ComplexToTxIns() (*common.TxIns, error) { 252 if cpx.MType != TxIns { 253 return nil, errors.ERR_SINK_TYPE_DIFF 254 } 255 var fs common.TxIns 256 err := rlp.Decode(bytes.NewReader(cpx.Data), &fs) 257 if err != nil { 258 return nil, err 259 } 260 return &fs, nil 261 } 262 263 func (cpx *ComplexType) ComplexToBigInt() (*big.Int, error) { 264 if cpx.MType != BigInt { 265 return nil, errors.ERR_SINK_TYPE_DIFF 266 } 267 return new(big.Int).SetBytes(cpx.Data), nil 268 } 269 270 func HashArrayToComplex(tas common.HashArray) (*ComplexType, error) { 271 buff := new(bytes.Buffer) 272 err := rlp.Encode(buff, tas) 273 if err != nil { 274 return nil, err 275 } 276 size, err := DataCheck(buff.Bytes()) 277 if err != nil { 278 return nil, err 279 } 280 ct := &ComplexType{ 281 Size: size, 282 MType: HashArray, 283 Data: buff.Bytes(), 284 } 285 return ct, nil 286 } 287 288 func (cpx *ComplexType) ComplexToHashArray() (common.HashArray, error) { 289 if cpx.MType != HashArray { 290 return nil, errors.ERR_SINK_TYPE_DIFF 291 } 292 var fs common.HashArray 293 err := rlp.Decode(bytes.NewReader(cpx.Data), &fs) 294 if err != nil { 295 return nil, err 296 } 297 return fs, nil 298 } 299 300 func BytesToComplex(bytes []byte) (*ComplexType, error) { 301 size, err := DataCheck(bytes) 302 if err != nil { 303 return nil, err 304 } 305 ct := &ComplexType{ 306 Size: size, 307 MType: Bytes, 308 Data: bytes, 309 } 310 return ct, nil 311 } 312 func (cpx *ComplexType) ComplexToBytes() ([]byte, error) { 313 if cpx.MType != Bytes { 314 return nil, errors.ERR_SINK_TYPE_DIFF 315 } 316 return cpx.Data, nil 317 } 318 func (cpx *ComplexType) ComplexToTxAddresses() ([]common.Address, error) { 319 if cpx.MType != Addresses { 320 return nil, errors.ERR_SINK_TYPE_DIFF 321 } 322 var fs []common.Address 323 err := rlp.Decode(bytes.NewReader(cpx.Data), &fs) 324 if err != nil { 325 return nil, err 326 } 327 return fs, nil 328 } 329 330 func AddressesToComplex(addresses []common.Address) (*ComplexType, error) { 331 buff := new(bytes.Buffer) 332 err := rlp.Encode(buff, addresses) 333 if err != nil { 334 return nil, err 335 } 336 size, err := DataCheck(buff.Bytes()) 337 if err != nil { 338 return nil, err 339 } 340 ct := &ComplexType{ 341 Size: size, 342 MType: Addresses, 343 Data: buff.Bytes(), 344 } 345 return ct, nil 346 } 347 348 func SigBufToComplex(tas common.SigBuf) (*ComplexType, error) { 349 buff := new(bytes.Buffer) 350 err := rlp.Encode(buff, tas) 351 if err != nil { 352 return nil, err 353 } 354 size, err := DataCheck(buff.Bytes()) 355 if err != nil { 356 return nil, err 357 } 358 ct := &ComplexType{ 359 Size: size, 360 MType: SigBuff, 361 Data: buff.Bytes(), 362 } 363 return ct, nil 364 } 365 366 func (cpx *ComplexType) ComplexToSigBuf() (common.SigBuf, error) { 367 if cpx.MType != SigBuff { 368 return nil, errors.ERR_SINK_TYPE_DIFF 369 } 370 var fs common.SigBuf 371 err := rlp.Decode(bytes.NewReader(cpx.Data), &fs) 372 if err != nil { 373 return nil, err 374 } 375 return fs, nil 376 } 377 378 func SigTmpltToComplex(tas *common.SigTmplt) (*ComplexType, error) { 379 if tas == nil { 380 tas = &common.SigTmplt{ 381 make([]*common.Maus, 0), 382 } 383 } 384 buff := new(bytes.Buffer) 385 err := rlp.Encode(buff, tas) 386 if err != nil { 387 return nil, err 388 } 389 size, err := DataCheck(buff.Bytes()) 390 if err != nil { 391 return nil, err 392 } 393 ct := &ComplexType{ 394 Size: size, 395 MType: Tmplt, 396 Data: buff.Bytes(), 397 } 398 return ct, nil 399 } 400 401 func (cpx *ComplexType) ComplexToSigTmplt() (*common.SigTmplt, error) { 402 if cpx.MType != Tmplt { 403 return nil, errors.ERR_SINK_TYPE_DIFF 404 } 405 var fs common.SigTmplt 406 err := rlp.Decode(bytes.NewReader(cpx.Data), &fs) 407 if err != nil { 408 return nil, err 409 } 410 return &fs, nil 411 }