OSDN Git Service

modify import path (#1805)
[bytom/bytom.git] / wallet / annotated.go
index 571f852..536ff0b 100644 (file)
@@ -5,19 +5,19 @@ import (
        "fmt"
 
        log "github.com/sirupsen/logrus"
-       "github.com/tendermint/tmlibs/db"
-
-       "github.com/bytom/account"
-       "github.com/bytom/asset"
-       "github.com/bytom/blockchain/query"
-       "github.com/bytom/blockchain/signers"
-       "github.com/bytom/common"
-       "github.com/bytom/consensus"
-       "github.com/bytom/consensus/segwit"
-       "github.com/bytom/crypto/sha3pool"
-       "github.com/bytom/protocol/bc"
-       "github.com/bytom/protocol/bc/types"
-       "github.com/bytom/protocol/vm/vmutil"
+
+       "github.com/bytom/bytom/account"
+       "github.com/bytom/bytom/asset"
+       "github.com/bytom/bytom/blockchain/query"
+       "github.com/bytom/bytom/blockchain/signers"
+       "github.com/bytom/bytom/common"
+       "github.com/bytom/bytom/consensus"
+       "github.com/bytom/bytom/consensus/segwit"
+       "github.com/bytom/bytom/crypto/sha3pool"
+       dbm "github.com/bytom/bytom/database/leveldb"
+       "github.com/bytom/bytom/protocol/bc"
+       "github.com/bytom/bytom/protocol/bc/types"
+       "github.com/bytom/bytom/protocol/vm/vmutil"
 )
 
 // annotateTxs adds asset data to transactions
@@ -35,7 +35,7 @@ func annotateTxsAsset(w *Wallet, txs []*query.AnnotatedTx) {
 }
 
 func (w *Wallet) getExternalDefinition(assetID *bc.AssetID) json.RawMessage {
-       definitionByte := w.DB.Get(asset.CalcExtAssetKey(assetID))
+       definitionByte := w.DB.Get(asset.ExtAssetKey(assetID))
        if definitionByte == nil {
                return nil
        }
@@ -45,24 +45,18 @@ func (w *Wallet) getExternalDefinition(assetID *bc.AssetID) json.RawMessage {
                return nil
        }
 
-       saveAlias := assetID.String()
-       storeBatch := w.DB.NewBatch()
-
+       alias := assetID.String()
        externalAsset := &asset.Asset{
                AssetID:           *assetID,
-               Alias:             &saveAlias,
+               Alias:             &alias,
                DefinitionMap:     definitionMap,
                RawDefinitionByte: definitionByte,
                Signer:            &signers.Signer{Type: "external"},
        }
 
-       if rawAsset, err := json.Marshal(externalAsset); err == nil {
-               log.WithFields(log.Fields{"assetID": assetID.String(), "alias": saveAlias}).Info("index external asset")
-               storeBatch.Set(asset.Key(assetID), rawAsset)
+       if err := w.AssetReg.SaveAsset(externalAsset, alias); err != nil {
+               log.WithFields(log.Fields{"module": logModule, "err": err, "assetID": alias}).Warning("fail on save external asset to internal asset DB")
        }
-       storeBatch.Set(asset.AliasKey(saveAlias), []byte(assetID.String()))
-       storeBatch.Write()
-
        return definitionByte
 }
 
@@ -91,14 +85,14 @@ func (w *Wallet) getAliasDefinition(assetID bc.AssetID) (string, json.RawMessage
 }
 
 // annotateTxs adds account data to transactions
-func annotateTxsAccount(txs []*query.AnnotatedTx, walletDB db.DB) {
+func annotateTxsAccount(txs []*query.AnnotatedTx, walletDB dbm.DB) {
        for i, tx := range txs {
                for j, input := range tx.Inputs {
                        //issue asset tx input SpentOutputID is nil
                        if input.SpentOutputID == nil {
                                continue
                        }
-                       localAccount, err := getAccountFromUTXO(*input.SpentOutputID, walletDB)
+                       localAccount, err := getAccountFromACP(input.ControlProgram, walletDB)
                        if localAccount == nil || err != nil {
                                continue
                        }
@@ -116,38 +110,14 @@ func annotateTxsAccount(txs []*query.AnnotatedTx, walletDB db.DB) {
        }
 }
 
-func getAccountFromUTXO(outputID bc.Hash, walletDB db.DB) (*account.Account, error) {
-       accountUTXO := account.UTXO{}
-       localAccount := account.Account{}
-
-       accountUTXOValue := walletDB.Get(account.StandardUTXOKey(outputID))
-       if accountUTXOValue == nil {
-               return nil, fmt.Errorf("failed get account utxo:%x ", outputID)
-       }
-
-       if err := json.Unmarshal(accountUTXOValue, &accountUTXO); err != nil {
-               return nil, err
-       }
-
-       accountValue := walletDB.Get(account.Key(accountUTXO.AccountID))
-       if accountValue == nil {
-               return nil, fmt.Errorf("failed get account:%s ", accountUTXO.AccountID)
-       }
-       if err := json.Unmarshal(accountValue, &localAccount); err != nil {
-               return nil, err
-       }
-
-       return &localAccount, nil
-}
-
-func getAccountFromACP(program []byte, walletDB db.DB) (*account.Account, error) {
+func getAccountFromACP(program []byte, walletDB dbm.DB) (*account.Account, error) {
        var hash common.Hash
        accountCP := account.CtrlProgram{}
        localAccount := account.Account{}
 
        sha3pool.Sum256(hash[:], program)
 
-       rawProgram := walletDB.Get(account.CPKey(hash))
+       rawProgram := walletDB.Get(account.ContractKey(hash))
        if rawProgram == nil {
                return nil, fmt.Errorf("failed get account control program:%x ", hash)
        }
@@ -187,6 +157,7 @@ func (w *Wallet) buildAnnotatedTransaction(orig *types.Tx, b *types.Block, statu
                Inputs:                 make([]*query.AnnotatedInput, 0, len(orig.Inputs)),
                Outputs:                make([]*query.AnnotatedOutput, 0, len(orig.Outputs)),
                StatusFail:             statusFail,
+               Size:                   orig.SerializedSize,
        }
        for i := range orig.Inputs {
                tx.Inputs = append(tx.Inputs, w.BuildAnnotatedInput(orig, uint32(i)))
@@ -206,9 +177,11 @@ func (w *Wallet) BuildAnnotatedInput(tx *types.Tx, i uint32) *query.AnnotatedInp
        if orig.InputType() != types.CoinbaseInputType {
                in.AssetID = orig.AssetID()
                in.Amount = orig.Amount()
+               in.SignData = tx.SigHash(i)
        }
 
        id := tx.Tx.InputIDs[i]
+       in.InputID = id
        e := tx.Entries[id]
        switch e := e.(type) {
        case *bc.Spend:
@@ -216,9 +189,21 @@ func (w *Wallet) BuildAnnotatedInput(tx *types.Tx, i uint32) *query.AnnotatedInp
                in.ControlProgram = orig.ControlProgram()
                in.Address = w.getAddressFromControlProgram(in.ControlProgram)
                in.SpentOutputID = e.SpentOutputId
+               arguments := orig.Arguments()
+               for _, arg := range arguments {
+                       in.WitnessArguments = append(in.WitnessArguments, arg)
+               }
        case *bc.Issuance:
                in.Type = "issue"
                in.IssuanceProgram = orig.IssuanceProgram()
+               arguments := orig.Arguments()
+               for _, arg := range arguments {
+                       in.WitnessArguments = append(in.WitnessArguments, arg)
+               }
+               if assetDefinition := orig.AssetDefinition(); isValidJSON(assetDefinition) {
+                       assetDefinition := json.RawMessage(assetDefinition)
+                       in.AssetDefinition = &assetDefinition
+               }
        case *bc.Coinbase:
                in.Type = "coinbase"
                in.Arbitrary = e.Arbitrary
@@ -241,7 +226,7 @@ func (w *Wallet) getAddressFromControlProgram(prog []byte) string {
 }
 
 func buildP2PKHAddress(pubHash []byte) string {
-       address, err := common.NewAddressWitnessPubKeyHash(pubHash, consensus.ActiveNetParams)
+       address, err := common.NewAddressWitnessPubKeyHash(pubHash, &consensus.ActiveNetParams)
        if err != nil {
                return ""
        }
@@ -250,7 +235,7 @@ func buildP2PKHAddress(pubHash []byte) string {
 }
 
 func buildP2SHAddress(scriptHash []byte) string {
-       address, err := common.NewAddressWitnessScriptHash(scriptHash, consensus.ActiveNetParams)
+       address, err := common.NewAddressWitnessScriptHash(scriptHash, &consensus.ActiveNetParams)
        if err != nil {
                return ""
        }