OSDN Git Service

0fe229dc7ff4be0d5c56a673e3c84790a68f7e23
[bytom/vapor.git] / protocol / bc / types / map.go
1 package types
2
3 import (
4         log "github.com/sirupsen/logrus"
5         "golang.org/x/crypto/sha3"
6
7         "github.com/vapor/consensus"
8         "github.com/vapor/protocol/bc"
9         "github.com/vapor/protocol/vm"
10         "github.com/vapor/protocol/vm/vmutil"
11 )
12
13 // MapTx converts a types TxData object into its entries-based
14 // representation.
15 func MapTx(oldTx *TxData) *bc.Tx {
16         txID, txHeader, entries := mapTx(oldTx)
17         tx := &bc.Tx{
18                 TxHeader: txHeader,
19                 ID:       txID,
20                 Entries:  entries,
21                 InputIDs: make([]bc.Hash, len(oldTx.Inputs)),
22         }
23
24         spentOutputIDs := make(map[bc.Hash]bool)
25         mainchainOutputIDs := make(map[bc.Hash]bool)
26         for id, e := range entries {
27                 var ord uint64
28                 switch e := e.(type) {
29                 case *bc.CrossChainInput:
30                         ord = e.Ordinal
31                         mainchainOutputIDs[*e.MainchainOutputId] = true
32                         if *e.WitnessDestination.Value.AssetId == *consensus.BTMAssetID {
33                                 tx.GasInputIDs = append(tx.GasInputIDs, id)
34                         }
35
36                 case *bc.Spend:
37                         ord = e.Ordinal
38                         spentOutputIDs[*e.SpentOutputId] = true
39                         if *e.WitnessDestination.Value.AssetId == *consensus.BTMAssetID {
40                                 tx.GasInputIDs = append(tx.GasInputIDs, id)
41                         }
42
43                 case *bc.VetoInput:
44                         ord = e.Ordinal
45                         spentOutputIDs[*e.SpentOutputId] = true
46                         if *e.WitnessDestination.Value.AssetId == *consensus.BTMAssetID {
47                                 tx.GasInputIDs = append(tx.GasInputIDs, id)
48                         }
49
50                 case *bc.Coinbase:
51                         ord = 0
52                         tx.GasInputIDs = append(tx.GasInputIDs, id)
53
54                 default:
55                         continue
56                 }
57
58                 if ord >= uint64(len(oldTx.Inputs)) {
59                         continue
60                 }
61                 tx.InputIDs[ord] = id
62         }
63
64         for id := range spentOutputIDs {
65                 tx.SpentOutputIDs = append(tx.SpentOutputIDs, id)
66         }
67         for id := range mainchainOutputIDs {
68                 tx.MainchainOutputIDs = append(tx.MainchainOutputIDs, id)
69         }
70         return tx
71 }
72
73 func mapTx(tx *TxData) (headerID bc.Hash, hdr *bc.TxHeader, entryMap map[bc.Hash]bc.Entry) {
74         entryMap = make(map[bc.Hash]bc.Entry)
75         addEntry := func(e bc.Entry) bc.Hash {
76                 id := bc.EntryID(e)
77                 entryMap[id] = e
78                 return id
79         }
80
81         var (
82                 spends     []*bc.Spend
83                 vetoInputs []*bc.VetoInput
84                 crossIns   []*bc.CrossChainInput
85                 coinbase   *bc.Coinbase
86         )
87
88         muxSources := make([]*bc.ValueSource, len(tx.Inputs))
89         for i, input := range tx.Inputs {
90                 switch inp := input.TypedInput.(type) {
91                 case *SpendInput:
92                         // create entry for prevout
93                         prog := &bc.Program{VmVersion: inp.VMVersion, Code: inp.ControlProgram}
94                         src := &bc.ValueSource{
95                                 Ref:      &inp.SourceID,
96                                 Value:    &inp.AssetAmount,
97                                 Position: inp.SourcePosition,
98                         }
99                         prevout := bc.NewIntraChainOutput(src, prog, 0) // ordinal doesn't matter for prevouts, only for result outputs
100                         prevoutID := addEntry(prevout)
101                         // create entry for spend
102                         spend := bc.NewSpend(&prevoutID, uint64(i))
103                         spend.WitnessArguments = inp.Arguments
104                         spendID := addEntry(spend)
105                         // setup mux
106                         muxSources[i] = &bc.ValueSource{
107                                 Ref:   &spendID,
108                                 Value: &inp.AssetAmount,
109                         }
110                         spends = append(spends, spend)
111
112                 case *CoinbaseInput:
113                         coinbase = bc.NewCoinbase(inp.Arbitrary)
114                         coinbaseID := addEntry(coinbase)
115
116                         out := tx.Outputs[0]
117                         value := out.AssetAmount()
118                         muxSources[i] = &bc.ValueSource{
119                                 Ref:   &coinbaseID,
120                                 Value: &value,
121                         }
122
123                 case *VetoInput:
124                         prog := &bc.Program{VmVersion: inp.VMVersion, Code: inp.ControlProgram}
125                         src := &bc.ValueSource{
126                                 Ref:      &inp.SourceID,
127                                 Value:    &inp.AssetAmount,
128                                 Position: inp.SourcePosition,
129                         }
130                         prevout := bc.NewVoteOutput(src, prog, 0, inp.Vote) // ordinal doesn't matter for prevouts, only for result outputs
131                         prevoutID := addEntry(prevout)
132                         // create entry for VetoInput
133                         vetoInput := bc.NewVetoInput(&prevoutID, uint64(i))
134                         vetoInput.WitnessArguments = inp.Arguments
135                         vetoVoteID := addEntry(vetoInput)
136                         // setup mux
137                         muxSources[i] = &bc.ValueSource{
138                                 Ref:   &vetoVoteID,
139                                 Value: &inp.AssetAmount,
140                         }
141                         vetoInputs = append(vetoInputs, vetoInput)
142
143                 case *CrossChainInput:
144                         prog := &bc.Program{VmVersion: inp.VMVersion, Code: inp.ControlProgram}
145                         src := &bc.ValueSource{
146                                 Ref:      &inp.SourceID,
147                                 Value:    &inp.AssetAmount,
148                                 Position: inp.SourcePosition,
149                         }
150
151                         prevout := bc.NewIntraChainOutput(src, prog, 0) // ordinal doesn't matter
152                         outputID := bc.EntryID(prevout)
153
154                         assetDefHash := bc.NewHash(sha3.Sum256(inp.AssetDefinition))
155                         assetDef := &bc.AssetDefinition{
156                                 Data: &assetDefHash,
157                                 IssuanceProgram: &bc.Program{
158                                         VmVersion: inp.VMVersion,
159                                         Code:      inp.IssuanceProgram,
160                                 },
161                         }
162
163                         crossIn := bc.NewCrossChainInput(&outputID, &inp.AssetAmount, prog, uint64(i), assetDef)
164                         crossIn.WitnessArguments = inp.Arguments
165                         crossInID := addEntry(crossIn)
166                         muxSources[i] = &bc.ValueSource{
167                                 Ref:   &crossInID,
168                                 Value: &inp.AssetAmount,
169                         }
170                         crossIns = append(crossIns, crossIn)
171                 }
172         }
173
174         mux := bc.NewMux(muxSources, &bc.Program{VmVersion: 1, Code: []byte{byte(vm.OP_TRUE)}})
175         muxID := addEntry(mux)
176
177         // connect the inputs to the mux
178         for _, spend := range spends {
179                 spentOutput := entryMap[*spend.SpentOutputId].(*bc.IntraChainOutput)
180                 spend.SetDestination(&muxID, spentOutput.Source.Value, spend.Ordinal)
181         }
182
183         for _, vetoInput := range vetoInputs {
184                 voteOutput := entryMap[*vetoInput.SpentOutputId].(*bc.VoteOutput)
185                 vetoInput.SetDestination(&muxID, voteOutput.Source.Value, vetoInput.Ordinal)
186         }
187
188         for _, crossIn := range crossIns {
189                 crossIn.SetDestination(&muxID, crossIn.Value, crossIn.Ordinal)
190         }
191
192         if coinbase != nil {
193                 coinbase.SetDestination(&muxID, mux.Sources[0].Value, 0)
194         }
195
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{
201                         Ref:      &muxID,
202                         Value:    &value,
203                         Position: uint64(i),
204                 }
205                 var resultID bc.Hash
206                 switch {
207                 // must deal with retirement first due to cases' priorities in the switch statement
208                 case vmutil.IsUnspendable(out.ControlProgram()):
209                         // retirement
210                         r := bc.NewRetirement(src, uint64(i))
211                         resultID = addEntry(r)
212
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)
218
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)
224
225                 case out.OutputType() == VoteOutputType:
226                         // non-retirement vote tx
227                         voteOut, _ := out.TypedOutput.(*VoteTxOutput)
228                         prog := &bc.Program{out.VMVersion(), out.ControlProgram()}
229                         o := bc.NewVoteOutput(src, prog, uint64(i), voteOut.Vote)
230                         resultID = addEntry(o)
231
232                 default:
233                         log.Warn("unknown outType")
234                 }
235
236                 dest := &bc.ValueDestination{
237                         Value:    src.Value,
238                         Ref:      &resultID,
239                         Position: 0,
240                 }
241                 resultIDs = append(resultIDs, &resultID)
242                 mux.WitnessDestinations = append(mux.WitnessDestinations, dest)
243         }
244
245         h := bc.NewTxHeader(tx.Version, tx.SerializedSize, tx.TimeRange, resultIDs)
246         return addEntry(h), h, entryMap
247 }
248
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
252 }
253
254 // MapBlock converts a types block to bc block
255 func MapBlock(old *Block) *bc.Block {
256         if old == nil {
257                 return nil
258         }
259
260         b := new(bc.Block)
261         b.ID, b.BlockHeader = mapBlockHeader(&old.BlockHeader)
262         for _, oldTx := range old.Transactions {
263                 b.Transactions = append(b.Transactions, oldTx.Tx)
264         }
265         return b
266 }