1 // Package account stores and tracks accounts within a Bytom Core.
5 log "github.com/sirupsen/logrus"
8 // ImageSlice record info of single account
9 type ImageSlice struct {
10 Account *Account `json:"account"`
11 ContractIndex uint64 `json:"contract_index"`
14 // Image is the struct for hold export account data
16 Slice []*ImageSlice `json:"slices"`
19 // Backup export all the account info into image
20 func (m *Manager) Backup() (*Image, error) {
22 defer m.accountMu.Unlock()
25 Slice: []*ImageSlice{},
28 accounts, err := m.store.ListAccounts("")
33 for _, account := range accounts {
34 image.Slice = append(image.Slice, &ImageSlice{
36 ContractIndex: m.store.GetContractIndex(account.ID),
42 // Restore import the accountImages into account manage
43 func (m *Manager) Restore(image *Image) error {
45 defer m.accountMu.Unlock()
47 newStore := m.store.InitBatch()
49 for _, slice := range image.Slice {
50 if _, err := newStore.GetAccountByID(slice.Account.ID); err == nil {
51 log.WithFields(log.Fields{
53 "alias": slice.Account.Alias,
54 "id": slice.Account.ID,
55 }).Warning("skip restore account due to already existed")
57 } else if err != ErrFindAccount {
61 if _, err := newStore.GetAccountByAlias(slice.Account.Alias); err == nil {
62 return ErrDuplicateAlias
63 } else if err != ErrFindAccount {
67 if err := newStore.SetAccount(slice.Account); err != nil {
72 return newStore.CommitBatch()