-
Notifications
You must be signed in to change notification settings - Fork 127
/
node.go
257 lines (239 loc) · 8.85 KB
/
node.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
package common
import (
"bytes"
"encoding/hex"
"fmt"
"github.com/MixinNetwork/mixin/crypto"
)
const (
NodeStatePledging = "PLEDGING"
NodeStateAccepted = "ACCEPTED"
NodeStateRemoved = "REMOVED"
NodeStateCancelled = "CANCELLED"
)
type Node struct {
Signer Address
Payee Address
State string
Transaction crypto.Hash
Timestamp uint64
}
func (n *Node) IdForNetwork(networkId crypto.Hash) crypto.Hash {
return n.Signer.Hash().ForNetwork(networkId)
}
func (tx *Transaction) NodeTransactionExtraAsSigner() *Address {
switch tx.AsVersioned().TransactionType() {
case TransactionTypeNodePledge:
case TransactionTypeNodeAccept:
case TransactionTypeNodeRemove:
default:
panic(tx.AsVersioned().PayloadHash())
}
var signer Address
copy(signer.PublicSpendKey[:], tx.Extra)
signer.PrivateViewKey = signer.PublicSpendKey.DeterministicHashDerive()
signer.PublicViewKey = signer.PrivateViewKey.Public()
return &signer
}
func (tx *Transaction) validateNodePledge(store DataStore, inputs map[string]*UTXO, snapTime uint64) error {
if tx.Asset != XINAssetId {
return fmt.Errorf("invalid node asset %s", tx.Asset.String())
}
if len(tx.Outputs) != 1 {
return fmt.Errorf("invalid outputs count %d for pledge transaction", len(tx.Outputs))
}
if len(tx.Inputs) != 1 || len(inputs) != len(tx.Inputs) {
return fmt.Errorf("invalid inputs count %d for pledge transaction", len(tx.Outputs))
}
fk := fmt.Sprintf("%s:%d", tx.Inputs[0].Hash.String(), tx.Inputs[0].Index)
if inputs[fk].Type != OutputTypeScript && inputs[fk].Type != OutputTypeNodeRemove {
return fmt.Errorf("invalid utxo type %d", inputs[fk].Type)
}
if len(tx.Extra) != 2*len(crypto.Key{}) {
return fmt.Errorf("invalid extra length %d for pledge transaction", len(tx.Extra))
}
var signerSpend crypto.Key
copy(signerSpend[:], tx.Extra)
nodes := store.ReadAllNodes(snapTime, false)
for _, n := range nodes {
if n.State != NodeStateAccepted && n.State != NodeStateCancelled && n.State != NodeStateRemoved {
return fmt.Errorf("invalid node pending state %s %s", n.Signer.String(), n.State)
}
if n.Signer.PublicSpendKey.String() == signerSpend.String() {
return fmt.Errorf("invalid node signer key %s %s", hex.EncodeToString(tx.Extra), n.Signer)
}
if n.Payee.PublicSpendKey.String() == signerSpend.String() {
return fmt.Errorf("invalid node signer key %s %s", hex.EncodeToString(tx.Extra), n.Payee)
}
}
return nil
}
func (tx *Transaction) validateNodeCancel(store DataStore, payloadHash crypto.Hash, sigs []map[uint16]*crypto.Signature, snapTime uint64) error {
if tx.Asset != XINAssetId {
return fmt.Errorf("invalid node asset %s", tx.Asset.String())
}
if len(tx.Outputs) != 2 {
return fmt.Errorf("invalid outputs count %d for cancel transaction", len(tx.Outputs))
}
if len(tx.Inputs) != 1 {
return fmt.Errorf("invalid inputs count %d for cancel transaction", len(tx.Inputs))
}
if len(sigs) != 1 || len(sigs[0]) != 1 || sigs[0][0] == nil {
return fmt.Errorf("invalid signatures %v for cancel transaction", sigs)
}
if len(tx.Extra) != len(crypto.Key{})*3 {
return fmt.Errorf("invalid extra %s for cancel transaction", hex.EncodeToString(tx.Extra))
}
cancel, script := tx.Outputs[0], tx.Outputs[1]
if cancel.Type != OutputTypeNodeCancel || script.Type != OutputTypeScript {
return fmt.Errorf("invalid outputs type %d %d for cancel transaction", cancel.Type, script.Type)
}
if len(script.Keys) != 1 {
return fmt.Errorf("invalid script output keys %d for cancel transaction", len(script.Keys))
}
if script.Script.String() != NewThresholdScript(1).String() {
return fmt.Errorf("invalid script output script %s for cancel transaction", script.Script)
}
var pledging *Node
filter := make(map[string]string)
nodes := store.ReadAllNodes(snapTime, false)
for _, n := range nodes {
filter[n.Signer.String()] = n.State
if n.State == NodeStateAccepted || n.State == NodeStateCancelled || n.State == NodeStateRemoved {
continue
}
if n.State == NodeStatePledging && pledging == nil {
pledging = n
} else {
return fmt.Errorf("invalid pledging nodes %s %s", pledging.Signer.String(), n.Signer.String())
}
}
if pledging == nil {
return fmt.Errorf("no pledging node needs to get cancelled")
}
if pledging.Transaction != tx.Inputs[0].Hash {
return fmt.Errorf("invalid plede utxo source %s %s", pledging.Transaction, tx.Inputs[0].Hash)
}
lastPledge, _, err := store.ReadTransaction(tx.Inputs[0].Hash)
if err != nil {
return err
}
if len(lastPledge.Outputs) != 1 {
return fmt.Errorf("invalid pledge utxo count %d", len(lastPledge.Outputs))
}
po := lastPledge.Outputs[0]
if po.Type != OutputTypeNodePledge {
return fmt.Errorf("invalid pledge utxo type %d", po.Type)
}
if cancel.Amount.Cmp(po.Amount.Div(100)) != 0 {
return fmt.Errorf("invalid script output amount %s for cancel transaction", cancel.Amount)
}
acc := lastPledge.NodeTransactionExtraAsSigner()
if filter[acc.String()] != NodeStatePledging {
return fmt.Errorf("invalid pledge utxo source %s", filter[acc.String()])
}
pit, _, err := store.ReadTransaction(lastPledge.Inputs[0].Hash)
if err != nil {
return err
}
if pit == nil {
return fmt.Errorf("invalid pledge input source %s:%d", lastPledge.Inputs[0].Hash, lastPledge.Inputs[0].Index)
}
pi := pit.Outputs[lastPledge.Inputs[0].Index]
if len(pi.Keys) != 1 {
return fmt.Errorf("invalid pledge input source keys %d", len(pi.Keys))
}
var a crypto.Key
copy(a[:], tx.Extra[len(crypto.Key{})*2:])
pledgeSpend := crypto.ViewGhostOutputKey(pi.Keys[0], &a, &pi.Mask, uint64(lastPledge.Inputs[0].Index))
targetSpend := crypto.ViewGhostOutputKey(script.Keys[0], &a, &script.Mask, 1)
if !bytes.Equal(lastPledge.Extra, tx.Extra[:len(crypto.Key{})*2]) {
return fmt.Errorf("invalid pledge and cancel key %s %s", hex.EncodeToString(lastPledge.Extra), hex.EncodeToString(tx.Extra))
}
if !bytes.Equal(pledgeSpend[:], targetSpend[:]) {
return fmt.Errorf("invalid pledge and cancel target %s %s", pledgeSpend, targetSpend)
}
if !pi.Keys[0].Verify(payloadHash, *sigs[0][0]) {
return fmt.Errorf("invalid cancel signature %s", sigs[0][0])
}
return nil
}
func (tx *Transaction) validateNodeAccept(store DataStore, snapTime uint64) error {
if tx.Asset != XINAssetId {
return fmt.Errorf("invalid node asset %s", tx.Asset.String())
}
if len(tx.Outputs) != 1 {
return fmt.Errorf("invalid outputs count %d for accept transaction", len(tx.Outputs))
}
if len(tx.Inputs) != 1 {
return fmt.Errorf("invalid inputs count %d for accept transaction", len(tx.Inputs))
}
var pledging *Node
filter := make(map[string]string)
nodes := store.ReadAllNodes(snapTime, false)
for _, n := range nodes {
filter[n.Signer.String()] = n.State
if n.State == NodeStateAccepted || n.State == NodeStateCancelled || n.State == NodeStateRemoved {
continue
}
if n.State == NodeStatePledging && pledging == nil {
pledging = n
} else {
return fmt.Errorf("invalid pledging nodes %s %s", pledging.Signer.String(), n.Signer.String())
}
}
if pledging == nil {
return fmt.Errorf("no pledging node needs to get accepted")
}
if pledging.Transaction != tx.Inputs[0].Hash {
return fmt.Errorf("invalid plede utxo source %s %s", pledging.Transaction, tx.Inputs[0].Hash)
}
lastPledge, _, err := store.ReadTransaction(tx.Inputs[0].Hash)
if err != nil {
return err
}
if len(lastPledge.Outputs) != 1 {
return fmt.Errorf("invalid pledge utxo count %d", len(lastPledge.Outputs))
}
po := lastPledge.Outputs[0]
if po.Type != OutputTypeNodePledge {
return fmt.Errorf("invalid pledge utxo type %d", po.Type)
}
acc := lastPledge.NodeTransactionExtraAsSigner()
if filter[acc.String()] != NodeStatePledging {
return fmt.Errorf("invalid pledge utxo source %s", filter[acc.String()])
}
if !bytes.Equal(lastPledge.Extra, tx.Extra) {
return fmt.Errorf("invalid pledge and accpet key %s %s", hex.EncodeToString(lastPledge.Extra), hex.EncodeToString(tx.Extra))
}
return nil
}
func (tx *Transaction) validateNodeRemove(store DataStore) error {
if tx.Asset != XINAssetId {
return fmt.Errorf("invalid node asset %s", tx.Asset.String())
}
if len(tx.Outputs) != 1 {
return fmt.Errorf("invalid outputs count %d for remove transaction", len(tx.Outputs))
}
if len(tx.Inputs) != 1 {
return fmt.Errorf("invalid inputs count %d for remove transaction", len(tx.Inputs))
}
accept, _, err := store.ReadTransaction(tx.Inputs[0].Hash)
if err != nil {
return err
}
if accept.PayloadHash() != tx.Inputs[0].Hash {
return fmt.Errorf("accept transaction malformed %s %s", tx.Inputs[0].Hash, accept.PayloadHash())
}
if len(accept.Outputs) != 1 {
return fmt.Errorf("invalid accept utxo count %d", len(accept.Outputs))
}
ao := accept.Outputs[0]
if ao.Type != OutputTypeNodeAccept {
return fmt.Errorf("invalid accept utxo type %d", ao.Type)
}
if !bytes.Equal(accept.Extra, tx.Extra) {
return fmt.Errorf("invalid accept and remove key %s %s", hex.EncodeToString(accept.Extra), hex.EncodeToString(tx.Extra))
}
return nil
}