forked from elastos/Elastos.ELA
/
txvalidator.go
545 lines (472 loc) · 15.6 KB
/
txvalidator.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
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
package blockchain
import (
"bytes"
"errors"
"fmt"
"math"
"github.com/elastos/Elastos.ELA/config"
. "github.com/elastos/Elastos.ELA/core"
. "github.com/elastos/Elastos.ELA/errors"
"github.com/elastos/Elastos.ELA/log"
. "github.com/elastos/Elastos.ELA.Utility/common"
. "github.com/elastos/Elastos.ELA.Utility/crypto"
)
// CheckTransactionSanity verifys received single transaction
func CheckTransactionSanity(version uint32, txn *Transaction) ErrCode {
if err := CheckTransactionSize(txn); err != nil {
log.Warn("[CheckTransactionSize],", err)
return ErrTransactionSize
}
if err := CheckTransactionInput(txn); err != nil {
log.Warn("[CheckTransactionInput],", err)
return ErrInvalidInput
}
if err := CheckTransactionOutput(version, txn); err != nil {
log.Warn("[CheckTransactionOutput],", err)
return ErrInvalidOutput
}
if err := CheckAssetPrecision(txn); err != nil {
log.Warn("[CheckAssetPrecesion],", err)
return ErrAssetPrecision
}
if err := CheckAttributeProgram(txn); err != nil {
log.Warn("[CheckAttributeProgram],", err)
return ErrAttributeProgram
}
if err := CheckTransactionPayload(txn); err != nil {
log.Warn("[CheckTransactionPayload],", err)
return ErrTransactionPayload
}
if err := CheckDuplicateSidechainTx(txn); err != nil {
log.Warn("[CheckDuplicateSidechainTx],", err)
return ErrSidechainTxDuplicate
}
// check iterms above for Coinbase transaction
if txn.IsCoinBaseTx() {
return Success
}
return Success
}
// CheckTransactionContext verifys a transaction with history transaction in ledger
func CheckTransactionContext(txn *Transaction) ErrCode {
// check if duplicated with transaction in ledger
if exist := DefaultLedger.Store.IsTxHashDuplicate(txn.Hash()); exist {
log.Warn("[CheckTransactionContext] duplicate transaction check failed.")
return ErrTransactionDuplicate
}
if txn.IsCoinBaseTx() {
return Success
}
if txn.IsSideChainPowTx() {
arbitrtor, err := GetCurrentArbiter()
if err != nil {
return ErrSideChainPowConsensus
}
if err = CheckSideChainPowConsensus(txn, arbitrtor); err != nil {
log.Warn("[CheckSideChainPowConsensus],", err)
return ErrSideChainPowConsensus
}
}
// check double spent transaction
if DefaultLedger.IsDoubleSpend(txn) {
log.Warn("[CheckTransactionContext] IsDoubleSpend check faild.")
return ErrDoubleSpend
}
references, err := DefaultLedger.Store.GetTxReference(txn)
if err != nil {
log.Warn("[CheckTransactionContext] get transaction reference failed")
return ErrUnknownReferredTx
}
if txn.IsWithdrawFromSideChainTx() {
if err := CheckWithdrawFromSideChainTransaction(txn, references); err != nil {
log.Warn("[CheckWithdrawFromSideChainTransaction],", err)
return ErrSidechainTxDuplicate
}
}
if txn.IsTransferCrossChainAssetTx() {
if err := CheckTransferCrossChainAssetTransaction(txn, references); err != nil {
log.Warn("[CheckTransferCrossChainAssetTransaction],", err)
return ErrInvalidOutput
}
}
if err := CheckTransactionUTXOLock(txn, references); err != nil {
log.Warn("[CheckTransactionUTXOLock],", err)
return ErrUTXOLocked
}
if err := CheckTransactionFee(txn, references); err != nil {
log.Warn("[CheckTransactionFee],", err)
return ErrTransactionBalance
}
if err := CheckDestructionAddress(references); err != nil {
log.Warn("[CheckDestructionAddress], ", err)
return ErrInvalidInput
}
if err := CheckTransactionSignature(txn, references); err != nil {
log.Warn("[CheckTransactionSignature],", err)
return ErrTransactionSignature
}
if err := CheckTransactionCoinbaseOutputLock(txn); err != nil {
log.Warn("[CheckTransactionCoinbaseLock]", err)
return ErrIneffectiveCoinbase
}
return Success
}
func CheckDestructionAddress(references map[*Input]*Output) error {
for _, output := range references {
// this uint168 code
// is the program hash of the Elastos foundation destruction address ELANULLXXXXXXXXXXXXXXXXXXXXXYvs3rr
// we allow no output from destruction address.
// So add a check here in case someone crack the private key of this address.
if output.ProgramHash == Uint168([21]uint8{33, 32, 254, 229, 215, 235, 62, 92, 125, 49, 151, 254, 207, 108, 13, 227, 15, 136, 154, 206, 247}) {
return errors.New("cannot use utxo in the Elastos foundation destruction address")
}
}
return nil
}
func CheckTransactionCoinbaseOutputLock(txn *Transaction) error {
type lockTxInfo struct {
isCoinbaseTx bool
locktime uint32
}
transactionCache := make(map[Uint256]lockTxInfo)
currentHeight := DefaultLedger.Store.GetHeight()
var referTxn *Transaction
for _, input := range txn.Inputs {
var lockHeight uint32
var isCoinbase bool
referHash := input.Previous.TxID
if _, ok := transactionCache[referHash]; ok {
lockHeight = transactionCache[referHash].locktime
isCoinbase = transactionCache[referHash].isCoinbaseTx
} else {
var err error
referTxn, _, err = DefaultLedger.Store.GetTransaction(referHash)
// TODO
// we have executed DefaultLedger.Store.GetTxReference(txn) before.
//So if we can't find referTxn here, there must be a data inconsistent problem,
// because we do not add lock correctly.This problem will be fixed later on.
if err != nil {
return errors.New("[CheckTransactionCoinbaseOutputLock] get tx reference failed:" + err.Error())
}
lockHeight = referTxn.LockTime
isCoinbase = referTxn.IsCoinBaseTx()
transactionCache[referHash] = lockTxInfo{isCoinbase, lockHeight}
}
if isCoinbase && currentHeight-lockHeight < config.Parameters.ChainParam.CoinbaseLockTime {
return errors.New("cannot unlock coinbase transaction output")
}
}
return nil
}
//validate the transaction of duplicate UTXO input
func CheckTransactionInput(txn *Transaction) error {
if txn.IsCoinBaseTx() {
if len(txn.Inputs) != 1 {
return errors.New("coinbase must has only one input")
}
coinbaseInputHash := txn.Inputs[0].Previous.TxID
coinbaseInputIndex := txn.Inputs[0].Previous.Index
//TODO :check sequence
if !coinbaseInputHash.IsEqual(EmptyHash) || coinbaseInputIndex != math.MaxUint16 {
return errors.New("invalid coinbase input")
}
return nil
}
if len(txn.Inputs) <= 0 {
return errors.New("transaction has no inputs")
}
existingTxInputs := make(map[string]struct{})
for _, input := range txn.Inputs {
if input.Previous.TxID.IsEqual(EmptyHash) && (input.Previous.Index == math.MaxUint16) {
return errors.New("invalid transaction input")
}
if _, exists := existingTxInputs[input.ReferKey()]; exists {
return errors.New("duplicated transaction inputs")
} else {
existingTxInputs[input.ReferKey()] = struct{}{}
}
}
return nil
}
func CheckTransactionOutput(version uint32, txn *Transaction) error {
if len(txn.Outputs) > math.MaxUint16 {
return errors.New("output count should not be greater than 65535(MaxUint16)")
}
if txn.IsCoinBaseTx() {
if len(txn.Outputs) < 2 {
return errors.New("coinbase output is not enough, at least 2")
}
var totalReward = Fixed64(0)
var foundationReward = Fixed64(0)
for _, output := range txn.Outputs {
if output.AssetID != DefaultLedger.Blockchain.AssetID {
return errors.New("asset ID in coinbase is invalid")
}
totalReward += output.Value
if output.ProgramHash.IsEqual(FoundationAddress) {
foundationReward += output.Value
}
}
if Fixed64(foundationReward) < Fixed64(float64(totalReward)*0.3) {
return errors.New("Reward to foundation in coinbase < 30%")
}
return nil
}
if len(txn.Outputs) < 1 {
return errors.New("transaction has no outputs")
}
// check if output address is valid
for _, output := range txn.Outputs {
if output.AssetID != DefaultLedger.Blockchain.AssetID {
return errors.New("asset ID in output is invalid")
}
// output value must >= 0
if output.Value < Fixed64(0) {
return errors.New("Invalide transaction UTXO output.")
}
if version&CheckTxOut == CheckTxOut {
if !CheckOutputProgramHash(output.ProgramHash) {
return errors.New("output address is invalid")
}
}
}
return nil
}
func CheckOutputProgramHash(programHash Uint168) bool {
prefix := programHash[0]
return prefix == PrefixStandard || prefix == PrefixMultisig || prefix == PrefixCrossChain
}
func CheckTransactionUTXOLock(txn *Transaction, references map[*Input]*Output) error {
if txn.IsCoinBaseTx() {
return nil
}
for input, output := range references {
if output.OutputLock == 0 {
//check next utxo
continue
}
if input.Sequence != math.MaxUint32-1 {
return errors.New("Invalid input sequence")
}
if txn.LockTime < output.OutputLock {
return errors.New("UTXO output locked")
}
}
return nil
}
func CheckTransactionSize(txn *Transaction) error {
size := txn.GetSize()
if size <= 0 || size > config.Parameters.MaxBlockSize {
return fmt.Errorf("Invalid transaction size: %d bytes", size)
}
return nil
}
func CheckAssetPrecision(txn *Transaction) error {
if len(txn.Outputs) == 0 {
return nil
}
assetOutputs := make(map[Uint256][]*Output)
for _, v := range txn.Outputs {
assetOutputs[v.AssetID] = append(assetOutputs[v.AssetID], v)
}
for k, outputs := range assetOutputs {
asset, err := DefaultLedger.GetAsset(k)
if err != nil {
return errors.New("The asset not exist in local blockchain.")
}
precision := asset.Precision
for _, output := range outputs {
if !checkAmountPrecise(output.Value, precision) {
return errors.New("The precision of asset is incorrect.")
}
}
}
return nil
}
func CheckTransactionFee(tx *Transaction, references map[*Input]*Output) error {
var outputValue Fixed64
var inputValue Fixed64
for _, output := range tx.Outputs {
outputValue += output.Value
}
for _, reference := range references {
inputValue += reference.Value
}
if inputValue < Fixed64(config.Parameters.PowConfiguration.MinTxFee)+outputValue {
return fmt.Errorf("transaction fee not enough")
}
return nil
}
func CheckAttributeProgram(tx *Transaction) error {
// Coinbase transaction do not check attribute and program
if tx.IsCoinBaseTx() {
return nil
}
// Check attributes
for _, attr := range tx.Attributes {
if !IsValidAttributeType(attr.Usage) {
return fmt.Errorf("invalid attribute usage %v", attr.Usage)
}
}
// Check programs
if len(tx.Programs) == 0 {
return fmt.Errorf("no programs found in transaction")
}
for _, program := range tx.Programs {
if program.Code == nil {
return fmt.Errorf("invalid program code nil")
}
if program.Parameter == nil {
return fmt.Errorf("invalid program parameter nil")
}
_, err := ToProgramHash(program.Code)
if err != nil {
return fmt.Errorf("invalid program code %x", program.Code)
}
}
return nil
}
func CheckTransactionSignature(tx *Transaction, references map[*Input]*Output) error {
hashes, err := GetTxProgramHashes(tx, references)
if err != nil {
return err
}
buf := new(bytes.Buffer)
tx.SerializeUnsigned(buf)
// Sort first
SortProgramHashes(hashes)
SortPrograms(tx.Programs)
return RunPrograms(buf.Bytes(), hashes, tx.Programs)
}
func checkAmountPrecise(amount Fixed64, precision byte) bool {
return amount.IntValue()%int64(math.Pow(10, float64(8-precision))) == 0
}
func CheckTransactionPayload(txn *Transaction) error {
switch pld := txn.Payload.(type) {
case *PayloadRegisterAsset:
if pld.Asset.Precision < MinPrecision || pld.Asset.Precision > MaxPrecision {
return errors.New("Invalide asset Precision.")
}
if !checkAmountPrecise(pld.Amount, pld.Asset.Precision) {
return errors.New("Invalide asset value,out of precise.")
}
case *PayloadTransferAsset:
case *PayloadRecord:
case *PayloadCoinBase:
case *PayloadSideChainPow:
case *PayloadWithdrawFromSideChain:
case *PayloadTransferCrossChainAsset:
default:
return errors.New("[txValidator],invalidate transaction payload type.")
}
return nil
}
//validate the transaction of duplicate sidechain transaction
func CheckDuplicateSidechainTx(txn *Transaction) error {
if txn.IsWithdrawFromSideChainTx() {
witPayload := txn.Payload.(*PayloadWithdrawFromSideChain)
existingHashs := make(map[Uint256]struct{})
for _, hash := range witPayload.SideChainTransactionHashes {
if _, exist := existingHashs[hash]; exist {
return errors.New("Duplicate sidechain tx detected in a transaction")
}
existingHashs[hash] = struct{}{}
}
}
return nil
}
func GetCurrentArbiter() ([]byte, error) {
arbitrators, err := config.Parameters.GetArbitrators()
if err != nil {
return nil, err
}
height := DefaultLedger.Store.GetHeight()
index := height % uint32(len(arbitrators))
arbitrator := arbitrators[index]
return arbitrator, nil
}
func CheckSideChainPowConsensus(txn *Transaction, arbitrator []byte) error {
payloadSideChainPow, ok := txn.Payload.(*PayloadSideChainPow)
if !ok {
return errors.New("Side mining transaction has invalid payload")
}
publicKey, err := DecodePoint(arbitrator)
if err != nil {
return err
}
buf := new(bytes.Buffer)
err = payloadSideChainPow.Serialize(buf, SideChainPowPayloadVersion)
if err != nil {
return err
}
err = Verify(*publicKey, buf.Bytes()[0:68], payloadSideChainPow.SignedData)
if err != nil {
return errors.New("Arbitrator is not matched")
}
return nil
}
func CheckWithdrawFromSideChainTransaction(txn *Transaction, references map[*Input]*Output) error {
witPayload, ok := txn.Payload.(*PayloadWithdrawFromSideChain)
if !ok {
return errors.New("Invalid withdraw from side chain payload type")
}
for _, hash := range witPayload.SideChainTransactionHashes {
if exist := DefaultLedger.Store.IsSidechainTxHashDuplicate(hash); exist {
return errors.New("Duplicate side chain transaction hash in paylod")
}
}
for _, v := range references {
if bytes.Compare(v.ProgramHash[0:1], []byte{PrefixCrossChain}) != 0 {
return errors.New("Invalid transaction inputs address, without \"X\" at beginning")
}
}
return nil
}
func CheckTransferCrossChainAssetTransaction(txn *Transaction, references map[*Input]*Output) error {
payloadObj, ok := txn.Payload.(*PayloadTransferCrossChainAsset)
if !ok {
return errors.New("Invalid transfer cross chain asset payload type")
}
if len(payloadObj.CrossChainAddresses) == 0 ||
len(payloadObj.CrossChainAddresses) > len(txn.Outputs) ||
len(payloadObj.CrossChainAddresses) != len(payloadObj.CrossChainAmounts) ||
len(payloadObj.CrossChainAmounts) != len(payloadObj.OutputIndexes) {
return errors.New("Invalid transaction payload content")
}
//check cross chain output index in payload
outputIndexMap := make(map[uint64]struct{})
for _, outputIndex := range payloadObj.OutputIndexes {
if _, exist := outputIndexMap[outputIndex]; exist || int(outputIndex) >= len(txn.Outputs) {
return errors.New("Invalid transaction payload cross chain index")
}
outputIndexMap[outputIndex] = struct{}{}
}
//check address in outputs and payload
for i := 0; i < len(payloadObj.CrossChainAddresses); i++ {
if bytes.Compare(txn.Outputs[payloadObj.OutputIndexes[i]].ProgramHash[0:1], []byte{PrefixCrossChain}) != 0 {
return errors.New("Invalid transaction output address, without \"X\" at beginning")
}
if payloadObj.CrossChainAddresses[i] == "" {
return errors.New("Invalid transaction cross chain address ")
}
}
//check cross chain amount in payload
for i := 0; i < len(payloadObj.CrossChainAmounts); i++ {
if payloadObj.CrossChainAmounts[i] < 0 || payloadObj.CrossChainAmounts[i] > txn.Outputs[payloadObj.OutputIndexes[i]].Value-Fixed64(config.Parameters.MinCrossChainTxFee) {
return errors.New("Invalid transaction cross chain amount")
}
}
//check transaction fee
var totalInput Fixed64
for _, v := range references {
totalInput += v.Value
}
var totalOutput Fixed64
for _, output := range txn.Outputs {
totalOutput += output.Value
}
if totalInput-totalOutput < Fixed64(config.Parameters.MinCrossChainTxFee) {
return errors.New("Invalid transaction fee")
}
return nil
}