-
Notifications
You must be signed in to change notification settings - Fork 5
/
block.go
495 lines (444 loc) · 12.4 KB
/
block.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
package main
import (
"bytes"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"encoding/gob"
"fmt"
"math/big"
)
type TokenInfo struct {
Symbol string
TotalSupply uint64
Decimals uint8
}
type BlockType uint8
const (
TOKEN_BLOCK BlockType = iota + 1
MATCH_BLOCK
STRING
)
type TransactionType uint8
const (
MATCH TransactionType = iota + 1
ORDER
TRANSFER
CANCEL_ORDER
CLAIM_FUNDS
CREATE_TOKEN
FREEZE
)
type Block struct {
Timestamp int64
Type BlockType
Data BlockData
PrevBlockHash []byte
Hash []byte
Nonce int
}
type BlockData interface{}
type MatchData struct {
Matches []Match
CancelOrders []CancelOrder
CreateTokens []CreateToken
}
type TokenData struct {
Orders []Order
ClaimFunds []ClaimFunds
Transfers []Transfer
Freezes []Freeze
}
type GenericTransaction struct {
Transaction interface{}
TransactionType TransactionType
}
func (gt *GenericTransaction) ID() string {
switch gt.TransactionType {
case CREATE_TOKEN:
return fmt.Sprintf("%v,%v", gt.TransactionType, gt.Transaction.(CreateToken).TokenInfo.Symbol)
case MATCH:
return fmt.Sprintf("%v,%v", gt.TransactionType, gt.Transaction.(Match).MatchID)
case ORDER:
return fmt.Sprintf("%v,%v", gt.TransactionType, gt.Transaction.(Order).ID)
case TRANSFER:
return fmt.Sprintf("%v,%v", gt.TransactionType, gt.Transaction.(Transfer).ID)
case CANCEL_ORDER:
return fmt.Sprintf("%v,%v", gt.TransactionType, gt.Transaction.(CancelOrder).OrderID)
case CLAIM_FUNDS:
return fmt.Sprintf("%v,%v", gt.TransactionType, gt.Transaction.(ClaimFunds).ID)
case FREEZE:
return fmt.Sprintf("%v,%v", gt.TransactionType, gt.Transaction.(Freeze).ID)
}
return ""
}
type CreateToken struct {
TokenInfo TokenInfo
CreatorAddress [addressLength]byte
Signature []byte
}
type UnsignedCreateToken struct {
TokenInfo TokenInfo
CreatorAddress [addressLength]byte
}
//surplus goes to miner
type Match struct {
MatchID uint64
SellSymbol string
SellOrderID uint64
SellerGain uint64
BuySymbol string
BuyOrderID uint64
BuyerLoss uint64
TransferAmt uint64
}
type Order struct {
ID uint64
BuySymbol string
AmountToSell uint64
AmountToBuy uint64
SellerAddress [addressLength]byte
Signature []byte
}
type UnsignedOrder struct {
ID uint64
BuySymbol string
AmountToSell uint64
AmountToBuy uint64
SellerAddress [addressLength]byte
}
type Transfer struct {
ID uint64
Amount uint64
FromAddress [addressLength]byte
ToAddress [addressLength]byte
Signature []byte
}
type Freeze struct {
ID uint64
Amount uint64
FromAddress [addressLength]byte
UnfreezeBlock uint64
Signature []byte
}
type UnsignedFreeze struct {
ID uint64
Amount uint64
FromAddress [addressLength]byte
UnfreezeBlock uint64
}
type UnsignedTransfer struct {
ID uint64
Amount uint64
FromAddress [addressLength]byte
ToAddress [addressLength]byte
}
type CancelOrder struct { //goes on match chain
OrderSymbol string
OrderID uint64
Signature []byte
}
type UnsignedCancelOrder struct { //goes on match chain
OrderSymbol string
OrderID uint64
}
type ClaimFunds struct {
ID uint64
Address [addressLength]byte
Amount uint64
}
func GetBytes(key interface{}) []byte {
return []byte(fmt.Sprintf("%v", key))
}
func NewGenesisBlock() *Block {
ws := NewWalletStore(true)
addresses := ws.GetAddresses()
if len(addresses) < 1 {
LogPanic("No wallet found. Run `nchainz createwallet` before starting node.")
}
w := ws.GetWallet(addresses[0])
createToken := CreateToken{
TokenInfo: TokenInfo{
Symbol: NATIVE_CHAIN,
TotalSupply: 100 * 1000 * 1000,
Decimals: 18,
},
CreatorAddress: w.PublicKey,
Signature: nil,
}
matchData := MatchData{
Matches: nil,
CancelOrders: nil,
CreateTokens: []CreateToken{createToken},
}
block := &Block{2, MATCH_BLOCK, matchData, []byte{}, []byte{}, 0}
block.Hash = NewProofOfWork(block).GetHash()
return NewBlock(matchData, MATCH_BLOCK, []byte{})
}
func NewTokenGenesisBlock(createToken CreateToken) *Block {
claimFunds := ClaimFunds{
Address: createToken.CreatorAddress,
Amount: createToken.TokenInfo.TotalSupply,
}
tokenData := TokenData{
Orders: nil,
ClaimFunds: []ClaimFunds{claimFunds},
Transfers: nil,
Freezes: nil,
}
return NewBlock(tokenData, TOKEN_BLOCK, []byte{})
}
func NewBlock(data BlockData, blockType BlockType, prevBlockHash []byte) *Block {
block := &Block{2, blockType, data, prevBlockHash, []byte{}, 0}
block.Hash = NewProofOfWork(block).GetHash()
return block
}
func (b *Block) AddTransaction(tx GenericTransaction) {
newData := b.Data
switch tx.TransactionType {
case MATCH:
temp := newData.(MatchData)
temp.Matches = append(temp.Matches, tx.Transaction.(Match))
newData = temp
case ORDER:
temp := newData.(TokenData)
temp.Orders = append(temp.Orders, tx.Transaction.(Order))
newData = temp
case TRANSFER:
temp := newData.(TokenData)
temp.Transfers = append(temp.Transfers, tx.Transaction.(Transfer))
newData = temp
case FREEZE:
temp := newData.(TokenData)
temp.Freezes = append(temp.Freezes, tx.Transaction.(Freeze))
newData = temp
case CANCEL_ORDER:
temp := newData.(MatchData)
temp.CancelOrders = append(temp.CancelOrders, tx.Transaction.(CancelOrder))
newData = temp
case CLAIM_FUNDS:
temp := newData.(TokenData)
temp.ClaimFunds = append(temp.ClaimFunds, tx.Transaction.(ClaimFunds))
newData = temp
case CREATE_TOKEN:
temp := newData.(MatchData)
temp.CreateTokens = append(temp.CreateTokens, tx.Transaction.(CreateToken))
newData = temp
default:
LogPanic("ERROR: unknown transaction type")
}
b.Data = newData
}
//
// Serializes block
//
func (b *Block) Serialize() []byte {
var result bytes.Buffer // buffer to store serialized data
encoder := gob.NewEncoder(&result)
err := encoder.Encode(b) // encode block
if err != nil {
LogPanic(err.Error())
}
return result.Bytes()
}
//
// Deserializes block
//
func DeserializeBlock(d []byte) *Block {
var block Block
decoder := gob.NewDecoder(bytes.NewReader(d))
err := decoder.Decode(&block)
if err != nil {
LogPanic(err.Error())
}
return &block
}
func (b *Block) Dump() string {
switch b.Type {
case TOKEN_BLOCK:
data := b.Data.(TokenData)
return fmt.Sprintf("%x %v %v %v %v", b.Hash, len(data.Orders), len(data.ClaimFunds), len(data.Transfers), len(data.Freezes))
case MATCH_BLOCK:
data := b.Data.(MatchData)
return fmt.Sprintf("%x %v %v %v", b.Hash, len(data.Matches), len(data.CancelOrders), len(data.CreateTokens))
default:
return ""
}
}
//
// Serialize a transaction (minus the signature) so we can hash and sign it
//
func (tx GenericTransaction) Serialize() []byte {
switch tx.TransactionType {
case MATCH:
LogPanic("ERROR: should not sign match transaction")
case ORDER:
order := tx.Transaction.(Order)
unsignedTx := UnsignedOrder{order.ID, order.BuySymbol, order.AmountToSell, order.AmountToBuy, order.SellerAddress}
return GetBytes(unsignedTx)
case TRANSFER:
transfer := tx.Transaction.(Transfer)
unsignedTx := UnsignedTransfer{transfer.ID, transfer.Amount, transfer.FromAddress, transfer.ToAddress}
return GetBytes(unsignedTx)
case FREEZE:
freeze := tx.Transaction.(Freeze)
unsignedTx := UnsignedFreeze{freeze.ID, freeze.Amount, freeze.FromAddress, freeze.UnfreezeBlock}
return GetBytes(unsignedTx)
case CANCEL_ORDER:
cancel := tx.Transaction.(CancelOrder)
unsignedTx := UnsignedCancelOrder{cancel.OrderSymbol, cancel.OrderID}
return GetBytes(unsignedTx)
case CLAIM_FUNDS:
LogPanic("ERROR: should not sign claim funds transaction")
case CREATE_TOKEN:
create := tx.Transaction.(CreateToken)
unsignedTx := UnsignedCreateToken{create.TokenInfo, create.CreatorAddress}
return GetBytes(unsignedTx)
default:
LogPanic("ERROR: unknown transaction type")
}
return nil
}
//
// Sign a transaction
// Input: private key
// Output: signature
//
func Sign(privateKey ecdsa.PrivateKey, tx GenericTransaction) []byte {
r, s, _ := ecdsa.Sign(rand.Reader, &privateKey, tx.Serialize())
signature := append(r.Bytes(), s.Bytes()...)
return signature
}
//
// Verify a transaction
//
func Verify(tx GenericTransaction, state ConsensusState) bool {
// Always valid if transaction doesn't need to be signed
if tx.TransactionType == MATCH || tx.TransactionType == CLAIM_FUNDS {
return true
}
// Get public key
ellipticCurve := elliptic.P256()
address := tx.GetTxAddress(state)
firstHalf := big.Int{}
secondHalf := big.Int{}
addrLen := len(address)
firstHalf.SetBytes(address[:(addrLen / 2)])
secondHalf.SetBytes(address[(addrLen / 2):])
publicKey := ecdsa.PublicKey{ellipticCurve, &firstHalf, &secondHalf}
// Get r, s (signature)
r := big.Int{}
s := big.Int{}
signature := tx.GetTxSignature()
sigLen := len(signature)
r.SetBytes(signature[:(sigLen / 2)])
s.SetBytes(signature[(sigLen / 2):])
// Verify
return ecdsa.Verify(&publicKey, tx.Serialize(), &r, &s)
}
//
// Get an address from a transaction
//
func (tx GenericTransaction) GetTxAddress(state ConsensusState) []byte {
switch tx.TransactionType {
case ORDER:
address := tx.Transaction.(Order).SellerAddress
return address[:]
case TRANSFER:
address := tx.Transaction.(Transfer).FromAddress
return address[:]
case FREEZE:
address := tx.Transaction.(Freeze).FromAddress
return address[:]
case CANCEL_ORDER:
cancelOrder := tx.Transaction.(CancelOrder)
success, address := state.GetCancelAddress(cancelOrder)
if !success {
LogPanic("Failed to get an address from a cancel order")
return []byte{}
} else {
return address[:]
}
case CREATE_TOKEN:
address := tx.Transaction.(CreateToken).CreatorAddress
return address[:]
default:
LogPanic("Getting an address from a transaction that doesn't need to be signed.")
return []byte{}
}
}
//
// Get a signature from a transaction
//
func (tx GenericTransaction) GetTxSignature() []byte {
switch tx.TransactionType {
case ORDER:
return tx.Transaction.(Order).Signature
case TRANSFER:
return tx.Transaction.(Transfer).Signature
case FREEZE:
return tx.Transaction.(Freeze).Signature
case CANCEL_ORDER:
return tx.Transaction.(CancelOrder).Signature
case CREATE_TOKEN:
return tx.Transaction.(CreateToken).Signature
default:
LogPanic("Getting a signature from a transaction that doesn't need to be signed.")
return []byte{}
}
}
func (tx GenericTransaction) String() string {
switch tx.TransactionType {
case MATCH:
match := tx.Transaction.(Match)
return fmt.Sprintf("{MATCH#%v %s#%v / %s#%v : %v - %v/%v}", match.MatchID, match.BuySymbol, match.BuyOrderID, match.SellSymbol, match.SellOrderID, match.TransferAmt, match.BuyerLoss, match.SellerGain)
case ORDER:
order := tx.Transaction.(Order)
return fmt.Sprintf("{ORDER#%v %v %s / %v %s}", order.ID, order.AmountToBuy, order.BuySymbol, order.AmountToSell, KeyToString(order.SellerAddress))
case TRANSFER:
transfer := tx.Transaction.(Transfer)
return fmt.Sprintf("{TRANSFER#%v %v %s -> %s}", transfer.ID, transfer.Amount, KeyToString(transfer.FromAddress), KeyToString(transfer.ToAddress))
case FREEZE:
freeze := tx.Transaction.(Freeze)
return fmt.Sprintf("{FREEZE#%v %v %s -> %v}", freeze.ID, freeze.Amount, KeyToString(freeze.FromAddress), freeze.UnfreezeBlock)
case CANCEL_ORDER:
cancel := tx.Transaction.(CancelOrder)
return fmt.Sprintf("{CANCEL %s#%v}", cancel.OrderSymbol, cancel.OrderID)
case CLAIM_FUNDS:
claim := tx.Transaction.(ClaimFunds)
return fmt.Sprintf("{CLAIM#%v %v %s}", claim.ID, claim.Amount, KeyToString(claim.Address))
case CREATE_TOKEN:
create := tx.Transaction.(CreateToken)
return fmt.Sprintf("{CREATE %v %s}", create.TokenInfo, KeyToString(create.CreatorAddress))
default:
return "{invalid tx}"
}
}
func (match Match) String() string {
tx := GenericTransaction{match, MATCH}
return tx.String()
}
func (order Order) String() string {
tx := GenericTransaction{order, ORDER}
return tx.String()
}
func (transfer Transfer) String() string {
tx := GenericTransaction{transfer, TRANSFER}
return tx.String()
}
func (freeze Freeze) String() string {
tx := GenericTransaction{freeze, FREEZE}
return tx.String()
}
func (cancel CancelOrder) String() string {
tx := GenericTransaction{cancel, CANCEL_ORDER}
return tx.String()
}
func (claim ClaimFunds) String() string {
tx := GenericTransaction{claim, CLAIM_FUNDS}
return tx.String()
}
func (create CreateToken) String() string {
tx := GenericTransaction{create, CREATE_TOKEN}
return tx.String()
}