github.com/iotexproject/iotex-core@v1.14.1-rc1/blockchain/block/runnable.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 block
     7  
     8  import (
     9  	"github.com/iotexproject/go-pkgs/hash"
    10  	"go.uber.org/zap"
    11  
    12  	"github.com/iotexproject/iotex-core/action"
    13  	"github.com/iotexproject/iotex-core/pkg/log"
    14  )
    15  
    16  // RunnableActions is abstructed from block which contains information to execute all actions in a block.
    17  type RunnableActions struct {
    18  	txHash  hash.Hash256
    19  	actions []*action.SealedEnvelope
    20  }
    21  
    22  // TxHash returns TxHash.
    23  func (ra RunnableActions) TxHash() hash.Hash256 { return ra.txHash }
    24  
    25  // Actions returns Actions.
    26  func (ra RunnableActions) Actions() []*action.SealedEnvelope {
    27  	return ra.actions
    28  }
    29  
    30  // RunnableActionsBuilder is used to construct RunnableActions.
    31  type RunnableActionsBuilder struct{ ra RunnableActions }
    32  
    33  // NewRunnableActionsBuilder creates a RunnableActionsBuilder.
    34  func NewRunnableActionsBuilder() *RunnableActionsBuilder { return &RunnableActionsBuilder{} }
    35  
    36  // AddActions adds actions for block which is building.
    37  func (b *RunnableActionsBuilder) AddActions(acts ...*action.SealedEnvelope) *RunnableActionsBuilder {
    38  	if b.ra.actions == nil {
    39  		b.ra.actions = make([]*action.SealedEnvelope, 0)
    40  	}
    41  	b.ra.actions = append(b.ra.actions, acts...)
    42  	return b
    43  }
    44  
    45  // Build signs and then builds a block.
    46  func (b *RunnableActionsBuilder) Build() RunnableActions {
    47  	var err error
    48  	b.ra.txHash, err = calculateTxRoot(b.ra.actions)
    49  	if err != nil {
    50  		log.L().Debug("error in getting hash ", zap.Error(err))
    51  		return RunnableActions{}
    52  	}
    53  	return b.ra
    54  }