aboutsummaryrefslogtreecommitdiffstats
path: root/core/vm/governance.go
blob: bf3e33c39934c3951c9dd6f581bbe8163278c69e (plain) (blame)
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
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
package vm

import (
    "math/big"
    "strings"

    "github.com/dexon-foundation/dexon/accounts/abi"
    "github.com/dexon-foundation/dexon/common"
    "github.com/dexon-foundation/dexon/crypto"
    "github.com/dexon-foundation/dexon/rlp"

    coreCommon "github.com/dexon-foundation/dexon-consensus-core/common"
    "github.com/dexon-foundation/dexon-consensus-core/core"
    coreCrypto "github.com/dexon-foundation/dexon-consensus-core/core/crypto"
    "github.com/dexon-foundation/dexon-consensus-core/core/types"
)

var GovernanceContractAddress = common.BytesToAddress([]byte{0XED}) // Reverse of DEX0
var multisigAddress = common.BytesToAddress([]byte("0x....."))
var minStake = big.NewInt(10000000000000)

const abiJSON = `
[
  {
    "constant": true,
    "inputs": [],
    "name": "round",
    "outputs": [
      {
        "name": "",
        "type": "uint256"
      }
    ],
    "payable": false,
    "stateMutability": "view",
    "type": "function"
  },
  {
    "constant": true,
    "inputs": [],
    "name": "lambdaBA",
    "outputs": [
      {
        "name": "",
        "type": "int256"
      }
    ],
    "payable": false,
    "stateMutability": "view",
    "type": "function"
  },
  {
    "constant": true,
    "inputs": [
      {
        "name": "",
        "type": "uint256"
      }
    ],
    "name": "crs",
    "outputs": [
      {
        "name": "",
        "type": "bytes32"
      }
    ],
    "payable": false,
    "stateMutability": "view",
    "type": "function"
  },
  {
    "constant": true,
    "inputs": [],
    "name": "phiRatio",
    "outputs": [
      {
        "name": "",
        "type": "int256"
      }
    ],
    "payable": false,
    "stateMutability": "view",
    "type": "function"
  },
  {
    "constant": true,
    "inputs": [
      {
        "name": "",
        "type": "address"
      }
    ],
    "name": "offset",
    "outputs": [
      {
        "name": "",
        "type": "uint256"
      }
    ],
    "payable": false,
    "stateMutability": "view",
    "type": "function"
  },
  {
    "constant": true,
    "inputs": [],
    "name": "lambdaDKG",
    "outputs": [
      {
        "name": "",
        "type": "int256"
      }
    ],
    "payable": false,
    "stateMutability": "view",
    "type": "function"
  },
  {
    "constant": true,
    "inputs": [],
    "name": "k",
    "outputs": [
      {
        "name": "",
        "type": "int256"
      }
    ],
    "payable": false,
    "stateMutability": "view",
    "type": "function"
  },
  {
    "constant": true,
    "inputs": [],
    "name": "numChains",
    "outputs": [
      {
        "name": "",
        "type": "int256"
      }
    ],
    "payable": false,
    "stateMutability": "view",
    "type": "function"
  },
  {
    "constant": true,
    "inputs": [
      {
        "name": "",
        "type": "uint256"
      },
      {
        "name": "",
        "type": "uint256"
      }
    ],
    "name": "DKGMasterPublicKeys",
    "outputs": [
      {
        "name": "",
        "type": "bytes"
      }
    ],
    "payable": false,
    "stateMutability": "view",
    "type": "function"
  },
  {
    "constant": true,
    "inputs": [
      {
        "name": "",
        "type": "uint256"
      },
      {
        "name": "",
        "type": "uint256"
      }
    ],
    "name": "DKGComplaints",
    "outputs": [
      {
        "name": "",
        "type": "bytes"
      }
    ],
    "payable": false,
    "stateMutability": "view",
    "type": "function"
  },
  {
    "constant": true,
    "inputs": [],
    "name": "governanceMultisig",
    "outputs": [
      {
        "name": "",
        "type": "address"
      }
    ],
    "payable": false,
    "stateMutability": "view",
    "type": "function"
  },
  {
    "constant": false,
    "inputs": [
      {
        "name": "",
        "type": "int256"
      },
      {
        "name": "",
        "type": "int256"
      },
      {
        "name": "",
        "type": "int256"
      },
      {
        "name": "",
        "type": "int256"
      },
      {
        "name": "",
        "type": "int256"
      }
    ],
    "name": "updateConfiguration",
    "outputs": [],
    "payable": false,
    "stateMutability": "nonpayable",
    "type": "function"
  },
  {
    "constant": false,
    "inputs": [
      {
        "name": "",
        "type": "uint256"
      },
      {
        "name": "",
        "type": "bytes"
      }
    ],
    "name": "proposeCRS",
    "outputs": [],
    "payable": false,
    "stateMutability": "nonpayable",
    "type": "function"
  },
  {
    "constant": false,
    "inputs": [
      {
        "name": "",
        "type": "uint256"
      },
      {
        "name": "",
        "type": "bytes"
      }
    ],
    "name": "addDKGMasterPublicKey",
    "outputs": [],
    "payable": false,
    "stateMutability": "nonpayable",
    "type": "function"
  },
  {
    "constant": false,
    "inputs": [
      {
        "name": "",
        "type": "uint256"
      },
      {
        "name": "",
        "type": "bytes"
      }
    ],
    "name": "addDKGComplaint",
    "outputs": [],
    "payable": false,
    "stateMutability": "nonpayable",
    "type": "function"
  },
  {
    "constant": false,
    "inputs": [
      {
        "name": "",
        "type": "bytes"
      }
    ],
    "name": "stake",
    "outputs": [],
    "payable": true,
    "stateMutability": "payable",
    "type": "function"
  },
  {
    "constant": false,
    "inputs": [],
    "name": "unstake",
    "outputs": [],
    "payable": false,
    "stateMutability": "nonpayable",
    "type": "function"
  }
]
`

var abiObject abi.ABI
var sig2Method map[string]abi.Method

func init() {
    // Parse governance contract ABI.
    abiObject, err := abi.JSON(strings.NewReader(abiJSON))
    if err != nil {
        panic(err)
    }

    sig2Method = make(map[string]abi.Method)

    // Construct dispatch table.
    for _, method := range abiObject.Methods {
        sig2Method[method.Sig()] = method
    }
}

// RunGovernanceContract executes governance contract.
func RunGovernanceContract(evm *EVM, input []byte, contract *Contract) (
    ret []byte, err error) {
    // Parse input.
    method, exists := sig2Method[string(input[:4])]
    if !exists {
        return nil, errExecutionReverted
    }

    // Dispatch method call.
    g := NewGovernanceContract(evm, contract)
    argument := input[4:]

    switch method.Name {
    case "stake":
        var publicKey []byte
        if err := method.Inputs.Unpack(&publicKey, argument); err != nil {
            return nil, errExecutionReverted
        }
        return g.stake(publicKey)
    case "unstake":
        return g.unstake()
    case "proposeCRS":
        args := struct {
            Round     *big.Int
            SignedCRS []byte
        }{}
        if err := method.Inputs.Unpack(&args, argument); err != nil {
            return nil, errExecutionReverted
        }
        return g.proposeCRS(args.Round, args.SignedCRS)
    case "addDKGMasterPublicKey":
        args := struct {
            Round              *big.Int
            DKGMasterPublicKey []byte
        }{}
        if err := method.Inputs.Unpack(&args, argument); err != nil {
            return nil, errExecutionReverted
        }
        return g.addDKGMasterPublicKey(args.Round, args.DKGMasterPublicKey)
    case "addDKGComplaint":
        args := struct {
            Round        *big.Int
            DKGComplaint []byte
        }{}
        if err := method.Inputs.Unpack(&args, argument); err != nil {
            return nil, errExecutionReverted
        }
        return g.addDKGComplaint(args.Round, args.DKGComplaint)
    }
    return nil, nil
}

// State manipulation helper fro the governance contract.
type StateHelper struct {
    StateDB StateDB
}

// struct Node {
//     address owner;
//     bytes publicKey;
//     uint256 staked;
// }
//
// 0: Node[] nodes;

type nodeInfo struct {
    owner     common.Address
    publicKey []byte
    staked    *big.Int
}

func (s *StateHelper) nodesLength() *big.Int {
    return nil
}
func (s *StateHelper) node(index *big.Int) *nodeInfo {
    return nil
}
func (s *StateHelper) pushNode(n *nodeInfo) {
}
func (s *StateHelper) updateNode(index *big.Int, n *nodeInfo) {
}

// 1: mapping(address => uint256) public offset;
func (s *StateHelper) offset(addr common.Address) *big.Int {
    return nil
}
func (s *StateHelper) putOffset(addr common.Address, offset *big.Int) {
}
func (s *StateHelper) deleteOffset(addr common.Address) {
}

// 2: mapping(uint256 => bytes32) public crs;
func (s *StateHelper) crs(round *big.Int) common.Hash {
    return common.Hash{}
}
func (s *StateHelper) putCRS(round *big.Int, crs []byte) common.Hash {
    return common.Hash{}
}

// 3: mapping(uint256 => bytes[]) public DKGMasterPublicKeys;
func (s *StateHelper) dkgMasterPublicKeys(round *big.Int) [][]byte {
    return nil
}
func (s *StateHelper) pushDKGMasterPublicKey(round *big.Int, pk []byte) {
}

// 4: mapping(uint256 => bytes[]) public DKGComplaints;
func (s *StateHelper) dkgComplaints(round *big.Int) [][]byte {
    return nil
}
func (s *StateHelper) addDKGComplaint(round *big.Int, complaint []byte) {
}

// 5: uint256 public round;
func (s *StateHelper) round() *big.Int {
    return nil
}
func (s *StateHelper) incRound() *big.Int {
    return nil
}

// 6: address public governanceMultisig;
func (s *StateHelper) governanceMultisig() common.Address {
    return common.Address{}
}

// 7: uint256 public numChains;
func (s *StateHelper) numChains() *big.Int {
    return nil
}

// 8: uint256 public lambdaBA;
func (s *StateHelper) lambdaBA() *big.Int {
    return nil
}

// 9: uint256 public lambdaDKG;
func (s *StateHelper) lambdaDKG() *big.Int {
    return nil
}

// 10: uint256 public k;
func (s *StateHelper) k() *big.Int {
    return nil
}

// 11: uint256 public phiRatio;  // stored as PhiRatio * 10^6
func (s *StateHelper) phiRatio() *big.Int {
    return nil
}

// 12: uint256 public numNotarySet;
func (s *StateHelper) numNotarySet() *big.Int {
    return nil
}

// 13: uint256 public numDKGSet;
func (s *StateHelper) numDKGSet() *big.Int {
    return nil
}

// 14: uint256 public roundInterval
func (s *StateHelper) roundInterval() *big.Int {
    return nil
}

// 15: uint256 public minBlockInterval
func (s *StateHelper) minBlockInterval() *big.Int {
    return nil
}

// 16: uint256 public maxBlockInterval
func (s *StateHelper) maxBlockInterval() *big.Int {
    return nil
}

type GovernanceContract struct {
    evm      *EVM
    state    StateHelper
    contract *Contract
}

func NewGovernanceContract(evm *EVM, contract *Contract) *GovernanceContract {
    return &GovernanceContract{
        evm:      evm,
        state:    StateHelper{evm.StateDB},
        contract: contract,
    }
}

func (G *GovernanceContract) updateConfiguration() ([]byte, error) {
    return nil, nil
}

func (g *GovernanceContract) stake(publicKey []byte) ([]byte, error) {
    caller := g.contract.Caller()
    offset := g.state.offset(caller)

    // Can not stake if already staked.
    if offset != nil {
        return nil, errExecutionReverted
    }

    // TODO(w): check of pk belongs to the address.
    offset = g.state.nodesLength()
    g.state.pushNode(&nodeInfo{
        owner:     caller,
        publicKey: publicKey,
        staked:    g.contract.Value(),
    })
    g.state.putOffset(caller, offset)
    return nil, nil
}

func (g *GovernanceContract) unstake() ([]byte, error) {
    caller := g.contract.Caller()
    offset := g.state.offset(caller)
    if offset == nil {
        return nil, errExecutionReverted
    }

    node := g.state.node(offset)
    length := g.state.nodesLength()
    lastIndex := new(big.Int).Sub(length, big.NewInt(1))

    // Delete the node.
    if offset != lastIndex {
        lastNode := g.state.node(lastIndex)
        g.state.updateNode(offset, lastNode)
        g.state.putOffset(lastNode.owner, offset)
        g.state.deleteOffset(caller)
    }

    // Return the staked fund.
    g.evm.Transfer(g.evm.StateDB, GovernanceContractAddress, caller, node.staked)
    return nil, nil
}

func (g *GovernanceContract) proposeCRS(round *big.Int, signedCRS []byte) ([]byte, error) {
    crs := g.state.crs(round)

    // Revert if CRS for that round already exists.
    if crs != (common.Hash{}) {
        return nil, errExecutionReverted
    }

    prevRound := g.state.round()
    prevCRS := g.state.crs(prevRound)

    // round should be the next round number, abort otherwise.
    if new(big.Int).Add(prevRound, big.NewInt(1)).Cmp(round) != 0 {
        return nil, errExecutionReverted
    }

    // Prepare DKGMasterPublicKeys.
    // TODO(w): make sure DKGMasterPKs are unique.
    var dkgMasterPKs []*types.DKGMasterPublicKey
    for _, pk := range g.state.dkgMasterPublicKeys(round) {
        x := new(types.DKGMasterPublicKey)
        if err := rlp.DecodeBytes(pk, x); err != nil {
            panic(err)
        }
        dkgMasterPKs = append(dkgMasterPKs, x)
    }

    // Prepare DKGComplaints.
    var dkgComplaints []*types.DKGComplaint
    for _, comp := range g.state.dkgComplaints(round) {
        x := new(types.DKGComplaint)
        if err := rlp.DecodeBytes(comp, x); err != nil {
            panic(err)
        }
        dkgComplaints = append(dkgComplaints, x)
    }

    threshold := int(g.state.numDKGSet().Uint64() / 3)

    dkgGPK, err := core.NewDKGGroupPublicKey(
        round.Uint64(), dkgMasterPKs, dkgComplaints, threshold)
    if err != nil {
        return nil, errExecutionReverted
    }
    signature := coreCrypto.Signature{
        Type:      "bls",
        Signature: signedCRS,
    }
    if !dkgGPK.VerifySignature(coreCommon.Hash(prevCRS), signature) {
        return nil, errExecutionReverted
    }

    // Save new CRS into state and increase round.
    newCRS := crypto.Keccak256(signedCRS)
    g.state.incRound()
    g.state.putCRS(round, newCRS)

    return nil, nil
}

func (g *GovernanceContract) addDKGMasterPublicKey(round *big.Int, pk []byte) ([]byte, error) {
    var dkgMasterPK types.DKGMasterPublicKey
    if err := rlp.DecodeBytes(pk, &dkgMasterPK); err != nil {
        return nil, errExecutionReverted
    }
    verified, _ := core.VerifyDKGMasterPublicKeySignature(&dkgMasterPK)
    if !verified {
        return nil, errExecutionReverted
    }

    g.state.pushDKGMasterPublicKey(round, pk)
    return nil, nil
}

func (g *GovernanceContract) addDKGComplaint(round *big.Int, comp []byte) ([]byte, error) {
    var dkgComplaint types.DKGComplaint
    if err := rlp.DecodeBytes(comp, &dkgComplaint); err != nil {
        return nil, errExecutionReverted
    }
    verified, _ := core.VerifyDKGComplaintSignature(&dkgComplaint)
    if !verified {
        return nil, errExecutionReverted
    }

    g.state.addDKGComplaint(round, comp)
    return nil, nil
}