From a98380e3d20a63280359fcdc587bbf97834816d2 Mon Sep 17 00:00:00 2001 From: robertsasu Date: Wed, 3 Jul 2024 16:13:16 +0300 Subject: [PATCH 1/2] fix and add test --- vmhost/contexts/storage.go | 2 +- vmhost/contexts/storage_test.go | 49 +++++++++++++++++++++++---------- 2 files changed, 36 insertions(+), 15 deletions(-) diff --git a/vmhost/contexts/storage.go b/vmhost/contexts/storage.go index 227fe87f9..d4dd22ddc 100644 --- a/vmhost/contexts/storage.go +++ b/vmhost/contexts/storage.go @@ -162,7 +162,7 @@ func (context *storageContext) GetStorageFromAddress(address []byte, key []byte) !core.IsSmartContractAddress(address) metadata := vmcommon.CodeMetadataFromBytes(userAcc.GetCodeMetadata()) - if !metadata.Readable || isReadFromUserAddress { + if !metadata.Readable && !isReadFromUserAddress { context.useExtraGasForKeyIfNeeded(key, false) return nil, 0, false, nil } diff --git a/vmhost/contexts/storage_test.go b/vmhost/contexts/storage_test.go index 46da228f5..ed8ff1f1f 100644 --- a/vmhost/contexts/storage_test.go +++ b/vmhost/contexts/storage_test.go @@ -3,6 +3,7 @@ package contexts import ( "bytes" "errors" + "github.com/multiversx/mx-chain-core-go/core" "math/big" "testing" @@ -444,11 +445,20 @@ func TestStorageContext_StorageProtection(t *testing.T) { func TestStorageContext_GetStorageFromAddress(t *testing.T) { t.Parallel() - scAddress := []byte("account") - readable := []byte("readable") - nonreadable := []byte("nonreadable") + scAddress := bytes.Repeat([]byte{0}, 32) + readable := bytes.Repeat([]byte{0}, 32) + readable[31] = 1 + nonreadable := bytes.Repeat([]byte{0}, 32) + nonreadable[31] = 2 - enableEpochsHandler := &worldmock.EnableEpochsHandlerStub{} + userAddress := bytes.Repeat([]byte{1}, 32) + + isEnabled := false + enableEpochsHandler := &worldmock.EnableEpochsHandlerStub{ + IsFlagEnabledCalled: func(_ core.EnableEpochFlag) bool { + return isEnabled + }, + } mockOutput := &contextmock.OutputContextMock{} account := mockOutput.NewVMOutputAccount(scAddress) @@ -470,9 +480,7 @@ func TestStorageContext_GetStorageFromAddress(t *testing.T) { t.Run("blockchain hook errors", func(t *testing.T) { errTooManyRequests := errors.New("too many requests") bcHook := makeBcHookStub( - scAddress, readable, - nonreadable, nil, errTooManyRequests) @@ -491,14 +499,22 @@ func TestStorageContext_GetStorageFromAddress(t *testing.T) { data, _, _, _ = storageCtx.GetStorageFromAddress(nonreadable, key) require.Nil(t, data) require.Equal(t, errTooManyRequests, err) + + isEnabled = false + data, _, _, _ = storageCtx.GetStorageFromAddress(userAddress, key) + require.Nil(t, data) + require.Equal(t, errTooManyRequests, err) + + isEnabled = true + data, _, _, _ = storageCtx.GetStorageFromAddress(userAddress, key) + require.Nil(t, data) + require.Equal(t, errTooManyRequests, err) }) t.Run("should work when blockchain hook does not error", func(t *testing.T) { internalData := []byte("internalData") bcHook := makeBcHookStub( - scAddress, readable, - nonreadable, internalData, nil) @@ -517,13 +533,21 @@ func TestStorageContext_GetStorageFromAddress(t *testing.T) { data, _, _, err = storageCtx.GetStorageFromAddress(nonreadable, key) require.Nil(t, err) require.Nil(t, data) + + isEnabled = false + data, _, _, err = storageCtx.GetStorageFromAddress(userAddress, key) + require.Nil(t, err) + require.Nil(t, data) + + isEnabled = true + data, _, _, err = storageCtx.GetStorageFromAddress(userAddress, key) + require.Nil(t, err) + require.Equal(t, data, internalData) }) } func makeBcHookStub( - scAddress []byte, readable []byte, - nonreadable []byte, internalData []byte, getStorageErr error, ) *contextmock.BlockchainHookStub { @@ -532,10 +556,7 @@ func makeBcHookStub( if bytes.Equal(readable, address) { return &worldmock.Account{CodeMetadata: []byte{4, 0}}, nil } - if bytes.Equal(nonreadable, address) || bytes.Equal(scAddress, address) { - return &worldmock.Account{CodeMetadata: []byte{0, 0}}, nil - } - return nil, nil + return &worldmock.Account{CodeMetadata: []byte{0, 0}}, nil }, GetStorageDataCalled: func(accountsAddress []byte, index []byte) ([]byte, uint32, error) { return internalData, 0, getStorageErr From ec175dad3ac8f689a09f835719b98bb2e88e7379 Mon Sep 17 00:00:00 2001 From: BeniaminDrasovean Date: Mon, 15 Jul 2024 14:11:21 +0300 Subject: [PATCH 2/2] update vm-common version --- go.mod | 2 +- go.sum | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index b2ef7b2a4..26b7da874 100644 --- a/go.mod +++ b/go.mod @@ -13,7 +13,7 @@ require ( github.com/multiversx/mx-chain-logger-go v1.0.15-0.20240508072523-3f00a726af57 github.com/multiversx/mx-chain-scenario-go v1.4.4-0.20240509103754-9e8129721f00 github.com/multiversx/mx-chain-storage-go v1.0.16-0.20240508073549-dcb8e6e0370f - github.com/multiversx/mx-chain-vm-common-go v1.5.13-0.20240509103544-247ce5639c7a + github.com/multiversx/mx-chain-vm-common-go v1.5.13-0.20240715100647-8ce0ec25ff1d github.com/multiversx/mx-components-big-int v1.0.0 github.com/pelletier/go-toml v1.9.3 github.com/stretchr/testify v1.8.1 diff --git a/go.sum b/go.sum index f6d4b1b37..01466f582 100644 --- a/go.sum +++ b/go.sum @@ -93,8 +93,8 @@ github.com/multiversx/mx-chain-scenario-go v1.4.4-0.20240509103754-9e8129721f00 github.com/multiversx/mx-chain-scenario-go v1.4.4-0.20240509103754-9e8129721f00/go.mod h1:pnIIfWopbDMQ1EW5Ddc6KDMqv8Qtx+hxbH9rorHpCyo= github.com/multiversx/mx-chain-storage-go v1.0.16-0.20240508073549-dcb8e6e0370f h1:yd/G8iPBGOEAwbaS8zndJpO6bQk7Tk72ZhmlqRasThI= github.com/multiversx/mx-chain-storage-go v1.0.16-0.20240508073549-dcb8e6e0370f/go.mod h1:E6nfj9EQzGxWDGM3Dn6eZWRC3qFy1G8IqOsYsBOcgWw= -github.com/multiversx/mx-chain-vm-common-go v1.5.13-0.20240509103544-247ce5639c7a h1:7M+jXVlnl43zd2NuimL1KnAVAdpUr/QoHqG0TUKoyaM= -github.com/multiversx/mx-chain-vm-common-go v1.5.13-0.20240509103544-247ce5639c7a/go.mod h1:RgGmPei0suQcFTHfO4cS5dxJSiokp2SM5lmNgp1icMo= +github.com/multiversx/mx-chain-vm-common-go v1.5.13-0.20240715100647-8ce0ec25ff1d h1:GqwJaWDgWFuHx4AsUBMwpHWzY4afyTbWBk0nwYG6lsY= +github.com/multiversx/mx-chain-vm-common-go v1.5.13-0.20240715100647-8ce0ec25ff1d/go.mod h1:RgGmPei0suQcFTHfO4cS5dxJSiokp2SM5lmNgp1icMo= github.com/multiversx/mx-components-big-int v1.0.0 h1:Wkr8lSzK2nDqixOrrBa47VNuqdhV1m/aJhaP1EMaiS8= github.com/multiversx/mx-components-big-int v1.0.0/go.mod h1:maIEMgHlNE2u78JaDD0oLzri+ShgU4okHfzP3LWGdQM= github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A=