4 log "github.com/sirupsen/logrus"
5 "golang.org/x/crypto/sha3"
7 "github.com/vapor/consensus"
8 "github.com/vapor/protocol/bc"
9 "github.com/vapor/protocol/vm"
10 "github.com/vapor/protocol/vm/vmutil"
13 // MapTx converts a types TxData object into its entries-based
15 func MapTx(oldTx *TxData) *bc.Tx {
16 txID, txHeader, entries := mapTx(oldTx)
21 InputIDs: make([]bc.Hash, len(oldTx.Inputs)),
24 spentOutputIDs := make(map[bc.Hash]bool)
25 mainchainOutputIDs := make(map[bc.Hash]bool)
26 for id, e := range entries {
28 switch e := e.(type) {
29 case *bc.CrossChainInput:
31 mainchainOutputIDs[*e.MainchainOutputId] = true
32 if *e.WitnessDestination.Value.AssetId == *consensus.BTMAssetID {
33 tx.GasInputIDs = append(tx.GasInputIDs, id)
38 spentOutputIDs[*e.SpentOutputId] = true
39 if *e.WitnessDestination.Value.AssetId == *consensus.BTMAssetID {
40 tx.GasInputIDs = append(tx.GasInputIDs, id)
45 spentOutputIDs[*e.SpentOutputId] = true
46 if *e.WitnessDestination.Value.AssetId == *consensus.BTMAssetID {
47 tx.GasInputIDs = append(tx.GasInputIDs, id)
52 tx.GasInputIDs = append(tx.GasInputIDs, id)
58 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 prog := &bc.Program{VmVersion: inp.VMVersion, Code: inp.ControlProgram}
144 src := &bc.ValueSource{
146 Value: &inp.AssetAmount,
147 Position: inp.SourcePosition,
150 prevout := bc.NewIntraChainOutput(src, prog, 0) // ordinal doesn't matter
151 mainchainOutputID := addEntry(prevout)
153 assetDefHash := bc.NewHash(sha3.Sum256(inp.AssetDefinition))
154 assetDef := &bc.AssetDefinition{
156 IssuanceProgram: &bc.Program{
157 VmVersion: inp.IssuanceVMVersion,
158 Code: inp.IssuanceProgram,
162 crossIn := bc.NewCrossChainInput(&mainchainOutputID, prog, uint64(i), assetDef)
163 crossIn.WitnessArguments = inp.Arguments
164 crossInID := addEntry(crossIn)
165 muxSources[i] = &bc.ValueSource{
167 Value: &inp.AssetAmount,
169 crossIns = append(crossIns, crossIn)
173 mux := bc.NewMux(muxSources, &bc.Program{VmVersion: 1, Code: []byte{byte(vm.OP_TRUE)}})
174 muxID := addEntry(mux)
176 // connect the inputs to the mux
177 for _, spend := range spends {
178 spentOutput := entryMap[*spend.SpentOutputId].(*bc.IntraChainOutput)
179 spend.SetDestination(&muxID, spentOutput.Source.Value, spend.Ordinal)
182 for _, vetoInput := range vetoInputs {
183 voteOutput := entryMap[*vetoInput.SpentOutputId].(*bc.VoteOutput)
184 vetoInput.SetDestination(&muxID, voteOutput.Source.Value, vetoInput.Ordinal)
187 for _, crossIn := range crossIns {
188 mainchainOutput := entryMap[*crossIn.MainchainOutputId].(*bc.IntraChainOutput)
189 crossIn.SetDestination(&muxID, mainchainOutput.Source.Value, crossIn.Ordinal)
193 coinbase.SetDestination(&muxID, mux.Sources[0].Value, 0)
196 // convert types.outputs to the bc.output
197 var resultIDs []*bc.Hash
198 for i, out := range tx.Outputs {
199 value := out.AssetAmount()
200 src := &bc.ValueSource{
207 // must deal with retirement first due to cases' priorities in the switch statement
208 case vmutil.IsUnspendable(out.ControlProgram()):
210 r := bc.NewRetirement(src, uint64(i))
211 resultID = addEntry(r)
213 case out.OutputType() == IntraChainOutputType:
214 // non-retirement intra-chain tx
215 prog := &bc.Program{out.VMVersion(), out.ControlProgram()}
216 o := bc.NewIntraChainOutput(src, prog, uint64(i))
217 resultID = addEntry(o)
219 case out.OutputType() == CrossChainOutputType:
220 // non-retirement cross-chain tx
221 prog := &bc.Program{out.VMVersion(), out.ControlProgram()}
222 o := bc.NewCrossChainOutput(src, prog, uint64(i))
223 resultID = addEntry(o)
225 case out.OutputType() == VoteOutputType:
226 // non-retirement vote tx
227 voteOut, _ := out.TypedOutput.(*VoteOutput)
228 prog := &bc.Program{out.VMVersion(), out.ControlProgram()}
229 o := bc.NewVoteOutput(src, prog, uint64(i), voteOut.Vote)
230 resultID = addEntry(o)
233 log.Warn("unknown outType")
236 dest := &bc.ValueDestination{
241 resultIDs = append(resultIDs, &resultID)
242 mux.WitnessDestinations = append(mux.WitnessDestinations, dest)
245 h := bc.NewTxHeader(tx.Version, tx.SerializedSize, tx.TimeRange, resultIDs)
246 return addEntry(h), h, entryMap
249 func mapBlockHeader(old *BlockHeader) (bc.Hash, *bc.BlockHeader) {
250 bh := bc.NewBlockHeader(old.Version, old.Height, &old.PreviousBlockHash, old.Timestamp, &old.TransactionsMerkleRoot, &old.TransactionStatusHash, old.Witness)
251 return bc.EntryID(bh), bh
254 // MapBlock converts a types block to bc block
255 func MapBlock(old *Block) *bc.Block {
261 b.ID, b.BlockHeader = mapBlockHeader(&old.BlockHeader)
262 for _, oldTx := range old.Transactions {
263 b.Transactions = append(b.Transactions, oldTx.Tx)