Apply go fmt changes and cleanup

This commit is contained in:
Elizabeth Engelman
2019-07-19 10:44:36 -05:00
parent d30b4ea4f2
commit 69ad521482
6 changed files with 32 additions and 29 deletions
+23 -18
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:
@@ -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"
)
+2 -2
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 {