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"`
19 AccountIndex uint64 `json:"account_index"`
22 // Backup export all the account info into image
23 func (m *Manager) Backup() (*Image, error) {
25 Slice: []*ImageSlice{},
26 AccountIndex: m.getNextAccountIndex(),
29 accountIter := m.db.IteratorPrefix(accountPrefix)
30 defer accountIter.Release()
31 for accountIter.Next() {
33 if err := json.Unmarshal(accountIter.Value(), a); err != nil {
37 image.Slice = append(image.Slice, &ImageSlice{
39 ContractIndex: m.getNextContractIndex(a.ID),
45 // Restore import the accountImages into account manage
46 func (m *Manager) Restore(image *Image) error {
47 storeBatch := m.db.NewBatch()
48 for _, slice := range image.Slice {
49 if existed := m.db.Get(aliasKey(slice.Account.Alias)); existed != nil {
50 return ErrDuplicateAlias
53 rawAccount, err := json.Marshal(slice.Account)
55 return ErrMarshalAccount
58 storeBatch.Set(Key(slice.Account.ID), rawAccount)
59 storeBatch.Set(aliasKey(slice.Account.Alias), []byte(slice.Account.ID))
60 storeBatch.Set(contractIndexKey(slice.Account.ID), common.Unit64ToBytes(slice.ContractIndex))
63 if localIndex := m.getNextAccountIndex(); localIndex < image.AccountIndex {
64 storeBatch.Set(accountIndexKey, common.Unit64ToBytes(image.AccountIndex))
68 for _, slice := range image.Slice {
69 for i := uint64(1); i < slice.ContractIndex; i++ {
70 if _, err := m.createAddress(nil, slice.Account, false); err != nil {