-
Notifications
You must be signed in to change notification settings - Fork 1
/
x2ethereumaction.go
503 lines (412 loc) · 15.7 KB
/
x2ethereumaction.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
package executor
import (
"strconv"
"strings"
"github.com/assetcloud/chain/account"
"github.com/assetcloud/chain/client"
"github.com/assetcloud/chain/common/address"
dbm "github.com/assetcloud/chain/common/db"
"github.com/assetcloud/chain/types"
x2eTy "github.com/assetcloud/plugin/plugin/dapp/x2ethereum/types"
"github.com/pkg/errors"
)
type action struct {
api client.QueueProtocolAPI
coinsAccount *account.DB
db dbm.KV
txhash []byte
fromaddr string
blocktime int64
height int64
index int32
execaddr string
oracle *Oracle
}
func newAction(a *x2ethereum, tx *types.Transaction, index int32) *action {
hash := tx.Hash()
fromaddr := tx.From()
return &action{a.GetAPI(), a.GetCoinsAccount(), a.GetStateDB(), hash, fromaddr,
a.GetBlockTime(), a.GetHeight(), index, address.ExecAddress(string(tx.Execer)), NewOracle(a.GetStateDB(), x2eTy.DefaultConsensusNeeded)}
}
// ethereum ---> chain
// lock
func (a *action) procEth2Chain_lock(ethBridgeClaim *x2eTy.Eth2Chain) (*types.Receipt, error) {
ethBridgeClaim.IssuerDotSymbol = strings.ToLower(ethBridgeClaim.IssuerDotSymbol)
receipt, err := a.checkConsensusThreshold()
if err != nil {
return nil, err
}
status, err := a.oracle.ProcessClaim(*ethBridgeClaim)
if err != nil {
return nil, err
}
ID := strconv.Itoa(int(ethBridgeClaim.EthereumChainID)) + strconv.Itoa(int(ethBridgeClaim.Nonce)) + ethBridgeClaim.EthereumSender + ethBridgeClaim.TokenContractAddress + "lock"
//记录ethProphecy
bz, err := a.db.Get(x2eTy.CalProphecyPrefix(ID))
if err != nil {
return nil, x2eTy.ErrProphecyGet
}
var dbProphecy x2eTy.ReceiptEthProphecy
err = types.Decode(bz, &dbProphecy)
if err != nil {
return nil, types.ErrUnmarshal
}
receipt.KV = append(receipt.KV, &types.KeyValue{
Key: x2eTy.CalProphecyPrefix(ID),
Value: bz,
})
receipt.Logs = append(receipt.Logs, &types.ReceiptLog{Ty: x2eTy.TyProphecyLog, Log: types.Encode(&dbProphecy)})
if status.Text == x2eTy.EthBridgeStatus_SuccessStatusText {
// mavl-x2ethereum-eth+tokenAddress
// 这里为了区分相同tokensymbol不同tokenAddress做了级联处理
accDB, err := account.NewAccountDB(a.api.GetConfig(), x2eTy.X2ethereumX, strings.ToLower(ethBridgeClaim.IssuerDotSymbol+ethBridgeClaim.TokenContractAddress), a.db)
if err != nil {
return nil, errors.Wrapf(err, "relay procMsgEth2Chain,exec=%s,sym=%s", x2eTy.X2ethereumX, ethBridgeClaim.IssuerDotSymbol)
}
r, err := a.oracle.ProcessSuccessfulClaimForLock(status.FinalClaim, a.execaddr, accDB)
if err != nil {
return nil, err
}
receipt.KV = append(receipt.KV, r.KV...)
receipt.Logs = append(receipt.Logs, r.Logs...)
//记录成功lock的日志
msgEthBridgeClaimBytes := types.Encode(ethBridgeClaim)
receipt.KV = append(receipt.KV, &types.KeyValue{Key: x2eTy.CalEth2ChainPrefix(), Value: msgEthBridgeClaimBytes})
execlog := &types.ReceiptLog{Ty: x2eTy.TyEth2ChainLog, Log: types.Encode(&x2eTy.ReceiptEth2Chain{
EthereumChainID: ethBridgeClaim.EthereumChainID,
BridgeContractAddress: ethBridgeClaim.BridgeContractAddress,
Nonce: ethBridgeClaim.Nonce,
IssuerDotSymbol: ethBridgeClaim.IssuerDotSymbol,
EthereumSender: ethBridgeClaim.EthereumSender,
ChainReceiver: ethBridgeClaim.ChainReceiver,
ValidatorAddress: ethBridgeClaim.ValidatorAddress,
Amount: ethBridgeClaim.Amount,
ClaimType: ethBridgeClaim.ClaimType,
XTxHash: a.txhash,
XHeight: uint64(a.height),
ProphecyID: ID,
Decimals: ethBridgeClaim.Decimals,
TokenAddress: ethBridgeClaim.TokenContractAddress,
})}
receipt.Logs = append(receipt.Logs, execlog)
}
receipt.Ty = types.ExecOk
return receipt, nil
}
// chain -> ethereum
// 返还在chain上生成的erc20代币
func (a *action) procChainToEth_burn(msgBurn *x2eTy.ChainToEth) (*types.Receipt, error) {
receipt, err := a.checkConsensusThreshold()
if err != nil {
return nil, err
}
accDB, err := account.NewAccountDB(a.api.GetConfig(), x2eTy.X2ethereumX, strings.ToLower(msgBurn.IssuerDotSymbol+msgBurn.TokenContract), a.db)
if err != nil {
return nil, errors.Wrapf(err, "relay procMsgBurn,exec=%s,sym=%s", x2eTy.X2ethereumX, msgBurn.IssuerDotSymbol)
}
r, err := a.oracle.ProcessBurn(a.fromaddr, msgBurn.Amount, accDB)
if err != nil {
return nil, err
}
receipt.KV = append(receipt.KV, r.KV...)
receipt.Logs = append(receipt.Logs, r.Logs...)
execlog := &types.ReceiptLog{Ty: x2eTy.TyWithdrawChainLog, Log: types.Encode(&x2eTy.ReceiptChainToEth{
ChainSender: a.fromaddr,
EthereumReceiver: msgBurn.EthereumReceiver,
Amount: msgBurn.Amount,
IssuerDotSymbol: msgBurn.IssuerDotSymbol,
Decimals: msgBurn.Decimals,
TokenContract: msgBurn.TokenContract,
})}
receipt.Logs = append(receipt.Logs, execlog)
msgBurnBytes := types.Encode(msgBurn)
receipt.KV = append(receipt.KV, &types.KeyValue{Key: x2eTy.CalWithdrawChainPrefix(), Value: msgBurnBytes})
receipt.Ty = types.ExecOk
return receipt, nil
}
func (a *action) procChainToEth_lock(msgLock *x2eTy.ChainToEth) (*types.Receipt, error) {
receipt, err := a.checkConsensusThreshold()
if err != nil {
return nil, err
}
var accDB *account.DB
exec, symbol, _ := x2eTy.DivideDot(msgLock.IssuerDotSymbol)
if exec == "coins" {
accDB = account.NewCoinsAccount(a.api.GetConfig())
accDB.SetDB(a.db)
} else {
accDB, err = account.NewAccountDB(a.api.GetConfig(), exec, strings.ToLower(symbol), a.db)
if err != nil {
return nil, errors.Wrap(err, "newAccountDB")
}
}
r, err := a.oracle.ProcessLock(a.fromaddr, address.ExecAddress(symbol), a.execaddr, msgLock.Amount, accDB)
if err != nil {
return nil, err
}
receipt.KV = append(receipt.KV, r.KV...)
receipt.Logs = append(receipt.Logs, r.Logs...)
execlog := &types.ReceiptLog{Ty: x2eTy.TyChainToEthLog, Log: types.Encode(&x2eTy.ReceiptChainToEth{
ChainSender: a.fromaddr,
EthereumReceiver: msgLock.EthereumReceiver,
Amount: msgLock.Amount,
IssuerDotSymbol: msgLock.IssuerDotSymbol,
Decimals: msgLock.Decimals,
TokenContract: msgLock.TokenContract,
})}
receipt.Logs = append(receipt.Logs, execlog)
msgLockBytes := types.Encode(msgLock)
receipt.KV = append(receipt.KV, &types.KeyValue{Key: x2eTy.CalChainToEthPrefix(), Value: msgLockBytes})
receipt.Ty = types.ExecOk
return receipt, nil
}
// ethereum -> chain
// burn
func (a *action) procEth2Chain_burn(withdrawEth *x2eTy.Eth2Chain) (*types.Receipt, error) {
elog.Info("procWithdrawEth", "receive a procWithdrawEth tx", "start")
receipt, err := a.checkConsensusThreshold()
if err != nil {
return nil, err
}
status, err := a.oracle.ProcessClaim(*withdrawEth)
if err != nil {
return nil, err
}
ID := strconv.Itoa(int(withdrawEth.EthereumChainID)) + strconv.Itoa(int(withdrawEth.Nonce)) + withdrawEth.EthereumSender + withdrawEth.TokenContractAddress + "burn"
//记录ethProphecy
bz, err := a.db.Get(x2eTy.CalProphecyPrefix(ID))
if err != nil {
return nil, x2eTy.ErrProphecyGet
}
var dbProphecy x2eTy.ReceiptEthProphecy
err = types.Decode(bz, &dbProphecy)
if err != nil {
return nil, types.ErrUnmarshal
}
receipt.KV = append(receipt.KV, &types.KeyValue{
Key: x2eTy.CalProphecyPrefix(ID),
Value: bz,
})
receipt.Logs = append(receipt.Logs, &types.ReceiptLog{Ty: x2eTy.TyProphecyLog, Log: types.Encode(&dbProphecy)})
if status.Text == x2eTy.EthBridgeStatus_SuccessStatusText {
var accDB *account.DB
exec, symbol, _ := x2eTy.DivideDot(withdrawEth.IssuerDotSymbol)
if exec == "coins" {
accDB = account.NewCoinsAccount(a.api.GetConfig())
accDB.SetDB(a.db)
} else {
accDB, err = account.NewAccountDB(a.api.GetConfig(), exec, strings.ToLower(symbol), a.db)
if err != nil {
return nil, errors.Wrap(err, "newAccountDB")
}
}
r, err := a.oracle.ProcessSuccessfulClaimForBurn(status.FinalClaim, a.execaddr, symbol, accDB)
if err != nil {
return nil, err
}
receipt.KV = append(receipt.KV, r.KV...)
receipt.Logs = append(receipt.Logs, r.Logs...)
msgWithdrawEthBytes := types.Encode(withdrawEth)
receipt.KV = append(receipt.KV, &types.KeyValue{Key: x2eTy.CalWithdrawEthPrefix(), Value: msgWithdrawEthBytes})
execlog := &types.ReceiptLog{Ty: x2eTy.TyWithdrawEthLog, Log: types.Encode(&x2eTy.ReceiptEth2Chain{
EthereumChainID: withdrawEth.EthereumChainID,
BridgeContractAddress: withdrawEth.BridgeContractAddress,
Nonce: withdrawEth.Nonce,
IssuerDotSymbol: withdrawEth.IssuerDotSymbol,
EthereumSender: withdrawEth.EthereumSender,
ChainReceiver: withdrawEth.ChainReceiver,
ValidatorAddress: withdrawEth.ValidatorAddress,
Amount: withdrawEth.Amount,
ClaimType: withdrawEth.ClaimType,
XTxHash: a.txhash,
XHeight: uint64(a.height),
ProphecyID: ID,
Decimals: withdrawEth.Decimals,
TokenAddress: withdrawEth.TokenContractAddress,
})}
receipt.Logs = append(receipt.Logs, execlog)
}
receipt.Ty = types.ExecOk
return receipt, nil
}
func (a *action) procMsgTransfer(msgTransfer *types.AssetsTransfer) (*types.Receipt, error) {
token := msgTransfer.GetCointoken()
receipt, err := a.checkConsensusThreshold()
if err != nil {
return nil, err
}
accDB, err := account.NewAccountDB(a.api.GetConfig(), x2eTy.X2ethereumX, token, a.db)
if err != nil {
return nil, err
}
r, err := accDB.ExecTransfer(a.fromaddr, msgTransfer.To, address.ExecAddress(x2eTy.X2ethereumX), msgTransfer.Amount)
if err != nil {
return nil, err
}
receipt.KV = append(receipt.KV, r.KV...)
receipt.Logs = append(receipt.Logs, r.Logs...)
receipt.Ty = types.ExecOk
return receipt, nil
}
func (a *action) procMsgTransferToExec(msgTransferToExec *types.AssetsTransferToExec) (*types.Receipt, error) {
token := msgTransferToExec.GetCointoken()
receipt, err := a.checkConsensusThreshold()
if err != nil {
return nil, err
}
accDB, err := account.NewAccountDB(a.api.GetConfig(), x2eTy.X2ethereumX, token, a.db)
if err != nil {
return nil, err
}
r, err := accDB.TransferToExec(a.fromaddr, address.ExecAddress(msgTransferToExec.ExecName), msgTransferToExec.Amount)
if err != nil {
return nil, err
}
receipt.KV = append(receipt.KV, r.KV...)
receipt.Logs = append(receipt.Logs, r.Logs...)
return receipt, nil
}
func (a *action) procMsgWithDrawFromExec(msgWithdrawFromExec *types.AssetsWithdraw) (*types.Receipt, error) {
token := msgWithdrawFromExec.GetCointoken()
receipt, err := a.checkConsensusThreshold()
if err != nil {
return nil, err
}
accDB, err := account.NewAccountDB(a.api.GetConfig(), x2eTy.X2ethereumX, token, a.db)
if err != nil {
return nil, err
}
r, err := accDB.TransferWithdraw(a.fromaddr, address.ExecAddress(msgWithdrawFromExec.ExecName), msgWithdrawFromExec.Amount)
if err != nil {
return nil, err
}
receipt.KV = append(receipt.KV, r.KV...)
receipt.Logs = append(receipt.Logs, r.Logs...)
return receipt, nil
}
//需要一笔交易来注册validator
//这里注册的validator的power之和可能不为1,需要在内部进行加权
//返回的回执中,KV包含所有validator的power值,Log中包含本次注册的validator的power值
func (a *action) procAddValidator(msgAddValidator *x2eTy.MsgValidator) (*types.Receipt, error) {
elog.Info("procAddValidator", "start", msgAddValidator)
receipt, err := a.checkConsensusThreshold()
if err != nil {
return nil, err
}
if !x2eTy.CheckPower(msgAddValidator.Power) {
return nil, x2eTy.ErrInvalidPower
}
r, err := a.oracle.ProcessAddValidator(msgAddValidator.Address, msgAddValidator.Power)
if err != nil {
return nil, err
}
receipt.KV = append(receipt.KV, r.KV...)
receipt.Logs = append(receipt.Logs, r.Logs...)
execlog := &types.ReceiptLog{Ty: x2eTy.TyAddValidatorLog, Log: types.Encode(&x2eTy.ReceiptValidator{
Address: msgAddValidator.Address,
Power: msgAddValidator.Power,
XTxHash: a.txhash,
XHeight: uint64(a.height),
})}
receipt.Logs = append(receipt.Logs, execlog)
receipt.Ty = types.ExecOk
return receipt, nil
}
func (a *action) procRemoveValidator(msgRemoveValidator *x2eTy.MsgValidator) (*types.Receipt, error) {
receipt, err := a.checkConsensusThreshold()
if err != nil {
return nil, err
}
r, err := a.oracle.ProcessRemoveValidator(msgRemoveValidator.Address)
if err != nil {
return nil, err
}
receipt.KV = append(receipt.KV, r.KV...)
receipt.Logs = append(receipt.Logs, r.Logs...)
execlog := &types.ReceiptLog{Ty: x2eTy.TyRemoveValidatorLog, Log: types.Encode(&x2eTy.ReceiptValidator{
Address: msgRemoveValidator.Address,
Power: msgRemoveValidator.Power,
XTxHash: a.txhash,
XHeight: uint64(a.height),
})}
receipt.Logs = append(receipt.Logs, execlog)
receipt.Ty = types.ExecOk
return receipt, nil
}
func (a *action) procModifyValidator(msgModifyValidator *x2eTy.MsgValidator) (*types.Receipt, error) {
receipt, err := a.checkConsensusThreshold()
if err != nil {
return nil, err
}
if !x2eTy.CheckPower(msgModifyValidator.Power) {
return nil, x2eTy.ErrInvalidPower
}
receipt, err = a.oracle.ProcessModifyValidator(msgModifyValidator.Address, msgModifyValidator.Power)
if err != nil {
return nil, err
}
execlog := &types.ReceiptLog{Ty: x2eTy.TyModifyPowerLog, Log: types.Encode(&x2eTy.ReceiptValidator{
Address: msgModifyValidator.Address,
Power: msgModifyValidator.Power,
XTxHash: a.txhash,
XHeight: uint64(a.height),
})}
receipt.Logs = append(receipt.Logs, execlog)
receipt.Ty = types.ExecOk
return receipt, nil
}
func (a *action) procMsgSetConsensusThreshold(msgSetConsensusThreshold *x2eTy.MsgConsensusThreshold) (*types.Receipt, error) {
receipt := new(types.Receipt)
if !x2eTy.CheckPower(msgSetConsensusThreshold.ConsensusThreshold) {
return nil, x2eTy.ErrInvalidPower
}
preConsensusNeeded, nowConsensusNeeded, err := a.oracle.ProcessSetConsensusNeeded(msgSetConsensusThreshold.ConsensusThreshold)
if err != nil {
return nil, err
}
setConsensusThreshold := &x2eTy.ReceiptSetConsensusThreshold{
PreConsensusThreshold: preConsensusNeeded,
NowConsensusThreshold: nowConsensusNeeded,
XTxHash: a.txhash,
XHeight: uint64(a.height),
}
execlog := &types.ReceiptLog{Ty: x2eTy.TySetConsensusThresholdLog, Log: types.Encode(setConsensusThreshold)}
receipt.Logs = append(receipt.Logs, execlog)
msgSetConsensusThresholdBytes := types.Encode(&x2eTy.ReceiptQueryConsensusThreshold{
ConsensusThreshold: nowConsensusNeeded,
})
receipt.KV = append(receipt.KV, &types.KeyValue{Key: x2eTy.CalConsensusThresholdPrefix(), Value: msgSetConsensusThresholdBytes})
receipt.Ty = types.ExecOk
return receipt, nil
}
func (a *action) checkConsensusThreshold() (*types.Receipt, error) {
receipt := new(types.Receipt)
consensusNeededBytes, err := a.db.Get(x2eTy.CalConsensusThresholdPrefix())
if err != nil {
if err == types.ErrNotFound {
setConsensusThreshold := &x2eTy.ReceiptQueryConsensusThreshold{ConsensusThreshold: x2eTy.DefaultConsensusNeeded}
msgSetConsensusThresholdBytes := types.Encode(setConsensusThreshold)
receipt.KV = append(receipt.KV, &types.KeyValue{
Key: x2eTy.CalConsensusThresholdPrefix(),
Value: msgSetConsensusThresholdBytes,
})
consensusThreshold := &x2eTy.ReceiptSetConsensusThreshold{
PreConsensusThreshold: int64(0),
NowConsensusThreshold: x2eTy.DefaultConsensusNeeded,
XTxHash: a.txhash,
XHeight: uint64(a.height),
}
receipt.Logs = append(receipt.Logs, &types.ReceiptLog{Ty: x2eTy.TySetConsensusThresholdLog, Log: types.Encode(consensusThreshold)})
} else {
return nil, err
}
} else {
var mc x2eTy.ReceiptQueryConsensusThreshold
_ = types.Decode(consensusNeededBytes, &mc)
_, _, err = a.oracle.ProcessSetConsensusNeeded(mc.ConsensusThreshold)
if err != nil {
return nil, err
}
}
return receipt, nil
}