github.com/iotexproject/iotex-core@v1.14.1-rc1/blockchain/block/utils_test.go (about)

     1  package block
     2  
     3  import (
     4  	"math/big"
     5  	"testing"
     6  
     7  	"github.com/iotexproject/iotex-core/action"
     8  	"github.com/iotexproject/iotex-core/pkg/unit"
     9  	"github.com/iotexproject/iotex-core/test/identityset"
    10  
    11  	"github.com/iotexproject/go-pkgs/hash"
    12  	"github.com/stretchr/testify/require"
    13  )
    14  
    15  func TestBody_CalculateTxRoot(t *testing.T) {
    16  	requireT := require.New(t)
    17  	var sevlps []*action.SealedEnvelope
    18  
    19  	for i := 1; i <= 10; i++ {
    20  		tsf, _ := action.NewTransfer(
    21  			uint64(i),
    22  			unit.ConvertIotxToRau(1000+int64(i)),
    23  			identityset.Address(i%identityset.Size()).String(),
    24  			nil,
    25  			20000+uint64(i),
    26  			unit.ConvertIotxToRau(1+int64(i)),
    27  		)
    28  		eb := action.EnvelopeBuilder{}
    29  		evlp := eb.
    30  			SetAction(tsf).
    31  			SetGasLimit(tsf.GasLimit()).
    32  			SetGasPrice(tsf.GasPrice()).
    33  			SetNonce(tsf.Nonce()).
    34  			SetVersion(1).
    35  			Build()
    36  		sevlp, err := action.Sign(evlp, identityset.PrivateKey((i+1)%identityset.Size()))
    37  		requireT.NoError(err)
    38  		sevlps = append(sevlps, sevlp)
    39  	}
    40  
    41  	c, err := calculateTxRoot(sevlps)
    42  	require.NoError(t, err)
    43  
    44  	c2 := []byte{158, 73, 244, 188, 155, 10, 251, 87, 98, 163, 234, 194, 38, 174,
    45  		215, 255, 8, 148, 44, 204, 10, 56, 102, 180, 99, 188, 79, 146, 66, 219, 41, 30}
    46  	c3 := hash.BytesToHash256(c2)
    47  	requireT.Equal(c, c3)
    48  }
    49  
    50  func TestBody_CalculateTransferAmount(t *testing.T) {
    51  	requireT := require.New(t)
    52  	var sevlps []*action.SealedEnvelope
    53  	transferAmount := big.NewInt(0)
    54  
    55  	for i := 1; i <= 10; i++ {
    56  		tsf, _ := action.NewTransfer(
    57  			uint64(i),
    58  			unit.ConvertIotxToRau(1000+int64(i)),
    59  			identityset.Address(i%identityset.Size()).String(),
    60  			nil,
    61  			20000+uint64(i),
    62  			unit.ConvertIotxToRau(1+int64(i)),
    63  		)
    64  		eb := action.EnvelopeBuilder{}
    65  		evlp := eb.
    66  			SetAction(tsf).
    67  			SetGasLimit(tsf.GasLimit()).
    68  			SetGasPrice(tsf.GasPrice()).
    69  			SetNonce(tsf.Nonce()).
    70  			SetVersion(1).
    71  			Build()
    72  		sevlp, err := action.Sign(evlp, identityset.PrivateKey((i+1)%identityset.Size()))
    73  		requireT.NoError(err)
    74  		transferAmount.Add(transferAmount, tsf.Amount())
    75  		sevlps = append(sevlps, sevlp)
    76  	}
    77  
    78  	amount := calculateTransferAmount(sevlps)
    79  	requireT.Equal(amount, transferAmount)
    80  }