OSDN Git Service

Merge pull request #644 from Bytom/wallet_optimise
authorYongfeng LI <wliyongfeng@gmail.com>
Mon, 16 Apr 2018 09:27:00 +0000 (17:27 +0800)
committerGitHub <noreply@github.com>
Mon, 16 Apr 2018 09:27:00 +0000 (17:27 +0800)
fix create-asset definition is nil, issue asset transaction display wrong

asset/asset.go
asset/asset_test.go
wallet/wallet_test.go

index 526ffc7..1c4045d 100644 (file)
@@ -22,6 +22,7 @@ import (
        "github.com/bytom/protocol/vm/vmutil"
 )
 
+// DefaultNativeAsset native BTM asset
 var DefaultNativeAsset *Asset
 
 const (
@@ -49,6 +50,7 @@ func initNativeAsset() {
        }
 }
 
+// AliasKey store asset alias prefix
 func AliasKey(name string) []byte {
        return []byte(AliasPrefix + name)
 }
@@ -200,7 +202,7 @@ func (reg *Registry) Define(xpubs []chainkd.XPub, quorum int, definition map[str
        return asset, nil
 }
 
-// findByID retrieves an Asset record along with its signer, given an assetID.
+// FindByID retrieves an Asset record along with its signer, given an assetID.
 func (reg *Registry) FindByID(ctx context.Context, id *bc.AssetID) (*Asset, error) {
        reg.cacheMu.Lock()
        cached, ok := reg.cache.Get(id.String())
@@ -301,10 +303,9 @@ func (reg *Registry) ListAssets(id string) ([]*Asset, error) {
 // As is the standard for Go's map[string] serialization, object keys will
 // appear in lexicographic order. Although this is mostly meant for machine
 // consumption, the JSON is pretty-printed for easy reading.
-// The empty asset def is an empty byte slice.
 func serializeAssetDef(def map[string]interface{}) ([]byte, error) {
        if def == nil {
-               return []byte{}, nil
+               def = make(map[string]interface{}, 0)
        }
        return json.MarshalIndent(def, "", "  ")
 }
index 7dce0f9..1d85c64 100644 (file)
@@ -129,7 +129,7 @@ func TestListAssets(t *testing.T) {
                testutil.FatalErr(t, err)
        }
 
-       wantAssets := []*Asset{DefaultNativeAsset, secondAsset, firstAsset}
+       wantAssets := []*Asset{DefaultNativeAsset, firstAsset, secondAsset}
 
        gotAssets, err := reg.ListAssets("")
        if err != nil {
index 3f5bcf7..e72269f 100644 (file)
@@ -64,23 +64,29 @@ func TestWalletUpdate(t *testing.T) {
 
        controlProg.KeyIndex = 1
 
-       utxo := mockUTXO(controlProg)
-       _, txData, err := mockTxData(utxo, testAccount)
+       reg := asset.NewRegistry(testDB, chain)
+       asset, err := reg.Define([]chainkd.XPub{xpub1.XPub}, 1, nil, "TESTASSET")
        if err != nil {
                t.Fatal(err)
        }
 
-       tx := types.NewTx(*txData)
-
-       reg := asset.NewRegistry(testDB, chain)
+       utxos := []*account.UTXO{}
+       btmUtxo := mockUTXO(controlProg, consensus.BTMAssetID)
+       utxos = append(utxos, btmUtxo)
+       OtherUtxo := mockUTXO(controlProg, &asset.AssetID)
+       utxos = append(utxos, OtherUtxo)
 
-       w := mockWallet(testDB, accountManager, reg, chain)
+       _, txData, err := mockTxData(utxos, testAccount)
+       if err != nil {
+               t.Fatal(err)
+       }
 
+       tx := types.NewTx(*txData)
        block := mockSingleBlock(tx)
-
        txStatus := bc.NewTransactionStatus()
        store.SaveBlock(block, txStatus)
 
+       w := mockWallet(testDB, accountManager, reg, chain)
        err = w.AttachBlock(block)
        if err != nil {
                t.Fatal(err)
@@ -196,11 +202,11 @@ func TestExportAndImportPrivKey(t *testing.T) {
        }
 }
 
-func mockUTXO(controlProg *account.CtrlProgram) *account.UTXO {
+func mockUTXO(controlProg *account.CtrlProgram, assetID *bc.AssetID) *account.UTXO {
        utxo := &account.UTXO{}
        utxo.OutputID = bc.Hash{V0: 1}
        utxo.SourceID = bc.Hash{V0: 2}
-       utxo.AssetID = *consensus.BTMAssetID
+       utxo.AssetID = *assetID
        utxo.Amount = 1000000000
        utxo.SourcePos = 0
        utxo.ControlProgram = controlProg.ControlProgram
@@ -210,17 +216,26 @@ func mockUTXO(controlProg *account.CtrlProgram) *account.UTXO {
        return utxo
 }
 
-func mockTxData(utxo *account.UTXO, testAccount *account.Account) (*txbuilder.Template, *types.TxData, error) {
-       txInput, sigInst, err := account.UtxoToInputs(testAccount.Signer, utxo)
-       if err != nil {
-               return nil, nil, err
+func mockTxData(utxos []*account.UTXO, testAccount *account.Account) (*txbuilder.Template, *types.TxData, error) {
+       tplBuilder := txbuilder.NewBuilder(time.Now())
+
+       for _, utxo := range utxos {
+               txInput, sigInst, err := account.UtxoToInputs(testAccount.Signer, utxo)
+               if err != nil {
+                       return nil, nil, err
+               }
+               tplBuilder.AddInput(txInput, sigInst)
+
+               out := &types.TxOutput{}
+               if utxo.AssetID == *consensus.BTMAssetID {
+                       out = types.NewTxOutput(utxo.AssetID, 100, utxo.ControlProgram)
+               } else {
+                       out = types.NewTxOutput(utxo.AssetID, utxo.Amount, utxo.ControlProgram)
+               }
+               tplBuilder.AddOutput(out)
        }
 
-       b := txbuilder.NewBuilder(time.Now())
-       b.AddInput(txInput, sigInst)
-       out := types.NewTxOutput(*consensus.BTMAssetID, 100, utxo.ControlProgram)
-       b.AddOutput(out)
-       return b.Build()
+       return tplBuilder.Build()
 }
 
 func mockWallet(walletDB dbm.DB, account *account.Manager, asset *asset.Registry, chain *protocol.Chain) *Wallet {