OSDN Git Service

list assets response with annotated asset (#1682)
authoroysheng <33340252+oysheng@users.noreply.github.com>
Tue, 9 Apr 2019 11:13:07 +0000 (19:13 +0800)
committerPaladz <yzhu101@uottawa.ca>
Tue, 9 Apr 2019 11:13:07 +0000 (19:13 +0800)
* list assets response with annotated asset

* optimise

* optimise

api/query.go
asset/annotate.go
blockchain/query/annotated.go
protocol/vm/vmutil/script.go

index 11a4eff..5265475 100644 (file)
@@ -8,6 +8,7 @@ import (
        log "github.com/sirupsen/logrus"
 
        "github.com/bytom/account"
+       "github.com/bytom/asset"
        "github.com/bytom/blockchain/query"
        "github.com/bytom/blockchain/signers"
        "github.com/bytom/blockchain/txbuilder"
@@ -52,13 +53,17 @@ func (a *API) listAccounts(ctx context.Context, filter struct {
 func (a *API) getAsset(ctx context.Context, filter struct {
        ID string `json:"id"`
 }) Response {
-       asset, err := a.wallet.AssetReg.GetAsset(filter.ID)
+       ass, err := a.wallet.AssetReg.GetAsset(filter.ID)
        if err != nil {
                log.Errorf("getAsset: %v", err)
                return NewErrorResponse(err)
        }
 
-       return NewSuccessResponse(asset)
+       annotatedAsset, err := asset.Annotated(ass)
+       if err != nil {
+               return NewErrorResponse(err)
+       }
+       return NewSuccessResponse(annotatedAsset)
 }
 
 // POST /list-assets
@@ -71,7 +76,15 @@ func (a *API) listAssets(ctx context.Context, filter struct {
                return NewErrorResponse(err)
        }
 
-       return NewSuccessResponse(assets)
+       annotatedAssets := []*query.AnnotatedAsset{}
+       for _, ass := range assets {
+               annotatedAsset, err := asset.Annotated(ass)
+               if err != nil {
+                       return NewErrorResponse(err)
+               }
+               annotatedAssets = append(annotatedAssets, annotatedAsset)
+       }
+       return NewSuccessResponse(annotatedAssets)
 }
 
 // POST /list-balances
index 706b8f0..167c7e8 100644 (file)
@@ -6,6 +6,7 @@ import (
        "github.com/bytom/blockchain/query"
        "github.com/bytom/blockchain/signers"
        chainjson "github.com/bytom/encoding/json"
+       "github.com/bytom/protocol/vm/vmutil"
 )
 
 func isValidJSON(b []byte) bool {
@@ -24,14 +25,15 @@ func Annotated(a *Asset) (*query.AnnotatedAsset, error) {
                jsonDefinition = json.RawMessage(a.RawDefinitionByte)
        }
 
-       aa := &query.AnnotatedAsset{
-               ID:              a.AssetID,
-               Definition:      &jsonDefinition,
-               IssuanceProgram: chainjson.HexBytes(a.IssuanceProgram),
-       }
-       if a.Alias != nil {
-               aa.Alias = *a.Alias
+       annotatedAsset := &query.AnnotatedAsset{
+               ID:                a.AssetID,
+               Alias:             *a.Alias,
+               RawDefinitionByte: a.RawDefinitionByte,
+               Definition:        &jsonDefinition,
+               IssuanceProgram:   chainjson.HexBytes(a.IssuanceProgram),
        }
+
+       annotatedAsset.LimitHeight, _ = vmutil.GetIssuanceProgramRestrictHeight(a.IssuanceProgram)
        if a.Signer != nil {
                path := signers.GetBip0032Path(a.Signer, signers.AssetKeySpace)
                var jsonPath []chainjson.HexBytes
@@ -40,13 +42,13 @@ func Annotated(a *Asset) (*query.AnnotatedAsset, error) {
                }
                for _, xpub := range a.Signer.XPubs {
                        derived := xpub.Derive(path)
-                       aa.Keys = append(aa.Keys, &query.AssetKey{
+                       annotatedAsset.Keys = append(annotatedAsset.Keys, &query.AssetKey{
                                RootXPub:            xpub,
                                AssetPubkey:         derived[:],
                                AssetDerivationPath: jsonPath,
                        })
                }
-               aa.Quorum = a.Signer.Quorum
+               annotatedAsset.Quorum = a.Signer.Quorum
        }
-       return aa, nil
+       return annotatedAsset, nil
 }
index dc7467a..c20e29c 100644 (file)
@@ -68,12 +68,15 @@ type AnnotatedAccount struct {
 
 //AnnotatedAsset means an annotated asset.
 type AnnotatedAsset struct {
-       ID              bc.AssetID         `json:"id"`
-       Alias           string             `json:"alias,omitempty"`
-       IssuanceProgram chainjson.HexBytes `json:"issuance_program"`
-       Keys            []*AssetKey        `json:"keys"`
-       Quorum          int                `json:"quorum"`
-       Definition      *json.RawMessage   `json:"definition"`
+       ID                bc.AssetID         `json:"id"`
+       Alias             string             `json:"alias"`
+       VMVersion         uint64             `json:"vm_version"`
+       IssuanceProgram   chainjson.HexBytes `json:"issue_program"`
+       Keys              []*AssetKey        `json:"keys"`
+       Quorum            int                `json:"quorum"`
+       RawDefinitionByte chainjson.HexBytes `json:"raw_definition_byte"`
+       Definition        *json.RawMessage   `json:"definition"`
+       LimitHeight       int64              `json:"limit_height"`
 }
 
 //AssetKey means an asset key.
index 062ad7e..7061491 100644 (file)
@@ -133,3 +133,16 @@ func checkMultiSigParams(nrequired, npubkeys int64) error {
        }
        return nil
 }
+
+// GetIssuanceProgramRestrictHeight return issuance program restrict height
+func GetIssuanceProgramRestrictHeight(program []byte) (int64, error) {
+       insts, err := vm.ParseProgram(program)
+       if err != nil {
+               return 0, err
+       }
+
+       if insts[0].IsPushdata() && insts[1].Op == vm.OP_BLOCKHEIGHT && insts[2].Op == vm.OP_GREATERTHAN && insts[3].Op == vm.OP_VERIFY {
+               return vm.AsInt64(insts[0].Data)
+       }
+       return 0, nil
+}