github.com/sixexorg/magnetic-ring@v0.0.0-20191119090307-31705a21e419/store/mainchain/states/league_state.go (about) 1 package states 2 3 import ( 4 "io" 5 "math/big" 6 7 "github.com/sixexorg/magnetic-ring/common/sink" 8 9 "encoding/binary" 10 11 "io/ioutil" 12 13 "bytes" 14 15 "sort" 16 17 "fmt" 18 19 "github.com/sixexorg/magnetic-ring/common" 20 "github.com/sixexorg/magnetic-ring/errors" 21 mcom "github.com/sixexorg/magnetic-ring/store/mainchain/common" 22 ) 23 24 type LeagueState struct { 25 Address common.Address 26 hash common.Hash 27 Symbol common.Symbol 28 Height uint64 29 MinBox uint64 // not stored in the db 30 Creator common.Address // not stored in the db 31 Rate uint32 // not stored in the db 32 Private bool 33 Data *League 34 } 35 36 type League struct { 37 Nonce uint64 38 Name common.Hash 39 FrozenBox *big.Int 40 MemberRoot common.Hash 41 } 42 43 func (this *LeagueState) IsPrivate() bool { 44 return this.Private 45 } 46 47 type LeagueStates []*LeagueState 48 49 func (s LeagueStates) Len() int { return len(s) } 50 func (s LeagueStates) Swap(i, j int) { s[i], s[j] = s[j], s[i] } 51 func (s LeagueStates) Less(i, j int) bool { return s[i].Hash().String() < s[j].Hash().String() } 52 func (s LeagueStates) GetHashRoot() common.Hash { 53 sort.Sort(s) 54 hashes := make([]common.Hash, 0, s.Len()) 55 for _, v := range s { 56 hashes = append(hashes, v.Hash()) 57 } 58 return common.ComputeMerkleRoot(hashes) 59 } 60 61 type LeagueAccountStatus byte 62 63 const ( 64 LAS_Apply LeagueAccountStatus = 0x01 65 LAS_Normal LeagueAccountStatus = 0x02 66 LAS_Exit LeagueAccountStatus = 0x03 67 ) 68 69 func (this *LeagueState) Hash() common.Hash { 70 buff := new(bytes.Buffer) 71 buff.Write(this.GetKey()) 72 this.Serialize(buff) 73 hash, _ := common.ParseHashFromBytes(common.Sha256(buff.Bytes())) 74 this.hash = hash 75 return hash 76 } 77 78 func (this *LeagueState) Serialize(w io.Writer) error { 79 sk := sink.NewZeroCopySink(nil) 80 sk.WriteUint64(this.Data.Nonce) 81 sk.WriteHash(this.Data.Name) 82 c1, _ := sink.BigIntToComplex(this.Data.FrozenBox) 83 sk.WriteComplex(c1) 84 sk.WriteHash(this.Data.MemberRoot) 85 w.Write(sk.Bytes()) 86 return nil 87 } 88 89 func (this *LeagueState) Deserialize(r io.Reader) error { 90 buff, err := ioutil.ReadAll(r) 91 if err != nil { 92 return err 93 } 94 var eof bool 95 source := sink.NewZeroCopySource(buff) 96 _, eof = source.NextByte() 97 this.Address, eof = source.NextAddress() 98 this.Height, eof = source.NextUint64() 99 this.Data = &League{} 100 this.Data.Nonce, eof = source.NextUint64() 101 this.Data.Name, eof = source.NextHash() 102 c1, eof := source.NextComplex() 103 this.Data.FrozenBox, err = c1.ComplexToBigInt() 104 if err != nil { 105 return err 106 } 107 this.Data.MemberRoot, eof = source.NextHash() 108 if eof { 109 return errors.ERR_TXRAW_EOF 110 } 111 return nil 112 } 113 func (this *LeagueState) GetKey() []byte { 114 buff := make([]byte, 1+common.AddrLength+8) 115 buff[0] = byte(mcom.ST_LEAGUE) 116 copy(buff[1:common.AddrLength+1], this.Address[:]) 117 binary.LittleEndian.PutUint64(buff[common.AddrLength+1:], this.Height) 118 return buff 119 } 120 121 func (this *LeagueState) AddMetaBox(num *big.Int) { 122 fmt.Println("☀️ AddMetaBox ", this.Data.FrozenBox.Uint64(), num.Uint64()) 123 this.Data.FrozenBox.Add(this.Data.FrozenBox, num) 124 }