Skip to content

Commit

Permalink
add integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
BeniaminDrasovean committed Jan 31, 2024
1 parent 9a9001e commit 88f2fb2
Show file tree
Hide file tree
Showing 7 changed files with 640 additions and 2 deletions.
157 changes: 157 additions & 0 deletions integrationTests/vm/txsFee/common.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,153 @@
package txsFee

import (
"bytes"
"encoding/hex"
"math/big"
"testing"

"github.com/multiversx/mx-chain-core-go/core"
"github.com/multiversx/mx-chain-core-go/data/esdt"
"github.com/multiversx/mx-chain-core-go/data/transaction"
"github.com/multiversx/mx-chain-go/common"
"github.com/multiversx/mx-chain-go/integrationTests/vm"
"github.com/multiversx/mx-chain-go/state"
"github.com/stretchr/testify/require"
)

const gasPrice = uint64(10)

type metaData struct {
tokenId []byte
nonce []byte
name []byte
royalties []byte
hash []byte
attributes []byte
uris [][]byte
}

func getDefaultMetaData() *metaData {
return &metaData{
tokenId: []byte(hex.EncodeToString([]byte("tokenId"))),
nonce: []byte(hex.EncodeToString(big.NewInt(0).Bytes())),
name: []byte(hex.EncodeToString([]byte("name"))),
royalties: []byte(hex.EncodeToString(big.NewInt(10).Bytes())),
hash: []byte(hex.EncodeToString([]byte("hash"))),
attributes: []byte(hex.EncodeToString([]byte("attributes"))),
uris: [][]byte{[]byte(hex.EncodeToString([]byte("uri1"))), []byte(hex.EncodeToString([]byte("uri2"))), []byte(hex.EncodeToString([]byte("uri3")))},
}
}

func getMetaDataFromAcc(t *testing.T, testContext *vm.VMTestContext, accWithMetaData []byte, token []byte) *esdt.MetaData {
account, err := testContext.Accounts.LoadAccount(accWithMetaData)
require.Nil(t, err)
userAccount, ok := account.(state.UserAccountHandler)
require.True(t, ok)

key := append(token, big.NewInt(0).SetUint64(1).Bytes()...)
esdtDataBytes, _, err := userAccount.RetrieveValue(key)
require.Nil(t, err)
esdtData := &esdt.ESDigitalToken{}
err = testContext.Marshalizer.Unmarshal(esdtData, esdtDataBytes)
require.Nil(t, err)

return esdtData.TokenMetaData
}

func checkMetaData(t *testing.T, testContext *vm.VMTestContext, accWithMetaData []byte, token []byte, expectedMetaData *metaData) {
retrievedMetaData := getMetaDataFromAcc(t, testContext, accWithMetaData, token)

require.Equal(t, expectedMetaData.nonce, []byte(hex.EncodeToString(big.NewInt(int64(retrievedMetaData.Nonce)).Bytes())))
require.Equal(t, expectedMetaData.name, []byte(hex.EncodeToString(retrievedMetaData.Name)))
require.Equal(t, expectedMetaData.royalties, []byte(hex.EncodeToString(big.NewInt(int64(retrievedMetaData.Royalties)).Bytes())))
require.Equal(t, expectedMetaData.hash, []byte(hex.EncodeToString(retrievedMetaData.Hash)))
for i, uri := range expectedMetaData.uris {
require.Equal(t, uri, []byte(hex.EncodeToString(retrievedMetaData.URIs[i])))
}
require.Equal(t, expectedMetaData.attributes, []byte(hex.EncodeToString(retrievedMetaData.Attributes)))
}

func getDynamicTokenTypes() []string {
return []string{
core.DynamicNFTESDT,
core.DynamicSFTESDT,
core.DynamicMetaESDT,
}
}

func getTokenTypes() []string {

Check failure on line 79 in integrationTests/vm/txsFee/common.go

View workflow job for this annotation

GitHub Actions / golangci linter

func `getTokenTypes` is unused (unused)
return []string{
core.FungibleESDT,
core.NonFungibleESDT,
core.NonFungibleESDTv2,
core.MetaESDT,
core.SemiFungibleESDT,
core.DynamicNFTESDT,
core.DynamicSFTESDT,
core.DynamicMetaESDT,
}
}

func createTokenTx(
sndAddr []byte,
rcvAddr []byte,
gasLimit uint64,
quantity int64,
metaData *metaData,
) *transaction.Transaction {
txDataField := bytes.Join(
[][]byte{
[]byte(core.BuiltInFunctionESDTNFTCreate),
metaData.tokenId,
[]byte(hex.EncodeToString(big.NewInt(quantity).Bytes())), // quantity
metaData.name,
metaData.royalties,
metaData.hash,
metaData.attributes,
[]byte(hex.EncodeToString([]byte("uri"))),
},
[]byte("@"),
)

return &transaction.Transaction{
Nonce: 0,
SndAddr: sndAddr,
RcvAddr: rcvAddr,
GasLimit: gasLimit,
GasPrice: gasPrice,
Data: txDataField,
Value: big.NewInt(0),
}
}

func setTokenTypeTx(
sndAddr []byte,
gasLimit uint64,
tokenId []byte,
tokenType string,
) *transaction.Transaction {
txDataField := bytes.Join(
[][]byte{
[]byte(core.ESDTSetTokenType),
[]byte(hex.EncodeToString(tokenId)),
[]byte(hex.EncodeToString([]byte(tokenType))),
},
[]byte("@"),
)

return &transaction.Transaction{
Nonce: 0,
SndAddr: sndAddr,
RcvAddr: core.SystemAccountAddress,
GasLimit: gasLimit,
GasPrice: gasPrice,

Data: txDataField,
Value: big.NewInt(0),
}
}

func getAccount(tb testing.TB, testContext *vm.VMTestContext, scAddress []byte) state.UserAccountHandler {
scAcc, err := testContext.Accounts.LoadAccount(scAddress)
require.Nil(tb, err)
Expand All @@ -25,3 +164,21 @@ func getAccountDataTrie(tb testing.TB, testContext *vm.VMTestContext, address []

return dataTrieInstance
}

func createAccWithBalance(t *testing.T, accnts state.AccountsAdapter, pubKey []byte, egldValue *big.Int) {
account, err := accnts.LoadAccount(pubKey)
require.Nil(t, err)

userAccount, ok := account.(state.UserAccountHandler)
require.True(t, ok)

userAccount.IncreaseNonce(0)
err = userAccount.AddToBalance(egldValue)
require.Nil(t, err)

err = accnts.SaveAccount(userAccount)
require.Nil(t, err)

_, err = accnts.Commit()
require.Nil(t, err)
}
99 changes: 99 additions & 0 deletions integrationTests/vm/txsFee/esdtMetaDataRecreate_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package txsFee

import (
"bytes"
"encoding/hex"
"math/big"
"testing"

"github.com/multiversx/mx-chain-core-go/core"
"github.com/multiversx/mx-chain-core-go/data/transaction"
"github.com/multiversx/mx-chain-go/config"
"github.com/multiversx/mx-chain-go/integrationTests/vm"
"github.com/multiversx/mx-chain-go/integrationTests/vm/txsFee/utils"
vmcommon "github.com/multiversx/mx-chain-vm-common-go"
"github.com/stretchr/testify/require"
)

func TestESDTMetaDataRecreate(t *testing.T) {
tokenTypes := getDynamicTokenTypes()
for _, tokenType := range tokenTypes {
testName := "metaDataRecreate for " + tokenType
t.Run(testName, func(t *testing.T) {
runEsdtMetaDataRecreateTest(t, tokenType)
})
}
}

func runEsdtMetaDataRecreateTest(t *testing.T, tokenType string) {
sndAddr := []byte("12345678901234567890123456789012")
token := []byte("tokenId")
roles := [][]byte{[]byte(core.ESDTMetaDataRecreate), []byte(core.ESDTRoleNFTCreate)}
baseEsdtKeyPrefix := core.ProtectedKeyPrefix + core.ESDTKeyIdentifier
key := append([]byte(baseEsdtKeyPrefix), token...)

testContext, err := vm.CreatePreparedTxProcessorWithVMs(config.EnableEpochs{})
require.Nil(t, err)
defer testContext.Close()

createAccWithBalance(t, testContext.Accounts, sndAddr, big.NewInt(100000000))
createAccWithBalance(t, testContext.Accounts, core.ESDTSCAddress, big.NewInt(100000000))
utils.SetESDTRoles(t, testContext.Accounts, sndAddr, token, roles)

tx := setTokenTypeTx(core.ESDTSCAddress, 100000, token, tokenType)
retCode, err := testContext.TxProcessor.ProcessTransaction(tx)
require.Equal(t, vmcommon.Ok, retCode)
require.Nil(t, err)

defaultMetaData := getDefaultMetaData()
tx = createTokenTx(sndAddr, sndAddr, 100000, 1, defaultMetaData)
retCode, err = testContext.TxProcessor.ProcessTransaction(tx)
require.Equal(t, vmcommon.Ok, retCode)
require.Nil(t, err)

// TODO change default metadata
defaultMetaData.nonce = []byte(hex.EncodeToString(big.NewInt(1).Bytes()))
tx = esdtMetaDataRecreateTx(sndAddr, sndAddr, 100000, defaultMetaData)
retCode, err = testContext.TxProcessor.ProcessTransaction(tx)
require.Equal(t, vmcommon.Ok, retCode)
require.Nil(t, err)

_, err = testContext.Accounts.Commit()
require.Nil(t, err)

checkMetaData(t, testContext, core.SystemAccountAddress, key, defaultMetaData)
}

func esdtMetaDataRecreateTx(
sndAddr []byte,
rcvAddr []byte,
gasLimit uint64,
metaData *metaData,
) *transaction.Transaction {
txDataField := bytes.Join(
[][]byte{
[]byte(core.ESDTMetaDataRecreate),
metaData.tokenId,
metaData.nonce,
metaData.name,
metaData.royalties,
metaData.hash,
metaData.attributes,
metaData.uris[0],
metaData.uris[1],
metaData.uris[2],
},
[]byte("@"),
)

return &transaction.Transaction{
Nonce: 1,
SndAddr: sndAddr,
RcvAddr: rcvAddr,
GasLimit: gasLimit,
GasPrice: gasPrice,

Data: txDataField,
Value: big.NewInt(0),
}
}
100 changes: 100 additions & 0 deletions integrationTests/vm/txsFee/esdtMetaDataUpdate_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package txsFee

import (
"bytes"
"encoding/hex"
"math/big"
"testing"

"github.com/multiversx/mx-chain-core-go/core"
"github.com/multiversx/mx-chain-core-go/data/transaction"
"github.com/multiversx/mx-chain-go/config"
"github.com/multiversx/mx-chain-go/integrationTests/vm"
"github.com/multiversx/mx-chain-go/integrationTests/vm/txsFee/utils"
vmcommon "github.com/multiversx/mx-chain-vm-common-go"
"github.com/stretchr/testify/require"
)

func TestESDTMetaDataUpdate(t *testing.T) {
tokenTypes := getDynamicTokenTypes()
for _, tokenType := range tokenTypes {
testName := "metaDataUpdate for " + tokenType
t.Run(testName, func(t *testing.T) {
runEsdtMetaDataUpdateTest(t, tokenType)
})
}
}

func runEsdtMetaDataUpdateTest(t *testing.T, tokenType string) {
sndAddr := []byte("12345678901234567890123456789012")
token := []byte("tokenId")
roles := [][]byte{[]byte(core.ESDTRoleNFTUpdate), []byte(core.ESDTRoleNFTCreate)}
baseEsdtKeyPrefix := core.ProtectedKeyPrefix + core.ESDTKeyIdentifier
key := append([]byte(baseEsdtKeyPrefix), token...)

testContext, err := vm.CreatePreparedTxProcessorWithVMs(config.EnableEpochs{})
require.Nil(t, err)
defer testContext.Close()

createAccWithBalance(t, testContext.Accounts, sndAddr, big.NewInt(100000000))
createAccWithBalance(t, testContext.Accounts, core.ESDTSCAddress, big.NewInt(100000000))
utils.SetESDTRoles(t, testContext.Accounts, sndAddr, token, roles)

tx := setTokenTypeTx(core.ESDTSCAddress, 100000, token, tokenType)
retCode, err := testContext.TxProcessor.ProcessTransaction(tx)
require.Equal(t, vmcommon.Ok, retCode)
require.Nil(t, err)

defaultMetaData := getDefaultMetaData()
tx = createTokenTx(sndAddr, sndAddr, 100000, 1, defaultMetaData)
retCode, err = testContext.TxProcessor.ProcessTransaction(tx)
require.Equal(t, vmcommon.Ok, retCode)
require.Nil(t, err)

// TODO change default metadata
defaultMetaData.nonce = []byte(hex.EncodeToString(big.NewInt(1).Bytes()))
defaultMetaData.name = []byte(hex.EncodeToString([]byte("newName")))
defaultMetaData.hash = []byte(hex.EncodeToString([]byte("newHash")))
defaultMetaData.uris = [][]byte{defaultMetaData.uris[1]}
tx = esdtMetaDataUpdateTx(sndAddr, sndAddr, 100000, defaultMetaData)
retCode, err = testContext.TxProcessor.ProcessTransaction(tx)
require.Equal(t, vmcommon.Ok, retCode)
require.Nil(t, err)

_, err = testContext.Accounts.Commit()
require.Nil(t, err)

checkMetaData(t, testContext, core.SystemAccountAddress, key, defaultMetaData)
}

func esdtMetaDataUpdateTx(
sndAddr []byte,
rcvAddr []byte,
gasLimit uint64,
metaData *metaData,
) *transaction.Transaction {
txDataField := bytes.Join(
[][]byte{
[]byte(core.ESDTMetaDataUpdate),
metaData.tokenId,
metaData.nonce,
metaData.name,
metaData.royalties,
metaData.hash,
metaData.attributes,
metaData.uris[0],
},
[]byte("@"),
)

return &transaction.Transaction{
Nonce: 1,
SndAddr: sndAddr,
RcvAddr: rcvAddr,
GasLimit: gasLimit,
GasPrice: gasPrice,

Data: txDataField,
Value: big.NewInt(0),
}
}
Loading

0 comments on commit 88f2fb2

Please sign in to comment.