github.com/hyperledger/burrow@v0.34.5-0.20220512172541-77f09336001d/execution/exec/stream_event_test.go (about)

     1  package exec
     2  
     3  import (
     4  	"encoding/json"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/hyperledger/burrow/crypto"
     9  	"github.com/hyperledger/burrow/genesis"
    10  	"github.com/hyperledger/burrow/txs"
    11  	"github.com/hyperledger/burrow/txs/payload"
    12  	"github.com/stretchr/testify/assert"
    13  	"github.com/stretchr/testify/require"
    14  	tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
    15  )
    16  
    17  var genesisDoc, accounts, _ = genesis.NewDeterministicGenesis(345234523).GenesisDoc(10, 0)
    18  
    19  func TestTxExecution(t *testing.T) {
    20  	txe := NewTxExecution(txs.Enclose(genesisDoc.GetChainID(), newCallTx(0, 1)))
    21  
    22  	stack := new(TxStack)
    23  	var txeOut *TxExecution
    24  	var err error
    25  
    26  	for _, ev := range txe.StreamEvents() {
    27  		txeOut, err = stack.Consume(ev)
    28  		require.NoError(t, err)
    29  		if txeOut != nil {
    30  			require.Equal(t, jsonString(t, txe), jsonString(t, txeOut))
    31  		}
    32  	}
    33  
    34  	assert.NotNil(t, txeOut, "should have consumed input TxExecution")
    35  }
    36  
    37  func TestConsumeBlockExecution(t *testing.T) {
    38  	height := int64(234242)
    39  	be := &BlockExecution{
    40  		Header: &tmproto.Header{
    41  			ChainID: genesisDoc.GetChainID(),
    42  			AppHash: crypto.Keccak256([]byte("hashily")),
    43  			Time:    time.Now(),
    44  			Height:  height,
    45  		},
    46  		Height: uint64(height),
    47  	}
    48  	be.AppendTxs(
    49  		NewTxExecution(txs.Enclose(genesisDoc.GetChainID(), newCallTx(0, 3))),
    50  		NewTxExecution(txs.Enclose(genesisDoc.GetChainID(), newCallTx(0, 2))),
    51  		NewTxExecution(txs.Enclose(genesisDoc.GetChainID(), newCallTx(2, 1))),
    52  	)
    53  
    54  	stack := NewBlockAccumulator()
    55  	var beOut *BlockExecution
    56  	var err error
    57  	for _, ev := range be.StreamEvents() {
    58  		beOut, err = stack.Consume(ev)
    59  		require.NoError(t, err)
    60  		if beOut != nil {
    61  			require.Equal(t, jsonString(t, be), jsonString(t, beOut))
    62  		}
    63  	}
    64  	assert.NotNil(t, beOut, "should have consumed input BlockExecution")
    65  }
    66  
    67  func newCallTx(fromIndex, toIndex int) *payload.CallTx {
    68  	from := accounts[fromIndex]
    69  	to := accounts[toIndex].GetAddress()
    70  	return payload.NewCallTxWithSequence(from.GetPublicKey(), &to, []byte{1, 2, 3}, 324, 34534534, 23, 1)
    71  }
    72  
    73  func jsonString(t testing.TB, conf interface{}) string {
    74  	bs, err := json.MarshalIndent(conf, "", "  ")
    75  	require.NoError(t, err, "must be able to convert interface to string for comparison")
    76  	return string(bs)
    77  }