forked from aergoio/aergo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
governance.go
71 lines (60 loc) · 1.6 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
/**
* @file
* @copyright defined in aergo/LICENSE.txt
*/
package chain
import (
"errors"
"math/big"
"github.com/aergoio/aergo/contract/system"
"github.com/aergoio/aergo/state"
"github.com/aergoio/aergo/types"
)
func executeGovernanceTx(states *state.StateDB, txBody *types.TxBody, sender, receiver *state.V,
blockNo types.BlockNo) error {
if len(txBody.Payload) <= 0 {
return types.ErrTxFormatInvalid
}
governance := string(txBody.Recipient)
if governance != types.AergoSystem {
return errors.New("receive unknown recipient")
}
scs, err := states.OpenContractState(receiver.AccountID(), receiver.State())
if err != nil {
return err
}
switch governance {
case types.AergoSystem:
err = system.ExecuteSystemTx(txBody, sender.State(), scs, blockNo)
if err == nil {
err = states.StageContractState(scs)
}
default:
logger.Warn().Str("governance", governance).Msg("receive unknown recipient")
err = types.ErrTxInvalidRecipient
}
return err
}
// InitGenesisBPs opens system contract and put initial voting result
// it also set *State in Genesis to use statedb
func InitGenesisBPs(states *state.StateDB, bps []string) 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 bps {
voteResult[v] = new(big.Int).SetUint64(0)
}
if err = system.InitVoteResult(scs, &voteResult); err != nil {
return err
}
if err = states.StageContractState(scs); err != nil {
return err
}
if err = states.Update(); err != nil {
return err
}
return nil
}