forked from cerc-io/ipld-eth-server
Pass storage values to repo.create as a map
This commit is contained in:
@@ -18,9 +18,8 @@ package utils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math/big"
|
||||
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
"math/big"
|
||||
)
|
||||
|
||||
func Decode(row StorageDiffRow, metadata StorageValueMetadata) (interface{}, error) {
|
||||
@@ -61,17 +60,20 @@ func decodeAddress(raw []byte) string {
|
||||
return common.BytesToAddress(raw).Hex()
|
||||
}
|
||||
|
||||
func decodePackedSlot(raw []byte, packedTypes map[int]ValueType) []string{
|
||||
func decodePackedSlot(raw []byte, packedTypes map[int]ValueType) map[int]string{
|
||||
storageSlot := raw
|
||||
var results []string
|
||||
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
|
||||
for _, valueType := range 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 = append(results, decodedValue)
|
||||
results[position] = decodedValue
|
||||
|
||||
//pop last item off slot before moving on
|
||||
storageSlot = storageSlot[0:itemStartingIndex]
|
||||
|
||||
@@ -85,12 +85,11 @@ var _ = Describe("Storage decoder", func() {
|
||||
}
|
||||
|
||||
result, err := utils.Decode(row, metadata)
|
||||
decodedValues := result.([]string)
|
||||
decodedValues := result.(map[int]string)
|
||||
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
expectedResult1 := big.NewInt(0).SetBytes(common.HexToHash("2a30").Bytes()).String()
|
||||
expectedResult2 := big.NewInt(0).SetBytes(common.HexToHash("2a300").Bytes()).String()
|
||||
Expect(decodedValues).To(ConsistOf(expectedResult1, expectedResult2))
|
||||
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()))
|
||||
})
|
||||
|
||||
It("decodes 5 uint48 items", func() {
|
||||
@@ -113,15 +112,14 @@ var _ = Describe("Storage decoder", func() {
|
||||
}
|
||||
|
||||
result, err := utils.Decode(row, metadata)
|
||||
decodedValues := result.([]string)
|
||||
decodedValues := result.(map[int]string)
|
||||
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
expectedResult1 := big.NewInt(0).SetBytes(common.HexToHash("A5D1A").Bytes()).String()
|
||||
expectedResult2 := big.NewInt(0).SetBytes(common.HexToHash("FFFFFFFFFFFE").Bytes()).String()
|
||||
expectedResult3 := big.NewInt(0).SetBytes(common.HexToHash("9F3C6").Bytes()).String()
|
||||
expectedResult4 := big.NewInt(0).SetBytes(common.HexToHash("2a300").Bytes()).String()
|
||||
expectedResult5 := big.NewInt(0).SetBytes(common.HexToHash("2a30").Bytes()).String()
|
||||
Expect(decodedValues).To(ConsistOf(expectedResult1, expectedResult2, expectedResult3, expectedResult4, expectedResult5))
|
||||
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()))
|
||||
})
|
||||
|
||||
It("decodes 2 uint128 items", func() {
|
||||
@@ -141,12 +139,11 @@ var _ = Describe("Storage decoder", func() {
|
||||
}
|
||||
|
||||
result, err := utils.Decode(row, metadata)
|
||||
decodedValues := result.([]string)
|
||||
decodedValues := result.(map[int]string)
|
||||
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
expectedResult1 := big.NewInt(0).SetBytes(common.HexToHash("000000038D7EA4C67FF8E502B6730000").Bytes()).String()
|
||||
expectedResult2 := big.NewInt(0).SetBytes(common.HexToHash("AB54A98CEB1F0AD2").Bytes()).String()
|
||||
Expect(decodedValues).To(ConsistOf(expectedResult1, expectedResult2))
|
||||
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()))
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -34,6 +34,7 @@ type StorageValueMetadata struct {
|
||||
Keys map[Key]string
|
||||
Type ValueType
|
||||
PackedTypes map[int]ValueType //type of each item packed and their order
|
||||
PackedNames map[int]string //name of each item packed and their order
|
||||
}
|
||||
|
||||
func GetStorageValueMetadata(name string, keys map[Key]string, t ValueType) StorageValueMetadata {
|
||||
|
||||
Reference in New Issue
Block a user