OSDN Git Service

show whether it is change for unspent utxos
authorYongfeng LI <wliyongfeng@gmail.com>
Sat, 14 Apr 2018 07:36:12 +0000 (15:36 +0800)
committerYongfeng LI <wliyongfeng@gmail.com>
Sat, 14 Apr 2018 07:36:12 +0000 (15:36 +0800)
account/accounts.go
account/builder.go
account/reserve.go
api/accounts.go
api/query.go
blockchain/query/annotated.go
wallet/indexer.go

index 272d2c6..2b07ea9 100644 (file)
@@ -262,6 +262,11 @@ func (m *Manager) GetAliasByID(id string) string {
        return account.Alias
 }
 
+// CreateAddressForChange generate an address for the UTXO change
+func (m *Manager) CreateCtrlProgramForChange(ctx context.Context, accountID string) (cp *CtrlProgram, err error) {
+       return m.CreateAddress(ctx, accountID, true)
+}
+
 // CreateAddress generate an address for the select account
 func (m *Manager) CreateAddress(ctx context.Context, accountID string, change bool) (cp *CtrlProgram, err error) {
        account, err := m.findByID(ctx, accountID)
@@ -352,7 +357,7 @@ type CtrlProgram struct {
        Address        string
        KeyIndex       uint64
        ControlProgram []byte
-       Change         bool
+       Change         bool // Mark whether this control program is for UTXO change
 }
 
 func (m *Manager) insertAccountControlProgram(ctx context.Context, progs ...*CtrlProgram) error {
index 5a175a6..945c085 100644 (file)
@@ -72,7 +72,7 @@ func (a *spendAction) Build(ctx context.Context, b *txbuilder.TemplateBuilder) e
        }
 
        if res.Change > 0 {
-               acp, err := a.accounts.CreateAddress(ctx, a.AccountID, true)
+               acp, err := a.accounts.CreateCtrlProgramForChange(ctx, a.AccountID)
                if err != nil {
                        return errors.Wrap(err, "creating control program")
                }
index 2c04cb4..c0dfbe1 100644 (file)
@@ -52,6 +52,7 @@ type UTXO struct {
        Address             string
        ControlProgramIndex uint64
        ValidHeight         uint64
+       Change              bool
 }
 
 func (u *UTXO) source() source {
index e4fbb32..2f696bf 100644 (file)
@@ -100,6 +100,7 @@ type addressResp struct {
        AccountAlias string `json:"account_alias"`
        AccountID    string `json:"account_id"`
        Address      string `json:"address"`
+       Change       bool   `json:"change"`
 }
 
 func (a *API) listAddresses(ctx context.Context, ins struct {
@@ -128,7 +129,12 @@ func (a *API) listAddresses(ctx context.Context, ins struct {
                }
 
                accountAlias := a.wallet.AccountMgr.GetAliasByID(cp.AccountID)
-               addresses = append(addresses, &addressResp{AccountAlias: accountAlias, AccountID: cp.AccountID, Address: cp.Address})
+               addresses = append(addresses, &addressResp{
+                       AccountAlias: accountAlias,
+                       AccountID:    cp.AccountID,
+                       Address:      cp.Address,
+                       Change:       cp.Change,
+               })
        }
        return NewSuccessResponse(addresses)
 }
index ebaf819..90d560b 100644 (file)
@@ -100,7 +100,7 @@ func (a *API) listUnspentOutputs(ctx context.Context, filter struct {
 
        var UTXOs []query.AnnotatedUTXO
        for _, utxo := range accountUTXOs {
-               UTXOs = append(UTXOs, query.AnnotatedUTXO{
+               UTXOs = append([]query.AnnotatedUTXO{{
                        AccountID:           utxo.AccountID,
                        OutputID:            utxo.OutputID.String(),
                        SourceID:            utxo.SourceID.String(),
@@ -113,7 +113,8 @@ func (a *API) listUnspentOutputs(ctx context.Context, filter struct {
                        ValidHeight:         utxo.ValidHeight,
                        Alias:               a.wallet.AccountMgr.GetAliasByID(utxo.AccountID),
                        AssetAlias:          a.wallet.AssetReg.GetAliasByID(utxo.AssetID.String()),
-               })
+                       Change:              utxo.Change,
+               }}, UTXOs...)
        }
 
        return NewSuccessResponse(UTXOs)
index a4d6b25..3b7c06a 100644 (file)
@@ -94,4 +94,5 @@ type AnnotatedUTXO struct {
        SourceID            string `json:"source_id"`
        SourcePos           uint64 `json:"source_pos"`
        ValidHeight         uint64 `json:"valid_height"`
+       Change              bool   `json:"change"`
 }
index 5f8b9df..4a55b13 100644 (file)
@@ -21,7 +21,7 @@ import (
 )
 
 type rawOutput struct {
-       OutputID bc.Hash
+       OutputID       bc.Hash
        bc.AssetAmount
        ControlProgram []byte
        txHash         bc.Hash
@@ -356,6 +356,7 @@ func upsertConfirmedAccountOutputs(outs []*accountOutput, batch db.Batch) error
                        AccountID:           out.AccountID,
                        Address:             out.Address,
                        ValidHeight:         out.ValidHeight,
+                       Change:              out.change,
                }
 
                data, err := json.Marshal(u)