OSDN Git Service

fk
authorHAOYUatHZ <haoyu@protonmail.com>
Wed, 22 May 2019 08:53:44 +0000 (16:53 +0800)
committerHAOYUatHZ <haoyu@protonmail.com>
Wed, 22 May 2019 08:53:44 +0000 (16:53 +0800)
asset/asset.go
asset/builder.go

index 12ae60b..3328ee5 100644 (file)
@@ -102,6 +102,33 @@ type Asset struct {
        DefinitionMap     map[string]interface{} `json:"definition"`
 }
 
+// SaveExtAsset store external asset
+func (reg *Registry) SaveExtAsset(a *Asset, alias string) error {
+       reg.assetMu.Lock()
+       defer reg.assetMu.Unlock()
+
+       aliasKey := aliasKey(alias)
+       if existed := reg.db.Get(aliasKey); existed != nil {
+               return ErrDuplicateAlias
+       }
+
+       assetKey := ExtAssetKey(&a.AssetID)
+       if existAsset := reg.db.Get(assetKey); existAsset != nil {
+               return ErrDuplicateAsset
+       }
+
+       rawAsset, err := json.Marshal(a)
+       if err != nil {
+               return ErrMarshalAsset
+       }
+
+       storeBatch := reg.db.NewBatch()
+       storeBatch.Set(aliasKey, []byte(a.AssetID.String()))
+       storeBatch.Set(assetKey, rawAsset)
+       storeBatch.Write()
+       return nil
+}
+
 // SaveAsset store asset
 func (reg *Registry) SaveAsset(a *Asset, alias string) error {
        reg.assetMu.Lock()
index d323edc..d8a410c 100644 (file)
@@ -24,7 +24,7 @@ func (r *Registry) DecodeCrossInAction(data []byte) (txbuilder.Action, error) {
 type crossInAction struct {
        reg *Registry
        bc.AssetAmount
-       SourceID        string                 `json:"source_id"` // AnnotatedUTXO
+       SourceID        string                 `json:"source_id"`
        SourcePos       uint64                 `json:"source_pos"`
        Program         chainjson.HexBytes     `json:"control_program"`
        AssetDefinition map[string]interface{} `json:"asset_definition"`
@@ -32,8 +32,7 @@ type crossInAction struct {
 }
 
 // TODO: also need to hard-code mapTx
-// TODO: iter cross-in and save asset, saveExternalAssetDefinition
-// TODO: federation can sign? check arguments length?
+// TODO: federation can sign? check arguments length? will path be diff?
 func (a *crossInAction) Build(ctx context.Context, builder *txbuilder.TemplateBuilder) error {
        var missing []string
        if len(a.Program) == 0 {
@@ -72,7 +71,7 @@ func (a *crossInAction) Build(ctx context.Context, builder *txbuilder.TemplateBu
                asset.AssetID = *a.AssetId
                extAlias := a.AssetId.String()
                asset.Alias = &(extAlias)
-               a.reg.SaveAsset(asset, extAlias)
+               a.reg.SaveExtAsset(asset, extAlias)
        }
 
        arguments := [][]byte{}