Apply go fmt changes and cleanup

This commit is contained in:
Elizabeth Engelman 2019-07-19 09:56:21 -05:00
parent d30b4ea4f2
commit 69ad521482
6 changed files with 32 additions and 29 deletions

View File

@ -47,11 +47,9 @@ func (transformer Transformer) Execute(row utils.StorageDiffRow) error {
if lookupErr != nil { if lookupErr != nil {
return lookupErr return lookupErr
} }
//packed storage slots return a slice of decoded values
value, decodeErr := utils.Decode(row, metadata) value, decodeErr := utils.Decode(row, metadata)
if decodeErr != nil { if decodeErr != nil {
return decodeErr return decodeErr
} }
return transformer.Repository.Create(row.BlockHeight, row.BlockHash.Hex(), metadata, value) return transformer.Repository.Create(row.BlockHeight, row.BlockHash.Hex(), metadata, value)
} }

View File

@ -103,10 +103,10 @@ var _ = Describe("Storage transformer", func() {
Describe("when a storage row contains more than one item packed in storage", func() { Describe("when a storage row contains more than one item packed in storage", func() {
var ( var (
rawValue = common.HexToAddress("000000000000000000000000000000000000000000000002a300000000002a30") rawValue = common.HexToAddress("000000000000000000000000000000000000000000000002a300000000002a30")
fakeBlockNumber = 123 fakeBlockNumber = 123
fakeBlockHash = "0x67890" fakeBlockHash = "0x67890"
packedTypes = make(map[int]utils.ValueType) packedTypes = make(map[int]utils.ValueType)
) )
packedTypes[0] = utils.Uint48 packedTypes[0] = utils.Uint48
packedTypes[1] = 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.PassedBlockHash).To(Equal(common.HexToHash(fakeBlockHash).Hex()))
Expect(repository.PassedMetadata).To(Equal(fakeMetadata)) Expect(repository.PassedMetadata).To(Equal(fakeMetadata))
expectedPassedValue := make(map[int]string) expectedPassedValue := make(map[int]string)
expectedPassedValue[0]= "10800" expectedPassedValue[0] = "10800"
expectedPassedValue[1]= "172800" expectedPassedValue[1] = "172800"
Expect(repository.PassedValue.(map[int]string)).To(Equal(expectedPassedValue)) Expect(repository.PassedValue.(map[int]string)).To(Equal(expectedPassedValue))
}) })

View File

@ -33,7 +33,6 @@ func (repository *MockStorageRepository) Create(blockNumber int, blockHash strin
repository.PassedBlockNumber = blockNumber repository.PassedBlockNumber = blockNumber
repository.PassedBlockHash = blockHash repository.PassedBlockHash = blockHash
repository.PassedMetadata = metadata repository.PassedMetadata = metadata
repository.PassedValue = value repository.PassedValue = value
return repository.CreateErr return repository.CreateErr
} }

View File

@ -18,8 +18,9 @@ package utils
import ( import (
"fmt" "fmt"
"github.com/ethereum/go-ethereum/common"
"math/big" "math/big"
"github.com/ethereum/go-ethereum/common"
) )
func Decode(row StorageDiffRow, metadata StorageValueMetadata) (interface{}, error) { func Decode(row StorageDiffRow, metadata StorageValueMetadata) (interface{}, error) {
@ -60,26 +61,30 @@ func decodeAddress(raw []byte) string {
return common.BytesToAddress(raw).Hex() return common.BytesToAddress(raw).Hex()
} }
func decodePackedSlot(raw []byte, packedTypes map[int]ValueType) map[int]string{ func decodePackedSlot(raw []byte, packedTypes map[int]ValueType) map[int]string {
storageSlot := raw storageSlotData := raw
var results = map[int]string{} decodedStorageSlotItems := 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
numberOfTypes := len(packedTypes) 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 for position := 0; position < numberOfTypes; position++ {
storageSlot = storageSlot[0:itemStartingIndex] //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 { 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 // 8 bits per byte
switch valueType { switch valueType {
case Uint48: case Uint48:

View File

@ -17,10 +17,11 @@
package utils_test package utils_test
import ( import (
"math/big"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
. "github.com/onsi/ginkgo" . "github.com/onsi/ginkgo"
. "github.com/onsi/gomega" . "github.com/onsi/gomega"
"math/big"
"github.com/vulcanize/vulcanizedb/libraries/shared/storage/utils" "github.com/vulcanize/vulcanizedb/libraries/shared/storage/utils"
) )

View File

@ -33,8 +33,8 @@ type StorageValueMetadata struct {
Name string Name string
Keys map[Key]string Keys map[Key]string
Type ValueType Type ValueType
PackedNames map[int]string //name of each item packed and their order PackedNames map[int]string //position in map (zero indexed) => name of packed item
PackedTypes map[int]ValueType //type of each item packed and their order PackedTypes map[int]ValueType //position in map (zero indexed)=> type of packed item
} }
func GetStorageValueMetadata(name string, keys map[Key]string, valueType ValueType) StorageValueMetadata { func GetStorageValueMetadata(name string, keys map[Key]string, valueType ValueType) StorageValueMetadata {