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 mainchainOutputIDs := make(map[bc.Hash]bool)
25 for id, e := range entries {
27 switch e := e.(type) {
28 case *bc.CrossChainInput:
30 mainchainOutputIDs[*e.MainchainOutputId] = true
31 if *e.WitnessDestination.Value.AssetId == *consensus.BTMAssetID {
32 tx.GasInputIDs = append(tx.GasInputIDs, id)
37 spentOutputIDs[*e.SpentOutputId] = true
38 if *e.WitnessDestination.Value.AssetId == *consensus.BTMAssetID {
39 tx.GasInputIDs = append(tx.GasInputIDs, id)
44 spentOutputIDs[*e.SpentOutputId] = true
45 if *e.WitnessDestination.Value.AssetId == *consensus.BTMAssetID {
46 tx.GasInputIDs = append(tx.GasInputIDs, id)
51 tx.GasInputIDs = append(tx.GasInputIDs, id)
57 if ord >= uint64(len(oldTx.Inputs)) {
63 for id := range spentOutputIDs {
64 tx.SpentOutputIDs = append(tx.SpentOutputIDs, id)
66 for id := range mainchainOutputIDs {
67 tx.MainchainOutputIDs = append(tx.MainchainOutputIDs, id)
72 func mapTx(tx *TxData) (headerID bc.Hash, hdr *bc.TxHeader, entryMap map[bc.Hash]bc.Entry) {
73 entryMap = make(map[bc.Hash]bc.Entry)
74 addEntry := func(e bc.Entry) bc.Hash {
82 vetoInputs []*bc.VetoInput
83 crossIns []*bc.CrossChainInput
87 muxSources := make([]*bc.ValueSource, len(tx.Inputs))
88 for i, input := range tx.Inputs {
89 switch inp := input.TypedInput.(type) {
91 // create entry for prevout
92 prog := &bc.Program{VmVersion: inp.VMVersion, Code: inp.ControlProgram}
93 src := &bc.ValueSource{
95 Value: &inp.AssetAmount,
96 Position: inp.SourcePosition,
98 prevout := bc.NewIntraChainOutput(src, prog, 0) // ordinal doesn't matter for prevouts, only for result outputs
99 prevoutID := addEntry(prevout)
100 // create entry for spend
101 spend := bc.NewSpend(&prevoutID, uint64(i))
102 spend.WitnessArguments = inp.Arguments
103 spendID := addEntry(spend)
105 muxSources[i] = &bc.ValueSource{
107 Value: &inp.AssetAmount,
109 spends = append(spends, spend)
112 coinbase = bc.NewCoinbase(inp.Arbitrary)
113 coinbaseID := addEntry(coinbase)
116 value := out.AssetAmount()
117 muxSources[i] = &bc.ValueSource{
123 prog := &bc.Program{VmVersion: inp.VMVersion, Code: inp.ControlProgram}
124 src := &bc.ValueSource{
126 Value: &inp.AssetAmount,
127 Position: inp.SourcePosition,
129 prevout := bc.NewVoteOutput(src, prog, 0, inp.Vote) // ordinal doesn't matter for prevouts, only for result outputs
130 prevoutID := addEntry(prevout)
131 // create entry for VetoInput
132 vetoInput := bc.NewVetoInput(&prevoutID, uint64(i))
133 vetoInput.WitnessArguments = inp.Arguments
134 vetoVoteID := addEntry(vetoInput)
136 muxSources[i] = &bc.ValueSource{
138 Value: &inp.AssetAmount,
140 vetoInputs = append(vetoInputs, vetoInput)
142 case *CrossChainInput:
143 // TODO: fed peg script
144 prog := &bc.Program{VmVersion: inp.VMVersion, Code: inp.ControlProgram}
145 src := &bc.ValueSource{
147 Value: &inp.AssetAmount,
148 Position: inp.SourcePosition,
150 prevout := bc.NewIntraChainOutput(src, prog, 0) // ordinal doesn't matter
151 outputID := bc.EntryID(prevout)
152 crossIn := bc.NewCrossChainInput(&outputID, &inp.AssetAmount, prog, uint64(i))
153 crossIn.WitnessArguments = inp.Arguments
154 crossInID := addEntry(crossIn)
155 muxSources[i] = &bc.ValueSource{
157 Value: &inp.AssetAmount,
159 crossIns = append(crossIns, crossIn)
163 mux := bc.NewMux(muxSources, &bc.Program{VmVersion: 1, Code: []byte{byte(vm.OP_TRUE)}})
164 muxID := addEntry(mux)
166 // connect the inputs to the mux
167 for _, spend := range spends {
168 spentOutput := entryMap[*spend.SpentOutputId].(*bc.IntraChainOutput)
169 spend.SetDestination(&muxID, spentOutput.Source.Value, spend.Ordinal)
172 for _, vetoInput := range vetoInputs {
173 voteOutput := entryMap[*vetoInput.SpentOutputId].(*bc.VoteOutput)
174 vetoInput.SetDestination(&muxID, voteOutput.Source.Value, vetoInput.Ordinal)
177 for _, crossIn := range crossIns {
178 crossIn.SetDestination(&muxID, crossIn.Value, crossIn.Ordinal)
182 coinbase.SetDestination(&muxID, mux.Sources[0].Value, 0)
185 // convert types.outputs to the bc.output
186 var resultIDs []*bc.Hash
187 for i, out := range tx.Outputs {
188 value := out.AssetAmount()
189 src := &bc.ValueSource{
196 // must deal with retirement first due to cases' priorities in the switch statement
197 case vmutil.IsUnspendable(out.ControlProgram()):
199 r := bc.NewRetirement(src, uint64(i))
200 resultID = addEntry(r)
202 case out.OutputType() == IntraChainOutputType:
203 // non-retirement intra-chain tx
204 prog := &bc.Program{out.VMVersion(), out.ControlProgram()}
205 o := bc.NewIntraChainOutput(src, prog, uint64(i))
206 resultID = addEntry(o)
208 case out.OutputType() == CrossChainOutputType:
209 // non-retirement cross-chain tx
210 prog := &bc.Program{out.VMVersion(), out.ControlProgram()}
211 o := bc.NewCrossChainOutput(src, prog, uint64(i))
212 resultID = addEntry(o)
214 case out.OutputType() == VoteOutputType:
215 // non-retirement vote tx
216 voteOut, _ := out.TypedOutput.(*VoteTxOutput)
217 prog := &bc.Program{out.VMVersion(), out.ControlProgram()}
218 o := bc.NewVoteOutput(src, prog, uint64(i), voteOut.Vote)
219 resultID = addEntry(o)
222 log.Warn("unknown outType")
225 dest := &bc.ValueDestination{
230 resultIDs = append(resultIDs, &resultID)
231 mux.WitnessDestinations = append(mux.WitnessDestinations, dest)
234 h := bc.NewTxHeader(tx.Version, tx.SerializedSize, tx.TimeRange, resultIDs)
235 return addEntry(h), h, entryMap
238 func mapBlockHeader(old *BlockHeader) (bc.Hash, *bc.BlockHeader) {
239 bh := bc.NewBlockHeader(old.Version, old.Height, &old.PreviousBlockHash, old.Timestamp, &old.TransactionsMerkleRoot, &old.TransactionStatusHash, old.Witness)
240 return bc.EntryID(bh), bh
243 // MapBlock converts a types block to bc block
244 func MapBlock(old *Block) *bc.Block {
250 b.ID, b.BlockHeader = mapBlockHeader(&old.BlockHeader)
251 for _, oldTx := range old.Transactions {
252 b.Transactions = append(b.Transactions, oldTx.Tx)