OSDN Git Service

edit for code review
authorpaladz <453256728@qq.com>
Thu, 19 Apr 2018 05:55:45 +0000 (13:55 +0800)
committerpaladz <453256728@qq.com>
Thu, 19 Apr 2018 05:55:45 +0000 (13:55 +0800)
account/image.go
asset/image.go

index 1d05f40..1b97bc4 100644 (file)
@@ -15,15 +15,13 @@ type ImageSlice struct {
 
 // Image is the struct for hold export account data
 type Image struct {
-       Slice        []*ImageSlice `json:"slices"`
-       AccountIndex uint64        `json:"account_index"`
+       Slice []*ImageSlice `json:"slices"`
 }
 
 // Backup export all the account info into image
 func (m *Manager) Backup() (*Image, error) {
        image := &Image{
-               Slice:        []*ImageSlice{},
-               AccountIndex: m.getNextAccountIndex(),
+               Slice: []*ImageSlice{},
        }
 
        accountIter := m.db.IteratorPrefix(accountPrefix)
@@ -44,6 +42,7 @@ func (m *Manager) Backup() (*Image, error) {
 
 // Restore import the accountImages into account manage
 func (m *Manager) Restore(image *Image) error {
+       maxAccountIndex := uint64(0)
        storeBatch := m.db.NewBatch()
        for _, slice := range image.Slice {
                if existed := m.db.Get(aliasKey(slice.Account.Alias)); existed != nil {
@@ -55,13 +54,16 @@ func (m *Manager) Restore(image *Image) error {
                        return ErrMarshalAccount
                }
 
+               if slice.Account.Signer.KeyIndex > maxAccountIndex {
+                       maxAccountIndex = slice.Account.Signer.KeyIndex
+               }
                storeBatch.Set(Key(slice.Account.ID), rawAccount)
                storeBatch.Set(aliasKey(slice.Account.Alias), []byte(slice.Account.ID))
                storeBatch.Set(contractIndexKey(slice.Account.ID), common.Unit64ToBytes(slice.ContractIndex))
        }
 
-       if localIndex := m.getNextAccountIndex(); localIndex < image.AccountIndex {
-               storeBatch.Set(accountIndexKey, common.Unit64ToBytes(image.AccountIndex))
+       if localIndex := m.getNextAccountIndex(); localIndex < maxAccountIndex {
+               storeBatch.Set(accountIndexKey, common.Unit64ToBytes(maxAccountIndex))
        }
        storeBatch.Write()
 
index a6c141e..194680d 100644 (file)
@@ -10,15 +10,13 @@ import (
 
 // Image is the struct for hold export asset data
 type Image struct {
-       Assets     []*Asset `json:"assets"`
-       AssetIndex uint64   `json:"asset_index"`
+       Assets []*Asset `json:"assets"`
 }
 
 // Backup export all the asset info into image
 func (reg *Registry) Backup() (*Image, error) {
        assetImage := &Image{
-               AssetIndex: reg.getNextAssetIndex(),
-               Assets:     []*Asset{},
+               Assets: []*Asset{},
        }
 
        assetIter := reg.db.IteratorPrefix([]byte(assetPrefix))
@@ -36,6 +34,7 @@ func (reg *Registry) Backup() (*Image, error) {
 
 // Restore load the image data into asset manage
 func (reg *Registry) Restore(image *Image) error {
+       maxAssetIndex := uint64(0)
        storeBatch := reg.db.NewBatch()
        for _, asset := range image.Assets {
                if localAssetID := reg.db.Get(AliasKey(*asset.Alias)); localAssetID != nil {
@@ -52,12 +51,15 @@ func (reg *Registry) Restore(image *Image) error {
                        return err
                }
 
+               if asset.Signer.KeyIndex > maxAssetIndex {
+                       maxAssetIndex = asset.Signer.KeyIndex
+               }
                storeBatch.Set(AliasKey(*asset.Alias), asset.AssetID.Bytes())
                storeBatch.Set(Key(&asset.AssetID), rawAsset)
        }
 
-       if localIndex := reg.getNextAssetIndex(); localIndex < image.AssetIndex {
-               storeBatch.Set(assetIndexKey, common.Unit64ToBytes(image.AssetIndex))
+       if localIndex := reg.getNextAssetIndex(); localIndex < maxAssetIndex {
+               storeBatch.Set(assetIndexKey, common.Unit64ToBytes(maxAssetIndex))
        }
        storeBatch.Write()
        return nil