github.com/bytom/bytom@v1.1.2-0.20221014091027-bbcba3df6075/test/bcrp_test_util.go (about)

     1  package test
     2  
     3  import (
     4  	"github.com/bytom/bytom/crypto/sha3pool"
     5  	"github.com/bytom/bytom/database"
     6  	"github.com/bytom/bytom/event"
     7  	"github.com/bytom/bytom/protocol"
     8  	"github.com/bytom/bytom/protocol/bc"
     9  	"github.com/bytom/bytom/protocol/bc/types"
    10  	"github.com/bytom/bytom/protocol/state"
    11  	"github.com/bytom/bytom/protocol/validation"
    12  	"github.com/bytom/bytom/protocol/vm"
    13  )
    14  
    15  func mockChainWithStore(store *database.Store) (*protocol.Chain, *database.Store, *protocol.TxPool, error) {
    16  	dispatcher := event.NewDispatcher()
    17  	txPool := protocol.NewTxPool(store, dispatcher)
    18  	chain, err := protocol.NewChain(store, txPool, dispatcher)
    19  	return chain, store, txPool, err
    20  }
    21  
    22  func registerContract(chain *protocol.Chain, store *database.Store, contract []byte) error {
    23  	tx, err := CreateRegisterContractTx(contract)
    24  	if err != nil {
    25  		return err
    26  	}
    27  
    28  	defaultCtrlProg := []byte{byte(vm.OP_TRUE)}
    29  	block, err := NewBlock(chain, []*types.Tx{tx}, defaultCtrlProg)
    30  	if err != nil {
    31  		return err
    32  	}
    33  
    34  	contractView := state.NewContractViewpoint()
    35  	if err := contractView.ApplyBlock(block); err != nil {
    36  		return err
    37  	}
    38  
    39  	utxoView := &state.UtxoViewpoint{}
    40  	return store.SaveChainStatus(&block.BlockHeader, []*types.BlockHeader{&block.BlockHeader}, utxoView, contractView, 0, &bc.Hash{})
    41  }
    42  
    43  func validateContract(chain *protocol.Chain, contract []byte, arguments [][]byte, stateData [][]byte) error {
    44  	var hash [32]byte
    45  	sha3pool.Sum256(hash[:], contract)
    46  
    47  	tx, err := CreateUseContractTx(hash[:], arguments, stateData)
    48  	if err != nil {
    49  		return err
    50  	}
    51  
    52  	defaultCtrlProg := []byte{byte(vm.OP_TRUE)}
    53  	block, err := NewBlock(chain, []*types.Tx{tx}, defaultCtrlProg)
    54  	if err != nil {
    55  		return err
    56  	}
    57  
    58  	_, err = validation.ValidateTx(tx.Tx, types.MapBlock(block), chain.ProgramConverter)
    59  	return err
    60  }