forked from aergoio/aergo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
governance.go
88 lines (75 loc) · 2.32 KB
/
governance.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
/**
* @file
* @copyright defined in aergo/LICENSE.txt
*/
package chain
import (
"math/big"
"github.com/aergoio/aergo/consensus"
"github.com/aergoio/aergo/contract"
"github.com/aergoio/aergo/contract/enterprise"
"github.com/aergoio/aergo/contract/name"
"github.com/aergoio/aergo/contract/system"
"github.com/aergoio/aergo/state"
"github.com/aergoio/aergo/types"
)
func executeGovernanceTx(ccc consensus.ChainConsensusCluster, bs *state.BlockState, txBody *types.TxBody, sender, receiver *state.V,
blockInfo *types.BlockHeaderInfo) ([]*types.Event, error) {
if len(txBody.Payload) <= 0 {
return nil, types.ErrTxFormatInvalid
}
governance := string(txBody.Recipient)
scs, err := bs.StateDB.OpenContractState(receiver.AccountID(), receiver.State())
if err != nil {
return nil, err
}
blockNo := blockInfo.No
var events []*types.Event
switch governance {
case types.AergoSystem:
events, err = system.ExecuteSystemTx(scs, txBody, sender, receiver, blockInfo)
case types.AergoName:
events, err = name.ExecuteNameTx(bs, scs, txBody, sender, receiver, blockInfo)
case types.AergoEnterprise:
events, err = enterprise.ExecuteEnterpriseTx(bs, ccc, scs, txBody, sender, receiver, blockNo)
if err != nil {
err = contract.NewGovEntErr(err)
}
default:
logger.Warn().Str("governance", governance).Msg("receive unknown recipient")
err = types.ErrTxInvalidRecipient
}
if err == nil {
err = bs.StateDB.StageContractState(scs)
}
return events, err
}
// InitGenesisBPs opens system contract and put initial voting result
// it also set *State in Genesis to use statedb
func InitGenesisBPs(states *state.StateDB, genesis *types.Genesis) error {
aid := types.ToAccountID([]byte(types.AergoSystem))
scs, err := states.OpenContractStateAccount(aid)
if err != nil {
return err
}
voteResult := make(map[string]*big.Int)
for _, v := range genesis.BPs {
voteResult[v] = new(big.Int).SetUint64(0)
}
if err = system.InitVoteResult(scs, voteResult); err != nil {
return err
}
// Set genesis.BPs to the votes-ordered BPs. This will be used later for
// bootstrapping.
genesis.BPs = system.BuildOrderedCandidates(voteResult)
if err = states.StageContractState(scs); err != nil {
return err
}
if err = states.Update(); err != nil {
return err
}
if err = states.Commit(); err != nil {
return err
}
return nil
}