github.com/iotexproject/iotex-core@v1.14.1-rc1/action/protocol/protocol.go (about)

     1  // Copyright (c) 2019 IoTeX Foundation
     2  // This source code is provided 'as is' and no warranties are given as to title or non-infringement, merchantability
     3  // or fitness for purpose and, to the extent permitted by law, all liability for your use of the code is disclaimed.
     4  // This source code is governed by Apache License 2.0 that can be found in the LICENSE file.
     5  
     6  package protocol
     7  
     8  import (
     9  	"context"
    10  
    11  	"github.com/iotexproject/go-pkgs/hash"
    12  	"github.com/iotexproject/iotex-address/address"
    13  	"github.com/pkg/errors"
    14  	"go.uber.org/zap"
    15  
    16  	"github.com/iotexproject/iotex-core/action"
    17  	"github.com/iotexproject/iotex-core/pkg/log"
    18  )
    19  
    20  var (
    21  	// ErrUnimplemented indicates a method is not implemented yet
    22  	ErrUnimplemented = errors.New("method is unimplemented")
    23  )
    24  
    25  const (
    26  	// SystemNamespace is the namespace to store system information such as candidates/probationList/unproductiveDelegates
    27  	SystemNamespace = "System"
    28  )
    29  
    30  // Protocol defines the protocol interfaces atop IoTeX blockchain
    31  type Protocol interface {
    32  	ActionHandler
    33  	ReadState(context.Context, StateReader, []byte, ...[]byte) ([]byte, uint64, error)
    34  	Register(*Registry) error
    35  	ForceRegister(*Registry) error
    36  	Name() string
    37  }
    38  
    39  // Starter starts the protocol
    40  type Starter interface {
    41  	Start(context.Context, StateReader) (interface{}, error)
    42  }
    43  
    44  // GenesisStateCreator creates some genesis states
    45  type GenesisStateCreator interface {
    46  	CreateGenesisStates(context.Context, StateManager) error
    47  }
    48  
    49  // PreStatesCreator creates preliminary states for state manager
    50  type PreStatesCreator interface {
    51  	CreatePreStates(context.Context, StateManager) error
    52  }
    53  
    54  // PreCommitter performs pre-commit action of the protocol
    55  type PreCommitter interface {
    56  	PreCommit(context.Context, StateManager) error
    57  }
    58  
    59  // Committer performs commit action of the protocol
    60  type Committer interface {
    61  	Commit(context.Context, StateManager) error
    62  }
    63  
    64  // PostSystemActionsCreator creates a list of system actions to be appended to block actions
    65  type PostSystemActionsCreator interface {
    66  	CreatePostSystemActions(context.Context, StateReader) ([]action.Envelope, error)
    67  }
    68  
    69  // ActionValidator is the interface of validating an action
    70  type ActionValidator interface {
    71  	Validate(context.Context, action.Action, StateReader) error
    72  }
    73  
    74  // ActionHandler is the interface for the action handlers. For each incoming action, the assembled actions will be
    75  // called one by one to process it. ActionHandler implementation is supposed to parse the sub-type of the action to
    76  // decide if it wants to handle this action or not.
    77  type ActionHandler interface {
    78  	Handle(context.Context, action.Action, StateManager) (*action.Receipt, error)
    79  }
    80  
    81  // View stores the view for all protocols
    82  type View map[string]interface{}
    83  
    84  func (view View) Read(name string) (interface{}, error) {
    85  	if v, hit := view[name]; hit {
    86  		return v, nil
    87  	}
    88  	return nil, ErrNoName
    89  }
    90  
    91  func (view View) Write(name string, v interface{}) error {
    92  	view[name] = v
    93  	return nil
    94  }
    95  
    96  // HashStringToAddress generates the contract address from the protocolID of each protocol
    97  func HashStringToAddress(str string) address.Address {
    98  	h := hash.Hash160b([]byte(str))
    99  	addr, err := address.FromBytes(h[:])
   100  	if err != nil {
   101  		log.L().Panic("Error when constructing the address of account protocol", zap.Error(err))
   102  	}
   103  	return addr
   104  }