Skip to content
This repository has been archived by the owner on Aug 31, 2021. It is now read-only.

Commit

Permalink
Merge pull request #125 from vulcanize/decode-packed-addresses
Browse files Browse the repository at this point in the history
(VDB-768) Allow addresses in packed slots to be decoded
  • Loading branch information
Gslaughl committed Aug 6, 2019
2 parents 65e762f + 4f8b057 commit baea974
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
12 changes: 7 additions & 5 deletions libraries/shared/storage/utils/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func decodePackedSlot(raw []byte, packedTypes map[int]ValueType) map[int]string
itemValueBytes := storageSlotData[itemStartingIndex:]

//decode item's bytes and set in results map
decodedValue := decodeIndividualItems(itemValueBytes, itemType)
decodedValue := decodeIndividualItem(itemValueBytes, itemType)
decodedStorageSlotItems[position] = decodedValue

//pop last item off raw slot data before moving on
Expand All @@ -81,12 +81,12 @@ func decodePackedSlot(raw []byte, packedTypes map[int]ValueType) map[int]string
return decodedStorageSlotItems
}

func decodeIndividualItems(itemBytes []byte, valueType ValueType) string {
func decodeIndividualItem(itemBytes []byte, valueType ValueType) string {
switch valueType {
case Uint48:
return decodeInteger(itemBytes)
case Uint128:
case Uint48, Uint128:
return decodeInteger(itemBytes)
case Address:
return decodeAddress(itemBytes)
default:
panic(fmt.Sprintf("can't decode unknown type: %d", valueType))
}
Expand All @@ -98,6 +98,8 @@ func getNumberOfBytes(valueType ValueType) int {
return 48 / bitsPerByte
case Uint128:
return 128 / bitsPerByte
case Address:
return 20
default:
panic(fmt.Sprintf("ValueType %d not recognized", valueType))
}
Expand Down
24 changes: 24 additions & 0 deletions libraries/shared/storage/utils/decoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,5 +146,29 @@ var _ = Describe("Storage decoder", func() {
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()))
})

It("decodes address + 2 uint48s", func() {
//TODO: replace with real data when available
addressHex := "0000000000000000000000000000000000012345"
packedStorage := common.HexToHash("00000002a300" + "000000002a30" + addressHex)
row := utils.StorageDiffRow{StorageValue: packedStorage}
packedTypes := map[int]utils.ValueType{}
packedTypes[0] = utils.Address
packedTypes[1] = utils.Uint48
packedTypes[2] = utils.Uint48

metadata := utils.StorageValueMetadata{
Type: utils.PackedSlot,
PackedTypes: packedTypes,
}

result, err := utils.Decode(row, metadata)
decodedValues := result.(map[int]string)

Expect(err).NotTo(HaveOccurred())
Expect(decodedValues[0]).To(Equal("0x" + addressHex))
Expect(decodedValues[1]).To(Equal(big.NewInt(0).SetBytes(common.HexToHash("2a30").Bytes()).String()))
Expect(decodedValues[2]).To(Equal(big.NewInt(0).SetBytes(common.HexToHash("2a300").Bytes()).String()))
})
})
})

0 comments on commit baea974

Please sign in to comment.