github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/x/wasm/types/proposal_custom.go (about)

     1  package types
     2  
     3  import (
     4  	"fmt"
     5  
     6  	sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types"
     7  )
     8  
     9  const (
    10  	maxAddressListLength = 100
    11  	maxMethodListLength  = 100
    12  )
    13  
    14  // ProposalRoute returns the routing key of a parameter change proposal.
    15  func (p UpdateDeploymentWhitelistProposal) ProposalRoute() string { return RouterKey }
    16  
    17  // ProposalType returns the type
    18  func (p UpdateDeploymentWhitelistProposal) ProposalType() string {
    19  	return string(ProposalTypeUpdateDeploymentWhitelist)
    20  }
    21  
    22  // ValidateBasic validates the proposal
    23  func (p UpdateDeploymentWhitelistProposal) ValidateBasic() error {
    24  	if err := validateProposalCommons(p.Title, p.Description); err != nil {
    25  		return err
    26  	}
    27  	l := len(p.DistributorAddrs)
    28  	if l == 0 || l > maxAddressListLength {
    29  		return fmt.Errorf("invalid distributor addresses len: %d", l)
    30  	}
    31  	return validateDistributorAddrs(p.DistributorAddrs)
    32  }
    33  
    34  // MarshalYAML pretty prints the wasm byte code
    35  func (p UpdateDeploymentWhitelistProposal) MarshalYAML() (interface{}, error) {
    36  	return struct {
    37  		Title            string   `yaml:"title"`
    38  		Description      string   `yaml:"description"`
    39  		DistributorAddrs []string `yaml:"distributor_addresses"`
    40  	}{
    41  		Title:            p.Title,
    42  		Description:      p.Description,
    43  		DistributorAddrs: p.DistributorAddrs,
    44  	}, nil
    45  }
    46  
    47  func validateDistributorAddrs(addrs []string) error {
    48  	if IsNobody(addrs) {
    49  		return nil
    50  	}
    51  	if IsAllAddress(addrs) {
    52  		return nil
    53  	}
    54  	for _, addr := range addrs {
    55  		if _, err := sdk.AccAddressFromBech32(addr); err != nil {
    56  			return err
    57  		}
    58  	}
    59  	return nil
    60  }
    61  
    62  func IsNobody(addrs []string) bool {
    63  	if len(addrs) == 1 && addrs[0] == "nobody" {
    64  		return true
    65  	}
    66  	return false
    67  }
    68  
    69  func IsAllAddress(addrs []string) bool {
    70  	return len(addrs) == 1 && addrs[0] == "all"
    71  }
    72  
    73  // ProposalRoute returns the routing key of a parameter change proposal.
    74  func (p UpdateWASMContractMethodBlockedListProposal) ProposalRoute() string { return RouterKey }
    75  
    76  // ProposalType returns the type
    77  func (p UpdateWASMContractMethodBlockedListProposal) ProposalType() string {
    78  	return string(ProposalTypeUpdateWasmContractMethodBlockedList)
    79  }
    80  
    81  // ValidateBasic validates the proposal
    82  func (p UpdateWASMContractMethodBlockedListProposal) ValidateBasic() error {
    83  	if err := validateProposalCommons(p.Title, p.Description); err != nil {
    84  		return err
    85  	}
    86  	return validateContractMethods(p.BlockedMethods)
    87  }
    88  
    89  func validateContractMethods(methods *ContractMethods) error {
    90  	l := len(methods.Methods)
    91  	if l == 0 || l > maxMethodListLength {
    92  		return fmt.Errorf("invalid contract methods len: %d", l)
    93  	}
    94  	if _, err := sdk.AccAddressFromBech32(methods.ContractAddr); err != nil {
    95  		return err
    96  	}
    97  	return nil
    98  }
    99  
   100  // MarshalYAML pretty prints the wasm byte code
   101  func (p UpdateWASMContractMethodBlockedListProposal) MarshalYAML() (interface{}, error) {
   102  	var methods []string
   103  	for _, method := range p.BlockedMethods.Methods {
   104  		methods = append(methods, method.FullName())
   105  	}
   106  	return struct {
   107  		Title        string   `yaml:"title"`
   108  		Description  string   `yaml:"description"`
   109  		ContractAddr string   `yaml:"contract_address"`
   110  		Methods      []string `yaml:"methods"`
   111  		IsDelete     bool     `yaml:"is_delete"`
   112  	}{
   113  		Title:        p.Title,
   114  		Description:  p.Description,
   115  		ContractAddr: p.BlockedMethods.ContractAddr,
   116  		Methods:      methods,
   117  		IsDelete:     p.IsDelete,
   118  	}, nil
   119  }
   120  
   121  func (c *Method) FullName() string {
   122  	if len(c.Extra) == 0 {
   123  		return c.Name
   124  	}
   125  	return c.Name + " " + c.Extra
   126  }
   127  
   128  func (c *ContractMethods) DeleteMethods(methods []*Method) {
   129  	for _, method := range methods {
   130  		for i := range c.Methods {
   131  			if c.Methods[i].FullName() == method.FullName() {
   132  				//delete method
   133  				c.Methods = append(c.Methods[:i], c.Methods[i+1:]...)
   134  				break
   135  			}
   136  		}
   137  	}
   138  }
   139  
   140  func (c *ContractMethods) AddMethods(methods []*Method) {
   141  	for _, method := range methods {
   142  		var exist bool
   143  		for i := range c.Methods {
   144  			if c.Methods[i].FullName() == method.FullName() {
   145  				exist = true
   146  				break
   147  			}
   148  		}
   149  		if exist {
   150  			exist = false
   151  		} else {
   152  			c.Methods = append(c.Methods, method)
   153  		}
   154  	}
   155  }
   156  
   157  func (c *ContractMethods) IsMethodBlocked(method string) bool {
   158  	if c == nil {
   159  		return false
   160  	}
   161  	for _, m := range c.Methods {
   162  		if m.FullName() == method {
   163  			return true
   164  		}
   165  	}
   166  	return false
   167  }
   168  
   169  func FindContractMethods(cms []*ContractMethods, contractAddr string) *ContractMethods {
   170  	for _, cm := range cms {
   171  		if cm.ContractAddr == contractAddr {
   172  			return cm
   173  		}
   174  	}
   175  	return nil
   176  }