github.com/aergoio/aergo@v1.3.1/contract/name/execute_test.go (about)

     1  package name
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/aergoio/aergo/state"
     7  	"github.com/aergoio/aergo/types"
     8  	"github.com/stretchr/testify/assert"
     9  )
    10  
    11  func TestExcuteNameTx(t *testing.T) {
    12  	initTest(t)
    13  	defer deinitTest()
    14  	txBody := &types.TxBody{}
    15  	txBody.Account = types.ToAddress("AmMXVdJ8DnEFysN58cox9RADC74dF1CLrQimKCMdB4XXMkJeuQgL")
    16  	txBody.Recipient = []byte(types.AergoName)
    17  	txBody.Amount = types.NamePrice.Bytes()
    18  
    19  	name := "AB1234567890"
    20  	txBody.Payload = buildNamePayload(name, types.NameCreate, "")
    21  
    22  	sender, _ := sdb.GetStateDB().GetAccountStateV(txBody.Account)
    23  	sender.AddBalance(types.MaxAER)
    24  	receiver, _ := sdb.GetStateDB().GetAccountStateV(txBody.Recipient)
    25  	bs := sdb.NewBlockState(sdb.GetRoot())
    26  	scs := openContractState(t, bs)
    27  
    28  	_, err := ExecuteNameTx(bs, scs, txBody, sender, receiver, 0)
    29  	assert.NoError(t, err, "execute name tx")
    30  
    31  	//race
    32  	tmpAddress := "AmNHAxiGbZJjKjdGGNj2NBoAXGwdzX9Bg59eqbek9n49JpiaZ3As"
    33  	txBody.Account = types.ToAddress(tmpAddress)
    34  	_, err = ExecuteNameTx(bs, scs, txBody, sender, receiver, 0)
    35  	assert.Error(t, err, "race execute name tx")
    36  
    37  	txBody.Account = types.ToAddress("AmMXVdJ8DnEFysN58cox9RADC74dF1CLrQimKCMdB4XXMkJeuQgL")
    38  	commitContractState(t, bs, scs)
    39  	scs = openContractState(t, bs)
    40  
    41  	ret := GetAddress(scs, []byte(name))
    42  	assert.Equal(t, txBody.Account, ret, "pubkey address")
    43  	ret = GetOwner(scs, []byte(name))
    44  	assert.Equal(t, txBody.Account, ret, "pubkey owner")
    45  
    46  	_, err = ExecuteNameTx(bs, scs, txBody, sender, receiver, 0)
    47  	assert.Error(t, err, "execute name tx")
    48  
    49  	buyer := "AmMSMkVHQ6qRVA7G7rqwjvv2NBwB48tTekJ2jFMrjfZrsofePgay"
    50  	txBody.Payload = buildNamePayload(name, types.NameUpdate, buyer)
    51  	_, err = ExecuteNameTx(bs, scs, txBody, sender, receiver, 1)
    52  	assert.NoError(t, err, "execute to update name")
    53  
    54  	commitContractState(t, bs, scs)
    55  	scs = openContractState(t, bs)
    56  
    57  	ret = GetAddress(scs, []byte(name))
    58  	assert.Equal(t, buyer, types.EncodeAddress(ret), "pubkey address")
    59  	ret = GetOwner(scs, []byte(name))
    60  	assert.Equal(t, buyer, types.EncodeAddress(ret), "pubkey owner")
    61  
    62  	//invalid case
    63  	_, err = ExecuteNameTx(bs, scs, txBody, sender, receiver, 2)
    64  	assert.Error(t, err, "execute invalid updating name")
    65  
    66  	txBody.Payload = txBody.Payload[1:]
    67  	_, err = ExecuteNameTx(bs, scs, txBody, sender, receiver, 2)
    68  	assert.Error(t, err, "execute invalid payload")
    69  }
    70  
    71  func TestExcuteFailNameTx(t *testing.T) {
    72  	initTest(t)
    73  	defer deinitTest()
    74  	txBody := &types.TxBody{}
    75  
    76  	txBody.Account = types.ToAddress("AmMXVdJ8DnEFysN58cox9RADC74dF1CLrQimKCMdB4XXMkJeuQgL")
    77  	txBody.Recipient = []byte(types.AergoName)
    78  
    79  	name := "AB1234567890"
    80  	txBody.Payload = buildNamePayload(name, types.NameCreate+"Broken", "")
    81  
    82  	sender, _ := sdb.GetStateDB().GetAccountStateV(txBody.Account)
    83  	receiver, _ := sdb.GetStateDB().GetAccountStateV(txBody.Recipient)
    84  	bs := sdb.NewBlockState(sdb.GetRoot())
    85  	scs := openContractState(t, bs)
    86  	_, err := ExecuteNameTx(bs, scs, txBody, sender, receiver, 0)
    87  	assert.Error(t, err, "execute name tx")
    88  }
    89  
    90  func openContractState(t *testing.T, bs *state.BlockState) *state.ContractState {
    91  	nameContractID := types.ToAccountID([]byte(types.AergoName))
    92  	nameContract, err := bs.GetAccountState(nameContractID)
    93  	assert.NoError(t, err, "could not get account state")
    94  	scs, err := bs.OpenContractState(nameContractID, nameContract)
    95  	assert.NoError(t, err, "could not open contract state")
    96  	return scs
    97  }
    98  
    99  func openSystemContractState(t *testing.T, bs *state.BlockState) *state.ContractState {
   100  	systemContractID := types.ToAccountID([]byte(types.AergoSystem))
   101  	systemContract, err := bs.GetAccountState(systemContractID)
   102  	assert.NoError(t, err, "could not get account state")
   103  	scs, err := bs.OpenContractState(systemContractID, systemContract)
   104  	assert.NoError(t, err, "could not open contract state")
   105  	return scs
   106  }
   107  
   108  func commitContractState(t *testing.T, bs *state.BlockState, scs *state.ContractState) {
   109  	bs.StageContractState(scs)
   110  	bs.Update()
   111  	bs.Commit()
   112  	sdb.UpdateRoot(bs)
   113  }
   114  func nextBlockContractState(t *testing.T, bs *state.BlockState, scs *state.ContractState) *state.ContractState {
   115  	commitContractState(t, bs, scs)
   116  	return openContractState(t, bs)
   117  }