github.com/aergoio/aergo@v1.3.1/chain/governance.go (about)

     1  /**
     2   *  @file
     3   *  @copyright defined in aergo/LICENSE.txt
     4   */
     5  
     6  package chain
     7  
     8  import (
     9  	"math/big"
    10  
    11  	"github.com/aergoio/aergo/consensus"
    12  
    13  	"github.com/aergoio/aergo/contract"
    14  	"github.com/aergoio/aergo/contract/enterprise"
    15  	"github.com/aergoio/aergo/contract/name"
    16  	"github.com/aergoio/aergo/contract/system"
    17  	"github.com/aergoio/aergo/state"
    18  	"github.com/aergoio/aergo/types"
    19  )
    20  
    21  func executeGovernanceTx(ccc consensus.ChainConsensusCluster, bs *state.BlockState, txBody *types.TxBody, sender, receiver *state.V,
    22  	blockNo types.BlockNo) ([]*types.Event, error) {
    23  
    24  	if len(txBody.Payload) <= 0 {
    25  		return nil, types.ErrTxFormatInvalid
    26  	}
    27  
    28  	governance := string(txBody.Recipient)
    29  
    30  	scs, err := bs.StateDB.OpenContractState(receiver.AccountID(), receiver.State())
    31  	if err != nil {
    32  		return nil, err
    33  	}
    34  	var events []*types.Event
    35  	switch governance {
    36  	case types.AergoSystem:
    37  		events, err = system.ExecuteSystemTx(scs, txBody, sender, receiver, blockNo)
    38  	case types.AergoName:
    39  		events, err = name.ExecuteNameTx(bs, scs, txBody, sender, receiver, blockNo)
    40  	case types.AergoEnterprise:
    41  		events, err = enterprise.ExecuteEnterpriseTx(bs, ccc, scs, txBody, sender, receiver, blockNo)
    42  		if err != nil {
    43  			err = contract.NewGovEntErr(err)
    44  		}
    45  	default:
    46  		logger.Warn().Str("governance", governance).Msg("receive unknown recipient")
    47  		err = types.ErrTxInvalidRecipient
    48  	}
    49  	if err == nil {
    50  		err = bs.StateDB.StageContractState(scs)
    51  	}
    52  
    53  	return events, err
    54  }
    55  
    56  // InitGenesisBPs opens system contract and put initial voting result
    57  // it also set *State in Genesis to use statedb
    58  func InitGenesisBPs(states *state.StateDB, genesis *types.Genesis) error {
    59  	aid := types.ToAccountID([]byte(types.AergoSystem))
    60  	scs, err := states.OpenContractStateAccount(aid)
    61  	if err != nil {
    62  		return err
    63  	}
    64  
    65  	voteResult := make(map[string]*big.Int)
    66  	for _, v := range genesis.BPs {
    67  		voteResult[v] = new(big.Int).SetUint64(0)
    68  	}
    69  	if err = system.InitVoteResult(scs, voteResult); err != nil {
    70  		return err
    71  	}
    72  
    73  	// Set genesis.BPs to the votes-ordered BPs. This will be used later for
    74  	// bootstrapping.
    75  	genesis.BPs = system.BuildOrderedCandidates(voteResult)
    76  	if err = states.StageContractState(scs); err != nil {
    77  		return err
    78  	}
    79  	if err = states.Update(); err != nil {
    80  		return err
    81  	}
    82  	if err = states.Commit(); err != nil {
    83  		return err
    84  	}
    85  
    86  	return nil
    87  }