github.com/hyperledger/burrow@v0.34.5-0.20220512172541-77f09336001d/deploy/jobs/jobs_governance.go (about)

     1  package jobs
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/hyperledger/burrow/logging"
     7  	"github.com/hyperledger/burrow/txs/payload"
     8  
     9  	"github.com/hyperledger/burrow/crypto"
    10  	"github.com/hyperledger/burrow/deploy/def"
    11  	"github.com/hyperledger/burrow/deploy/util"
    12  	"github.com/hyperledger/burrow/execution/evm/abi"
    13  )
    14  
    15  func FormulateUpdateAccountJob(gov *def.UpdateAccount, account string, client *def.Client, logger *logging.Logger) (*payload.GovTx, []*abi.Variable, error) {
    16  	gov.Source = FirstOf(gov.Source, account)
    17  	perms := make([]string, len(gov.Permissions))
    18  
    19  	for i, p := range gov.Permissions {
    20  		perms[i] = string(p)
    21  	}
    22  	arg := &def.GovArg{
    23  		Input:       gov.Source,
    24  		Sequence:    gov.Sequence,
    25  		Power:       gov.Power,
    26  		Native:      gov.Native,
    27  		Roles:       gov.Roles,
    28  		Permissions: perms,
    29  	}
    30  	newAccountMatch := def.NewKeyRegex.FindStringSubmatch(gov.Target)
    31  	if len(newAccountMatch) > 0 {
    32  		keyName, curveType := def.KeyNameCurveType(newAccountMatch)
    33  		publicKey, err := client.CreateKey(keyName, curveType, logger)
    34  		if err != nil {
    35  			return nil, nil, fmt.Errorf("could not create key for new account: %v", err)
    36  		}
    37  		arg.Address = publicKey.GetAddress().String()
    38  		arg.PublicKey = publicKey.String()
    39  	} else if len(gov.Target) == crypto.AddressHexLength {
    40  		arg.Address = gov.Target
    41  	} else {
    42  		arg.PublicKey = gov.Target
    43  		pk, err := def.PublicKeyFromString(gov.Target)
    44  		if err != nil {
    45  			panic(err)
    46  		}
    47  		arg.Address = pk.GetAddress().String()
    48  	}
    49  
    50  	tx, err := client.UpdateAccount(arg, logger)
    51  	if err != nil {
    52  		return nil, nil, err
    53  	}
    54  
    55  	return tx, util.Variables(arg), nil
    56  }
    57  
    58  func UpdateAccountJob(tx *payload.GovTx, client *def.Client, logger *logging.Logger) error {
    59  	txe, err := client.SignAndBroadcast(tx, logger)
    60  	if err != nil {
    61  		return fmt.Errorf("error in UpdateAccountJob with payload %v: %w", tx, err)
    62  	}
    63  
    64  	LogTxExecution(txe, logger)
    65  	if err != nil {
    66  		return err
    67  	}
    68  
    69  	return nil
    70  }
    71  
    72  func FormulateBondJob(bond *def.Bond, account string, client *def.Client, logger *logging.Logger) (*payload.BondTx, error) {
    73  	// Use Default
    74  	bond.Source = FirstOf(bond.Source, account)
    75  
    76  	// Formulate tx
    77  	logger.InfoMsg("Bonding Transaction",
    78  		"source", bond.Source,
    79  		"amount", bond.Amount)
    80  
    81  	arg := &def.BondArg{
    82  		Input:    bond.Source,
    83  		Amount:   bond.Amount,
    84  		Sequence: bond.Sequence,
    85  	}
    86  
    87  	return client.Bond(arg, logger)
    88  }
    89  
    90  func BondJob(tx *payload.BondTx, client *def.Client, logger *logging.Logger) (string, error) {
    91  	// Sign, broadcast, display
    92  	txe, err := client.SignAndBroadcast(tx, logger)
    93  	if err != nil {
    94  		return "", fmt.Errorf("error in BondJob with payload %v: %w", tx, err)
    95  	}
    96  
    97  	LogTxExecution(txe, logger)
    98  	if err != nil {
    99  		return "", err
   100  	}
   101  
   102  	return txe.Receipt.TxHash.String(), nil
   103  }
   104  
   105  func FormulateUnbondJob(unbond *def.Unbond, account string, client *def.Client, logger *logging.Logger) (*payload.UnbondTx, error) {
   106  	// Use Default
   107  	unbond.Source = FirstOf(unbond.Source, account)
   108  
   109  	// Formulate tx
   110  	logger.InfoMsg("Unbonding Transaction",
   111  		"source", unbond.Source)
   112  
   113  	arg := &def.UnbondArg{
   114  		Output:   unbond.Source,
   115  		Amount:   unbond.Amount,
   116  		Sequence: unbond.Sequence,
   117  	}
   118  
   119  	return client.Unbond(arg, logger)
   120  }
   121  
   122  func UnbondJob(tx *payload.UnbondTx, client *def.Client, logger *logging.Logger) (string, error) {
   123  	// Sign, broadcast, display
   124  	txe, err := client.SignAndBroadcast(tx, logger)
   125  	if err != nil {
   126  		return "", fmt.Errorf("error in UnbondJob with payload %v: %w", tx, err)
   127  	}
   128  
   129  	LogTxExecution(txe, logger)
   130  	if err != nil {
   131  		return "", err
   132  	}
   133  
   134  	return txe.Receipt.TxHash.String(), nil
   135  }