X-Git-Url: http://git.osdn.net/view?a=blobdiff_plain;ds=sidebyside;f=blockchain%2Ftxbuilder%2Factions.go;h=99f35bc28356d8fd610a2cb9f49930e85ef2e1eb;hb=6f7fe6fd7442ddcec8ee959c09b6d45639ef045f;hp=13268be1b7a1a27e9b8ad64088e64a530c591dc8;hpb=b8a3caa70b8053ae98bc2a74a282b283e997ba75;p=bytom%2Fvapor.git diff --git a/blockchain/txbuilder/actions.go b/blockchain/txbuilder/actions.go index 13268be1..99f35bc2 100644 --- a/blockchain/txbuilder/actions.go +++ b/blockchain/txbuilder/actions.go @@ -4,12 +4,9 @@ import ( "context" stdjson "encoding/json" "errors" - "fmt" - "io" - "os" - "strings" - ipfs "github.com/ipfs/go-ipfs-api" + "github.com/vapor/config" + "github.com/vapor/common" "github.com/vapor/consensus" "github.com/vapor/encoding/json" @@ -49,9 +46,9 @@ func (a *controlAddressAction) Build(ctx context.Context, b *TemplateBuilder) er if err != nil { return err } + redeemContract := address.ScriptAddress() program := []byte{} - switch address.(type) { case *common.AddressWitnessPubKeyHash: program, err = vmutil.P2WPKHProgram(redeemContract) @@ -64,7 +61,7 @@ func (a *controlAddressAction) Build(ctx context.Context, b *TemplateBuilder) er return err } - out := types.NewTxOutput(*a.AssetId, a.Amount, program) + out := types.NewIntraChainOutput(*a.AssetId, a.Amount, program) return b.AddOutput(out) } @@ -99,7 +96,7 @@ func (a *controlProgramAction) Build(ctx context.Context, b *TemplateBuilder) er return MissingFieldsError(missing...) } - out := types.NewTxOutput(*a.AssetId, a.Amount, a.Program) + out := types.NewIntraChainOutput(*a.AssetId, a.Amount, a.Program) return b.AddOutput(out) } @@ -135,7 +132,7 @@ func (a *retireAction) Build(ctx context.Context, b *TemplateBuilder) error { if err != nil { return err } - out := types.NewTxOutput(*a.AssetId, a.Amount, program) + out := types.NewIntraChainOutput(*a.AssetId, a.Amount, program) return b.AddOutput(out) } @@ -143,52 +140,156 @@ func (a *retireAction) ActionType() string { return "retire" } -const ( - file uint32 = iota - data -) +// DecodeCrossOutAction convert input data to action struct +func DecodeCrossOutAction(data []byte) (Action, error) { + a := new(crossOutAction) + err := stdjson.Unmarshal(data, a) + return a, err +} + +type crossOutAction struct { + bc.AssetAmount + Address string `json:"address"` +} + +func (a *crossOutAction) Build(ctx context.Context, b *TemplateBuilder) error { + var missing []string + if a.Address == "" { + missing = append(missing, "address") + } + if a.AssetId.IsZero() { + missing = append(missing, "asset_id") + } + if a.Amount == 0 { + missing = append(missing, "amount") + } + if len(missing) > 0 { + return MissingFieldsError(missing...) + } + + address, err := common.DecodeAddress(a.Address, &consensus.MainNetParams) + if err != nil { + return err + } -type dataAction struct { - Type uint32 `json:"type"` - Data string `json:"data"` -} - -func (a *dataAction) Build(ctx context.Context, b *TemplateBuilder) error { - - var r io.Reader - - switch a.Type { - case file: - // 检查文件是否存在 - fi, err := os.Stat(a.Data) - if os.IsNotExist(err) { - return err - } - if fi.IsDir() { - return fmt.Errorf("data [%s] is directory", a.Data) - } - r, err = os.Open(a.Data) - if err != nil { - return err - } - - case data: - if a.Data == "" { - return errors.New("data is empty") - } - // 生成文件对象 - r = strings.NewReader(a.Data) + redeemContract := address.ScriptAddress() + program := []byte{} + switch address.(type) { + case *common.AddressWitnessPubKeyHash: + program, err = vmutil.P2WPKHProgram(redeemContract) + case *common.AddressWitnessScriptHash: + program, err = vmutil.P2WSHProgram(redeemContract) default: + return errors.New("unsupport address type") + } + if err != nil { + return err } - // 连接ipfs节点 - sh := ipfs.NewShell("localhost:5001") - cid, err := sh.Add(r) + out := types.NewCrossChainOutput(*a.AssetId, a.Amount, program) + return b.AddOutput(out) +} + +func (a *crossOutAction) ActionType() string { + return "cross_chain_out" +} + +// DecodeVoteOutputAction convert input data to action struct +func DecodeVoteOutputAction(data []byte) (Action, error) { + a := new(voteOutputAction) + err := stdjson.Unmarshal(data, a) + return a, err +} + +type voteOutputAction struct { + bc.AssetAmount + Address string `json:"address"` + Vote json.HexBytes `json:"vote"` +} + +func (a *voteOutputAction) Build(ctx context.Context, b *TemplateBuilder) error { + var missing []string + if a.Address == "" { + missing = append(missing, "address") + } + if a.AssetId.IsZero() { + missing = append(missing, "asset_id") + } + if a.Amount == 0 { + missing = append(missing, "amount") + } + if len(a.Vote) == 0 { + missing = append(missing, "vote") + } + if len(missing) > 0 { + return MissingFieldsError(missing...) + } + + address, err := common.DecodeAddress(a.Address, &consensus.ActiveNetParams) if err != nil { return err } - fmt.Println(cid) + redeemContract := address.ScriptAddress() + program := []byte{} + switch address.(type) { + case *common.AddressWitnessPubKeyHash: + program, err = vmutil.P2WPKHProgram(redeemContract) + case *common.AddressWitnessScriptHash: + program, err = vmutil.P2WSHProgram(redeemContract) + default: + return errors.New("unsupport address type") + } + if err != nil { + return err + } + + out := types.NewVoteOutput(*a.AssetId, a.Amount, program, a.Vote) + return b.AddOutput(out) +} + +func (a *voteOutputAction) ActionType() string { + return "vote_output" +} + +// DecodeCrossInAction convert input data to action struct +func DecodeCrossInAction(data []byte) (Action, error) { + a := new(crossInAction) + err := stdjson.Unmarshal(data, a) + return a, err +} + +type crossInAction struct { + bc.AssetAmount + SourceID bc.Hash `json:"source_id"` + SourcePos uint64 `json:"source_pos"` + RawDefinitionByte json.HexBytes `json:"raw_definition_byte"` +} + +func (a *crossInAction) Build(ctx context.Context, builder *TemplateBuilder) error { + var missing []string + if a.SourceID.IsZero() { + missing = append(missing, "source_id") + } + if a.AssetId.IsZero() { + missing = append(missing, "asset_id") + } + if a.Amount == 0 { + missing = append(missing, "amount") + } + if len(missing) > 0 { + return MissingFieldsError(missing...) + } + + // arguments will be set when materializeWitnesses + fedProg := config.FederationProgrom(config.CommonConfig) + txin := types.NewCrossChainInput(nil, a.SourceID, *a.AssetId, a.Amount, a.SourcePos, fedProg, a.RawDefinitionByte) + tplIn := &SigningInstruction{} + fed := config.CommonConfig.Federation + tplIn.AddRawWitnessKeys(fed.Xpubs, nil, fed.Quorum) + return builder.AddInput(txin, tplIn) +} - return nil +func (a *crossInAction) ActionType() string { + return "cross_chain_in" }