From 69ad521482f3659d43aebe72e1ebb2649bddca76 Mon Sep 17 00:00:00 2001 From: Elizabeth Engelman Date: Fri, 19 Jul 2019 09:56:21 -0500 Subject: [PATCH] Apply go fmt changes and cleanup --- .../shared/factories/storage/transformer.go | 2 - .../factories/storage/transformer_test.go | 10 ++--- libraries/shared/mocks/storage_repository.go | 1 - libraries/shared/storage/utils/decoder.go | 41 +++++++++++-------- .../shared/storage/utils/decoder_test.go | 3 +- libraries/shared/storage/utils/value.go | 4 +- 6 files changed, 32 insertions(+), 29 deletions(-) diff --git a/libraries/shared/factories/storage/transformer.go b/libraries/shared/factories/storage/transformer.go index 58282e85..fce04465 100644 --- a/libraries/shared/factories/storage/transformer.go +++ b/libraries/shared/factories/storage/transformer.go @@ -47,11 +47,9 @@ func (transformer Transformer) Execute(row utils.StorageDiffRow) error { if lookupErr != nil { return lookupErr } - //packed storage slots return a slice of decoded values value, decodeErr := utils.Decode(row, metadata) if decodeErr != nil { return decodeErr } - return transformer.Repository.Create(row.BlockHeight, row.BlockHash.Hex(), metadata, value) } diff --git a/libraries/shared/factories/storage/transformer_test.go b/libraries/shared/factories/storage/transformer_test.go index 09b91d80..59982a20 100644 --- a/libraries/shared/factories/storage/transformer_test.go +++ b/libraries/shared/factories/storage/transformer_test.go @@ -103,10 +103,10 @@ var _ = Describe("Storage transformer", func() { Describe("when a storage row contains more than one item packed in storage", func() { var ( - rawValue = common.HexToAddress("000000000000000000000000000000000000000000000002a300000000002a30") + rawValue = common.HexToAddress("000000000000000000000000000000000000000000000002a300000000002a30") fakeBlockNumber = 123 - fakeBlockHash = "0x67890" - packedTypes = make(map[int]utils.ValueType) + fakeBlockHash = "0x67890" + packedTypes = make(map[int]utils.ValueType) ) packedTypes[0] = utils.Uint48 packedTypes[1] = utils.Uint48 @@ -135,8 +135,8 @@ var _ = Describe("Storage transformer", func() { Expect(repository.PassedBlockHash).To(Equal(common.HexToHash(fakeBlockHash).Hex())) Expect(repository.PassedMetadata).To(Equal(fakeMetadata)) expectedPassedValue := make(map[int]string) - expectedPassedValue[0]= "10800" - expectedPassedValue[1]= "172800" + expectedPassedValue[0] = "10800" + expectedPassedValue[1] = "172800" Expect(repository.PassedValue.(map[int]string)).To(Equal(expectedPassedValue)) }) diff --git a/libraries/shared/mocks/storage_repository.go b/libraries/shared/mocks/storage_repository.go index 9a22a307..c4e351be 100644 --- a/libraries/shared/mocks/storage_repository.go +++ b/libraries/shared/mocks/storage_repository.go @@ -33,7 +33,6 @@ func (repository *MockStorageRepository) Create(blockNumber int, blockHash strin repository.PassedBlockNumber = blockNumber repository.PassedBlockHash = blockHash repository.PassedMetadata = metadata - repository.PassedValue = value return repository.CreateErr } diff --git a/libraries/shared/storage/utils/decoder.go b/libraries/shared/storage/utils/decoder.go index c672cd31..31e8caf1 100644 --- a/libraries/shared/storage/utils/decoder.go +++ b/libraries/shared/storage/utils/decoder.go @@ -18,8 +18,9 @@ package utils import ( "fmt" - "github.com/ethereum/go-ethereum/common" "math/big" + + "github.com/ethereum/go-ethereum/common" ) func Decode(row StorageDiffRow, metadata StorageValueMetadata) (interface{}, error) { @@ -60,26 +61,30 @@ func decodeAddress(raw []byte) string { return common.BytesToAddress(raw).Hex() } -func decodePackedSlot(raw []byte, packedTypes map[int]ValueType) map[int]string{ - storageSlot := raw - var results = map[int]string{} - - //the reason we're using a map and not a slice is that golang doesn't guarantee the order of a slice +func decodePackedSlot(raw []byte, packedTypes map[int]ValueType) map[int]string { + storageSlotData := raw + decodedStorageSlotItems := map[int]string{} numberOfTypes := len(packedTypes) - for position := 0; position < numberOfTypes; position++ { - valueType := packedTypes[position] - lengthOfStorageSlot := len(storageSlot) - lengthOfItem := getNumberOfBytes(valueType) - itemStartingIndex := lengthOfStorageSlot - lengthOfItem - value := storageSlot[itemStartingIndex:] - decodedValue := decodeIndividualItems(value, valueType) - results[position] = decodedValue - //pop last item off slot before moving on - storageSlot = storageSlot[0:itemStartingIndex] + for position := 0; position < numberOfTypes; position++ { + //get length of remaining storage date + lengthOfStorageData := len(storageSlotData) + + //get item details (type, length, starting index, value bytes) + itemType := packedTypes[position] + lengthOfItem := getNumberOfBytes(itemType) + itemStartingIndex := lengthOfStorageData - lengthOfItem + itemValueBytes := storageSlotData[itemStartingIndex:] + + //decode item's bytes and set in results map + decodedValue := decodeIndividualItems(itemValueBytes, itemType) + decodedStorageSlotItems[position] = decodedValue + + //pop last item off raw slot data before moving on + storageSlotData = storageSlotData[0:itemStartingIndex] } - return results + return decodedStorageSlotItems } func decodeIndividualItems(itemBytes []byte, valueType ValueType) string { @@ -93,7 +98,7 @@ func decodeIndividualItems(itemBytes []byte, valueType ValueType) string { } } -func getNumberOfBytes(valueType ValueType) int{ +func getNumberOfBytes(valueType ValueType) int { // 8 bits per byte switch valueType { case Uint48: diff --git a/libraries/shared/storage/utils/decoder_test.go b/libraries/shared/storage/utils/decoder_test.go index 67df8d52..868828d6 100644 --- a/libraries/shared/storage/utils/decoder_test.go +++ b/libraries/shared/storage/utils/decoder_test.go @@ -17,10 +17,11 @@ package utils_test import ( + "math/big" + "github.com/ethereum/go-ethereum/common" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" - "math/big" "github.com/vulcanize/vulcanizedb/libraries/shared/storage/utils" ) diff --git a/libraries/shared/storage/utils/value.go b/libraries/shared/storage/utils/value.go index ba463ab1..36056255 100644 --- a/libraries/shared/storage/utils/value.go +++ b/libraries/shared/storage/utils/value.go @@ -33,8 +33,8 @@ type StorageValueMetadata struct { Name string Keys map[Key]string Type ValueType - PackedNames map[int]string //name of each item packed and their order - PackedTypes map[int]ValueType //type of each item packed and their order + PackedNames map[int]string //position in map (zero indexed) => name of packed item + PackedTypes map[int]ValueType //position in map (zero indexed)=> type of packed item } func GetStorageValueMetadata(name string, keys map[Key]string, valueType ValueType) StorageValueMetadata {