github.com/sixexorg/magnetic-ring@v0.0.0-20191119090307-31705a21e419/store/mainchain/extstates/league_block.go (about) 1 package extstates 2 3 import ( 4 "encoding/binary" 5 "io" 6 "sort" 7 8 "math/big" 9 10 "github.com/sixexorg/magnetic-ring/common" 11 "github.com/sixexorg/magnetic-ring/common/serialization" 12 "github.com/sixexorg/magnetic-ring/common/sink" 13 orgtypes "github.com/sixexorg/magnetic-ring/core/orgchain/types" 14 common2 "github.com/sixexorg/magnetic-ring/store/mainchain/common" 15 ) 16 17 type LeagueBlockSimple struct { 18 *orgtypes.Header 19 EnergyUsed *big.Int 20 TxHashes common.HashArray 21 } 22 23 type LeaguegBlocks []*LeagueBlockSimple 24 25 func (s LeaguegBlocks) Len() int { return len(s) } 26 func (s LeaguegBlocks) Swap(i, j int) { s[i], s[j] = s[j], s[i] } 27 func (s LeaguegBlocks) Less(i, j int) bool { return s[i].Hash().String() < s[j].Hash().String() } 28 func (s LeaguegBlocks) GetHashRoot() common.Hash { 29 sort.Sort(s) 30 hashes := make([]common.Hash, 0, s.Len()) 31 for _, v := range s { 32 hashes = append(hashes, v.Hash()) 33 } 34 return common.ComputeMerkleRoot(hashes) 35 } 36 37 func (this *LeagueBlockSimple) Serialize(w io.Writer) error { 38 /* err := this.Header.Serialize(w) 39 if err != nil { 40 return err 41 }*/ 42 sk := sink.NewZeroCopySink(nil) 43 err := this.Header.Serialization(sk) 44 if err != nil { 45 return err 46 } 47 gasUsed, err := sink.BigIntToComplex(this.EnergyUsed) 48 if err != nil { 49 return err 50 } 51 sk.WriteComplex(gasUsed) 52 t, err := sink.HashArrayToComplex(this.TxHashes) 53 if err != nil { 54 return err 55 } 56 sk.WriteComplex(t) 57 _, err = w.Write(sk.Bytes()) 58 return err 59 } 60 61 func (this *LeagueBlockSimple) Deserialize(r io.Reader) error { 62 if this == nil { 63 this = &LeagueBlockSimple{} 64 } 65 if this.Header == nil { 66 this.Header = &orgtypes.Header{} 67 } 68 this.Header.Deserialize(r) 69 cp1, err := serialization.ReadComplex(r) 70 if err != nil { 71 return err 72 } 73 this.EnergyUsed, err = cp1.ComplexToBigInt() 74 if err != nil { 75 return err 76 } 77 cp2, err := serialization.ReadComplex(r) 78 if err != nil { 79 return err 80 } 81 this.TxHashes, err = cp2.ComplexToHashArray() 82 return err 83 84 /* buff, err := ioutil.ReadAll(r) 85 if err != nil { 86 return err 87 } 88 var eof bool 89 source := sink.NewZeroCopySource(buff) 90 _, eof = source.NextByte() 91 this.Header = new(orgtypes.Header) 92 this.Header.Version, eof = source.NextByte() 93 prevBlockHash, eof := source.NextHash() 94 95 this.Header.PrevBlockHash = prevBlockHash 96 txRoot, eof := source.NextHash() 97 this.Header.TxRoot = txRoot 98 stateRoot, eof := source.NextHash() 99 this.Header.StateRoot = stateRoot 100 101 this.Header.ReceiptsRoot, eof = source.NextHash() 102 this.Header.Timestamp, eof = source.NextInt64() 103 this.Header.Height, eof = source.NextUint64() 104 if eof { 105 return errors.ERR_TXRAW_EOF 106 } 107 return nil*/ 108 } 109 110 func (this *LeagueBlockSimple) GetKey() []byte { 111 buff := make([]byte, 1+common.AddrLength+8) 112 buff[0] = byte(common2.EXT_LEAGUE_DATA_BLOCK) 113 copy(buff[1:common.AddrLength+1], this.LeagueId[:]) 114 binary.LittleEndian.PutUint64(buff[common.AddrLength+1:], this.Header.Height) 115 return buff 116 }