github.com/neatlab/neatio@v1.7.3-0.20220425043230-d903e92fcc75/chain/core/tx_callback.go (about)

     1  package core
     2  
     3  import (
     4  	"errors"
     5  	"math/big"
     6  	"sync"
     7  
     8  	"github.com/neatlab/neatio/chain/consensus/neatcon/epoch"
     9  	"github.com/neatlab/neatio/chain/core/state"
    10  	"github.com/neatlab/neatio/chain/core/types"
    11  	neatAbi "github.com/neatlab/neatio/neatabi/abi"
    12  	"github.com/neatlab/neatio/neatcli"
    13  	"github.com/neatlab/neatio/utilities/common"
    14  	"github.com/neatlib/crypto-go"
    15  	dbm "github.com/neatlib/db-go"
    16  )
    17  
    18  type TX3LocalCache interface {
    19  	GetTX3(chainId string, txHash common.Hash) *types.Transaction
    20  	DeleteTX3(chainId string, txHash common.Hash)
    21  
    22  	WriteTX3ProofData(proofData *types.TX3ProofData) error
    23  
    24  	GetTX3ProofData(chainId string, txHash common.Hash) *types.TX3ProofData
    25  	GetAllTX3ProofData() []*types.TX3ProofData
    26  }
    27  
    28  type CrossChainHelper interface {
    29  	GetMutex() *sync.Mutex
    30  	GetClient() *neatcli.Client
    31  	GetMainChainId() string
    32  	GetChainInfoDB() dbm.DB
    33  
    34  	CanCreateSideChain(from common.Address, chainId string, minValidators uint16, minDepositAmount, startupCost *big.Int, startBlock, endBlock *big.Int) error
    35  	CreateSideChain(from common.Address, chainId string, minValidators uint16, minDepositAmount *big.Int, startBlock, endBlock *big.Int) error
    36  	ValidateJoinSideChain(from common.Address, pubkey []byte, chainId string, depositAmount *big.Int, signature []byte) error
    37  	JoinSideChain(from common.Address, pubkey crypto.PubKey, chainId string, depositAmount *big.Int) error
    38  	ReadyForLaunchSideChain(height *big.Int, stateDB *state.StateDB) ([]string, []byte, []string)
    39  	ProcessPostPendingData(newPendingIdxBytes []byte, deleteSideChainIds []string)
    40  
    41  	VoteNextEpoch(ep *epoch.Epoch, from common.Address, voteHash common.Hash, txHash common.Hash) error
    42  	RevealVote(ep *epoch.Epoch, from common.Address, pubkey crypto.PubKey, depositAmount *big.Int, salt string, txHash common.Hash) error
    43  	UpdateNextEpoch(ep *epoch.Epoch, from common.Address, pubkey crypto.PubKey, depositAmount *big.Int, salt string, txHash common.Hash) error
    44  
    45  	GetHeightFromMainChain() *big.Int
    46  	GetEpochFromMainChain() (string, *epoch.Epoch)
    47  	GetTxFromMainChain(txHash common.Hash) *types.Transaction
    48  
    49  	ChangeValidators(chainId string)
    50  
    51  	// for epoch only
    52  	VerifySideChainProofData(bs []byte) error
    53  	SaveSideChainProofDataToMainChain(bs []byte) error
    54  
    55  	TX3LocalCache
    56  	ValidateTX3ProofData(proofData *types.TX3ProofData) error
    57  	ValidateTX4WithInMemTX3ProofData(tx4 *types.Transaction, tx3ProofData *types.TX3ProofData) error
    58  
    59  	////SaveDataToMainV1 acceps both epoch and tx3
    60  	//VerifySideChainProofDataV1(proofData *types.SideChainProofDataV1) error
    61  	//SaveSideChainProofDataToMainChainV1(proofData *types.SideChainProofDataV1) error
    62  }
    63  
    64  // CrossChain Callback
    65  type CrossChainValidateCb = func(tx *types.Transaction, state *state.StateDB, cch CrossChainHelper) error
    66  type CrossChainApplyCb = func(tx *types.Transaction, state *state.StateDB, ops *types.PendingOps, cch CrossChainHelper, mining bool) error
    67  
    68  // Non-CrossChain Callback
    69  type NonCrossChainValidateCb = func(tx *types.Transaction, state *state.StateDB, bc *BlockChain) error
    70  type NonCrossChainApplyCb = func(tx *types.Transaction, state *state.StateDB, bc *BlockChain, ops *types.PendingOps) error
    71  
    72  type EtdInsertBlockCb func(bc *BlockChain, block *types.Block)
    73  
    74  var validateCbMap = make(map[neatAbi.FunctionType]interface{})
    75  var applyCbMap = make(map[neatAbi.FunctionType]interface{})
    76  var insertBlockCbMap = make(map[string]EtdInsertBlockCb)
    77  
    78  func RegisterValidateCb(function neatAbi.FunctionType, validateCb interface{}) error {
    79  
    80  	_, ok := validateCbMap[function]
    81  	if ok {
    82  		return errors.New("the name has registered in validateCbMap")
    83  	}
    84  
    85  	validateCbMap[function] = validateCb
    86  	return nil
    87  }
    88  
    89  func GetValidateCb(function neatAbi.FunctionType) interface{} {
    90  
    91  	cb, ok := validateCbMap[function]
    92  	if ok {
    93  		return cb
    94  	}
    95  
    96  	return nil
    97  }
    98  
    99  func RegisterApplyCb(function neatAbi.FunctionType, applyCb interface{}) error {
   100  
   101  	_, ok := applyCbMap[function]
   102  	if ok {
   103  		return errors.New("the name has registered in applyCbMap")
   104  	}
   105  
   106  	applyCbMap[function] = applyCb
   107  
   108  	return nil
   109  }
   110  
   111  func GetApplyCb(function neatAbi.FunctionType) interface{} {
   112  
   113  	cb, ok := applyCbMap[function]
   114  	if ok {
   115  		return cb
   116  	}
   117  
   118  	return nil
   119  }
   120  
   121  func RegisterInsertBlockCb(name string, insertBlockCb EtdInsertBlockCb) error {
   122  
   123  	_, ok := insertBlockCbMap[name]
   124  	if ok {
   125  		return errors.New("the name has registered in insertBlockCbMap")
   126  	}
   127  
   128  	insertBlockCbMap[name] = insertBlockCb
   129  
   130  	return nil
   131  }
   132  
   133  func GetInsertBlockCb(name string) EtdInsertBlockCb {
   134  
   135  	cb, ok := insertBlockCbMap[name]
   136  	if ok {
   137  		return cb
   138  	}
   139  
   140  	return nil
   141  }
   142  
   143  func GetInsertBlockCbMap() map[string]EtdInsertBlockCb {
   144  
   145  	return insertBlockCbMap
   146  }