2018-11-07 21:50:43 +00:00
|
|
|
// VulcanizeDB
|
|
|
|
// Copyright © 2018 Vulcanize
|
|
|
|
|
|
|
|
// 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/>.
|
2018-05-05 20:25:54 +00:00
|
|
|
|
|
|
|
package every_block_test
|
|
|
|
|
|
|
|
import (
|
2018-08-31 19:48:43 +00:00
|
|
|
"math/big"
|
|
|
|
"math/rand"
|
|
|
|
"strconv"
|
|
|
|
|
2018-05-05 20:25:54 +00:00
|
|
|
. "github.com/onsi/ginkgo"
|
|
|
|
. "github.com/onsi/gomega"
|
2018-08-31 19:48:43 +00:00
|
|
|
|
2018-09-19 17:22:05 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/examples/erc20_watcher"
|
2018-05-05 20:25:54 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/examples/erc20_watcher/every_block"
|
|
|
|
"github.com/vulcanize/vulcanizedb/examples/mocks"
|
2018-08-22 19:11:15 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/examples/test_helpers"
|
2018-09-19 17:22:05 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/libraries/shared"
|
2018-07-20 16:37:46 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/fakes"
|
2018-11-07 21:50:43 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/omni/constants"
|
2018-05-05 20:25:54 +00:00
|
|
|
)
|
|
|
|
|
2018-09-19 17:22:05 +00:00
|
|
|
var testContractConfig = shared.ContractConfig{
|
2018-05-05 20:25:54 +00:00
|
|
|
Address: constants.DaiContractAddress,
|
|
|
|
Abi: constants.DaiAbiString,
|
|
|
|
FirstBlock: int64(4752008),
|
|
|
|
LastBlock: int64(5750050),
|
|
|
|
Name: "Dai",
|
|
|
|
}
|
|
|
|
|
|
|
|
var config = testContractConfig
|
|
|
|
|
|
|
|
var _ = Describe("Everyblock transformer", func() {
|
Moved fetcher to generic directory (methods have to remain public since it is in seperate package now), added FetchHash method, created ERC20 and generic getters which call the fetcher with specific contract methods (GetTotalSupply, GetBalance, GetAllowance for ERC20 getter, and GetOwner, GetStoppedStatus, GetStringName, GetHashName, GetStringSymbol, GetHashSymbol, and GetDecimals for generic getter). Getter tests cover all but GetBalance and GetAllowance, and also cover all of the Fetcher methods- but with only nil methodArgs. GetAllowance and GetBalance tests are not working against infura and these are the only contract method calls with arguments passed in so I suspect this might be where the issue lies. Have tested GetBalance using previous version of FetchContractData without the variadic input to the Pack method and it fails with the same error so I don’t think it is due to those changes.
2018-08-15 04:17:22 +00:00
|
|
|
var getter mocks.Getter
|
2018-08-09 16:58:06 +00:00
|
|
|
var repository mocks.ERC20TokenRepository
|
2018-08-31 19:48:43 +00:00
|
|
|
var transformer every_block.ERC20Transformer
|
2018-07-20 16:37:46 +00:00
|
|
|
var blockChain *fakes.MockBlockChain
|
2018-05-05 20:25:54 +00:00
|
|
|
var initialSupply = "27647235749155415536952630"
|
|
|
|
var initialSupplyPlusOne = "27647235749155415536952631"
|
|
|
|
var initialSupplyPlusTwo = "27647235749155415536952632"
|
|
|
|
var initialSupplyPlusThree = "27647235749155415536952633"
|
|
|
|
var defaultLastBlock = big.Int{}
|
|
|
|
|
|
|
|
BeforeEach(func() {
|
2018-07-20 16:37:46 +00:00
|
|
|
blockChain = fakes.NewMockBlockChain()
|
|
|
|
blockChain.SetLastBlock(&defaultLastBlock)
|
Moved fetcher to generic directory (methods have to remain public since it is in seperate package now), added FetchHash method, created ERC20 and generic getters which call the fetcher with specific contract methods (GetTotalSupply, GetBalance, GetAllowance for ERC20 getter, and GetOwner, GetStoppedStatus, GetStringName, GetHashName, GetStringSymbol, GetHashSymbol, and GetDecimals for generic getter). Getter tests cover all but GetBalance and GetAllowance, and also cover all of the Fetcher methods- but with only nil methodArgs. GetAllowance and GetBalance tests are not working against infura and these are the only contract method calls with arguments passed in so I suspect this might be where the issue lies. Have tested GetBalance using previous version of FetchContractData without the variadic input to the Pack method and it fails with the same error so I don’t think it is due to those changes.
2018-08-15 04:17:22 +00:00
|
|
|
getter = mocks.NewGetter(blockChain)
|
|
|
|
getter.Fetcher.SetSupply(initialSupply)
|
2018-08-09 16:58:06 +00:00
|
|
|
repository = mocks.ERC20TokenRepository{}
|
|
|
|
repository.SetMissingSupplyBlocks([]int64{config.FirstBlock})
|
2018-08-22 19:11:15 +00:00
|
|
|
db := test_helpers.CreateNewDatabase()
|
2018-09-19 17:22:05 +00:00
|
|
|
rt := erc20_watcher.NewTokenHolderRetriever(db, config.Address)
|
2018-05-05 20:25:54 +00:00
|
|
|
//setting the mock repository to return the first block as the missing blocks
|
|
|
|
|
2018-08-31 19:48:43 +00:00
|
|
|
transformer = every_block.ERC20Transformer{
|
Moved fetcher to generic directory (methods have to remain public since it is in seperate package now), added FetchHash method, created ERC20 and generic getters which call the fetcher with specific contract methods (GetTotalSupply, GetBalance, GetAllowance for ERC20 getter, and GetOwner, GetStoppedStatus, GetStringName, GetHashName, GetStringSymbol, GetHashSymbol, and GetDecimals for generic getter). Getter tests cover all but GetBalance and GetAllowance, and also cover all of the Fetcher methods- but with only nil methodArgs. GetAllowance and GetBalance tests are not working against infura and these are the only contract method calls with arguments passed in so I suspect this might be where the issue lies. Have tested GetBalance using previous version of FetchContractData without the variadic input to the Pack method and it fails with the same error so I don’t think it is due to those changes.
2018-08-15 04:17:22 +00:00
|
|
|
Getter: &getter,
|
2018-05-05 20:25:54 +00:00
|
|
|
Repository: &repository,
|
2018-08-22 19:11:15 +00:00
|
|
|
Retriever: rt,
|
|
|
|
Config: config,
|
2018-05-05 20:25:54 +00:00
|
|
|
}
|
|
|
|
transformer.SetConfiguration(config)
|
|
|
|
})
|
|
|
|
|
|
|
|
It("fetches and persists the total supply of a token for a single block", func() {
|
|
|
|
err := transformer.Execute()
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
|
Moved fetcher to generic directory (methods have to remain public since it is in seperate package now), added FetchHash method, created ERC20 and generic getters which call the fetcher with specific contract methods (GetTotalSupply, GetBalance, GetAllowance for ERC20 getter, and GetOwner, GetStoppedStatus, GetStringName, GetHashName, GetStringSymbol, GetHashSymbol, and GetDecimals for generic getter). Getter tests cover all but GetBalance and GetAllowance, and also cover all of the Fetcher methods- but with only nil methodArgs. GetAllowance and GetBalance tests are not working against infura and these are the only contract method calls with arguments passed in so I suspect this might be where the issue lies. Have tested GetBalance using previous version of FetchContractData without the variadic input to the Pack method and it fails with the same error so I don’t think it is due to those changes.
2018-08-15 04:17:22 +00:00
|
|
|
Expect(len(getter.Fetcher.FetchedBlocks)).To(Equal(1))
|
|
|
|
Expect(getter.Fetcher.FetchedBlocks).To(ConsistOf(config.FirstBlock))
|
|
|
|
Expect(getter.Fetcher.Abi).To(Equal(config.Abi))
|
|
|
|
Expect(getter.Fetcher.ContractAddress).To(Equal(config.Address))
|
2018-05-05 20:25:54 +00:00
|
|
|
|
|
|
|
Expect(repository.StartingBlock).To(Equal(config.FirstBlock))
|
|
|
|
Expect(repository.EndingBlock).To(Equal(config.LastBlock))
|
|
|
|
Expect(len(repository.TotalSuppliesCreated)).To(Equal(1))
|
|
|
|
expectedSupply := big.Int{}
|
|
|
|
expectedSupply.SetString(initialSupply, 10)
|
|
|
|
expectedSupply.Add(&expectedSupply, big.NewInt(1))
|
|
|
|
|
|
|
|
Expect(repository.TotalSuppliesCreated[0].Value).To(Equal(expectedSupply.String()))
|
|
|
|
})
|
|
|
|
|
|
|
|
It("retrieves the total supply for every missing block", func() {
|
|
|
|
missingBlocks := []int64{
|
|
|
|
config.FirstBlock,
|
|
|
|
config.FirstBlock + 1,
|
|
|
|
config.FirstBlock + 2,
|
|
|
|
}
|
2018-08-09 16:58:06 +00:00
|
|
|
repository.SetMissingSupplyBlocks(missingBlocks)
|
|
|
|
err := transformer.Execute()
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
2018-05-05 20:25:54 +00:00
|
|
|
|
Moved fetcher to generic directory (methods have to remain public since it is in seperate package now), added FetchHash method, created ERC20 and generic getters which call the fetcher with specific contract methods (GetTotalSupply, GetBalance, GetAllowance for ERC20 getter, and GetOwner, GetStoppedStatus, GetStringName, GetHashName, GetStringSymbol, GetHashSymbol, and GetDecimals for generic getter). Getter tests cover all but GetBalance and GetAllowance, and also cover all of the Fetcher methods- but with only nil methodArgs. GetAllowance and GetBalance tests are not working against infura and these are the only contract method calls with arguments passed in so I suspect this might be where the issue lies. Have tested GetBalance using previous version of FetchContractData without the variadic input to the Pack method and it fails with the same error so I don’t think it is due to those changes.
2018-08-15 04:17:22 +00:00
|
|
|
Expect(len(getter.Fetcher.FetchedBlocks)).To(Equal(3))
|
|
|
|
Expect(getter.Fetcher.FetchedBlocks).To(ConsistOf(config.FirstBlock, config.FirstBlock+1, config.FirstBlock+2))
|
|
|
|
Expect(getter.Fetcher.Abi).To(Equal(config.Abi))
|
|
|
|
Expect(getter.Fetcher.ContractAddress).To(Equal(config.Address))
|
2018-05-05 20:25:54 +00:00
|
|
|
|
|
|
|
Expect(len(repository.TotalSuppliesCreated)).To(Equal(3))
|
|
|
|
Expect(repository.TotalSuppliesCreated[0].Value).To(Equal(initialSupplyPlusOne))
|
|
|
|
Expect(repository.TotalSuppliesCreated[1].Value).To(Equal(initialSupplyPlusTwo))
|
|
|
|
Expect(repository.TotalSuppliesCreated[2].Value).To(Equal(initialSupplyPlusThree))
|
|
|
|
})
|
|
|
|
|
|
|
|
It("uses the set contract configuration", func() {
|
2018-08-09 16:58:06 +00:00
|
|
|
repository.SetMissingSupplyBlocks([]int64{testContractConfig.FirstBlock})
|
2018-05-05 20:25:54 +00:00
|
|
|
transformer.SetConfiguration(testContractConfig)
|
|
|
|
err := transformer.Execute()
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
|
Moved fetcher to generic directory (methods have to remain public since it is in seperate package now), added FetchHash method, created ERC20 and generic getters which call the fetcher with specific contract methods (GetTotalSupply, GetBalance, GetAllowance for ERC20 getter, and GetOwner, GetStoppedStatus, GetStringName, GetHashName, GetStringSymbol, GetHashSymbol, and GetDecimals for generic getter). Getter tests cover all but GetBalance and GetAllowance, and also cover all of the Fetcher methods- but with only nil methodArgs. GetAllowance and GetBalance tests are not working against infura and these are the only contract method calls with arguments passed in so I suspect this might be where the issue lies. Have tested GetBalance using previous version of FetchContractData without the variadic input to the Pack method and it fails with the same error so I don’t think it is due to those changes.
2018-08-15 04:17:22 +00:00
|
|
|
Expect(getter.Fetcher.FetchedBlocks).To(ConsistOf(testContractConfig.FirstBlock))
|
|
|
|
Expect(getter.Fetcher.Abi).To(Equal(testContractConfig.Abi))
|
|
|
|
Expect(getter.Fetcher.ContractAddress).To(Equal(testContractConfig.Address))
|
2018-05-05 20:25:54 +00:00
|
|
|
|
|
|
|
Expect(repository.StartingBlock).To(Equal(testContractConfig.FirstBlock))
|
|
|
|
Expect(repository.EndingBlock).To(Equal(testContractConfig.LastBlock))
|
|
|
|
Expect(len(repository.TotalSuppliesCreated)).To(Equal(1))
|
|
|
|
expectedTokenSupply := every_block.TokenSupply{
|
|
|
|
Value: initialSupplyPlusOne,
|
|
|
|
TokenAddress: testContractConfig.Address,
|
|
|
|
BlockNumber: testContractConfig.FirstBlock,
|
|
|
|
}
|
|
|
|
Expect(repository.TotalSuppliesCreated[0]).To(Equal(expectedTokenSupply))
|
|
|
|
})
|
|
|
|
|
|
|
|
It("uses the most recent block if the Config.LastBlock is -1", func() {
|
|
|
|
testContractConfig.LastBlock = -1
|
|
|
|
transformer.SetConfiguration(testContractConfig)
|
|
|
|
|
|
|
|
randomBlockNumber := rand.Int63()
|
|
|
|
numberToString := strconv.FormatInt(randomBlockNumber, 10)
|
|
|
|
mostRecentBlock := big.Int{}
|
|
|
|
mostRecentBlock.SetString(numberToString, 10)
|
|
|
|
|
2018-07-20 16:37:46 +00:00
|
|
|
blockChain.SetLastBlock(&mostRecentBlock)
|
2018-05-05 20:25:54 +00:00
|
|
|
|
|
|
|
err := transformer.Execute()
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
|
|
|
|
Expect(repository.EndingBlock).To(Equal(randomBlockNumber))
|
|
|
|
})
|
|
|
|
|
|
|
|
It("returns an error if the call to get missing blocks fails", func() {
|
|
|
|
failureRepository := mocks.FailureRepository{}
|
2018-08-09 16:58:06 +00:00
|
|
|
failureRepository.SetMissingSupplyBlocksFail(true)
|
2018-08-31 19:48:43 +00:00
|
|
|
transformer = every_block.ERC20Transformer{
|
Moved fetcher to generic directory (methods have to remain public since it is in seperate package now), added FetchHash method, created ERC20 and generic getters which call the fetcher with specific contract methods (GetTotalSupply, GetBalance, GetAllowance for ERC20 getter, and GetOwner, GetStoppedStatus, GetStringName, GetHashName, GetStringSymbol, GetHashSymbol, and GetDecimals for generic getter). Getter tests cover all but GetBalance and GetAllowance, and also cover all of the Fetcher methods- but with only nil methodArgs. GetAllowance and GetBalance tests are not working against infura and these are the only contract method calls with arguments passed in so I suspect this might be where the issue lies. Have tested GetBalance using previous version of FetchContractData without the variadic input to the Pack method and it fails with the same error so I don’t think it is due to those changes.
2018-08-15 04:17:22 +00:00
|
|
|
Getter: &getter,
|
2018-05-05 20:25:54 +00:00
|
|
|
Repository: &failureRepository,
|
|
|
|
}
|
|
|
|
err := transformer.Execute()
|
|
|
|
Expect(err).To(HaveOccurred())
|
2018-07-20 16:37:46 +00:00
|
|
|
Expect(err.Error()).To(ContainSubstring(fakes.FakeError.Error()))
|
2018-08-22 19:11:15 +00:00
|
|
|
Expect(err.Error()).To(ContainSubstring("fetching missing blocks"))
|
2018-05-05 20:25:54 +00:00
|
|
|
})
|
|
|
|
|
2018-07-20 16:37:46 +00:00
|
|
|
It("returns an error if the call to the blockChain fails", func() {
|
|
|
|
failureBlockchain := fakes.NewMockBlockChain()
|
2018-05-05 20:25:54 +00:00
|
|
|
failureBlockchain.SetLastBlock(&defaultLastBlock)
|
2018-07-20 16:37:46 +00:00
|
|
|
failureBlockchain.SetFetchContractDataErr(fakes.FakeError)
|
Moved fetcher to generic directory (methods have to remain public since it is in seperate package now), added FetchHash method, created ERC20 and generic getters which call the fetcher with specific contract methods (GetTotalSupply, GetBalance, GetAllowance for ERC20 getter, and GetOwner, GetStoppedStatus, GetStringName, GetHashName, GetStringSymbol, GetHashSymbol, and GetDecimals for generic getter). Getter tests cover all but GetBalance and GetAllowance, and also cover all of the Fetcher methods- but with only nil methodArgs. GetAllowance and GetBalance tests are not working against infura and these are the only contract method calls with arguments passed in so I suspect this might be where the issue lies. Have tested GetBalance using previous version of FetchContractData without the variadic input to the Pack method and it fails with the same error so I don’t think it is due to those changes.
2018-08-15 04:17:22 +00:00
|
|
|
getter := every_block.NewGetter(failureBlockchain)
|
2018-08-31 19:48:43 +00:00
|
|
|
transformer = every_block.ERC20Transformer{
|
Moved fetcher to generic directory (methods have to remain public since it is in seperate package now), added FetchHash method, created ERC20 and generic getters which call the fetcher with specific contract methods (GetTotalSupply, GetBalance, GetAllowance for ERC20 getter, and GetOwner, GetStoppedStatus, GetStringName, GetHashName, GetStringSymbol, GetHashSymbol, and GetDecimals for generic getter). Getter tests cover all but GetBalance and GetAllowance, and also cover all of the Fetcher methods- but with only nil methodArgs. GetAllowance and GetBalance tests are not working against infura and these are the only contract method calls with arguments passed in so I suspect this might be where the issue lies. Have tested GetBalance using previous version of FetchContractData without the variadic input to the Pack method and it fails with the same error so I don’t think it is due to those changes.
2018-08-15 04:17:22 +00:00
|
|
|
Getter: &getter,
|
2018-05-05 20:25:54 +00:00
|
|
|
Repository: &repository,
|
|
|
|
}
|
|
|
|
err := transformer.Execute()
|
|
|
|
Expect(err).To(HaveOccurred())
|
2018-07-20 16:37:46 +00:00
|
|
|
Expect(err.Error()).To(ContainSubstring(fakes.FakeError.Error()))
|
2018-05-05 20:25:54 +00:00
|
|
|
Expect(err.Error()).To(ContainSubstring("supply"))
|
|
|
|
})
|
|
|
|
|
|
|
|
It("returns an error if the call to save the token_supply fails", func() {
|
|
|
|
failureRepository := mocks.FailureRepository{}
|
2018-08-09 16:58:06 +00:00
|
|
|
failureRepository.SetMissingSupplyBlocks([]int64{config.FirstBlock})
|
|
|
|
failureRepository.SetCreateSupplyFail(true)
|
2018-05-05 20:25:54 +00:00
|
|
|
|
2018-08-31 19:48:43 +00:00
|
|
|
transformer = every_block.ERC20Transformer{
|
Moved fetcher to generic directory (methods have to remain public since it is in seperate package now), added FetchHash method, created ERC20 and generic getters which call the fetcher with specific contract methods (GetTotalSupply, GetBalance, GetAllowance for ERC20 getter, and GetOwner, GetStoppedStatus, GetStringName, GetHashName, GetStringSymbol, GetHashSymbol, and GetDecimals for generic getter). Getter tests cover all but GetBalance and GetAllowance, and also cover all of the Fetcher methods- but with only nil methodArgs. GetAllowance and GetBalance tests are not working against infura and these are the only contract method calls with arguments passed in so I suspect this might be where the issue lies. Have tested GetBalance using previous version of FetchContractData without the variadic input to the Pack method and it fails with the same error so I don’t think it is due to those changes.
2018-08-15 04:17:22 +00:00
|
|
|
Getter: &getter,
|
2018-05-05 20:25:54 +00:00
|
|
|
Repository: &failureRepository,
|
|
|
|
}
|
|
|
|
err := transformer.Execute()
|
|
|
|
Expect(err).To(HaveOccurred())
|
2018-07-20 16:37:46 +00:00
|
|
|
Expect(err.Error()).To(ContainSubstring(fakes.FakeError.Error()))
|
2018-05-05 20:25:54 +00:00
|
|
|
Expect(err.Error()).To(ContainSubstring("supply"))
|
|
|
|
})
|
|
|
|
})
|