import (
"context"
stdjson "encoding/json"
- "time"
+ "fmt"
log "github.com/sirupsen/logrus"
"github.com/vapor/blockchain/txbuilder"
- chainjson "github.com/vapor/encoding/json"
+ "github.com/vapor/consensus/federation"
"github.com/vapor/errors"
"github.com/vapor/protocol/bc"
"github.com/vapor/protocol/bc/types"
- "github.com/vapor/testutil"
)
// DecodeCrossInAction convert input data to action struct
func (r *Registry) DecodeCrossInAction(data []byte) (txbuilder.Action, error) {
- a := &crossInAction{assets: r}
+ a := &crossInAction{reg: r}
err := stdjson.Unmarshal(data, a)
return a, err
}
type crossInAction struct {
- assets *Registry
+ reg *Registry
bc.AssetAmount
- SourceID string `json:"source_id"` // AnnotatedUTXO
+ SourceID bc.Hash `json:"source_id"`
SourcePos uint64 `json:"source_pos"`
- Program chainjson.HexBytes `json:"control_program"`
AssetDefinition map[string]interface{} `json:"asset_definition"`
- Arguments []chainjson.HexBytes `json:"arguments"`
}
-// func (reg *Registry) DefineCrossInAsset(a *Asset, alias string) error {
-// defHash := bc.NewHash(sha3.Sum256(rawDefinition))
-// a := &Asset{
-// DefinitionMap: definition,
-// RawDefinitionByte: rawDefinition,
-// VMVersion: vmver,
-// IssuanceProgram: issuanceProgram,
-// AssetID: bc.ComputeAssetID(issuanceProgram, vmver, &defHash),
-// Alias: &alias,
-// }
-// return a, reg.SaveAsset(a, alias)
-// }
-
-// TODO: also need to hard-code mapTx
-// TODO: iter cross-in and save asset
-// TODO: federation can sign? check arguments length?
func (a *crossInAction) Build(ctx context.Context, builder *txbuilder.TemplateBuilder) error {
var missing []string
- if len(a.Program) == 0 {
- missing = append(missing, "control_program")
- }
- if a.SourceID == "" {
+ if a.SourceID.IsZero() {
missing = append(missing, "source_id")
}
if a.AssetId.IsZero() {
return txbuilder.MissingFieldsError(missing...)
}
- asset := &Asset{}
- var err error
- // Handle asset definition.
- // Asset issuance's legality is guaranteed by the federation.
- if preAsset, _ := a.assets.GetAsset(a.AssetId.String()); preAsset == nil {
- asset.RawDefinitionByte, err = serializeAssetDef(a.AssetDefinition)
- if err != nil {
- return ErrSerializing
- }
-
- // TODO: save AssetDefinition
- if !chainjson.IsValidJSON(asset.RawDefinitionByte) {
- return errors.New("asset definition is not in valid json format")
- }
+ sourceKey := []byte(fmt.Sprintf("SC:%v:%v", a.SourceID, a.SourcePos))
+ a.reg.assetMu.Lock()
+ defer a.reg.assetMu.Unlock()
+ if existed := a.reg.db.Get(sourceKey); existed != nil {
+ return errors.New("mainchain output double spent")
}
- arguments := [][]byte{}
- for _, argument := range a.Arguments {
- arguments = append(arguments, argument)
+ rawDefinitionByte, err := serializeAssetDef(a.AssetDefinition)
+ if err != nil {
+ return ErrSerializing
}
- sourceID := testutil.MustDecodeHash(a.SourceID)
- // TODO: refactor rawDefinition
- txin := types.NewCrossChainInput(arguments, sourceID, *a.AssetId, a.Amount, a.SourcePos, a.Program, asset.RawDefinitionByte)
- log.Info("cross-chain input action build")
- builder.RestrictMinTime(time.Now())
- return builder.AddInput(txin, &txbuilder.SigningInstruction{})
+
+ // 1. arguments will be set when materializeWitnesses
+ // 2. need to fill in issuance program here
+ txin := types.NewCrossChainInput(nil, a.SourceID, *a.AssetId, a.Amount, a.SourcePos, nil, rawDefinitionByte)
+ log.Info("cross-chain input action built")
+ tplIn := &txbuilder.SigningInstruction{}
+ fed := federation.GetFederation()
+ tplIn.AddRawWitnessKeys(fed.XPubs, fed.Path(), fed.Quorum)
+ a.reg.db.Set(sourceKey, []byte("true"))
+ return builder.AddInput(txin, tplIn)
}
func (a *crossInAction) ActionType() string {