OSDN Git Service

Merge pull request #201 from Bytom/v0.1
[bytom/vapor.git] / protocol / bc / types / txinput.go
index 8eaafaf..888c942 100644 (file)
@@ -11,10 +11,10 @@ import (
 
 // serflag variables for input types.
 const (
-       IssuanceInputType uint8 = iota
+       CrossChainInputType uint8 = iota
        SpendInputType
        CoinbaseInputType
-       ClainPeginInputType
+       VetoInputType
 )
 
 type (
@@ -24,7 +24,6 @@ type (
                TypedInput
                CommitmentSuffix []byte
                WitnessSuffix    []byte
-               Peginwitness     [][]byte
        }
 
        // TypedInput return the txinput type.
@@ -38,28 +37,29 @@ var errBadAssetID = errors.New("asset ID does not match other issuance parameter
 // AssetAmount return the asset id and amount of the txinput.
 func (t *TxInput) AssetAmount() bc.AssetAmount {
        switch inp := t.TypedInput.(type) {
-       case *IssuanceInput:
-               assetID := inp.AssetID()
-               return bc.AssetAmount{
-                       AssetId: &assetID,
-                       Amount:  inp.Amount,
-               }
        case *SpendInput:
                return inp.AssetAmount
-       case *ClaimInput:
+
+       case *CrossChainInput:
+               return inp.AssetAmount
+
+       case *VetoInput:
                return inp.AssetAmount
        }
+
        return bc.AssetAmount{}
 }
 
 // AssetID return the assetID of the txinput
 func (t *TxInput) AssetID() bc.AssetID {
        switch inp := t.TypedInput.(type) {
-       case *IssuanceInput:
-               return inp.AssetID()
        case *SpendInput:
                return *inp.AssetId
-       case *ClaimInput:
+
+       case *CrossChainInput:
+               return *inp.AssetAmount.AssetId
+
+       case *VetoInput:
                return *inp.AssetId
 
        }
@@ -69,48 +69,46 @@ func (t *TxInput) AssetID() bc.AssetID {
 // Amount return the asset amount of the txinput
 func (t *TxInput) Amount() uint64 {
        switch inp := t.TypedInput.(type) {
-       case *IssuanceInput:
-               return inp.Amount
        case *SpendInput:
                return inp.Amount
-       case *ClaimInput:
+
+       case *CrossChainInput:
+               return inp.AssetAmount.Amount
+
+       case *VetoInput:
                return inp.Amount
+
        }
        return 0
 }
 
 // ControlProgram return the control program of the spend input
 func (t *TxInput) ControlProgram() []byte {
-       if si, ok := t.TypedInput.(*SpendInput); ok {
-               return si.ControlProgram
-       }
-       return nil
-}
+       switch inp := t.TypedInput.(type) {
+       case *SpendInput:
+               return inp.ControlProgram
 
-// IssuanceProgram return the control program of the issuance input
-func (t *TxInput) IssuanceProgram() []byte {
-       if ii, ok := t.TypedInput.(*IssuanceInput); ok {
-               return ii.IssuanceProgram
-       }
-       return nil
-}
+       case *CrossChainInput:
+               return inp.ControlProgram
+
+       case *VetoInput:
+               return inp.ControlProgram
 
-// AssetDefinition return the asset definition of the issuance input
-func (t *TxInput) AssetDefinition() []byte {
-       if ii, ok := t.TypedInput.(*IssuanceInput); ok {
-               return ii.AssetDefinition
        }
+
        return nil
 }
 
 // Arguments get the args for the input
 func (t *TxInput) Arguments() [][]byte {
        switch inp := t.TypedInput.(type) {
-       case *IssuanceInput:
-               return inp.Arguments
        case *SpendInput:
                return inp.Arguments
-       case *ClaimInput:
+
+       case *CrossChainInput:
+               return inp.Arguments
+
+       case *VetoInput:
                return inp.Arguments
        }
        return nil
@@ -119,20 +117,27 @@ func (t *TxInput) Arguments() [][]byte {
 // SetArguments set the args for the input
 func (t *TxInput) SetArguments(args [][]byte) {
        switch inp := t.TypedInput.(type) {
-       case *IssuanceInput:
-               inp.Arguments = args
        case *SpendInput:
                inp.Arguments = args
-       case *ClaimInput:
+
+       case *CrossChainInput:
+               inp.Arguments = args
+
+       case *VetoInput:
                inp.Arguments = args
        }
 }
 
 // SpentOutputID calculate the hash of spended output
 func (t *TxInput) SpentOutputID() (o bc.Hash, err error) {
-       if si, ok := t.TypedInput.(*SpendInput); ok {
-               o, err = ComputeOutputID(&si.SpendCommitment)
+       switch inp := t.TypedInput.(type) {
+       case *SpendInput:
+               o, err = ComputeOutputID(&inp.SpendCommitment, SpendInputType, nil)
+
+       case *VetoInput:
+               o, err = ComputeOutputID(&inp.SpendCommitment, VetoInputType, inp.Vote)
        }
+
        return o, err
 }
 
@@ -141,10 +146,6 @@ func (t *TxInput) readFrom(r *blockchain.Reader) (err error) {
                return err
        }
 
-       if t.Peginwitness, err = blockchain.ReadVarstrList(r); err != nil {
-               return err
-       }
-       var assetID bc.AssetID
        t.CommitmentSuffix, err = blockchain.ReadExtensibleString(r, func(r *blockchain.Reader) error {
                if t.AssetVersion != 1 {
                        return nil
@@ -153,39 +154,37 @@ func (t *TxInput) readFrom(r *blockchain.Reader) (err error) {
                if _, err = io.ReadFull(r, icType[:]); err != nil {
                        return errors.Wrap(err, "reading input commitment type")
                }
-               switch icType[0] {
-               case IssuanceInputType:
-                       ii := new(IssuanceInput)
-                       t.TypedInput = ii
-
-                       if ii.Nonce, err = blockchain.ReadVarstr31(r); err != nil {
-                               return err
-                       }
-                       if _, err = assetID.ReadFrom(r); err != nil {
-                               return err
-                       }
-                       if ii.Amount, err = blockchain.ReadVarint63(r); err != nil {
-                               return err
-                       }
 
+               switch icType[0] {
                case SpendInputType:
                        si := new(SpendInput)
                        t.TypedInput = si
                        if si.SpendCommitmentSuffix, err = si.SpendCommitment.readFrom(r, 1); err != nil {
                                return err
                        }
-               case ClainPeginInputType:
-                       ci := new(ClaimInput)
-                       t.TypedInput = ci
-                       if ci.SpendCommitmentSuffix, err = ci.SpendCommitment.readFrom(r, 1); err != nil {
-                               return err
-                       }
+
                case CoinbaseInputType:
                        ci := new(CoinbaseInput)
                        t.TypedInput = ci
                        if ci.Arbitrary, err = blockchain.ReadVarstr31(r); err != nil {
                                return err
                        }
+
+               case CrossChainInputType:
+                       ci := new(CrossChainInput)
+                       t.TypedInput = ci
+                       if ci.SpendCommitmentSuffix, err = ci.SpendCommitment.readFrom(r, 1); err != nil {
+                               return err
+                       }
+
+               case VetoInputType:
+                       ui := new(VetoInput)
+                       t.TypedInput = ui
+                       if ui.VetoCommitmentSuffix, err = ui.SpendCommitment.readFrom(r, 1); err != nil {
+
+                               return err
+                       }
+
                default:
                        return fmt.Errorf("unsupported input type %d", icType[0])
                }
@@ -201,36 +200,44 @@ func (t *TxInput) readFrom(r *blockchain.Reader) (err error) {
                }
 
                switch inp := t.TypedInput.(type) {
-               case *IssuanceInput:
-                       if inp.AssetDefinition, err = blockchain.ReadVarstr31(r); err != nil {
+               case *SpendInput:
+                       if inp.Arguments, err = blockchain.ReadVarstrList(r); err != nil {
                                return err
                        }
-                       if inp.VMVersion, err = blockchain.ReadVarint63(r); err != nil {
+
+               case *CrossChainInput:
+                       if inp.Arguments, err = blockchain.ReadVarstrList(r); err != nil {
                                return err
                        }
-                       if inp.IssuanceProgram, err = blockchain.ReadVarstr31(r); err != nil {
+
+                       if inp.VMVersion, err = blockchain.ReadVarint63(r); err != nil {
                                return err
                        }
-                       if inp.AssetID() != assetID {
-                               return errBadAssetID
+
+                       if inp.AssetDefinition, err = blockchain.ReadVarstr31(r); err != nil {
+                               return err
                        }
-                       if inp.Arguments, err = blockchain.ReadVarstrList(r); err != nil {
+
+                       if inp.IssuanceProgram, err = blockchain.ReadVarstr31(r); err != nil {
                                return err
                        }
 
-               case *SpendInput:
+               case *VetoInput:
                        if inp.Arguments, err = blockchain.ReadVarstrList(r); err != nil {
                                return err
                        }
-               case *ClaimInput:
-                       if inp.Arguments, err = blockchain.ReadVarstrList(r); err != nil {
+                       if inp.Vote, err = blockchain.ReadVarstr31(r); err != nil {
                                return err
                        }
+
                }
                return nil
        })
+       if err != nil {
+               return err
+       }
 
-       return err
+       return nil
 }
 
 func (t *TxInput) writeTo(w io.Writer) error {
@@ -238,15 +245,15 @@ func (t *TxInput) writeTo(w io.Writer) error {
                return errors.Wrap(err, "writing asset version")
        }
 
-       if _, err := blockchain.WriteVarstrList(w, t.Peginwitness); err != nil {
-               return errors.Wrap(err, "writing pegin witness")
-       }
        if _, err := blockchain.WriteExtensibleString(w, t.CommitmentSuffix, t.writeInputCommitment); err != nil {
                return errors.Wrap(err, "writing input commitment")
        }
 
-       _, err := blockchain.WriteExtensibleString(w, t.WitnessSuffix, t.writeInputWitness)
-       return errors.Wrap(err, "writing input witness")
+       if _, err := blockchain.WriteExtensibleString(w, t.WitnessSuffix, t.writeInputWitness); err != nil {
+               return errors.Wrap(err, "writing input witness")
+       }
+
+       return nil
 }
 
 func (t *TxInput) writeInputCommitment(w io.Writer) (err error) {
@@ -255,30 +262,18 @@ func (t *TxInput) writeInputCommitment(w io.Writer) (err error) {
        }
 
        switch inp := t.TypedInput.(type) {
-       case *IssuanceInput:
-               if _, err = w.Write([]byte{IssuanceInputType}); err != nil {
-                       return err
-               }
-               if _, err = blockchain.WriteVarstr31(w, inp.Nonce); err != nil {
-                       return err
-               }
-               assetID := t.AssetID()
-               if _, err = assetID.WriteTo(w); err != nil {
-                       return err
-               }
-               _, err = blockchain.WriteVarint63(w, inp.Amount)
-               return err
-
        case *SpendInput:
                if _, err = w.Write([]byte{SpendInputType}); err != nil {
                        return err
                }
                return inp.SpendCommitment.writeExtensibleString(w, inp.SpendCommitmentSuffix, t.AssetVersion)
-       case *ClaimInput:
-               if _, err = w.Write([]byte{ClainPeginInputType}); err != nil {
+
+       case *CrossChainInput:
+               if _, err = w.Write([]byte{CrossChainInputType}); err != nil {
                        return err
                }
                return inp.SpendCommitment.writeExtensibleString(w, inp.SpendCommitmentSuffix, t.AssetVersion)
+
        case *CoinbaseInput:
                if _, err = w.Write([]byte{CoinbaseInputType}); err != nil {
                        return err
@@ -286,6 +281,12 @@ func (t *TxInput) writeInputCommitment(w io.Writer) (err error) {
                if _, err = blockchain.WriteVarstr31(w, inp.Arbitrary); err != nil {
                        return errors.Wrap(err, "writing coinbase arbitrary")
                }
+
+       case *VetoInput:
+               if _, err = w.Write([]byte{VetoInputType}); err != nil {
+                       return err
+               }
+               return inp.SpendCommitment.writeExtensibleString(w, inp.VetoCommitmentSuffix, t.AssetVersion)
        }
        return nil
 }
@@ -294,27 +295,34 @@ func (t *TxInput) writeInputWitness(w io.Writer) error {
        if t.AssetVersion != 1 {
                return nil
        }
+
        switch inp := t.TypedInput.(type) {
-       case *IssuanceInput:
-               if _, err := blockchain.WriteVarstr31(w, inp.AssetDefinition); err != nil {
+       case *SpendInput:
+               _, err := blockchain.WriteVarstrList(w, inp.Arguments)
+               return err
+
+       case *CrossChainInput:
+               if _, err := blockchain.WriteVarstrList(w, inp.Arguments); err != nil {
                        return err
                }
+
                if _, err := blockchain.WriteVarint63(w, inp.VMVersion); err != nil {
                        return err
                }
-               if _, err := blockchain.WriteVarstr31(w, inp.IssuanceProgram); err != nil {
+
+               if _, err := blockchain.WriteVarstr31(w, inp.AssetDefinition); err != nil {
                        return err
                }
-               _, err := blockchain.WriteVarstrList(w, inp.Arguments)
-               return err
 
-       case *SpendInput:
-               _, err := blockchain.WriteVarstrList(w, inp.Arguments)
-               return err
-       case *ClaimInput:
-               _, err := blockchain.WriteVarstrList(w, inp.Arguments)
+               _, err := blockchain.WriteVarstr31(w, inp.IssuanceProgram)
 
                return err
+       case *VetoInput:
+               if _, err := blockchain.WriteVarstrList(w, inp.Arguments); err != nil {
+                       return err
+               }
+               _, err := blockchain.WriteVarstr31(w, inp.Vote)
+               return err
        }
        return nil
 }