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

     1  package name
     2  
     3  import (
     4  	"encoding/json"
     5  	"math/big"
     6  	"os"
     7  	"testing"
     8  
     9  	"github.com/aergoio/aergo-lib/db"
    10  	"github.com/aergoio/aergo/state"
    11  	"github.com/aergoio/aergo/types"
    12  	"github.com/stretchr/testify/assert"
    13  )
    14  
    15  var sdb *state.ChainStateDB
    16  var block *types.Block
    17  
    18  func initTest(t *testing.T) {
    19  	genesis := types.GetTestGenesis()
    20  	sdb = state.NewChainStateDB()
    21  	sdb.Init(string(db.BadgerImpl), "test", genesis.Block(), false)
    22  	err := sdb.SetGenesis(genesis, nil)
    23  	if err != nil {
    24  		t.Fatalf("failed init : %s", err.Error())
    25  	}
    26  	block = genesis.Block()
    27  }
    28  
    29  func deinitTest() {
    30  	sdb.Close()
    31  	os.RemoveAll("test")
    32  }
    33  
    34  func TestName(t *testing.T) {
    35  	initTest(t)
    36  	defer deinitTest()
    37  	name := "AB1234567890"
    38  	owner := types.ToAddress("AmMXVdJ8DnEFysN58cox9RADC74dF1CLrQimKCMdB4XXMkJeuQgL")
    39  	buyer := "AmMSMkVHQ6qRVA7G7rqwjvv2NBwB48tTekJ2jFMrjfZrsofePgay"
    40  
    41  	tx := &types.TxBody{Account: owner, Payload: buildNamePayload(name, types.NameCreate, "")}
    42  	tx.Recipient = []byte(types.AergoName)
    43  
    44  	sender, _ := sdb.GetStateDB().GetAccountStateV(tx.Account)
    45  	receiver, _ := sdb.GetStateDB().GetAccountStateV(tx.Recipient)
    46  	bs := sdb.NewBlockState(sdb.GetRoot())
    47  	scs := openContractState(t, bs)
    48  	systemcs := openSystemContractState(t, bs)
    49  
    50  	err := CreateName(scs, tx, sender, receiver, name)
    51  	assert.NoError(t, err, "create name")
    52  
    53  	scs = nextBlockContractState(t, bs, scs)
    54  	_, err = ValidateNameTx(tx, sender, scs, systemcs)
    55  	assert.Error(t, err, "same name")
    56  
    57  	ret := getAddress(scs, []byte(name))
    58  	assert.Equal(t, owner, ret, "registed owner")
    59  
    60  	tx.Payload = buildNamePayload(name, types.NameUpdate, buyer)
    61  	err = UpdateName(bs, scs, tx, sender, receiver, name, buyer)
    62  	assert.NoError(t, err, "update name")
    63  
    64  	scs = nextBlockContractState(t, bs, scs)
    65  
    66  	ret = getAddress(scs, []byte(name))
    67  	assert.Equal(t, buyer, types.EncodeAddress(ret), "registed owner")
    68  }
    69  
    70  func TestNameRecursive(t *testing.T) {
    71  	initTest(t)
    72  	defer deinitTest()
    73  	name1 := "AB1234567890"
    74  	name2 := "1234567890CD"
    75  	owner := types.ToAddress("AmMXVdJ8DnEFysN58cox9RADC74dF1CLrQimKCMdB4XXMkJeuQgL")
    76  	buyer := "AmMSMkVHQ6qRVA7G7rqwjvv2NBwB48tTekJ2jFMrjfZrsofePgay"
    77  
    78  	tx := &types.TxBody{Account: owner, Recipient: []byte(types.AergoName), Payload: buildNamePayload(name1, types.NameCreate, "")}
    79  
    80  	sender, _ := sdb.GetStateDB().GetAccountStateV(tx.Account)
    81  	receiver, _ := sdb.GetStateDB().GetAccountStateV(tx.Recipient)
    82  	bs := sdb.NewBlockState(sdb.GetRoot())
    83  	scs := openContractState(t, bs)
    84  	err := CreateName(scs, tx, sender, receiver, name1)
    85  	assert.NoError(t, err, "create name")
    86  
    87  	tx.Account = []byte(name1)
    88  	tx.Recipient = []byte(types.AergoName)
    89  	tx.Payload = buildNamePayload(name2, types.NameCreate, "")
    90  
    91  	scs = nextBlockContractState(t, bs, scs)
    92  	err = CreateName(scs, tx, sender, receiver, name2)
    93  	assert.NoError(t, err, "redirect name")
    94  
    95  	scs = nextBlockContractState(t, bs, scs)
    96  	ret := getAddress(scs, []byte(name2))
    97  	assert.Equal(t, owner, ret, "registed owner")
    98  	name1Owner := GetOwner(scs, []byte(name1))
    99  	t.Logf("name1 owner is %s", types.EncodeAddress(name1Owner))
   100  	assert.Equal(t, owner, name1Owner, "check registed pubkey owner")
   101  	name2Owner := GetOwner(scs, []byte(name2))
   102  	t.Logf("name2 owner is %s", types.EncodeAddress(name2Owner))
   103  	assert.Equal(t, owner, name2Owner, "check registed named owner")
   104  
   105  	tx.Payload = buildNamePayload(name1, types.NameUpdate, buyer)
   106  
   107  	err = UpdateName(bs, scs, tx, sender, receiver, name1, buyer)
   108  	assert.NoError(t, err, "update name")
   109  	scs = nextBlockContractState(t, bs, scs)
   110  	ret = getAddress(scs, []byte(name1))
   111  	assert.Equal(t, buyer, types.EncodeAddress(ret), "registed owner")
   112  }
   113  
   114  func TestNameNil(t *testing.T) {
   115  	initTest(t)
   116  	defer deinitTest()
   117  	name1 := "AB1234567890"
   118  	name2 := "1234567890CD"
   119  
   120  	scs, err := sdb.GetStateDB().OpenContractStateAccount(types.ToAccountID([]byte("aergo.system")))
   121  	assert.NoError(t, err, "could not open contract state")
   122  	tx := &types.TxBody{Account: []byte(name1), Payload: buildNamePayload(name2, types.NameCreate, "")}
   123  	sender, _ := sdb.GetStateDB().GetAccountStateV(tx.Account)
   124  	receiver, _ := sdb.GetStateDB().GetAccountStateV(tx.Recipient)
   125  
   126  	err = CreateName(scs, tx, sender, receiver, name2)
   127  	assert.NoError(t, err, "create name")
   128  }
   129  
   130  func TestNameSetContractOwner(t *testing.T) {
   131  	initTest(t)
   132  	defer deinitTest()
   133  	//name := "AB1234567890"
   134  	requester := types.ToAddress("AmMXVdJ8DnEFysN58cox9RADC74dF1CLrQimKCMdB4XXMkJeuQgL")
   135  	//ownerAddr := types.ToAddress("AmNExAm3zWL8j4xXDkx2UBCMoLoTgX13K3M4GM8mLPhRCq4KuLeR")
   136  	tx := &types.TxBody{
   137  		Account: requester,
   138  		Payload: []byte(`{"Name":"v1setOwner","Args":["AmNExAm3zWL8j4xXDkx2UBCMoLoTgX13K3M4GM8mLPhRCq4KuLeR"]}`),
   139  	}
   140  	tx.Recipient = []byte(types.AergoName)
   141  
   142  	sender, _ := sdb.GetStateDB().GetAccountStateV(tx.Account)
   143  	receiver, _ := sdb.GetStateDB().GetAccountStateV(tx.Recipient)
   144  	//owner, _ := sdb.GetStateDB().GetAccountStateV(ownerAddr)
   145  
   146  	receiver.AddBalance(big.NewInt(1000))
   147  
   148  	bs := sdb.NewBlockState(sdb.GetRoot())
   149  	scs := openContractState(t, bs)
   150  	//systemcs := openSystemContractState(t, bs)
   151  
   152  	blockNo := uint64(1)
   153  	_, err := ExecuteNameTx(bs, scs, tx, sender, receiver, blockNo)
   154  	assert.NoError(t, err, "execute name")
   155  	assert.Equal(t, big.NewInt(0), receiver.Balance(), "check remain")
   156  }
   157  
   158  func buildNamePayload(name string, operation string, buyer string) []byte {
   159  	var ci types.CallInfo
   160  	ci.Name = operation
   161  	if buyer != "" {
   162  		ci.Args = append(ci.Args, name, buyer)
   163  	} else {
   164  		ci.Args = append(ci.Args, name)
   165  	}
   166  	payload, err := json.Marshal(ci)
   167  	if err != nil {
   168  		return nil
   169  	}
   170  	return payload
   171  }
   172  
   173  func TestNameMap(t *testing.T) {
   174  	initTest(t)
   175  	defer deinitTest()
   176  	name1 := "AB1234567890"
   177  	name2 := "1234567890CD"
   178  	owner := types.ToAddress("AmMXVdJ8DnEFysN58cox9RADC74dF1CLrQimKCMdB4XXMkJeuQgL")
   179  	destination := types.ToAddress("AmhhvY54uW2ZbcyeADPs6MKtj6CrTYcuhHpxn1VxPST1usEY1rDm")
   180  
   181  	bs := sdb.NewBlockState(sdb.GetRoot())
   182  	scs := openContractState(t, bs)
   183  
   184  	testNameMap := &NameMap{Owner: owner, Destination: destination, Version: 1}
   185  	err := setNameMap(scs, []byte(name1), testNameMap)
   186  	assert.NoError(t, err, "error return in setNameMap")
   187  
   188  	err = setNameMap(scs, []byte(name2), testNameMap)
   189  	assert.NoError(t, err, "error return in setNameMap")
   190  
   191  	res := getNameMap(scs, []byte(name1), false)
   192  	assert.Equal(t, testNameMap.Owner, res.Owner, "Owner")
   193  	assert.Equal(t, testNameMap.Destination, res.Destination, "Destination")
   194  	assert.Equal(t, testNameMap.Version, res.Version, "Version")
   195  
   196  	res = getNameMap(scs, []byte(name2), false)
   197  	assert.Equal(t, testNameMap.Owner, res.Owner, "Owner")
   198  	assert.Equal(t, testNameMap.Destination, res.Destination, "Destination")
   199  	assert.Equal(t, testNameMap.Version, res.Version, "Version")
   200  
   201  	resOwner := getOwner(scs, []byte(name1), false)
   202  	assert.Equal(t, testNameMap.Owner, resOwner, "getOwner")
   203  
   204  	scs = nextBlockContractState(t, bs, scs)
   205  
   206  	res = getNameMap(scs, []byte(name1), true)
   207  	assert.Equal(t, testNameMap.Owner, res.Owner, "Owner")
   208  	assert.Equal(t, testNameMap.Destination, res.Destination, "Destination")
   209  	assert.Equal(t, testNameMap.Version, res.Version, "Version")
   210  
   211  	res = getNameMap(scs, []byte(name2), true)
   212  	assert.Equal(t, testNameMap.Owner, res.Owner, "Owner")
   213  	assert.Equal(t, testNameMap.Destination, res.Destination, "Destination")
   214  	assert.Equal(t, testNameMap.Version, res.Version, "Version")
   215  
   216  	resOwner = GetOwner(scs, []byte(name1))
   217  	assert.Equal(t, testNameMap.Owner, resOwner, "GetOwner")
   218  	resAddr := getAddress(scs, []byte(name1))
   219  	assert.Equal(t, testNameMap.Destination, resAddr, "getAddress")
   220  
   221  }