github.com/mavryk-network/mvgo@v1.19.9/micheline/features.go (about)

     1  // Copyright (c) 2020-2021 Blockwatch Data Inc.
     2  // Author: alex@blockwatch.cc
     3  
     4  package micheline
     5  
     6  import (
     7  	"encoding/json"
     8  	"strings"
     9  )
    10  
    11  type Features uint16
    12  
    13  const (
    14  	FeatureSpendable Features = 1 << iota
    15  	FeatureDelegatable
    16  	FeatureAccountFactory
    17  	FeatureContractFactory
    18  	FeatureSetDelegate
    19  	FeatureLambda
    20  	FeatureTransferTokens
    21  	FeatureChainId
    22  	FeatureTicket
    23  	FeatureSapling
    24  	FeatureView
    25  	FeatureGlobalConstant
    26  	FeatureTimelock
    27  )
    28  
    29  func (f Features) Contains(x Features) bool {
    30  	return f&x > 0
    31  }
    32  
    33  func (f Features) String() string {
    34  	return strings.Join(f.Array(), ",")
    35  }
    36  
    37  func (f Features) Array() []string {
    38  	s := make([]string, 0)
    39  	var i Features = 1
    40  	for f > 0 {
    41  		switch f & i {
    42  		case FeatureAccountFactory:
    43  			s = append(s, "account_factory")
    44  		case FeatureContractFactory:
    45  			s = append(s, "contract_factory")
    46  		case FeatureSetDelegate:
    47  			s = append(s, "set_delegate")
    48  		case FeatureLambda:
    49  			s = append(s, "lambda")
    50  		case FeatureTransferTokens:
    51  			s = append(s, "transfer_tokens")
    52  		case FeatureChainId:
    53  			s = append(s, "chain_id")
    54  		case FeatureTicket:
    55  			s = append(s, "ticket")
    56  		case FeatureSapling:
    57  			s = append(s, "sapling")
    58  		case FeatureView:
    59  			s = append(s, "view")
    60  		case FeatureGlobalConstant:
    61  			s = append(s, "global_constant")
    62  		case FeatureTimelock:
    63  			s = append(s, "timelock")
    64  		}
    65  		f &= ^i
    66  		i <<= 1
    67  	}
    68  	return s
    69  }
    70  
    71  func (f Features) MarshalJSON() ([]byte, error) {
    72  	return json.Marshal(f.Array())
    73  }
    74  
    75  func (s *Script) Features() Features {
    76  	return s.Code.Param.Features() |
    77  		s.Code.Storage.Features() |
    78  		s.Code.Code.Features() |
    79  		s.Code.View.Features() |
    80  		s.Code.BadCode.Features()
    81  }
    82  
    83  func (p Prim) Features() Features {
    84  	var f Features
    85  	_ = p.Walk(func(p Prim) error {
    86  		switch p.OpCode {
    87  		case I_CREATE_ACCOUNT:
    88  			f |= FeatureAccountFactory
    89  		case I_CREATE_CONTRACT:
    90  			f |= FeatureContractFactory
    91  		case I_SET_DELEGATE:
    92  			f |= FeatureSetDelegate
    93  		case I_LAMBDA, I_EXEC, I_APPLY:
    94  			f |= FeatureLambda
    95  		case I_TRANSFER_TOKENS:
    96  			f |= FeatureTransferTokens
    97  		case I_CHAIN_ID:
    98  			f |= FeatureChainId
    99  		case I_TICKET, I_READ_TICKET, I_SPLIT_TICKET, I_JOIN_TICKETS:
   100  			f |= FeatureTicket
   101  		case I_SAPLING_VERIFY_UPDATE:
   102  			f |= FeatureSapling
   103  		case H_CONSTANT:
   104  			f |= FeatureGlobalConstant
   105  		case K_VIEW:
   106  			f |= FeatureView
   107  		case T_CHEST_KEY, T_CHEST, I_OPEN_CHEST:
   108  			f |= FeatureTimelock
   109  		}
   110  		return nil
   111  	})
   112  	return f
   113  }