OSDN Git Service

update SetMiningAddress
authorChengcheng Zhang <943420582@qq.com>
Thu, 27 Jun 2019 16:45:43 +0000 (00:45 +0800)
committerChengcheng Zhang <943420582@qq.com>
Thu, 27 Jun 2019 16:45:43 +0000 (00:45 +0800)
account/accounts.go
account/store.go
database/account_store.go

index 632db13..37062e6 100644 (file)
@@ -35,16 +35,17 @@ const (
 
 // pre-define errors for supporting bytom errorFormatter
 var (
-       ErrDuplicateAlias  = errors.New("Duplicate account alias")
-       ErrDuplicateIndex  = errors.New("Duplicate account with same xPubs and index")
-       ErrFindAccount     = errors.New("Failed to find account")
-       ErrMarshalAccount  = errors.New("Failed to marshal account")
-       ErrInvalidAddress  = errors.New("Invalid address")
-       ErrFindCtrlProgram = errors.New("Failed to find account control program")
-       ErrDeriveRule      = errors.New("Invalid key derivation rule")
-       ErrContractIndex   = errors.New("Exceeded maximum addresses per account")
-       ErrAccountIndex    = errors.New("Exceeded maximum accounts per xpub")
-       ErrFindTransaction = errors.New("No transaction")
+       ErrDuplicateAlias    = errors.New("Duplicate account alias")
+       ErrDuplicateIndex    = errors.New("Duplicate account with same xPubs and index")
+       ErrFindAccount       = errors.New("Failed to find account")
+       ErrMarshalAccount    = errors.New("Failed to marshal account")
+       ErrInvalidAddress    = errors.New("Invalid address")
+       ErrFindCtrlProgram   = errors.New("Failed to find account control program")
+       ErrDeriveRule        = errors.New("Invalid key derivation rule")
+       ErrContractIndex     = errors.New("Exceeded maximum addresses per account")
+       ErrAccountIndex      = errors.New("Exceeded maximum accounts per xpub")
+       ErrFindTransaction   = errors.New("No transaction")
+       ErrFindMiningAddress = errors.New("Failed to find mining address")
 )
 
 // Account is structure of Bytom account
@@ -409,9 +410,12 @@ func (m *Manager) GetCoinbaseControlProgram() ([]byte, error) {
 
 // GetCoinbaseCtrlProgram will return the coinbase CtrlProgram
 func (m *Manager) GetCoinbaseCtrlProgram() (*CtrlProgram, error) {
-       if data := m.store.GetMiningAddress(); data != nil {
-               cp := &CtrlProgram{}
-               return cp, json.Unmarshal(data, cp)
+       cp, err := m.store.GetMiningAddress()
+       if err != nil {
+               return nil, err
+       }
+       if cp != nil {
+               return cp, nil
        }
 
        firstAccount := make([]byte, 0)
@@ -432,13 +436,10 @@ func (m *Manager) GetCoinbaseCtrlProgram() (*CtrlProgram, error) {
                return nil, err
        }
 
-       rawCP, err := json.Marshal(program)
-       if err != nil {
+       if err := m.store.SetMiningAddress(program); err != nil {
                return nil, err
        }
 
-       m.store.SetMiningAddress(rawCP)
-
        return program, nil
 }
 
@@ -548,12 +549,10 @@ func (m *Manager) SetMiningAddress(miningAddress string) (string, error) {
                Address:        miningAddress,
                ControlProgram: program,
        }
-       rawCP, err := json.Marshal(cp)
-       if err != nil {
-               return "", err
-       }
 
-       m.store.SetMiningAddress(rawCP)
+       if err := m.store.SetMiningAddress(cp); err != nil {
+               return cp.Address, err
+       }
        return m.GetMiningAddress()
 }
 
index 2df813a..1640b97 100644 (file)
@@ -24,8 +24,8 @@ type AccountStorer interface {
        DeleteStandardUTXO(bc.Hash)
        GetCoinbaseArbitrary() []byte
        SetCoinbaseArbitrary([]byte)
-       GetMiningAddress() []byte
-       SetMiningAddress([]byte)
+       GetMiningAddress() (*CtrlProgram, error)
+       SetMiningAddress(*CtrlProgram) error
        GetBip44ContractIndex(string, bool) []byte
        GetRawProgram(common.Hash) []byte
        GetAccounts(string) [][]byte
index 6331f7d..7c2bf7e 100644 (file)
@@ -212,17 +212,32 @@ func (store *AccountStore) SetCoinbaseArbitrary(arbitrary []byte) {
 }
 
 // GetMiningAddress get mining address
-func (store *AccountStore) GetMiningAddress() []byte {
-       return store.accountDB.Get([]byte(MiningAddressKey))
+func (store *AccountStore) GetMiningAddress() (*acc.CtrlProgram, error) {
+       rawCP := store.accountDB.Get([]byte(MiningAddressKey))
+       if rawCP == nil {
+               return nil, acc.ErrFindMiningAddress
+       }
+       cp := new(acc.CtrlProgram)
+       if err := json.Unmarshal(rawCP, cp); err != nil {
+               return nil, err
+       }
+
+       return cp, nil
 }
 
 // SetMiningAddress set mining address
-func (store *AccountStore) SetMiningAddress(rawProgram []byte) {
+func (store *AccountStore) SetMiningAddress(program *acc.CtrlProgram) error {
+       rawProgram, err := json.Marshal(program)
+       if err != nil {
+               return err
+       }
+
        if store.batch == nil {
                store.accountDB.Set([]byte(MiningAddressKey), rawProgram)
        } else {
                store.batch.Set([]byte(MiningAddressKey), rawProgram)
        }
+       return nil
 }
 
 // GetBip44ContractIndex get bip44 contract index