diff --git a/cr/state/committee.go b/cr/state/committee.go index bc48fef4d..61ad9f015 100644 --- a/cr/state/committee.go +++ b/cr/state/committee.go @@ -247,18 +247,18 @@ func (c *Committee) GetMember(did common.Uint168) *CRMember { return c.getMember(did) } -func (c *Committee) GetReservedCustomIDLists() [][]string { +func (c *Committee) GetReservedCustomIDLists() []string { c.mtx.RLock() defer c.mtx.RUnlock() return c.getReservedCustomIDLists() } -func (c *Committee) getReservedCustomIDLists() [][]string { +func (c *Committee) getReservedCustomIDLists() []string { return c.manager.ReservedCustomIDLists } -func (c *Committee) GetReceivedCustomIDLists() [][]string { +func (c *Committee) GetReceivedCustomIDLists() []string { c.mtx.RLock() defer c.mtx.RUnlock() @@ -272,7 +272,7 @@ func (c *Committee) GetPendingReceivedCustomIDMap() map[string]struct{} { return c.manager.PendingReceivedCustomIDMap } -func (c *Committee) getReceivedCustomIDLists() [][]string { +func (c *Committee) getReceivedCustomIDLists() []string { return c.manager.ReceivedCustomIDLists } diff --git a/cr/state/keyframe.go b/cr/state/keyframe.go index 5ffedf3cb..4c4119d1f 100644 --- a/cr/state/keyframe.go +++ b/cr/state/keyframe.go @@ -241,10 +241,10 @@ type ProposalKeyFrame struct { // publicKey of SecretaryGeneral SecretaryGeneralPublicKey string // reserved custom id list - ReservedCustomIDLists [][]string + ReservedCustomIDLists []string // received custom id list - PendingReceivedCustomIDMap map[string]struct{} // todo: serialize and deserialize - ReceivedCustomIDLists [][]string + PendingReceivedCustomIDMap map[string]struct{} + ReceivedCustomIDLists []string // registered side chain name RegisteredSideChainNames []string // magic numbers @@ -937,6 +937,10 @@ func (p *ProposalState) Serialize(w io.Writer) (err error) { return } + if err = common.WriteUint8(w, p.TxPayloadVer); err != nil { + return + } + if err = common.WriteUint32(w, p.RegisterHeight); err != nil { return } @@ -1001,6 +1005,12 @@ func (p *ProposalState) Deserialize(r io.Reader) (err error) { } p.Status = ProposalStatus(status) + var payloadVersion uint8 + if payloadVersion, err = common.ReadUint8(r); err != nil { + return + } + p.TxPayloadVer = payloadVersion + if p.RegisterHeight, err = common.ReadUint32(r); err != nil { return } @@ -1158,6 +1168,30 @@ func (p *ProposalKeyFrame) Serialize(w io.Writer) (err error) { return } + if err = common.WriteVarUint(w, uint64(len(p.ReservedCustomIDLists))); err != nil { + return + } + for _, name := range p.ReservedCustomIDLists { + err = common.WriteVarString(w, name) + if err != nil { + return + } + } + + if err = p.serializeMapStringNULL(w, p.PendingReceivedCustomIDMap); err != nil { + return + } + + if err = common.WriteVarUint(w, uint64(len(p.ReceivedCustomIDLists))); err != nil { + return + } + for _, name := range p.ReceivedCustomIDLists { + err = common.WriteVarString(w, name) + if err != nil { + return + } + } + if err = common.WriteVarUint(w, uint64(len(p.RegisteredSideChainNames))); err != nil { return } @@ -1187,6 +1221,11 @@ func (p *ProposalKeyFrame) Serialize(w io.Writer) (err error) { return } } + + if err = p.serializeRegisterSideChainData(w, p.RegisteredSideChainPayloadInfo); err != nil { + return + } + ////ReservedCustomID if err = common.WriteElements(w, p.ReservedCustomID); err != nil { return @@ -1210,6 +1249,104 @@ func (p *ProposalKeyFrame) serializeDraftDataMap(draftData map[common.Uint256][] return } +func (p *ProposalKeyFrame) serializeMapStringNULL(w io.Writer, data map[string]struct{}) (err error) { + if err = common.WriteVarUint(w, uint64(len(data))); err != nil { + return + } + for k, _ := range data { + if err = common.WriteVarString(w, k); err != nil { + return + } + } + return +} + +func (p *ProposalKeyFrame) deserializeMapStringNULL(r io.Reader) ( + result map[string]struct{}, err error) { + var count uint64 + if count, err = common.ReadVarUint(r, 0); err != nil { + return + } + result = make(map[string]struct{}) + for i := uint64(0); i < count; i++ { + + var str string + str, err = common.ReadVarString(r) + if err != nil { + return + } + result[str] = struct{}{} + } + return +} + +func (p *ProposalKeyFrame) serializeRegisterSideChainData(w io.Writer, + data map[uint32]map[common.Uint256]payload.SideChainInfo) (err error) { + if err = common.WriteVarUint(w, uint64(len(data))); err != nil { + return + } + for k1, v1 := range data { + // write key + if err = common.WriteUint32(w, k1); err != nil { + return + } + + // write value + if err = common.WriteVarUint(w, uint64(len(v1))); err != nil { + return + } + for k2, v2 := range v1 { + if err = k2.Serialize(w); err != nil { + return + } + if err = v2.Serialize(w); err != nil { + return + } + } + } + return +} + +func (p *ProposalKeyFrame) deserializeRegisterSideChainData(r io.Reader) ( + result map[uint32]map[common.Uint256]payload.SideChainInfo, err error) { + var count1 uint64 + if count1, err = common.ReadVarUint(r, 0); err != nil { + return + } + result = make(map[uint32]map[common.Uint256]payload.SideChainInfo) + for i := uint64(0); i < count1; i++ { + var height uint32 + height, err = common.ReadUint32(r) + if err != nil { + return + } + + var count2 uint64 + if count2, err = common.ReadVarUint(r, 0); err != nil { + return + } + data := make(map[common.Uint256]payload.SideChainInfo) + for i := uint64(0); i < count2; i++ { + var hash common.Uint256 + err = hash.Deserialize(r) + if err != nil { + return + } + + var sideChainInfo payload.SideChainInfo + err = sideChainInfo.Deserialize(r) + if err != nil { + return + } + + data[hash] = sideChainInfo + } + + result[height] = data + } + return +} + func (p *ProposalKeyFrame) serializeProposalHashsMap(proposalHashMap map[common.Uint168]ProposalHashSet, w io.Writer) (err error) { if err = common.WriteVarUint(w, uint64(len(proposalHashMap))); err != nil { @@ -1302,6 +1439,36 @@ func (p *ProposalKeyFrame) Deserialize(r io.Reader) (err error) { return } + if count, err = common.ReadVarUint(r, 0); err != nil { + return + } + p.ReservedCustomIDLists = make([]string, 0) + for i := uint64(0); i < count; i++ { + var name string + name, err = common.ReadVarString(r) + p.ReservedCustomIDLists = append(p.ReservedCustomIDLists, name) + if err != nil { + return + } + } + + if p.PendingReceivedCustomIDMap, err = p.deserializeMapStringNULL(r); err != nil { + return + } + + if count, err = common.ReadVarUint(r, 0); err != nil { + return + } + p.ReceivedCustomIDLists = make([]string, 0) + for i := uint64(0); i < count; i++ { + var name string + name, err = common.ReadVarString(r) + p.ReceivedCustomIDLists = append(p.ReceivedCustomIDLists, name) + if err != nil { + return + } + } + if count, err = common.ReadVarUint(r, 0); err != nil { return } @@ -1341,6 +1508,10 @@ func (p *ProposalKeyFrame) Deserialize(r io.Reader) (err error) { p.RegisteredGenesisHashes = append(p.RegisteredGenesisHashes, h) } + if p.RegisteredSideChainPayloadInfo, err = p.deserializeRegisterSideChainData(r); err != nil { + return + } + if err = common.ReadElements(r, &p.ReservedCustomID); err != nil { return } diff --git a/cr/state/proposalmanager.go b/cr/state/proposalmanager.go index 0204b5052..41ffa63a1 100644 --- a/cr/state/proposalmanager.go +++ b/cr/state/proposalmanager.go @@ -399,14 +399,14 @@ func (p *ProposalManager) dealProposal(proposalState *ProposalState, unusedAmoun case payload.ReserveCustomID: oriReservedCustomIDLists := p.ReservedCustomIDLists p.history.Append(height, func() { - p.ReservedCustomIDLists = append(p.ReservedCustomIDLists, proposalState.Proposal.ReservedCustomIDList) + p.ReservedCustomIDLists = append(p.ReservedCustomIDLists, proposalState.Proposal.ReservedCustomIDList...) }, func() { p.ReservedCustomIDLists = oriReservedCustomIDLists }) case payload.ReceiveCustomID: oriReceivedCustomIDLists := p.ReceivedCustomIDLists p.history.Append(height, func() { - p.ReceivedCustomIDLists = append(p.ReceivedCustomIDLists, proposalState.Proposal.ReceivedCustomIDList) + p.ReceivedCustomIDLists = append(p.ReceivedCustomIDLists, proposalState.Proposal.ReceivedCustomIDList...) }, func() { p.ReceivedCustomIDLists = oriReceivedCustomIDLists }) diff --git a/utils/common.go b/utils/common.go index 2277d3eb6..6c3e9983b 100644 --- a/utils/common.go +++ b/utils/common.go @@ -55,12 +55,10 @@ func FileExisted(filename string) bool { return err == nil || os.IsExist(err) } -func StringExisted(src [][]string, check string) bool { +func StringExisted(src []string, check string) bool { for _, ar := range src { - for _, v := range ar { - if v == check { - return true - } + if ar == check { + return true } } return false