1 // Package account stores and tracks accounts within a Chain Core.
7 "github.com/bytom/common"
10 // ImageSlice record info of single account
11 type ImageSlice struct {
12 Account *Account `json:"account"`
13 ContractIndex uint64 `json:"contract_index"`
16 // Image is the struct for hold export account data
18 Slice []*ImageSlice `json:"slices"`
21 // Backup export all the account info into image
22 func (m *Manager) Backup() (*Image, error) {
24 Slice: []*ImageSlice{},
27 accountIter := m.db.IteratorPrefix(accountPrefix)
28 defer accountIter.Release()
29 for accountIter.Next() {
31 if err := json.Unmarshal(accountIter.Value(), a); err != nil {
35 image.Slice = append(image.Slice, &ImageSlice{
37 ContractIndex: m.getNextContractIndex(a.ID),
43 // Restore import the accountImages into account manage
44 func (m *Manager) Restore(image *Image) error {
45 maxAccountIndex := uint64(0)
46 storeBatch := m.db.NewBatch()
47 for _, slice := range image.Slice {
48 if existed := m.db.Get(aliasKey(slice.Account.Alias)); existed != nil {
49 return ErrDuplicateAlias
52 rawAccount, err := json.Marshal(slice.Account)
54 return ErrMarshalAccount
57 if slice.Account.Signer.KeyIndex > maxAccountIndex {
58 maxAccountIndex = slice.Account.Signer.KeyIndex
60 storeBatch.Set(Key(slice.Account.ID), rawAccount)
61 storeBatch.Set(aliasKey(slice.Account.Alias), []byte(slice.Account.ID))
62 storeBatch.Set(contractIndexKey(slice.Account.ID), common.Unit64ToBytes(slice.ContractIndex))
65 if localIndex := m.getNextAccountIndex(); localIndex < maxAccountIndex {
66 storeBatch.Set(accountIndexKey, common.Unit64ToBytes(maxAccountIndex))
70 for _, slice := range image.Slice {
71 for i := uint64(1); i < slice.ContractIndex; i++ {
72 if _, err := m.createAddress(nil, slice.Account, false); err != nil {