Skip to content

Commit

Permalink
Merge pull request #237 from multiversx/extra-checks-fields
Browse files Browse the repository at this point in the history
Extra check `tokens` index fields
  • Loading branch information
miiu96 authored Jul 20, 2023
2 parents c5c8b76 + 3412ebb commit 0d9bed3
Show file tree
Hide file tree
Showing 11 changed files with 62 additions and 34 deletions.
3 changes: 3 additions & 0 deletions data/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,7 @@ const (
MaxESDTValueLength = 100
// MaxFieldLength defines the maximum length for a keyword field, approximating the maximum length of the keyword type.
MaxFieldLength = 30000

// MaxKeywordFieldLengthBeforeBase64Encoding defines the maximum length for a keyword field that will be base64 encoded
MaxKeywordFieldLengthBeforeBase64Encoding = 22500
)
9 changes: 5 additions & 4 deletions integrationtests/testdata/updateNFT/token-after-add-uris.json

Large diffs are not rendered by default.

9 changes: 5 additions & 4 deletions integrationtests/testdata/updateNFT/token-after-freeze.json

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

9 changes: 6 additions & 3 deletions integrationtests/testdata/updateNFT/token.json

Large diffs are not rendered by default.

8 changes: 5 additions & 3 deletions integrationtests/updateNFT_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package integrationtests

import (
"bytes"
"encoding/json"
"math/big"
"testing"
Expand All @@ -23,9 +24,10 @@ func TestNFTUpdateMetadata(t *testing.T) {
esClient, err := createESClient(esURL)
require.Nil(t, err)

bigUri := bytes.Repeat([]byte("a"), 50000)
esdtCreateData := &esdt.ESDigitalToken{
TokenMetaData: &esdt.MetaData{
URIs: [][]byte{[]byte("uri"), []byte("uri")},
URIs: [][]byte{[]byte("uri"), []byte("uri"), bigUri, bigUri, bigUri},
},
}
marshalizedCreate, _ := json.Marshal(esdtCreateData)
Expand Down Expand Up @@ -77,7 +79,7 @@ func TestNFTUpdateMetadata(t *testing.T) {
{
Address: decodeAddress(address),
Identifier: []byte(core.BuiltInFunctionESDTNFTAddURI),
Topics: [][]byte{[]byte("NFT-abcd"), big.NewInt(14).Bytes(), big.NewInt(0).Bytes(), []byte("uri1"), []byte("uri2")},
Topics: [][]byte{[]byte("NFT-abcd"), big.NewInt(14).Bytes(), big.NewInt(0).Bytes(), []byte("uri"), bigUri},
},
nil,
},
Expand All @@ -98,7 +100,7 @@ func TestNFTUpdateMetadata(t *testing.T) {
{
Address: decodeAddress(address),
Identifier: []byte(core.BuiltInFunctionESDTNFTAddURI),
Topics: [][]byte{[]byte("NFT-abcd"), big.NewInt(14).Bytes(), big.NewInt(0).Bytes(), []byte("uri1"), []byte("uri2")},
Topics: [][]byte{[]byte("NFT-abcd"), big.NewInt(14).Bytes(), big.NewInt(0).Bytes(), []byte("uri"), bigUri},
},
nil,
},
Expand Down
10 changes: 10 additions & 0 deletions process/elasticproc/converters/field.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@ func TruncateFieldIfExceedsMaxLength(field string) string {
return field
}

// TruncateFieldIfExceedsMaxLengthBase64 will truncate the provided field if the max length after base64 encoding would be exceeded
// this function will be used for the fields that after will be base64 encoded
func TruncateFieldIfExceedsMaxLengthBase64(field string) string {
if len(field) > data.MaxKeywordFieldLengthBeforeBase64Encoding {
return field[:data.MaxKeywordFieldLengthBeforeBase64Encoding]
}

return field
}

//TruncateSliceElementsIfExceedsMaxLength will truncate the provided slice of the field if the max length is exceeded
func TruncateSliceElementsIfExceedsMaxLength(fields []string) []string {
var localFields []string
Expand Down
15 changes: 10 additions & 5 deletions process/elasticproc/converters/tokenMetaData.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ func PrepareTokenMetaData(tokenMetadata *outport.TokenMetaData) *data.TokenMetaD

var uris [][]byte
for _, uri := range tokenMetadata.URIs {
truncatedURI := TruncateFieldIfExceedsMaxLength(string(uri))
truncatedURI := TruncateFieldIfExceedsMaxLengthBase64(string(uri))
uris = append(uris, []byte(truncatedURI))
}

tags := ExtractTagsFromAttributes(tokenMetadata.Attributes)
attributes := TruncateFieldIfExceedsMaxLength(string(tokenMetadata.Attributes))
attributes := TruncateFieldIfExceedsMaxLengthBase64(string(tokenMetadata.Attributes))
return &data.TokenMetaData{
Name: TruncateFieldIfExceedsMaxLength(tokenMetadata.Name),
Creator: tokenMetadata.Creator,
Expand Down Expand Up @@ -90,8 +90,9 @@ func PrepareNFTUpdateData(buffSlice *data.BufferSlice, updateNFTData []*data.NFT
return buffSlice.PutData(metaData, prepareSerializedDataForPauseAndUnPause(nftUpdate))
}

base64Attr := base64.StdEncoding.EncodeToString(nftUpdate.NewAttributes)
newTags := ExtractTagsFromAttributes(nftUpdate.NewAttributes)
truncatedAttributes := TruncateFieldIfExceedsMaxLengthBase64(string(nftUpdate.NewAttributes))
base64Attr := base64.StdEncoding.EncodeToString([]byte(truncatedAttributes))
newTags := TruncateSliceElementsIfExceedsMaxLength(ExtractTagsFromAttributes(nftUpdate.NewAttributes))
newMetadata := ExtractMetaDataFromAttributes(nftUpdate.NewAttributes)

marshalizedTags, errM := json.Marshal(newTags)
Expand Down Expand Up @@ -122,7 +123,11 @@ func PrepareNFTUpdateData(buffSlice *data.BufferSlice, updateNFTData []*data.NFT
FormatPainlessSource(codeToExecute), base64Attr, newMetadata, marshalizedTags),
)
if len(nftUpdate.URIsToAdd) != 0 {
marshalizedURIS, err := json.Marshal(nftUpdate.URIsToAdd)
uris := make([]string, 0, len(nftUpdate.URIsToAdd))
for _, uri := range nftUpdate.URIsToAdd {
uris = append(uris, base64.StdEncoding.EncodeToString(uri))
}
marshalizedURIS, err := json.Marshal(TruncateSliceElementsIfExceedsMaxLength(uris))
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions process/elasticproc/transactions/transactionDBBuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ func (dtb *dbTransactionBuilder) prepareTransaction(
guardianAddress = dtb.addressPubkeyConverter.Encode(tx.GuardianAddr)
}

senderUserName := converters.TruncateFieldIfExceedsMaxLength(string(tx.SndUserName))
receiverUserName := converters.TruncateFieldIfExceedsMaxLength(string(tx.RcvUserName))
senderUserName := converters.TruncateFieldIfExceedsMaxLengthBase64(string(tx.SndUserName))
receiverUserName := converters.TruncateFieldIfExceedsMaxLengthBase64(string(tx.RcvUserName))
return &data.Transaction{
Hash: hex.EncodeToString(txHash),
MBHash: hex.EncodeToString(mbHash),
Expand Down

0 comments on commit 0d9bed3

Please sign in to comment.