1 // Package account stores and tracks accounts within a Bytom Core.
7 log "github.com/sirupsen/logrus"
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 defer m.accountMu.Unlock()
27 Slice: []*ImageSlice{},
31 rawAccounts := m.store.GetAccounts("")
33 for _, rawAccount := range rawAccounts {
34 account := new(Account)
35 if err := json.Unmarshal(rawAccount, account); err != nil {
38 image.Slice = append(image.Slice, &ImageSlice{
40 ContractIndex: m.GetContractIndex(account.ID),
46 // Restore import the accountImages into account manage
47 func (m *Manager) Restore(image *Image) error {
49 defer m.accountMu.Unlock()
52 defer m.store.CommitBatch()
54 for _, slice := range image.Slice {
55 if existed := m.store.GetAccountByAccountID(slice.Account.ID); existed != nil {
56 log.WithFields(log.Fields{
58 "alias": slice.Account.Alias,
59 "id": slice.Account.ID,
60 }).Warning("skip restore account due to already existed")
63 if existed := m.store.GetAccountByAccountAlias(slice.Account.Alias); existed != nil {
64 return ErrDuplicateAlias
67 rawAccount, err := json.Marshal(slice.Account)
69 return ErrMarshalAccount
72 m.store.SetAccount(slice.Account.ID, slice.Account.Alias, rawAccount)