Skip to content

Commit

Permalink
Add customID related data into checkpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
RainFallsSilent committed Oct 26, 2021
1 parent 2b58f10 commit 87f4ba7
Show file tree
Hide file tree
Showing 4 changed files with 183 additions and 14 deletions.
8 changes: 4 additions & 4 deletions cr/state/committee.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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
}

Expand Down
177 changes: 174 additions & 3 deletions cr/state/keyframe.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions cr/state/proposalmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
})
Expand Down
8 changes: 3 additions & 5 deletions utils/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 87f4ba7

Please sign in to comment.