2018-11-07 21:50:43 +00:00
|
|
|
// VulcanizeDB
|
2019-03-12 15:46:42 +00:00
|
|
|
// Copyright © 2019 Vulcanize
|
2018-11-07 21:50:43 +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/>.
|
|
|
|
|
2017-10-23 15:56:29 +00:00
|
|
|
package fakes
|
|
|
|
|
2017-11-28 16:45:21 +00:00
|
|
|
import (
|
2019-03-27 04:05:30 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2017-12-04 18:54:33 +00:00
|
|
|
"math/big"
|
|
|
|
|
2018-08-07 15:51:34 +00:00
|
|
|
"github.com/ethereum/go-ethereum"
|
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
2018-07-24 16:38:49 +00:00
|
|
|
. "github.com/onsi/gomega"
|
|
|
|
|
2018-01-06 20:31:53 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/core"
|
2017-11-28 16:45:21 +00:00
|
|
|
)
|
2017-10-23 15:56:29 +00:00
|
|
|
|
2018-07-18 20:59:40 +00:00
|
|
|
type MockBlockChain struct {
|
2018-07-20 16:37:46 +00:00
|
|
|
fetchContractDataErr error
|
|
|
|
fetchContractDataPassedAbi string
|
|
|
|
fetchContractDataPassedAddress string
|
|
|
|
fetchContractDataPassedMethod string
|
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
|
|
|
fetchContractDataPassedMethodArgs []interface{}
|
2018-07-20 16:37:46 +00:00
|
|
|
fetchContractDataPassedResult interface{}
|
|
|
|
fetchContractDataPassedBlockNumber int64
|
|
|
|
getBlockByNumberErr error
|
2019-03-27 04:05:30 +00:00
|
|
|
GetTransactionsCalled bool
|
|
|
|
GetTransactionsError error
|
|
|
|
GetTransactionsPassedHashes []common.Hash
|
2018-08-07 15:51:34 +00:00
|
|
|
logQuery ethereum.FilterQuery
|
|
|
|
logQueryErr error
|
2018-07-24 16:38:49 +00:00
|
|
|
logQueryReturnLogs []types.Log
|
2018-07-20 16:37:46 +00:00
|
|
|
lastBlock *big.Int
|
|
|
|
node core.Node
|
2019-03-27 04:05:30 +00:00
|
|
|
Transactions []core.TransactionModel
|
2019-04-04 20:21:39 +00:00
|
|
|
accountBalanceReturnValue *big.Int
|
2019-03-22 03:43:06 +00:00
|
|
|
getAccountBalanceErr error
|
2018-07-20 16:37:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewMockBlockChain() *MockBlockChain {
|
|
|
|
return &MockBlockChain{
|
|
|
|
node: core.Node{GenesisBlock: "GENESIS", NetworkID: 1, ID: "x123", ClientName: "Geth"},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-24 16:38:49 +00:00
|
|
|
func (chain *MockBlockChain) SetFetchContractDataErr(err error) {
|
|
|
|
chain.fetchContractDataErr = err
|
2018-07-20 16:37:46 +00:00
|
|
|
}
|
|
|
|
|
2018-07-24 16:38:49 +00:00
|
|
|
func (chain *MockBlockChain) SetLastBlock(blockNumber *big.Int) {
|
|
|
|
chain.lastBlock = blockNumber
|
2018-07-20 16:37:46 +00:00
|
|
|
}
|
|
|
|
|
2018-07-24 16:38:49 +00:00
|
|
|
func (chain *MockBlockChain) SetGetBlockByNumberErr(err error) {
|
|
|
|
chain.getBlockByNumberErr = err
|
2018-07-17 21:23:07 +00:00
|
|
|
}
|
|
|
|
|
2018-07-24 16:38:49 +00:00
|
|
|
func (chain *MockBlockChain) SetGetEthLogsWithCustomQueryErr(err error) {
|
|
|
|
chain.logQueryErr = err
|
2018-08-07 15:51:34 +00:00
|
|
|
}
|
|
|
|
|
2018-07-24 16:38:49 +00:00
|
|
|
func (chain *MockBlockChain) SetGetEthLogsWithCustomQueryReturnLogs(logs []types.Log) {
|
|
|
|
chain.logQueryReturnLogs = logs
|
2018-08-07 15:51:34 +00:00
|
|
|
}
|
|
|
|
|
2018-08-09 16:58:06 +00:00
|
|
|
func (blockChain *MockBlockChain) FetchContractData(abiJSON string, address string, method string, methodArgs []interface{}, result interface{}, blockNumber int64) error {
|
2018-07-20 16:37:46 +00:00
|
|
|
blockChain.fetchContractDataPassedAbi = abiJSON
|
|
|
|
blockChain.fetchContractDataPassedAddress = address
|
|
|
|
blockChain.fetchContractDataPassedMethod = method
|
2018-08-09 16:58:06 +00:00
|
|
|
blockChain.fetchContractDataPassedMethodArgs = methodArgs
|
2018-07-20 16:37:46 +00:00
|
|
|
blockChain.fetchContractDataPassedResult = result
|
|
|
|
blockChain.fetchContractDataPassedBlockNumber = blockNumber
|
|
|
|
return blockChain.fetchContractDataErr
|
2017-12-07 19:32:16 +00:00
|
|
|
}
|
|
|
|
|
2018-07-24 16:38:49 +00:00
|
|
|
func (chain *MockBlockChain) GetBlockByNumber(blockNumber int64) (core.Block, error) {
|
|
|
|
return core.Block{Number: blockNumber}, chain.getBlockByNumberErr
|
2017-12-20 20:06:22 +00:00
|
|
|
}
|
|
|
|
|
2018-07-24 16:38:49 +00:00
|
|
|
func (blockChain *MockBlockChain) GetEthLogsWithCustomQuery(query ethereum.FilterQuery) ([]types.Log, error) {
|
|
|
|
blockChain.logQuery = query
|
|
|
|
return blockChain.logQueryReturnLogs, blockChain.logQueryErr
|
2017-12-11 21:08:00 +00:00
|
|
|
}
|
|
|
|
|
2018-07-24 16:38:49 +00:00
|
|
|
func (chain *MockBlockChain) GetHeaderByNumber(blockNumber int64) (core.Header, error) {
|
|
|
|
return core.Header{BlockNumber: blockNumber}, nil
|
2017-11-27 15:39:53 +00:00
|
|
|
}
|
|
|
|
|
2019-05-07 22:24:10 +00:00
|
|
|
func (chain *MockBlockChain) GetHeadersByNumbers(blockNumbers []int64) ([]core.Header, error) {
|
2018-12-12 16:01:50 +00:00
|
|
|
var headers []core.Header
|
|
|
|
for _, blockNumber := range blockNumbers {
|
|
|
|
var header = core.Header{BlockNumber: int64(blockNumber)}
|
|
|
|
headers = append(headers, header)
|
|
|
|
}
|
|
|
|
return headers, nil
|
|
|
|
}
|
|
|
|
|
2019-08-06 21:57:08 +00:00
|
|
|
func (chain *MockBlockChain) GetFullSyncLogs(contract core.Contract, startingBlockNumber, endingBlockNumber *big.Int) ([]core.FullSyncLog, error) {
|
|
|
|
return []core.FullSyncLog{}, nil
|
2017-11-06 20:36:12 +00:00
|
|
|
}
|
|
|
|
|
2019-03-27 04:05:30 +00:00
|
|
|
func (chain *MockBlockChain) GetTransactions(transactionHashes []common.Hash) ([]core.TransactionModel, error) {
|
|
|
|
chain.GetTransactionsCalled = true
|
|
|
|
chain.GetTransactionsPassedHashes = transactionHashes
|
|
|
|
return chain.Transactions, chain.GetTransactionsError
|
|
|
|
}
|
|
|
|
|
2018-07-24 16:38:49 +00:00
|
|
|
func (chain *MockBlockChain) CallContract(contractHash string, input []byte, blockNumber *big.Int) ([]byte, error) {
|
|
|
|
return []byte{}, nil
|
|
|
|
}
|
|
|
|
|
2019-02-14 15:04:12 +00:00
|
|
|
func (chain *MockBlockChain) LastBlock() (*big.Int, error) {
|
|
|
|
return chain.lastBlock, nil
|
2018-07-24 16:38:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (chain *MockBlockChain) Node() core.Node {
|
|
|
|
return chain.node
|
|
|
|
}
|
|
|
|
|
2018-11-23 18:12:24 +00:00
|
|
|
func (chain *MockBlockChain) AssertFetchContractDataCalledWith(abiJSON string, address string, method string, methodArgs []interface{}, result interface{}, blockNumber int64) {
|
2018-07-24 16:38:49 +00:00
|
|
|
Expect(chain.fetchContractDataPassedAbi).To(Equal(abiJSON))
|
|
|
|
Expect(chain.fetchContractDataPassedAddress).To(Equal(address))
|
|
|
|
Expect(chain.fetchContractDataPassedMethod).To(Equal(method))
|
2018-11-23 18:12:24 +00:00
|
|
|
if methodArgs != nil {
|
|
|
|
Expect(chain.fetchContractDataPassedMethodArgs).To(Equal(methodArgs))
|
2018-07-24 16:38:49 +00:00
|
|
|
}
|
2018-07-26 18:57:38 +00:00
|
|
|
Expect(chain.fetchContractDataPassedResult).To(BeAssignableToTypeOf(result))
|
2018-07-24 16:38:49 +00:00
|
|
|
Expect(chain.fetchContractDataPassedBlockNumber).To(Equal(blockNumber))
|
2017-10-23 15:56:29 +00:00
|
|
|
}
|
2018-08-07 15:51:34 +00:00
|
|
|
|
|
|
|
func (blockChain *MockBlockChain) AssertGetEthLogsWithCustomQueryCalledWith(query ethereum.FilterQuery) {
|
|
|
|
Expect(blockChain.logQuery).To(Equal(query))
|
|
|
|
}
|
2019-03-22 03:43:06 +00:00
|
|
|
|
|
|
|
func (blockChain *MockBlockChain) SetGetAccountBalanceErr(err error) {
|
|
|
|
blockChain.getAccountBalanceErr = err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (blockChain *MockBlockChain) SetGetAccountBalance(balance *big.Int) {
|
2019-04-04 20:21:39 +00:00
|
|
|
blockChain.accountBalanceReturnValue = balance
|
2019-03-22 03:43:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (blockChain *MockBlockChain) GetAccountBalance(address common.Address, blockNumber *big.Int) (*big.Int, error) {
|
2019-04-04 20:21:39 +00:00
|
|
|
return blockChain.accountBalanceReturnValue, blockChain.getAccountBalanceErr
|
2019-03-22 03:43:06 +00:00
|
|
|
}
|