-
Notifications
You must be signed in to change notification settings - Fork 126
/
self.go
123 lines (114 loc) · 3.78 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package kernel
import (
"encoding/hex"
"errors"
"fmt"
"time"
"github.com/MixinNetwork/mixin/common"
"github.com/MixinNetwork/mixin/config"
"github.com/MixinNetwork/mixin/logger"
"github.com/dgraph-io/badger/v4"
)
func (node *Node) validateSnapshotTransaction(s *common.Snapshot, finalized bool) (*common.VersionedTransaction, bool, error) {
tx, snap, err := node.persistStore.ReadTransaction(s.SoleTransaction())
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.SoleTransaction())
if err != nil || tx == nil {
return nil, false, err
}
err = tx.Validate(node.persistStore, s.Timestamp, finalized)
if err != nil {
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 i := time.Duration(0); i < time.Second; i += time.Millisecond * 100 {
err := tx.LockInputs(node.persistStore, finalized)
if errors.Is(err, badger.ErrConflict) {
time.Sleep(i)
continue
} else if err != nil {
return err
}
err = node.persistStore.WriteTransaction(tx)
if errors.Is(err, badger.ErrConflict) {
time.Sleep(i)
continue
}
return err
}
panic(fmt.Errorf("lockAndPersistTransaction timeout %v %v\n",
tx.PayloadHash(), finalized))
}
func (node *Node) validateKernelSnapshot(s *common.Snapshot, tx *common.VersionedTransaction, finalized bool) error {
if finalized && node.networkId.String() == config.KernelNetworkId &&
s.Timestamp < mainnetConsensusOperationElectionForkAt {
return nil
}
switch tx.TransactionType() {
case common.TransactionTypeMint:
if finalized && tx.Inputs[0].Mint.Batch < mainnetMintDayGapSkipForkBatch &&
node.networkId.String() == config.KernelNetworkId {
return nil
}
err := node.validateMintSnapshot(s, tx)
if err != nil {
logger.Printf("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.Printf("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.Printf("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.Printf("validateNodeAcceptSnapshot ERROR %v %s %s\n",
s, hex.EncodeToString(tx.PayloadMarshal()), err.Error())
return err
}
case common.TransactionTypeNodeRemove:
err := node.validateNodeRemoveSnapshot(s, tx, finalized)
if err != nil {
logger.Printf("validateNodeRemoveSnapshot ERROR %v %s %s\n",
s, hex.EncodeToString(tx.PayloadMarshal()), err.Error())
return err
}
case common.TransactionTypeCustodianUpdateNodes:
err := node.validateCustodianUpdateNodes(s, tx, finalized)
if err != nil {
logger.Printf("validateCustodianUpdateNodes ERROR %v %s %s\n",
s, hex.EncodeToString(tx.PayloadMarshal()), err.Error())
return err
}
case common.TransactionTypeCustodianSlashNodes:
return fmt.Errorf("not implemented %v", tx)
}
if s.NodeId != node.IdForNetwork && s.RoundNumber == 0 &&
tx.TransactionType() != common.TransactionTypeNodeAccept {
return fmt.Errorf("invalid initial transaction type %d", tx.TransactionType())
}
return nil
}