2019-01-28 22:52:47 +00:00
|
|
|
// VulcanizeDB
|
2019-03-12 15:46:42 +00:00
|
|
|
// Copyright © 2019 Vulcanize
|
2019-01-28 22:52:47 +00:00
|
|
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2019-01-31 23:14:42 +00:00
|
|
|
package utils_test
|
2019-01-28 22:52:47 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2019-08-16 21:01:01 +00:00
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
|
|
|
"github.com/ethereum/go-ethereum/statediff"
|
2019-01-28 22:52:47 +00:00
|
|
|
. "github.com/onsi/ginkgo"
|
|
|
|
. "github.com/onsi/gomega"
|
2019-02-10 22:42:41 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/libraries/shared/storage/utils"
|
2019-08-16 21:01:01 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/fakes"
|
|
|
|
"math/big"
|
|
|
|
"math/rand"
|
2019-01-28 22:52:47 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var _ = Describe("Storage row parsing", func() {
|
2019-08-16 21:01:01 +00:00
|
|
|
Describe("FromParityCsvRow", func() {
|
|
|
|
It("converts an array of strings to a row struct", func() {
|
|
|
|
contract := "0x123"
|
|
|
|
blockHash := "0x456"
|
|
|
|
blockHeight := "789"
|
|
|
|
storageKey := "0x987"
|
|
|
|
storageValue := "0x654"
|
|
|
|
data := []string{contract, blockHash, blockHeight, storageKey, storageValue}
|
|
|
|
|
|
|
|
result, err := utils.FromParityCsvRow(data)
|
|
|
|
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
expectedKeccakOfContractAddress := common.BytesToHash(crypto.Keccak256(common.FromHex(contract)))
|
|
|
|
Expect(result.KeccakOfContractAddress).To(Equal(expectedKeccakOfContractAddress))
|
|
|
|
Expect(result.BlockHash).To(Equal(common.HexToHash(blockHash)))
|
|
|
|
Expect(result.BlockHeight).To(Equal(789))
|
|
|
|
Expect(result.StorageKey).To(Equal(common.HexToHash(storageKey)))
|
|
|
|
Expect(result.StorageValue).To(Equal(common.HexToHash(storageValue)))
|
|
|
|
})
|
2019-01-28 22:52:47 +00:00
|
|
|
|
2019-08-16 21:01:01 +00:00
|
|
|
It("returns an error if row is missing data", func() {
|
|
|
|
_, err := utils.FromParityCsvRow([]string{"0x123"})
|
2019-01-28 22:52:47 +00:00
|
|
|
|
2019-08-16 21:01:01 +00:00
|
|
|
Expect(err).To(HaveOccurred())
|
|
|
|
Expect(err).To(MatchError(utils.ErrRowMalformed{Length: 1}))
|
|
|
|
})
|
|
|
|
|
|
|
|
It("returns error if block height malformed", func() {
|
|
|
|
_, err := utils.FromParityCsvRow([]string{"", "", "", "", ""})
|
|
|
|
|
|
|
|
Expect(err).To(HaveOccurred())
|
|
|
|
})
|
2019-01-28 22:52:47 +00:00
|
|
|
})
|
|
|
|
|
2019-08-16 21:01:01 +00:00
|
|
|
Describe("FromGethStateDiff", func() {
|
|
|
|
It("adds relevant fields to diff", func() {
|
|
|
|
accountDiff := statediff.AccountDiff{Key: []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0}}
|
|
|
|
stateDiff := &statediff.StateDiff{
|
|
|
|
BlockNumber: big.NewInt(rand.Int63()),
|
|
|
|
BlockHash: fakes.FakeHash,
|
|
|
|
}
|
|
|
|
storageDiff := statediff.StorageDiff{
|
|
|
|
Key: []byte{0, 9, 8, 7, 6, 5, 4, 3, 2, 1},
|
|
|
|
Value: []byte{1, 2, 3, 4, 5, 0, 9, 8, 7, 6},
|
|
|
|
}
|
|
|
|
|
|
|
|
result := utils.FromGethStateDiff(accountDiff, stateDiff, storageDiff)
|
2019-01-28 22:52:47 +00:00
|
|
|
|
2019-08-16 21:01:01 +00:00
|
|
|
expectedAddress := common.BytesToHash(accountDiff.Key)
|
|
|
|
Expect(result.KeccakOfContractAddress).To(Equal(expectedAddress))
|
|
|
|
Expect(result.BlockHash).To(Equal(fakes.FakeHash))
|
|
|
|
expectedBlockHeight := int(stateDiff.BlockNumber.Int64())
|
|
|
|
Expect(result.BlockHeight).To(Equal(expectedBlockHeight))
|
|
|
|
expectedStorageKey := common.BytesToHash(storageDiff.Key)
|
|
|
|
Expect(result.StorageKey).To(Equal(expectedStorageKey))
|
|
|
|
expectedStorageValue := common.BytesToHash(storageDiff.Value)
|
|
|
|
Expect(result.StorageValue).To(Equal(expectedStorageValue))
|
|
|
|
})
|
2019-01-28 22:52:47 +00:00
|
|
|
})
|
|
|
|
})
|