github.com/intfoundation/intchain@v0.0.0-20220727031208-4316ad31ca73/core/tx_callback.go (about)

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