Extract helper for converting hex to keccak256 hash
- Also prefer crypto.Keccak256Hash(x) to common.BytesToHash(crypto.Keccak256(x))
This commit is contained in:
committed by
Elizabeth Engelman
parent
d06dddbfaa
commit
2ff88de859
@@ -58,19 +58,17 @@ func AddHashedKeys(currentMappings map[common.Hash]utils.StorageValueMetadata) m
|
||||
}
|
||||
|
||||
func hashKey(key common.Hash) common.Hash {
|
||||
return common.BytesToHash(crypto.Keccak256(key.Bytes()))
|
||||
return crypto.Keccak256Hash(key.Bytes())
|
||||
}
|
||||
|
||||
func GetMapping(indexOnContract, key string) common.Hash {
|
||||
keyBytes := common.FromHex(key + indexOnContract)
|
||||
encoded := crypto.Keccak256(keyBytes)
|
||||
return common.BytesToHash(encoded)
|
||||
return crypto.Keccak256Hash(keyBytes)
|
||||
}
|
||||
|
||||
func GetNestedMapping(indexOnContract, primaryKey, secondaryKey string) common.Hash {
|
||||
primaryMappingIndex := crypto.Keccak256(common.FromHex(primaryKey + indexOnContract))
|
||||
secondaryMappingIndex := crypto.Keccak256(common.FromHex(secondaryKey), primaryMappingIndex)
|
||||
return common.BytesToHash(secondaryMappingIndex)
|
||||
return crypto.Keccak256Hash(common.FromHex(secondaryKey), primaryMappingIndex)
|
||||
}
|
||||
|
||||
func GetIncrementedKey(original common.Hash, incrementBy int64) common.Hash {
|
||||
|
||||
@@ -18,7 +18,6 @@ package storage_test
|
||||
|
||||
import (
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
"github.com/ethereum/go-ethereum/crypto"
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
"github.com/vulcanize/vulcanizedb/libraries/shared/storage"
|
||||
@@ -35,10 +34,9 @@ var _ = Describe("Storage queue", func() {
|
||||
)
|
||||
|
||||
BeforeEach(func() {
|
||||
fakeAddr := common.FromHex("0x123456")
|
||||
hashedFakeAddr := crypto.Keccak256(fakeAddr)
|
||||
fakeAddr := "0x123456"
|
||||
diff = utils.StorageDiff{
|
||||
KeccakOfContractAddress: common.BytesToHash(hashedFakeAddr),
|
||||
KeccakOfContractAddress: utils.HexToKeccak256Hash(fakeAddr),
|
||||
BlockHash: common.HexToHash("0x678901"),
|
||||
BlockHeight: 987,
|
||||
StorageKey: common.HexToHash("0x654321"),
|
||||
@@ -83,10 +81,9 @@ var _ = Describe("Storage queue", func() {
|
||||
})
|
||||
|
||||
It("gets all storage diffs from db", func() {
|
||||
fakeAddr := common.FromHex("0x234567")
|
||||
hashedFakeAddr := crypto.Keccak256(fakeAddr)
|
||||
fakeAddr := "0x234567"
|
||||
diffTwo := utils.StorageDiff{
|
||||
KeccakOfContractAddress: common.BytesToHash(hashedFakeAddr),
|
||||
KeccakOfContractAddress: utils.HexToKeccak256Hash(fakeAddr),
|
||||
BlockHash: common.HexToHash("0x678902"),
|
||||
BlockHeight: 988,
|
||||
StorageKey: common.HexToHash("0x654322"),
|
||||
|
||||
@@ -42,9 +42,8 @@ func FromParityCsvRow(csvRow []string) (StorageDiff, error) {
|
||||
if err != nil {
|
||||
return StorageDiff{}, err
|
||||
}
|
||||
hashedAddr := crypto.Keccak256(common.FromHex(csvRow[0]))
|
||||
return StorageDiff{
|
||||
KeccakOfContractAddress: common.BytesToHash(hashedAddr),
|
||||
KeccakOfContractAddress: HexToKeccak256Hash(csvRow[0]),
|
||||
BlockHash: common.HexToHash(csvRow[1]),
|
||||
BlockHeight: height,
|
||||
StorageKey: common.HexToHash(csvRow[3]),
|
||||
@@ -61,3 +60,7 @@ func FromGethStateDiff(account statediff.AccountDiff, stateDiff *statediff.State
|
||||
StorageValue: common.BytesToHash(storage.Value),
|
||||
}
|
||||
}
|
||||
|
||||
func HexToKeccak256Hash(addr string) common.Hash {
|
||||
return crypto.Keccak256Hash(common.FromHex(addr))
|
||||
}
|
||||
|
||||
@@ -18,7 +18,6 @@ package utils_test
|
||||
|
||||
import (
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
"github.com/ethereum/go-ethereum/crypto"
|
||||
"github.com/ethereum/go-ethereum/statediff"
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
@@ -41,7 +40,7 @@ var _ = Describe("Storage row parsing", func() {
|
||||
result, err := utils.FromParityCsvRow(data)
|
||||
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
expectedKeccakOfContractAddress := common.BytesToHash(crypto.Keccak256(common.FromHex(contract)))
|
||||
expectedKeccakOfContractAddress := utils.HexToKeccak256Hash(contract)
|
||||
Expect(result.KeccakOfContractAddress).To(Equal(expectedKeccakOfContractAddress))
|
||||
Expect(result.BlockHash).To(Equal(common.HexToHash(blockHash)))
|
||||
Expect(result.BlockHeight).To(Equal(789))
|
||||
|
||||
Reference in New Issue
Block a user