OSDN Git Service

versoin1.1.9 (#594)
[bytom/vapor.git] / protocol / bc / types / map.go
index 80fad77..65533e2 100644 (file)
@@ -1,10 +1,13 @@
 package types
 
 import (
-       "github.com/vapor/consensus"
-       "github.com/vapor/protocol/bc"
-       "github.com/vapor/protocol/vm"
-       "github.com/vapor/protocol/vm/vmutil"
+       log "github.com/sirupsen/logrus"
+       "golang.org/x/crypto/sha3"
+
+       "github.com/bytom/vapor/consensus"
+       "github.com/bytom/vapor/protocol/bc"
+       "github.com/bytom/vapor/protocol/vm"
+       "github.com/bytom/vapor/protocol/vm/vmutil"
 )
 
 // MapTx converts a types TxData object into its entries-based
@@ -19,11 +22,16 @@ func MapTx(oldTx *TxData) *bc.Tx {
        }
 
        spentOutputIDs := make(map[bc.Hash]bool)
+       mainchainOutputIDs := make(map[bc.Hash]bool)
        for id, e := range entries {
                var ord uint64
                switch e := e.(type) {
-               case *bc.Issuance:
+               case *bc.CrossChainInput:
                        ord = e.Ordinal
+                       mainchainOutputIDs[*e.MainchainOutputId] = true
+                       if *e.WitnessDestination.Value.AssetId == *consensus.BTMAssetID {
+                               tx.GasInputIDs = append(tx.GasInputIDs, id)
+                       }
 
                case *bc.Spend:
                        ord = e.Ordinal
@@ -32,6 +40,13 @@ func MapTx(oldTx *TxData) *bc.Tx {
                                tx.GasInputIDs = append(tx.GasInputIDs, id)
                        }
 
+               case *bc.VetoInput:
+                       ord = e.Ordinal
+                       spentOutputIDs[*e.SpentOutputId] = true
+                       if *e.WitnessDestination.Value.AssetId == *consensus.BTMAssetID {
+                               tx.GasInputIDs = append(tx.GasInputIDs, id)
+                       }
+
                case *bc.Coinbase:
                        ord = 0
                        tx.GasInputIDs = append(tx.GasInputIDs, id)
@@ -40,15 +55,17 @@ func MapTx(oldTx *TxData) *bc.Tx {
                        continue
                }
 
-               if ord >= uint64(len(oldTx.Inputs)) {
-                       continue
+               if ord < uint64(len(oldTx.Inputs)) {
+                       tx.InputIDs[ord] = id
                }
-               tx.InputIDs[ord] = id
        }
 
        for id := range spentOutputIDs {
                tx.SpentOutputIDs = append(tx.SpentOutputIDs, id)
        }
+       for id := range mainchainOutputIDs {
+               tx.MainchainOutputIDs = append(tx.MainchainOutputIDs, id)
+       }
        return tx
 }
 
@@ -61,36 +78,15 @@ func mapTx(tx *TxData) (headerID bc.Hash, hdr *bc.TxHeader, entryMap map[bc.Hash
        }
 
        var (
-               spends    []*bc.Spend
-               issuances []*bc.Issuance
-               coinbase  *bc.Coinbase
+               spends     []*bc.Spend
+               vetoInputs []*bc.VetoInput
+               crossIns   []*bc.CrossChainInput
+               coinbase   *bc.Coinbase
        )
 
        muxSources := make([]*bc.ValueSource, len(tx.Inputs))
        for i, input := range tx.Inputs {
                switch inp := input.TypedInput.(type) {
-               case *IssuanceInput:
-                       nonceHash := inp.NonceHash()
-                       assetDefHash := inp.AssetDefinitionHash()
-                       value := input.AssetAmount()
-
-                       issuance := bc.NewIssuance(&nonceHash, &value, uint64(i))
-                       issuance.WitnessAssetDefinition = &bc.AssetDefinition{
-                               Data: &assetDefHash,
-                               IssuanceProgram: &bc.Program{
-                                       VmVersion: inp.VMVersion,
-                                       Code:      inp.IssuanceProgram,
-                               },
-                       }
-                       issuance.WitnessArguments = inp.Arguments
-                       issuanceID := addEntry(issuance)
-
-                       muxSources[i] = &bc.ValueSource{
-                               Ref:   &issuanceID,
-                               Value: &value,
-                       }
-                       issuances = append(issuances, issuance)
-
                case *SpendInput:
                        // create entry for prevout
                        prog := &bc.Program{VmVersion: inp.VMVersion, Code: inp.ControlProgram}
@@ -99,7 +95,7 @@ func mapTx(tx *TxData) (headerID bc.Hash, hdr *bc.TxHeader, entryMap map[bc.Hash
                                Value:    &inp.AssetAmount,
                                Position: inp.SourcePosition,
                        }
-                       prevout := bc.NewOutput(src, prog, 0) // ordinal doesn't matter for prevouts, only for result outputs
+                       prevout := bc.NewIntraChainOutput(src, prog, 0) // ordinal doesn't matter for prevouts, only for result outputs
                        prevoutID := addEntry(prevout)
                        // create entry for spend
                        spend := bc.NewSpend(&prevoutID, uint64(i))
@@ -115,12 +111,66 @@ func mapTx(tx *TxData) (headerID bc.Hash, hdr *bc.TxHeader, entryMap map[bc.Hash
                case *CoinbaseInput:
                        coinbase = bc.NewCoinbase(inp.Arbitrary)
                        coinbaseID := addEntry(coinbase)
+                       totalAmount := uint64(0)
+                       for _, out := range tx.Outputs {
+                               totalAmount += out.AssetAmount().Amount
+                       }
+                       muxSources[i] = &bc.ValueSource{
+                               Ref: &coinbaseID,
+                               Value: &bc.AssetAmount{
+                                       AssetId: consensus.BTMAssetID,
+                                       Amount:  totalAmount,
+                               },
+                       }
 
-                       out := tx.Outputs[0]
+               case *VetoInput:
+                       prog := &bc.Program{VmVersion: inp.VMVersion, Code: inp.ControlProgram}
+                       src := &bc.ValueSource{
+                               Ref:      &inp.SourceID,
+                               Value:    &inp.AssetAmount,
+                               Position: inp.SourcePosition,
+                       }
+                       prevout := bc.NewVoteOutput(src, prog, 0, inp.Vote) // ordinal doesn't matter for prevouts, only for result outputs
+                       prevoutID := addEntry(prevout)
+                       // create entry for VetoInput
+                       vetoInput := bc.NewVetoInput(&prevoutID, uint64(i))
+                       vetoInput.WitnessArguments = inp.Arguments
+                       vetoVoteID := addEntry(vetoInput)
+                       // setup mux
                        muxSources[i] = &bc.ValueSource{
-                               Ref:   &coinbaseID,
-                               Value: &out.AssetAmount,
+                               Ref:   &vetoVoteID,
+                               Value: &inp.AssetAmount,
+                       }
+                       vetoInputs = append(vetoInputs, vetoInput)
+
+               case *CrossChainInput:
+                       prog := &bc.Program{VmVersion: inp.VMVersion, Code: inp.ControlProgram}
+                       src := &bc.ValueSource{
+                               Ref:      &inp.SourceID,
+                               Value:    &inp.AssetAmount,
+                               Position: inp.SourcePosition,
                        }
+
+                       prevout := bc.NewIntraChainOutput(src, prog, 0) // ordinal doesn't matter
+                       mainchainOutputID := addEntry(prevout)
+
+                       assetDefHash := bc.NewHash(sha3.Sum256(inp.AssetDefinition))
+                       assetDef := &bc.AssetDefinition{
+                               Data: &assetDefHash,
+                               IssuanceProgram: &bc.Program{
+                                       VmVersion: inp.IssuanceVMVersion,
+                                       Code:      inp.IssuanceProgram,
+                               },
+                       }
+
+                       crossIn := bc.NewCrossChainInput(&mainchainOutputID, prog, uint64(i), assetDef, inp.AssetDefinition)
+                       crossIn.WitnessArguments = inp.Arguments
+                       crossInID := addEntry(crossIn)
+                       muxSources[i] = &bc.ValueSource{
+                               Ref:   &crossInID,
+                               Value: &inp.AssetAmount,
+                       }
+                       crossIns = append(crossIns, crossIn)
                }
        }
 
@@ -129,11 +179,18 @@ func mapTx(tx *TxData) (headerID bc.Hash, hdr *bc.TxHeader, entryMap map[bc.Hash
 
        // connect the inputs to the mux
        for _, spend := range spends {
-               spentOutput := entryMap[*spend.SpentOutputId].(*bc.Output)
+               spentOutput := entryMap[*spend.SpentOutputId].(*bc.IntraChainOutput)
                spend.SetDestination(&muxID, spentOutput.Source.Value, spend.Ordinal)
        }
-       for _, issuance := range issuances {
-               issuance.SetDestination(&muxID, issuance.Value, issuance.Ordinal)
+
+       for _, vetoInput := range vetoInputs {
+               voteOutput := entryMap[*vetoInput.SpentOutputId].(*bc.VoteOutput)
+               vetoInput.SetDestination(&muxID, voteOutput.Source.Value, vetoInput.Ordinal)
+       }
+
+       for _, crossIn := range crossIns {
+               mainchainOutput := entryMap[*crossIn.MainchainOutputId].(*bc.IntraChainOutput)
+               crossIn.SetDestination(&muxID, mainchainOutput.Source.Value, crossIn.Ordinal)
        }
 
        if coinbase != nil {
@@ -143,21 +200,41 @@ func mapTx(tx *TxData) (headerID bc.Hash, hdr *bc.TxHeader, entryMap map[bc.Hash
        // convert types.outputs to the bc.output
        var resultIDs []*bc.Hash
        for i, out := range tx.Outputs {
+               value := out.AssetAmount()
                src := &bc.ValueSource{
                        Ref:      &muxID,
-                       Value:    &out.AssetAmount,
+                       Value:    &value,
                        Position: uint64(i),
                }
                var resultID bc.Hash
-               if vmutil.IsUnspendable(out.ControlProgram) {
+               switch {
+               // must deal with retirement first due to cases' priorities in the switch statement
+               case vmutil.IsUnspendable(out.ControlProgram()):
                        // retirement
                        r := bc.NewRetirement(src, uint64(i))
                        resultID = addEntry(r)
-               } else {
-                       // non-retirement
-                       prog := &bc.Program{out.VMVersion, out.ControlProgram}
-                       o := bc.NewOutput(src, prog, uint64(i))
+
+               case out.OutputType() == IntraChainOutputType:
+                       // non-retirement intra-chain tx
+                       prog := &bc.Program{out.VMVersion(), out.ControlProgram()}
+                       o := bc.NewIntraChainOutput(src, prog, uint64(i))
+                       resultID = addEntry(o)
+
+               case out.OutputType() == CrossChainOutputType:
+                       // non-retirement cross-chain tx
+                       prog := &bc.Program{out.VMVersion(), out.ControlProgram()}
+                       o := bc.NewCrossChainOutput(src, prog, uint64(i))
                        resultID = addEntry(o)
+
+               case out.OutputType() == VoteOutputType:
+                       // non-retirement vote tx
+                       voteOut, _ := out.TypedOutput.(*VoteOutput)
+                       prog := &bc.Program{out.VMVersion(), out.ControlProgram()}
+                       o := bc.NewVoteOutput(src, prog, uint64(i), voteOut.Vote)
+                       resultID = addEntry(o)
+
+               default:
+                       log.Warn("unknown outType")
                }
 
                dest := &bc.ValueDestination{
@@ -174,7 +251,7 @@ func mapTx(tx *TxData) (headerID bc.Hash, hdr *bc.TxHeader, entryMap map[bc.Hash
 }
 
 func mapBlockHeader(old *BlockHeader) (bc.Hash, *bc.BlockHeader) {
-       bh := bc.NewBlockHeader(old.Version, old.Height, &old.PreviousBlockHash, old.Timestamp, &old.TransactionsMerkleRoot, &old.TransactionStatusHash, old.Nonce, old.Bits)
+       bh := bc.NewBlockHeader(old.Version, old.Height, &old.PreviousBlockHash, old.Timestamp, &old.TransactionsMerkleRoot, &old.TransactionStatusHash, old.Witness)
        return bc.EntryID(bh), bh
 }