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 {
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)
}

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() {
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))
})

View File

@ -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
}

View File

@ -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:

View File

@ -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"
)

View File

@ -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 {