2019-10-21 22:08:09 +00:00
|
|
|
// VulcanizeDB
|
|
|
|
// Copyright © 2019 Vulcanize
|
|
|
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package utils_test
|
2019-02-28 14:59:04 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
. "github.com/onsi/ginkgo"
|
|
|
|
. "github.com/onsi/gomega"
|
2019-08-13 23:09:40 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/libraries/shared/storage/utils"
|
2019-02-28 14:59:04 +00:00
|
|
|
)
|
|
|
|
|
2019-10-21 22:08:09 +00:00
|
|
|
var _ = Describe("Storage keys loader utils", func() {
|
|
|
|
Describe("GetStorageKeyForMapping", func() {
|
2019-02-28 14:59:04 +00:00
|
|
|
It("returns the storage key for a mapping when passed the mapping's index on the contract and the desired value's key", func() {
|
|
|
|
// ex. solidity:
|
|
|
|
// mapping (bytes32 => uint) public amounts
|
2019-03-06 16:54:08 +00:00
|
|
|
// to access amounts, pass in the index of the mapping on the contract + the bytes32 key for the uint val being looked up
|
2019-10-21 22:08:09 +00:00
|
|
|
indexOfMappingOnContract := utils.IndexZero
|
2019-03-04 18:10:34 +00:00
|
|
|
keyForDesiredValueInMapping := "1234567890abcdef"
|
2019-02-28 14:59:04 +00:00
|
|
|
|
2019-10-21 22:08:09 +00:00
|
|
|
storageKey := utils.GetStorageKeyForMapping(indexOfMappingOnContract, keyForDesiredValueInMapping)
|
2019-02-28 14:59:04 +00:00
|
|
|
|
2019-03-04 18:10:34 +00:00
|
|
|
expectedStorageKey := common.HexToHash("0xee0c1b59a3856bafbfb8730e7694c4badc271eb5f01ce4a8d7a53d8a6499676f")
|
2019-02-28 14:59:04 +00:00
|
|
|
Expect(storageKey).To(Equal(expectedStorageKey))
|
|
|
|
})
|
2019-06-13 23:33:22 +00:00
|
|
|
|
|
|
|
It("returns same result if value includes hex prefix", func() {
|
2019-10-21 22:08:09 +00:00
|
|
|
indexOfMappingOnContract := utils.IndexZero
|
2019-06-13 23:33:22 +00:00
|
|
|
keyForDesiredValueInMapping := "0x1234567890abcdef"
|
|
|
|
|
2019-10-21 22:08:09 +00:00
|
|
|
storageKey := utils.GetStorageKeyForMapping(indexOfMappingOnContract, keyForDesiredValueInMapping)
|
2019-06-13 23:33:22 +00:00
|
|
|
|
|
|
|
expectedStorageKey := common.HexToHash("0xee0c1b59a3856bafbfb8730e7694c4badc271eb5f01ce4a8d7a53d8a6499676f")
|
|
|
|
Expect(storageKey).To(Equal(expectedStorageKey))
|
|
|
|
})
|
2019-02-28 14:59:04 +00:00
|
|
|
})
|
|
|
|
|
2019-10-21 22:08:09 +00:00
|
|
|
Describe("GetStorageKeyForNestedMapping", func() {
|
2019-02-28 14:59:04 +00:00
|
|
|
It("returns the storage key for a nested mapping when passed the mapping's index on the contract and the desired value's keys", func() {
|
|
|
|
// ex. solidity:
|
2019-03-06 16:54:08 +00:00
|
|
|
// mapping (bytes32 => uint) public amounts
|
2019-02-28 14:59:04 +00:00
|
|
|
// mapping (address => mapping (uint => bytes32)) public addressNames
|
2019-03-06 16:54:08 +00:00
|
|
|
// to access addressNames, pass in the index of the mapping on the contract + the address and uint keys for the bytes32 val being looked up
|
2019-10-21 22:08:09 +00:00
|
|
|
indexOfMappingOnContract := utils.IndexOne
|
2019-03-04 18:10:34 +00:00
|
|
|
keyForOuterMapping := "1234567890abcdef"
|
2019-02-28 14:59:04 +00:00
|
|
|
keyForInnerMapping := "123"
|
|
|
|
|
2019-10-21 22:08:09 +00:00
|
|
|
storageKey := utils.GetStorageKeyForNestedMapping(indexOfMappingOnContract, keyForOuterMapping, keyForInnerMapping)
|
2019-02-28 14:59:04 +00:00
|
|
|
|
2019-03-04 18:10:34 +00:00
|
|
|
expectedStorageKey := common.HexToHash("0x82113529f6cd61061d1a6f0de53f2bdd067a1addd3d2b46be50a99abfcdb1661")
|
2019-02-28 14:59:04 +00:00
|
|
|
Expect(storageKey).To(Equal(expectedStorageKey))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2019-10-21 22:08:09 +00:00
|
|
|
Describe("GetIncrementedStorageKey", func() {
|
2019-02-28 14:59:04 +00:00
|
|
|
It("returns the storage key for later values sharing an index on the contract with other earlier values", func() {
|
|
|
|
// ex. solidity:
|
2019-03-06 16:54:08 +00:00
|
|
|
// mapping (bytes32 => uint) public amounts
|
|
|
|
// mapping (address => mapping (uint => bytes32)) public addressNames
|
2019-02-28 14:59:04 +00:00
|
|
|
// struct Data {
|
|
|
|
// uint256 quantity;
|
|
|
|
// uint256 quality;
|
|
|
|
// }
|
|
|
|
// mapping (bytes32 => Data) public itemData;
|
2019-03-06 16:54:08 +00:00
|
|
|
// to access quality from itemData, pass in the storage key for the zero-indexed value (quantity) + the number of increments required.
|
2019-02-28 14:59:04 +00:00
|
|
|
// (For "quality", we must increment the storage key for the corresponding "quantity" by 1).
|
2019-10-21 22:08:09 +00:00
|
|
|
indexOfMappingOnContract := utils.IndexTwo
|
2019-03-04 18:10:34 +00:00
|
|
|
keyForDesiredValueInMapping := "1234567890abcdef"
|
2019-10-21 22:08:09 +00:00
|
|
|
storageKeyForFirstPropertyOnStruct := utils.GetStorageKeyForMapping(indexOfMappingOnContract, keyForDesiredValueInMapping)
|
2019-02-28 14:59:04 +00:00
|
|
|
|
2019-10-21 22:08:09 +00:00
|
|
|
storageKey := utils.GetIncrementedStorageKey(storageKeyForFirstPropertyOnStruct, 1)
|
2019-02-28 14:59:04 +00:00
|
|
|
|
2019-03-04 18:10:34 +00:00
|
|
|
expectedStorageKey := common.HexToHash("0x69b38749f0a8ed5d505c8474f7fb62c7828aad8a7627f1c67e07af1d2368cad4")
|
2019-02-28 14:59:04 +00:00
|
|
|
Expect(storageKey).To(Equal(expectedStorageKey))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|