4 log "github.com/sirupsen/logrus"
6 "github.com/vapor/consensus"
7 "github.com/vapor/protocol/bc"
8 "github.com/vapor/protocol/vm"
9 "github.com/vapor/protocol/vm/vmutil"
12 // MapTx converts a types TxData object into its entries-based
14 func MapTx(oldTx *TxData) *bc.Tx {
15 txID, txHeader, entries := mapTx(oldTx)
20 InputIDs: make([]bc.Hash, len(oldTx.Inputs)),
23 spentOutputIDs := make(map[bc.Hash]bool)
24 for id, e := range entries {
26 switch e := e.(type) {
29 spentOutputIDs[*e.SpentOutputId] = true
30 if *e.WitnessDestination.Value.AssetId == *consensus.BTMAssetID {
31 tx.GasInputIDs = append(tx.GasInputIDs, id)
36 tx.GasInputIDs = append(tx.GasInputIDs, id)
42 if ord >= uint64(len(oldTx.Inputs)) {
48 for id := range spentOutputIDs {
49 tx.SpentOutputIDs = append(tx.SpentOutputIDs, id)
54 func mapTx(tx *TxData) (headerID bc.Hash, hdr *bc.TxHeader, entryMap map[bc.Hash]bc.Entry) {
55 entryMap = make(map[bc.Hash]bc.Entry)
56 addEntry := func(e bc.Entry) bc.Hash {
67 muxSources := make([]*bc.ValueSource, len(tx.Inputs))
68 for i, input := range tx.Inputs {
69 switch inp := input.TypedInput.(type) {
71 // create entry for prevout
72 prog := &bc.Program{VmVersion: inp.VMVersion, Code: inp.ControlProgram}
73 src := &bc.ValueSource{
75 Value: &inp.AssetAmount,
76 Position: inp.SourcePosition,
78 prevout := bc.NewIntraChainOutput(src, prog, 0) // ordinal doesn't matter for prevouts, only for result outputs
79 prevoutID := addEntry(prevout)
80 // create entry for spend
81 spend := bc.NewSpend(&prevoutID, uint64(i))
82 spend.WitnessArguments = inp.Arguments
83 spendID := addEntry(spend)
85 muxSources[i] = &bc.ValueSource{
87 Value: &inp.AssetAmount,
89 spends = append(spends, spend)
92 coinbase = bc.NewCoinbase(inp.Arbitrary)
93 coinbaseID := addEntry(coinbase)
96 value := out.AssetAmount()
97 muxSources[i] = &bc.ValueSource{
103 // create entry for prevout
104 prog := &bc.Program{VmVersion: inp.VMVersion, Code: inp.ControlProgram}
105 src := &bc.ValueSource{
107 Value: &inp.AssetAmount,
108 Position: inp.SourcePosition,
110 prevout := bc.NewVoteOutput(src, prog, 0, inp.Vote) // ordinal doesn't matter for prevouts, only for result outputs
111 prevoutID := addEntry(prevout)
112 // create entry for spend
113 spend := bc.NewSpend(&prevoutID, uint64(i))
114 spend.WitnessArguments = inp.Arguments
115 spendID := addEntry(spend)
117 muxSources[i] = &bc.ValueSource{
119 Value: &inp.AssetAmount,
121 spends = append(spends, spend)
126 mux := bc.NewMux(muxSources, &bc.Program{VmVersion: 1, Code: []byte{byte(vm.OP_TRUE)}})
127 muxID := addEntry(mux)
129 // connect the inputs to the mux
130 for _, spend := range spends {
131 spentOutput := entryMap[*spend.SpentOutputId].(*bc.IntraChainOutput)
132 spend.SetDestination(&muxID, spentOutput.Source.Value, spend.Ordinal)
136 coinbase.SetDestination(&muxID, mux.Sources[0].Value, 0)
139 // convert types.outputs to the bc.output
140 var resultIDs []*bc.Hash
141 for i, out := range tx.Outputs {
142 value := out.AssetAmount()
143 src := &bc.ValueSource{
150 // must deal with retirement first due to cases' priorities in the switch statement
151 case vmutil.IsUnspendable(out.ControlProgram()):
153 r := bc.NewRetirement(src, uint64(i))
154 resultID = addEntry(r)
156 case out.OutputType() == IntraChainOutputType:
157 // non-retirement intra-chain tx
158 prog := &bc.Program{out.VMVersion(), out.ControlProgram()}
159 o := bc.NewIntraChainOutput(src, prog, uint64(i))
160 resultID = addEntry(o)
162 case out.OutputType() == CrossChainOutputType:
163 // non-retirement cross-chain tx
164 prog := &bc.Program{out.VMVersion(), out.ControlProgram()}
165 o := bc.NewCrossChainOutput(src, prog, uint64(i))
166 resultID = addEntry(o)
168 case out.OutputType() == VoteOutputType:
169 // non-retirement vote tx
170 voteOut, _ := out.TypedOutput.(*VoteTxOutput)
171 prog := &bc.Program{out.VMVersion(), out.ControlProgram()}
172 o := bc.NewVoteOutput(src, prog, uint64(i), voteOut.Vote)
173 resultID = addEntry(o)
176 log.Warn("unknown outType")
179 dest := &bc.ValueDestination{
184 resultIDs = append(resultIDs, &resultID)
185 mux.WitnessDestinations = append(mux.WitnessDestinations, dest)
188 h := bc.NewTxHeader(tx.Version, tx.SerializedSize, tx.TimeRange, resultIDs)
189 return addEntry(h), h, entryMap
192 func mapBlockHeader(old *BlockHeader) (bc.Hash, *bc.BlockHeader) {
193 bh := bc.NewBlockHeader(old.Version, old.Height, &old.PreviousBlockHash, old.Timestamp, &old.TransactionsMerkleRoot, &old.TransactionStatusHash, old.Witness)
194 return bc.EntryID(bh), bh
197 // MapBlock converts a types block to bc block
198 func MapBlock(old *Block) *bc.Block {
204 b.ID, b.BlockHeader = mapBlockHeader(&old.BlockHeader)
205 for _, oldTx := range old.Transactions {
206 b.Transactions = append(b.Transactions, oldTx.Tx)