-
Notifications
You must be signed in to change notification settings - Fork 127
/
self.go
99 lines (90 loc) · 3.04 KB
/
self.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
package kernel
import (
"encoding/hex"
"errors"
"fmt"
"github.com/MixinNetwork/mixin/common"
"github.com/MixinNetwork/mixin/config"
"github.com/MixinNetwork/mixin/logger"
"github.com/dgraph-io/badger/v2"
)
func (node *Node) validateSnapshotTransaction(s *common.Snapshot, finalized bool) (*common.VersionedTransaction, bool, error) {
tx, snap, err := node.persistStore.ReadTransaction(s.Transaction)
if err == nil && tx != nil {
err = node.validateKernelSnapshot(s, tx, finalized)
}
if err != nil || tx != nil {
return tx, len(snap) > 0, err
}
tx, err = node.persistStore.CacheGetTransaction(s.Transaction)
if err != nil || tx == nil {
return nil, false, err
}
err = tx.Validate(node.persistStore)
if err != nil {
if node.networkId.String() == config.MainnetId && transactionForkHackCheck(tx.PayloadHash()) {
logger.Printf("transaction fork hack %s\n", tx.PayloadHash())
} else {
return nil, false, err
}
}
err = node.validateKernelSnapshot(s, tx, finalized)
if err != nil {
return nil, false, err
}
err = node.lockAndPersistTransaction(tx, finalized)
return tx, false, err
}
func (node *Node) lockAndPersistTransaction(tx *common.VersionedTransaction, finalized bool) error {
for {
err := tx.LockInputs(node.persistStore, finalized)
if errors.Is(err, badger.ErrConflict) {
continue
} else if err != nil {
return err
}
err = node.persistStore.WriteTransaction(tx)
if errors.Is(err, badger.ErrConflict) {
continue
}
return err
}
}
func (node *Node) validateKernelSnapshot(s *common.Snapshot, tx *common.VersionedTransaction, finalized bool) error {
switch tx.TransactionType() {
case common.TransactionTypeMint:
err := node.validateMintSnapshot(s, tx)
if err != nil {
logger.Verbosef("validateMintSnapshot ERROR %v %s %s\n", s, hex.EncodeToString(tx.PayloadMarshal()), err.Error())
return err
}
case common.TransactionTypeNodePledge:
err := node.validateNodePledgeSnapshot(s, tx)
if err != nil {
logger.Verbosef("validateNodePledgeSnapshot ERROR %v %s %s\n", s, hex.EncodeToString(tx.PayloadMarshal()), err.Error())
return err
}
case common.TransactionTypeNodeCancel:
err := node.validateNodeCancelSnapshot(s, tx, finalized)
if err != nil {
logger.Verbosef("validateNodeCancelSnapshot ERROR %v %s %s\n", s, hex.EncodeToString(tx.PayloadMarshal()), err.Error())
return err
}
case common.TransactionTypeNodeAccept:
err := node.validateNodeAcceptSnapshot(s, tx, finalized)
if err != nil {
logger.Verbosef("validateNodeAcceptSnapshot ERROR %v %s %s\n", s, hex.EncodeToString(tx.PayloadMarshal()), err.Error())
return err
}
case common.TransactionTypeNodeRemove:
err := node.validateNodeRemoveSnapshot(s, tx)
if err != nil {
logger.Verbosef("validateNodeRemoveSnapshot ERROR %v %s %s\n", s, hex.EncodeToString(tx.PayloadMarshal()), err.Error())
return err
}
}
if s.NodeId != node.IdForNetwork && s.RoundNumber == 0 && tx.TransactionType() != common.TransactionTypeNodeAccept {
return fmt.Errorf("invalid initial transaction type %d", tx.TransactionType())
}
return nil
}