OSDN Git Service

9a3e11e7741a199c959e43f08ddafad9d209e835
[bytom/vapor.git] / protocol / bc / types / map.go
1 package types
2
3 import (
4         "github.com/vapor/consensus"
5         "github.com/vapor/protocol/bc"
6         "github.com/vapor/protocol/vm"
7         "github.com/vapor/protocol/vm/vmutil"
8 )
9
10 // MapTx converts a types TxData object into its entries-based
11 // representation.
12 func MapTx(oldTx *TxData) *bc.Tx {
13         txID, txHeader, entries := mapTx(oldTx)
14         tx := &bc.Tx{
15                 TxHeader: txHeader,
16                 ID:       txID,
17                 Entries:  entries,
18                 InputIDs: make([]bc.Hash, len(oldTx.Inputs)),
19         }
20
21         spentOutputIDs := make(map[bc.Hash]bool)
22         for id, e := range entries {
23                 var ord uint64
24                 switch e := e.(type) {
25                 case *bc.Issuance:
26                         ord = e.Ordinal
27
28                 case *bc.Spend:
29                         ord = e.Ordinal
30                         spentOutputIDs[*e.SpentOutputId] = true
31                         if *e.WitnessDestination.Value.AssetId == *consensus.BTMAssetID {
32                                 tx.GasInputIDs = append(tx.GasInputIDs, id)
33                         }
34
35                 case *bc.Coinbase:
36                         ord = 0
37                 case *bc.Claim:
38                         ord = 0
39                 default:
40                         continue
41                 }
42
43                 if ord >= uint64(len(oldTx.Inputs)) {
44                         continue
45                 }
46                 tx.InputIDs[ord] = id
47         }
48
49         for id := range spentOutputIDs {
50                 tx.SpentOutputIDs = append(tx.SpentOutputIDs, id)
51         }
52         return tx
53 }
54
55 func mapTx(tx *TxData) (headerID bc.Hash, hdr *bc.TxHeader, entryMap map[bc.Hash]bc.Entry) {
56         entryMap = make(map[bc.Hash]bc.Entry)
57         addEntry := func(e bc.Entry) bc.Hash {
58                 id := bc.EntryID(e)
59                 entryMap[id] = e
60                 return id
61         }
62
63         var (
64                 spends    []*bc.Spend
65                 issuances []*bc.Issuance
66                 coinbase  *bc.Coinbase
67                 claim     *bc.Claim
68         )
69
70         muxSources := make([]*bc.ValueSource, len(tx.Inputs))
71         for i, input := range tx.Inputs {
72                 switch inp := input.TypedInput.(type) {
73                 case *IssuanceInput:
74                         nonceHash := inp.NonceHash()
75                         assetDefHash := inp.AssetDefinitionHash()
76                         value := input.AssetAmount()
77
78                         issuance := bc.NewIssuance(&nonceHash, &value, uint64(i))
79                         issuance.WitnessAssetDefinition = &bc.AssetDefinition{
80                                 Data: &assetDefHash,
81                                 IssuanceProgram: &bc.Program{
82                                         VmVersion: inp.VMVersion,
83                                         Code:      inp.IssuanceProgram,
84                                 },
85                         }
86                         issuance.WitnessArguments = inp.Arguments
87                         issuanceID := addEntry(issuance)
88
89                         muxSources[i] = &bc.ValueSource{
90                                 Ref:   &issuanceID,
91                                 Value: &value,
92                         }
93                         issuances = append(issuances, issuance)
94
95                 case *SpendInput:
96                         // create entry for prevout
97                         prog := &bc.Program{VmVersion: inp.VMVersion, Code: inp.ControlProgram}
98                         src := &bc.ValueSource{
99                                 Ref:      &inp.SourceID,
100                                 Value:    &inp.AssetAmount,
101                                 Position: inp.SourcePosition,
102                         }
103                         prevout := bc.NewOutput(src, prog, 0) // ordinal doesn't matter for prevouts, only for result outputs
104                         prevoutID := addEntry(prevout)
105                         // create entry for spend
106                         spend := bc.NewSpend(&prevoutID, uint64(i))
107                         spend.WitnessArguments = inp.Arguments
108                         spendID := addEntry(spend)
109                         // setup mux
110                         muxSources[i] = &bc.ValueSource{
111                                 Ref:   &spendID,
112                                 Value: &inp.AssetAmount,
113                         }
114                         spends = append(spends, spend)
115
116                 case *CoinbaseInput:
117                         coinbase = bc.NewCoinbase(inp.Arbitrary)
118                         coinbaseID := addEntry(coinbase)
119
120                         out := tx.Outputs[0]
121                         muxSources[i] = &bc.ValueSource{
122                                 Ref:   &coinbaseID,
123                                 Value: &out.AssetAmount,
124                         }
125                 case *ClaimInput:
126                         // create entry for prevout
127                         prog := &bc.Program{VmVersion: inp.VMVersion, Code: inp.ControlProgram}
128                         src := &bc.ValueSource{
129                                 Ref:      &inp.SourceID,
130                                 Value:    &inp.AssetAmount,
131                                 Position: inp.SourcePosition,
132                         }
133                         prevout := bc.NewOutput(src, prog, 0) // ordinal doesn't matter for prevouts, only for result outputs
134                         prevoutID := addEntry(prevout)
135
136                         claim = bc.NewClaim(&prevoutID, uint64(i), input.Peginwitness)
137                         claim.WitnessArguments = inp.Arguments
138                         claimID := addEntry(claim)
139                         muxSources[i] = &bc.ValueSource{
140                                 Ref:   &claimID,
141                                 Value: &inp.AssetAmount,
142                         }
143                 }
144         }
145
146         mux := bc.NewMux(muxSources, &bc.Program{VmVersion: 1, Code: []byte{byte(vm.OP_TRUE)}})
147         muxID := addEntry(mux)
148
149         // connect the inputs to the mux
150         for _, spend := range spends {
151                 spentOutput := entryMap[*spend.SpentOutputId].(*bc.Output)
152                 spend.SetDestination(&muxID, spentOutput.Source.Value, spend.Ordinal)
153         }
154         for _, issuance := range issuances {
155                 issuance.SetDestination(&muxID, issuance.Value, issuance.Ordinal)
156         }
157
158         if coinbase != nil {
159                 coinbase.SetDestination(&muxID, mux.Sources[0].Value, 0)
160         }
161
162         if claim != nil {
163                 claim.SetDestination(&muxID, mux.Sources[0].Value, 0)
164         }
165
166         // convert types.outputs to the bc.output
167         var resultIDs []*bc.Hash
168         for i, out := range tx.Outputs {
169                 src := &bc.ValueSource{
170                         Ref:      &muxID,
171                         Value:    &out.AssetAmount,
172                         Position: uint64(i),
173                 }
174                 var resultID bc.Hash
175                 if vmutil.IsUnspendable(out.ControlProgram) {
176                         // retirement
177                         r := bc.NewRetirement(src, uint64(i))
178                         resultID = addEntry(r)
179                 } else {
180                         // non-retirement
181                         prog := &bc.Program{out.VMVersion, out.ControlProgram}
182                         o := bc.NewOutput(src, prog, uint64(i))
183                         resultID = addEntry(o)
184                 }
185
186                 dest := &bc.ValueDestination{
187                         Value:    src.Value,
188                         Ref:      &resultID,
189                         Position: 0,
190                 }
191                 resultIDs = append(resultIDs, &resultID)
192                 mux.WitnessDestinations = append(mux.WitnessDestinations, dest)
193         }
194         h := bc.NewTxHeader(tx.Version, tx.SerializedSize, tx.TimeRange, resultIDs)
195         return addEntry(h), h, entryMap
196 }
197
198 func mapBlockHeader(old *BlockHeader) (bc.Hash, *bc.BlockHeader) {
199         proof := &bc.Proof{Sign: old.Proof.Sign, ControlProgram: old.Proof.ControlProgram}
200         bh := bc.NewBlockHeader(old.Version, old.Height, &old.PreviousBlockHash, old.Timestamp, &old.TransactionsMerkleRoot, &old.TransactionStatusHash, proof)
201         return bc.EntryID(bh), bh
202 }
203
204 // MapBlock converts a types block to bc block
205 func MapBlock(old *Block) *bc.Block {
206         if old == nil {
207                 return nil
208         }
209
210         b := new(bc.Block)
211         b.ID, b.BlockHeader = mapBlockHeader(&old.BlockHeader)
212         for _, oldTx := range old.Transactions {
213                 b.Transactions = append(b.Transactions, oldTx.Tx)
214         }
215         return b
216 }