From 3287bbe713da6ec6021a792b06f0177e680bec77 Mon Sep 17 00:00:00 2001 From: miiu Date: Thu, 12 Sep 2024 14:01:20 +0300 Subject: [PATCH] bug fix multiple recreate --- .../testdata/updateNFT/token-color-1.json | 16 +++ .../testdata/updateNFT/token-color-2.json | 16 +++ integrationtests/updateNFT_test.go | 110 ++++++++++++++++++ .../elasticproc/converters/tokenMetaData.go | 34 +++++- 4 files changed, 171 insertions(+), 5 deletions(-) create mode 100644 integrationtests/testdata/updateNFT/token-color-1.json create mode 100644 integrationtests/testdata/updateNFT/token-color-2.json diff --git a/integrationtests/testdata/updateNFT/token-color-1.json b/integrationtests/testdata/updateNFT/token-color-1.json new file mode 100644 index 00000000..4c4f1a45 --- /dev/null +++ b/integrationtests/testdata/updateNFT/token-color-1.json @@ -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==" + } +} diff --git a/integrationtests/testdata/updateNFT/token-color-2.json b/integrationtests/testdata/updateNFT/token-color-2.json new file mode 100644 index 00000000..87eccc69 --- /dev/null +++ b/integrationtests/testdata/updateNFT/token-color-2.json @@ -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==" + } +} diff --git a/integrationtests/updateNFT_test.go b/integrationtests/updateNFT_test.go index 11fb2b84..5a482431 100644 --- a/integrationtests/updateNFT_test.go +++ b/integrationtests/updateNFT_test.go @@ -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)) +} diff --git a/process/elasticproc/converters/tokenMetaData.go b/process/elasticproc/converters/tokenMetaData.go index b51a620b..8cff539b 100644 --- a/process/elasticproc/converters/tokenMetaData.go +++ b/process/elasticproc/converters/tokenMetaData.go @@ -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))