2019-01-28 22:52:47 +00:00
|
|
|
// VulcanizeDB
|
2019-03-12 15:46:42 +00:00
|
|
|
// Copyright © 2019 Vulcanize
|
2019-01-28 22:52:47 +00:00
|
|
|
|
|
|
|
// 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/>.
|
|
|
|
|
2019-01-31 23:14:42 +00:00
|
|
|
package utils_test
|
2019-01-28 22:52:47 +00:00
|
|
|
|
|
|
|
import (
|
2019-07-19 14:56:21 +00:00
|
|
|
"math/big"
|
|
|
|
|
2019-01-28 22:52:47 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
. "github.com/onsi/ginkgo"
|
|
|
|
. "github.com/onsi/gomega"
|
2019-01-31 23:14:42 +00:00
|
|
|
|
2019-02-10 22:42:41 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/libraries/shared/storage/utils"
|
2019-01-28 22:52:47 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var _ = Describe("Storage decoder", func() {
|
|
|
|
It("decodes uint256", func() {
|
|
|
|
fakeInt := common.HexToHash("0000000000000000000000000000000000000000000000000000000000000539")
|
2019-07-29 19:28:20 +00:00
|
|
|
diff := utils.StorageDiff{StorageValue: fakeInt}
|
2019-01-31 23:14:42 +00:00
|
|
|
metadata := utils.StorageValueMetadata{Type: utils.Uint256}
|
2019-01-28 22:52:47 +00:00
|
|
|
|
2019-07-29 19:28:20 +00:00
|
|
|
result, err := utils.Decode(diff, metadata)
|
2019-01-28 22:52:47 +00:00
|
|
|
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
Expect(result).To(Equal(big.NewInt(0).SetBytes(fakeInt.Bytes()).String()))
|
|
|
|
})
|
|
|
|
|
2019-07-18 21:18:20 +00:00
|
|
|
It("decodes uint128", func() {
|
|
|
|
fakeInt := common.HexToHash("0000000000000000000000000000000000000000000000000000000000011123")
|
2019-07-29 19:28:20 +00:00
|
|
|
diff := utils.StorageDiff{StorageValue: fakeInt}
|
2019-07-18 21:18:20 +00:00
|
|
|
metadata := utils.StorageValueMetadata{Type: utils.Uint128}
|
|
|
|
|
2019-07-29 19:28:20 +00:00
|
|
|
result, err := utils.Decode(diff, metadata)
|
2019-07-18 21:18:20 +00:00
|
|
|
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
Expect(result).To(Equal(big.NewInt(0).SetBytes(fakeInt.Bytes()).String()))
|
|
|
|
})
|
|
|
|
|
2019-03-08 17:12:02 +00:00
|
|
|
It("decodes uint48", func() {
|
|
|
|
fakeInt := common.HexToHash("0000000000000000000000000000000000000000000000000000000000000123")
|
2019-07-29 19:28:20 +00:00
|
|
|
diff := utils.StorageDiff{StorageValue: fakeInt}
|
2019-03-08 17:12:02 +00:00
|
|
|
metadata := utils.StorageValueMetadata{Type: utils.Uint48}
|
|
|
|
|
2019-07-29 19:28:20 +00:00
|
|
|
result, err := utils.Decode(diff, metadata)
|
2019-03-08 17:12:02 +00:00
|
|
|
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
Expect(result).To(Equal(big.NewInt(0).SetBytes(fakeInt.Bytes()).String()))
|
|
|
|
})
|
|
|
|
|
2019-07-18 21:18:20 +00:00
|
|
|
It("decodes address", func() {
|
|
|
|
fakeAddress := common.HexToAddress("0x12345")
|
2019-07-29 19:28:20 +00:00
|
|
|
diff := utils.StorageDiff{StorageValue: fakeAddress.Hash()}
|
2019-07-18 21:18:20 +00:00
|
|
|
metadata := utils.StorageValueMetadata{Type: utils.Address}
|
|
|
|
|
2019-07-29 19:28:20 +00:00
|
|
|
result, err := utils.Decode(diff, metadata)
|
2019-07-18 21:18:20 +00:00
|
|
|
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
Expect(result).To(Equal(fakeAddress.Hex()))
|
|
|
|
})
|
|
|
|
|
2019-07-18 18:30:04 +00:00
|
|
|
Describe("when there are multiple items packed in the storage slot", func() {
|
2019-07-18 21:18:20 +00:00
|
|
|
It("decodes uint48 items", func() {
|
|
|
|
//this is a real storage data example
|
2019-07-18 18:30:04 +00:00
|
|
|
packedStorage := common.HexToHash("000000000000000000000000000000000000000000000002a300000000002a30")
|
2019-07-29 19:28:20 +00:00
|
|
|
diff := utils.StorageDiff{StorageValue: packedStorage}
|
2019-07-18 18:30:04 +00:00
|
|
|
packedTypes := map[int]utils.ValueType{}
|
|
|
|
packedTypes[0] = utils.Uint48
|
|
|
|
packedTypes[1] = utils.Uint48
|
|
|
|
|
|
|
|
metadata := utils.StorageValueMetadata{
|
|
|
|
Type: utils.PackedSlot,
|
|
|
|
PackedTypes: packedTypes,
|
|
|
|
}
|
|
|
|
|
2019-07-29 19:28:20 +00:00
|
|
|
result, err := utils.Decode(diff, metadata)
|
2019-07-18 22:09:07 +00:00
|
|
|
decodedValues := result.(map[int]string)
|
2019-07-18 18:30:04 +00:00
|
|
|
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
2019-07-18 22:09:07 +00:00
|
|
|
Expect(decodedValues[0]).To(Equal(big.NewInt(0).SetBytes(common.HexToHash("2a30").Bytes()).String()))
|
|
|
|
Expect(decodedValues[1]).To(Equal(big.NewInt(0).SetBytes(common.HexToHash("2a300").Bytes()).String()))
|
2019-07-18 18:30:04 +00:00
|
|
|
})
|
|
|
|
|
2019-07-18 21:18:20 +00:00
|
|
|
It("decodes 5 uint48 items", func() {
|
|
|
|
//TODO: this packedStorageHex was generated by hand, it would be nice to test this against
|
|
|
|
//real storage data that has several items packed into it
|
|
|
|
packedStorageHex := "0000000A5D1AFFFFFFFFFFFE00000009F3C600000002A300000000002A30"
|
2019-07-18 18:30:04 +00:00
|
|
|
|
2019-07-18 21:18:20 +00:00
|
|
|
packedStorage := common.HexToHash(packedStorageHex)
|
2019-07-29 19:28:20 +00:00
|
|
|
diff := utils.StorageDiff{StorageValue: packedStorage}
|
2019-07-18 21:18:20 +00:00
|
|
|
packedTypes := map[int]utils.ValueType{}
|
|
|
|
packedTypes[0] = utils.Uint48
|
|
|
|
packedTypes[1] = utils.Uint48
|
|
|
|
packedTypes[2] = utils.Uint48
|
|
|
|
packedTypes[3] = utils.Uint48
|
|
|
|
packedTypes[4] = utils.Uint48
|
2019-01-28 22:52:47 +00:00
|
|
|
|
2019-07-18 21:18:20 +00:00
|
|
|
metadata := utils.StorageValueMetadata{
|
|
|
|
Type: utils.PackedSlot,
|
|
|
|
PackedTypes: packedTypes,
|
|
|
|
}
|
2019-01-28 22:52:47 +00:00
|
|
|
|
2019-07-29 19:28:20 +00:00
|
|
|
result, err := utils.Decode(diff, metadata)
|
2019-07-18 22:09:07 +00:00
|
|
|
decodedValues := result.(map[int]string)
|
2019-07-18 21:18:20 +00:00
|
|
|
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
2019-07-18 22:09:07 +00:00
|
|
|
Expect(decodedValues[0]).To(Equal(big.NewInt(0).SetBytes(common.HexToHash("2a30").Bytes()).String()))
|
|
|
|
Expect(decodedValues[1]).To(Equal(big.NewInt(0).SetBytes(common.HexToHash("2a300").Bytes()).String()))
|
|
|
|
Expect(decodedValues[2]).To(Equal(big.NewInt(0).SetBytes(common.HexToHash("9F3C6").Bytes()).String()))
|
|
|
|
Expect(decodedValues[3]).To(Equal(big.NewInt(0).SetBytes(common.HexToHash("FFFFFFFFFFFE").Bytes()).String()))
|
|
|
|
Expect(decodedValues[4]).To(Equal(big.NewInt(0).SetBytes(common.HexToHash("A5D1A").Bytes()).String()))
|
2019-07-18 21:18:20 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
It("decodes 2 uint128 items", func() {
|
|
|
|
//TODO: this packedStorageHex was generated by hand, it would be nice to test this against
|
|
|
|
//real storage data that has several items packed into it
|
|
|
|
packedStorageHex := "000000038D7EA4C67FF8E502B6730000" +
|
|
|
|
"0000000000000000AB54A98CEB1F0AD2"
|
|
|
|
packedStorage := common.HexToHash(packedStorageHex)
|
2019-07-29 19:28:20 +00:00
|
|
|
diff := utils.StorageDiff{StorageValue: packedStorage}
|
2019-07-18 21:18:20 +00:00
|
|
|
packedTypes := map[int]utils.ValueType{}
|
|
|
|
packedTypes[0] = utils.Uint128
|
|
|
|
packedTypes[1] = utils.Uint128
|
|
|
|
|
|
|
|
metadata := utils.StorageValueMetadata{
|
|
|
|
Type: utils.PackedSlot,
|
|
|
|
PackedTypes: packedTypes,
|
|
|
|
}
|
|
|
|
|
2019-07-29 19:28:20 +00:00
|
|
|
result, err := utils.Decode(diff, metadata)
|
2019-07-18 22:09:07 +00:00
|
|
|
decodedValues := result.(map[int]string)
|
2019-07-18 21:18:20 +00:00
|
|
|
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
2019-07-18 22:09:07 +00:00
|
|
|
Expect(decodedValues[0]).To(Equal(big.NewInt(0).SetBytes(common.HexToHash("AB54A98CEB1F0AD2").Bytes()).String()))
|
|
|
|
Expect(decodedValues[1]).To(Equal(big.NewInt(0).SetBytes(common.HexToHash("38D7EA4C67FF8E502B6730000").Bytes()).String()))
|
2019-07-18 21:18:20 +00:00
|
|
|
})
|
2019-08-02 03:39:43 +00:00
|
|
|
|
|
|
|
It("decodes address + 2 uint48s", func() {
|
|
|
|
//TODO: replace with real data when available
|
|
|
|
addressHex := "0000000000000000000000000000000000012345"
|
|
|
|
packedStorage := common.HexToHash("00000002a300" + "000000002a30" + addressHex)
|
2019-07-29 19:28:20 +00:00
|
|
|
row := utils.StorageDiff{StorageValue: packedStorage}
|
2019-08-02 03:39:43 +00:00
|
|
|
packedTypes := map[int]utils.ValueType{}
|
|
|
|
packedTypes[0] = utils.Address
|
|
|
|
packedTypes[1] = utils.Uint48
|
|
|
|
packedTypes[2] = utils.Uint48
|
|
|
|
|
|
|
|
metadata := utils.StorageValueMetadata{
|
|
|
|
Type: utils.PackedSlot,
|
|
|
|
PackedTypes: packedTypes,
|
|
|
|
}
|
|
|
|
|
|
|
|
result, err := utils.Decode(row, metadata)
|
|
|
|
decodedValues := result.(map[int]string)
|
|
|
|
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
Expect(decodedValues[0]).To(Equal("0x" + addressHex))
|
|
|
|
Expect(decodedValues[1]).To(Equal(big.NewInt(0).SetBytes(common.HexToHash("2a30").Bytes()).String()))
|
|
|
|
Expect(decodedValues[2]).To(Equal(big.NewInt(0).SetBytes(common.HexToHash("2a300").Bytes()).String()))
|
|
|
|
})
|
2019-01-28 22:52:47 +00:00
|
|
|
})
|
|
|
|
})
|