Skip to content

Commit

Permalink
Merge pull request #296 from multiversx/bug-fix-multiple-recreate
Browse files Browse the repository at this point in the history
Bug-fix esdt metadata recreate
miiu96 authored Sep 13, 2024
2 parents c555bdf + 3287bbe commit db67228
Showing 4 changed files with 171 additions and 5 deletions.
16 changes: 16 additions & 0 deletions integrationtests/testdata/updateNFT/token-color-1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"identifier": "COLORS-df0e82-01",
"token": "COLORS-df0e82",
"numDecimals": 0,
"nonce": 1,
"timestamp": 5040,
"data": {
"uris": [
"dXJp"
],
"whiteListedStorage": false,
"name": "GREEN",
"nonEmptyURIs": true,
"hash": "aGFzaA=="
}
}
16 changes: 16 additions & 0 deletions integrationtests/testdata/updateNFT/token-color-2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"identifier": "COLORS-df0e82-02",
"token": "COLORS-df0e82",
"numDecimals": 0,
"nonce": 2,
"timestamp": 5040,
"data": {
"uris": [
"dXJp"
],
"whiteListedStorage": false,
"name": "GREEN",
"nonEmptyURIs": true,
"hash": "aGFzaA=="
}
}
110 changes: 110 additions & 0 deletions integrationtests/updateNFT_test.go
Original file line number Diff line number Diff line change
@@ -428,3 +428,113 @@ func TestCreateNFTAndMetaDataRecreate(t *testing.T) {
require.Nil(t, err)
require.JSONEq(t, readExpectedResult("./testdata/updateNFT/token-after-update.json"), string(genericResponse.Docs[0].Source))
}

func TestMultipleESDTMetadataRecreate(t *testing.T) {
setLogLevelDebug()

esClient, err := createESClient(esURL)
require.Nil(t, err)

esdtCreateData := &esdt.ESDigitalToken{
TokenMetaData: &esdt.MetaData{
Name: []byte("YELLOW"),
URIs: [][]byte{[]byte("uri"), []byte("uri")},
},
}
marshalizedCreate, _ := json.Marshal(esdtCreateData)

esProc, err := CreateElasticProcessor(esClient)
require.Nil(t, err)

header := &dataBlock.Header{
Round: 50,
TimeStamp: 5040,
ShardID: 1,
}
body := &dataBlock.Body{}

// CREATE NFT data
address := "erd1w7jyzuj6cv4ngw8luhlkakatjpmjh3ql95lmxphd3vssc4vpymks6k5th7"
pool := &outport.TransactionPool{
Logs: []*outport.LogData{
{
TxHash: hex.EncodeToString([]byte("h1")),
Log: &transaction.Log{
Events: []*transaction.Event{
{
Address: decodeAddress(address),
Identifier: []byte(core.BuiltInFunctionESDTNFTCreate),
Topics: [][]byte{[]byte("COLORS-df0e82"), big.NewInt(1).Bytes(), big.NewInt(1).Bytes(), marshalizedCreate},
},
nil,
},
},
},
{
TxHash: hex.EncodeToString([]byte("h2")),
Log: &transaction.Log{
Events: []*transaction.Event{
{
Address: decodeAddress(address),
Identifier: []byte(core.BuiltInFunctionESDTNFTCreate),
Topics: [][]byte{[]byte("COLORS-df0e82"), big.NewInt(2).Bytes(), big.NewInt(1).Bytes(), marshalizedCreate},
},
nil,
},
},
},
},
}
err = esProc.SaveTransactions(createOutportBlockWithHeader(body, header, pool, nil, testNumOfShards))
require.Nil(t, err)

// RECREATE
reCreate := &esdt.ESDigitalToken{
TokenMetaData: &esdt.MetaData{
Name: []byte("GREEN"),
URIs: [][]byte{[]byte("uri")},
Hash: []byte("hash"),
},
}
marshalizedReCreate, _ := json.Marshal(reCreate)

pool = &outport.TransactionPool{
Logs: []*outport.LogData{
{
TxHash: hex.EncodeToString([]byte("h1")),
Log: &transaction.Log{
Events: []*transaction.Event{
{
Address: decodeAddress(address),
Identifier: []byte(core.ESDTMetaDataRecreate),
Topics: [][]byte{[]byte("COLORS-df0e82"), big.NewInt(1).Bytes(), big.NewInt(0).Bytes(), marshalizedReCreate},
},
nil,
},
},
},
{
TxHash: hex.EncodeToString([]byte("h2")),
Log: &transaction.Log{
Events: []*transaction.Event{
{
Address: decodeAddress(address),
Identifier: []byte(core.ESDTMetaDataRecreate),
Topics: [][]byte{[]byte("COLORS-df0e82"), big.NewInt(2).Bytes(), big.NewInt(0).Bytes(), marshalizedReCreate},
},
nil,
},
},
},
},
}
err = esProc.SaveTransactions(createOutportBlockWithHeader(body, header, pool, nil, testNumOfShards))
require.Nil(t, err)

ids := []string{"COLORS-df0e82-01", "COLORS-df0e82-02"}
genericResponse := &GenericResponse{}
err = esClient.DoMultiGet(context.Background(), ids, indexerdata.TokensIndex, true, genericResponse)
require.Nil(t, err)
require.JSONEq(t, readExpectedResult("./testdata/updateNFT/token-color-1.json"), string(genericResponse.Docs[0].Source))
require.JSONEq(t, readExpectedResult("./testdata/updateNFT/token-color-2.json"), string(genericResponse.Docs[1].Source))
}
34 changes: 29 additions & 5 deletions process/elasticproc/converters/tokenMetaData.go
Original file line number Diff line number Diff line change
@@ -83,25 +83,49 @@ func PrepareNFTUpdateData(buffSlice *data.BufferSlice, updateNFTData []*data.NFT
metaData := []byte(fmt.Sprintf(`{"update":{ "_index":"%s","_id":"%s"}}%s`, index, id, "\n"))
freezeOrUnfreezeTokenIndex := (nftUpdate.Freeze || nftUpdate.UnFreeze) && !isAccountsESDTIndex
if freezeOrUnfreezeTokenIndex {
return buffSlice.PutData(metaData, prepareSerializeDataForFreezeAndUnFreeze(nftUpdate))
err := buffSlice.PutData(metaData, prepareSerializeDataForFreezeAndUnFreeze(nftUpdate))
if err != nil {
return err
}
continue
}
pauseOrUnPauseTokenIndex := (nftUpdate.Pause || nftUpdate.UnPause) && !isAccountsESDTIndex
if pauseOrUnPauseTokenIndex {
return buffSlice.PutData(metaData, prepareSerializedDataForPauseAndUnPause(nftUpdate))
err := buffSlice.PutData(metaData, prepareSerializedDataForPauseAndUnPause(nftUpdate))
if err != nil {
return err
}

continue
}
if nftUpdate.NewMetaData != nil {
serializedData, err := prepareSerializedDataForMetaDataRecreate(nftUpdate)
if err != nil {
return err
}
return buffSlice.PutData(metaData, serializedData)
err = buffSlice.PutData(metaData, serializedData)
if err != nil {
return err
}

continue
}

if nftUpdate.NewCreator != "" {
return buffSlice.PutData(metaData, prepareSerializeDataForNewCreator(nftUpdate))
err := buffSlice.PutData(metaData, prepareSerializeDataForNewCreator(nftUpdate))
if err != nil {
return err
}

continue
}
if nftUpdate.NewRoyalties.HasValue {
return buffSlice.PutData(metaData, prepareSerializeDataForNewRoyalties(nftUpdate))
err := buffSlice.PutData(metaData, prepareSerializeDataForNewRoyalties(nftUpdate))
if err != nil {
return err
}

continue
}

truncatedAttributes := TruncateFieldIfExceedsMaxLengthBase64(string(nftUpdate.NewAttributes))

0 comments on commit db67228

Please sign in to comment.