-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_flight.go
201 lines (167 loc) · 4.49 KB
/
api_flight.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package ethapi
import (
"context"
"github.com/dece-cash/go-dece/common/address"
"github.com/pkg/errors"
"github.com/dece-cash/go-dece/common/hexutil"
"github.com/dece-cash/go-dece/core/types"
"github.com/dece-cash/go-dece/common"
"github.com/dece-cash/go-dece/czero/c_type"
"github.com/dece-cash/go-dece/core/rawdb"
"github.com/dece-cash/go-dece/zero/txs/assets"
"github.com/dece-cash/go-dece/zero/txtool/flight"
"github.com/dece-cash/go-dece/zero/txtool"
)
type PublicFlightAPI struct {
exchange *PublicExchangeAPI
}
func (s *PublicFlightAPI) GetBlocksInfo(ctx context.Context, start uint64, count uint64) ([]txtool.Block, error) {
block, err := flight.SRI_Inst.GetBlocksInfoByDelay(start, count, 0)
if err != nil {
return nil, err
}
return block, err
}
func (s *PublicFlightAPI) GetBlockByNumber(ctx context.Context, blockNum *int64) (map[string]interface{}, error) {
return s.exchange.GetBlockByNumber(ctx, blockNum)
}
type GOutArgs struct {
PKr PKrAddress
Asset assets.Asset
Memo c_type.Uint512
}
func (self *GOutArgs) ToOut() (ret txtool.GOut) {
ret.PKr = *self.PKr.ToPKr()
ret.Asset = self.Asset
ret.Memo = self.Memo
return
}
type PreTxParamArgs struct {
Gas uint64
GasPrice uint64
From PKrAddress
Ins []c_type.Uint256
Outs []GOutArgs
}
func (self *PreTxParamArgs) ToParam() (ret flight.PreTxParam) {
ret.Gas = self.Gas
ret.GasPrice = self.GasPrice
ret.From = *self.From.ToPKr()
ret.Ins = self.Ins
for _, out := range self.Outs {
ret.Outs = append(ret.Outs, out.ToOut())
}
return
}
func (s *PublicFlightAPI) GenTxParam(ctx context.Context, param PreTxParamArgs, tk address.TKAddress) (p txtool.GTxParam, e error) {
preTxParam := param.ToParam()
return flight.GenTxParam(&preTxParam, tk.ToTk())
}
func (s *PublicFlightAPI) CommitTx(ctx context.Context, args *txtool.GTx) error {
return s.exchange.CommitTx(ctx, args)
}
func (s *PublicFlightAPI) Trace2Root(ctx context.Context, tk address.TKAddress, trace c_type.Uint256, base c_type.Uint256) (root c_type.Uint256, e error) {
if r := flight.Trace2Root(tk.ToTk().NewRef(), &trace, &base); r != nil {
root = *r
return
} else {
e = errors.New("can not find the root")
return
}
}
func (s *PublicFlightAPI) GetOut(ctx context.Context, root c_type.Uint256) (out *txtool.Out, e error) {
rt := flight.GetOut(&root, 0)
if rt == nil {
return
} else {
out = &txtool.Out{}
out.Root = root
out.State = *rt
return
}
}
func (s *PublicFlightAPI) GetTx(ctx context.Context, txhash c_type.Uint256) (gtx txtool.GTx, e error) {
hash := common.Hash{}
copy(hash[:], txhash[:])
var tx *types.Transaction
tx, _, _, _ = rawdb.ReadTransaction(s.exchange.b.ChainDb(), hash)
if tx == nil {
tx = s.exchange.b.GetPoolTransaction(hash)
}
if tx != nil {
gtx.Hash = txhash
gtx.Gas = hexutil.Uint64(tx.Gas())
gtx.GasPrice = hexutil.Big(*tx.GasPrice())
gtx.Tx = *tx.GetZZSTX()
return
} else {
e = errors.New("tx not exist")
return
}
}
type TxReceipt struct {
State uint64
TxHash c_type.Uint256
BNum uint64
BHash c_type.Uint256
Outs []c_type.Uint256
Nils []c_type.Uint256
Pkgs []c_type.Uint256
ShareId *c_type.Uint256
PoolId *c_type.Uint256
}
func (s *PublicFlightAPI) GetTxReceipt(ctx context.Context, txhash c_type.Uint256) (ret *TxReceipt, e error) {
hash := common.Hash{}
copy(hash[:], txhash[:])
tx, bhash, bnum, tindex := rawdb.ReadTransaction(s.exchange.b.ChainDb(), hash)
if tx == nil {
return
}
receipts, err := s.exchange.b.GetReceipts(ctx, bhash)
if err != nil {
e = err
return
}
if len(receipts) <= int(tindex) {
e = errors.New("the receipts count is not match")
return
}
receipt := receipts[tindex]
blocks, err := flight.SRI_Inst.GetBlocksInfoByDelay(bnum, 1, 0)
if err != nil {
e = err
return
}
if len(blocks) != 1 {
return
}
if blocks[0].Hash != *bhash.HashToUint256() {
e = errors.New("block hash is not match")
return
}
ret = &TxReceipt{}
for _, out := range blocks[0].Outs {
if out.State.TxHash == txhash {
ret.Outs = append(ret.Outs, out.Root)
}
}
if tx.GetZZSTX().Tx1.Count() > 0 {
for _, oin := range tx.GetZZSTX().Tx1.Ins_P {
ret.Nils = append(ret.Nils, oin.Nil)
}
for _, oin := range tx.GetZZSTX().Tx1.Ins_C {
ret.Nils = append(ret.Nils, oin.Nil)
}
}
if receipt.ShareId != nil {
ret.ShareId = receipt.ShareId.HashToUint256()
}
if receipt.PoolId != nil {
ret.PoolId = receipt.PoolId.HashToUint256()
}
ret.State = receipt.Status
ret.BNum = bnum
ret.BHash = *bhash.HashToUint256()
ret.TxHash = txhash
return
}