github.com/aergoio/aergo@v1.3.1/chain/chainhandle_test.go (about)

     1  /**
     2   *  @file
     3   *  @copyright defined in aergo/LICENSE.txt
     4   */
     5  package chain
     6  
     7  import (
     8  	"io/ioutil"
     9  	"math/big"
    10  	"os"
    11  	"testing"
    12  
    13  	"github.com/aergoio/aergo-lib/db"
    14  	"github.com/aergoio/aergo/account/key"
    15  	"github.com/aergoio/aergo/contract"
    16  	"github.com/aergoio/aergo/contract/system"
    17  	"github.com/aergoio/aergo/state"
    18  	"github.com/aergoio/aergo/types"
    19  	"github.com/stretchr/testify/assert"
    20  )
    21  
    22  var sdb *state.ChainStateDB
    23  var keystore *key.Store
    24  var chainID []byte
    25  
    26  func initTest(t *testing.T, testmode bool) {
    27  	sdb = state.NewChainStateDB()
    28  	tmpdir, _ := ioutil.TempDir("", "test")
    29  	keystore = key.NewStore(tmpdir, 0)
    30  	sdb.Init(string(db.BadgerImpl), tmpdir, nil, testmode)
    31  	genesis := types.GetTestGenesis()
    32  	chainID = genesis.Block().GetHeader().ChainID
    33  
    34  	err := sdb.SetGenesis(genesis, nil)
    35  	if err != nil {
    36  		t.Fatalf("failed init : %s", err.Error())
    37  	}
    38  	types.InitGovernance("dpos", true)
    39  	system.InitGovernance("dpos")
    40  }
    41  
    42  func deinitTest() {
    43  	sdb.Close()
    44  	os.RemoveAll("test")
    45  }
    46  func makeTestAddress(t *testing.T) []byte {
    47  	addr, err := keystore.CreateKey("test")
    48  	assert.NoError(t, err, "could not create key")
    49  	return addr
    50  }
    51  
    52  func signTestAddress(t *testing.T, tx *types.Tx) {
    53  	_, err := keystore.Unlock(tx.GetBody().GetAccount(), "test")
    54  	assert.NoError(t, err, "could not unlock key")
    55  	err = keystore.SignTx(tx, nil)
    56  	assert.NoError(t, err, "could not sign key")
    57  }
    58  
    59  func TestErrorInExecuteTx(t *testing.T) {
    60  	initTest(t, true)
    61  	defer deinitTest()
    62  	bs := state.NewBlockState(sdb.GetStateDB())
    63  
    64  	tx := &types.Tx{}
    65  
    66  	err := executeTx(nil, nil, bs, types.NewTransaction(tx), 0, 0, nil, contract.ChainService, chainID)
    67  	assert.EqualError(t, err, types.ErrTxFormatInvalid.Error(), "execute empty tx")
    68  
    69  	tx.Body = &types.TxBody{}
    70  
    71  	err = executeTx(nil, nil, bs, types.NewTransaction(tx), 0, 0, nil, contract.ChainService, chainID)
    72  	assert.EqualError(t, err, types.ErrTxInvalidChainIdHash.Error(), "execute empty tx body")
    73  
    74  	tx.Body.ChainIdHash = chainID
    75  	tx.Body.Account = makeTestAddress(t)
    76  	tx.Body.Recipient = makeTestAddress(t)
    77  	err = executeTx(nil, nil, bs, types.NewTransaction(tx), 0, 0, nil, contract.ChainService, chainID)
    78  	assert.EqualError(t, err, types.ErrTxHasInvalidHash.Error(), "execute tx body with account")
    79  
    80  	signTestAddress(t, tx)
    81  	err = executeTx(nil, nil, bs, types.NewTransaction(tx), 0, 0, nil, contract.ChainService, chainID)
    82  	assert.EqualError(t, err, types.ErrTxNonceTooLow.Error(), "execute tx body with account")
    83  
    84  	tx.Body.Nonce = 1
    85  	tx.Body.Amount = new(big.Int).Add(types.StakingMinimum, types.StakingMinimum).Bytes()
    86  	signTestAddress(t, tx)
    87  	err = executeTx(nil, nil, bs, types.NewTransaction(tx), 0, 0, nil, contract.ChainService, chainID)
    88  	assert.EqualError(t, err, types.ErrInsufficientBalance.Error(), "execute tx body with nonce")
    89  
    90  	tx.Body.Amount = types.MaxAER.Bytes()
    91  	signTestAddress(t, tx)
    92  	err = executeTx(nil, nil, bs, types.NewTransaction(tx), 0, 0, nil, contract.ChainService, chainID)
    93  	assert.EqualError(t, err, types.ErrInsufficientBalance.Error(), "execute tx body with nonce")
    94  }
    95  
    96  func TestBasicExecuteTx(t *testing.T) {
    97  	initTest(t, true)
    98  	defer deinitTest()
    99  	bs := state.NewBlockState(sdb.GetStateDB())
   100  
   101  	tx := &types.Tx{Body: &types.TxBody{}}
   102  
   103  	tx.Body.ChainIdHash = chainID
   104  	tx.Body.Account = makeTestAddress(t)
   105  	tx.Body.Recipient = makeTestAddress(t)
   106  	tx.Body.Nonce = 1
   107  	signTestAddress(t, tx)
   108  	err := executeTx(nil, nil, bs, types.NewTransaction(tx), 0, 0, nil, contract.ChainService, chainID)
   109  	assert.NoError(t, err, "execute amount 0")
   110  
   111  	tx.Body.Nonce = 2
   112  	tx.Body.Amount = new(big.Int).SetUint64(1000).Bytes()
   113  	signTestAddress(t, tx)
   114  	err = executeTx(nil, nil, bs, types.NewTransaction(tx), 0, 0, nil, contract.ChainService, chainID)
   115  	assert.NoError(t, err, "execute amount 1000")
   116  
   117  	tx.Body.Nonce = 3
   118  	tx.Body.Amount = (new(big.Int).Add(types.StakingMinimum, new(big.Int).SetUint64(1))).Bytes()
   119  	tx.Body.Amount = types.StakingMinimum.Bytes()
   120  	tx.Body.Recipient = []byte(types.AergoSystem)
   121  	tx.Body.Type = types.TxType_GOVERNANCE
   122  	tx.Body.Payload = []byte(`{"Name":"v1stake"}`)
   123  	signTestAddress(t, tx)
   124  	err = executeTx(nil, nil, bs, types.NewTransaction(tx), 0, 0, nil, contract.ChainService, chainID)
   125  	assert.NoError(t, err, "execute governance type")
   126  
   127  }