publisher unit test
This commit is contained in:
@@ -63,32 +63,26 @@ func (f *EthIPLDFetcher) FetchCIDs(cids CIDWrapper) (*IPLDWrapper, error) {
|
||||
|
||||
err := f.fetchHeaders(cids, blocks)
|
||||
if err != nil {
|
||||
println(1)
|
||||
return nil, err
|
||||
}
|
||||
err = f.fetchUncles(cids, blocks)
|
||||
if err != nil {
|
||||
println(2)
|
||||
return nil, err
|
||||
}
|
||||
err = f.fetchTrxs(cids, blocks)
|
||||
if err != nil {
|
||||
println(3)
|
||||
return nil, err
|
||||
}
|
||||
err = f.fetchRcts(cids, blocks)
|
||||
if err != nil {
|
||||
println(4)
|
||||
return nil, err
|
||||
}
|
||||
err = f.fetchStorage(cids, blocks)
|
||||
if err != nil {
|
||||
println(5)
|
||||
return nil, err
|
||||
}
|
||||
err = f.fetchState(cids, blocks)
|
||||
if err != nil {
|
||||
println(6)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
|
||||
@@ -29,7 +29,6 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
// these need to be actual typed objects so that the cid.Decode works
|
||||
mockHeaderData = []byte{0, 1, 2, 3, 4}
|
||||
mockUncleData = []byte{1, 2, 3, 4, 5}
|
||||
mockTrxData = []byte{2, 3, 4, 5, 6}
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
// VulcanizeDB
|
||||
// Copyright © 2019 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/>.
|
||||
|
||||
package mocks
|
||||
|
||||
import "errors"
|
||||
|
||||
// DagPutter is a mock for testing the ipfs publisher
|
||||
type DagPutter struct {
|
||||
CIDsToReturn []string
|
||||
ErrToReturn error
|
||||
}
|
||||
|
||||
// DagPut returns the pre-loaded CIDs or error
|
||||
func (dp *DagPutter) DagPut(raw interface{}) ([]string, error) {
|
||||
return dp.CIDsToReturn, dp.ErrToReturn
|
||||
}
|
||||
|
||||
// IncrementingDagPutter is a mock for testing the ipfs publisher
|
||||
type IncrementingDagPutter struct {
|
||||
CIDsToReturn []string
|
||||
iterator int
|
||||
ErrToReturn error
|
||||
}
|
||||
|
||||
// DagPut returns the pre-loaded CIDs or error
|
||||
func (dp *IncrementingDagPutter) DagPut(raw interface{}) ([]string, error) {
|
||||
if len(dp.CIDsToReturn) >= dp.iterator+1 {
|
||||
cid := dp.CIDsToReturn[dp.iterator]
|
||||
dp.iterator++
|
||||
return []string{cid}, dp.ErrToReturn
|
||||
}
|
||||
return nil, errors.New("dag putter iterator is out of range")
|
||||
}
|
||||
+64
-39
@@ -183,67 +183,92 @@ var (
|
||||
ReceiptsRlp: []byte{},
|
||||
}
|
||||
|
||||
MockIPLDPayload = ipfs.IPLDPayload{
|
||||
BlockNumber: big.NewInt(1),
|
||||
BlockHash: MockBlock.Hash(),
|
||||
Receipts: MockReceipts,
|
||||
HeaderRLP: MockHeaderRlp,
|
||||
BlockBody: MockBlock.Body(),
|
||||
TrxMetaData: MockTrxMeta,
|
||||
ReceiptMetaData: MockRctMeta,
|
||||
StorageNodes: MockStorageNodes,
|
||||
StateNodes: MockStateNodes,
|
||||
MockIPLDPayload = &ipfs.IPLDPayload{
|
||||
BlockNumber: big.NewInt(1),
|
||||
BlockHash: MockBlock.Hash(),
|
||||
Receipts: MockReceipts,
|
||||
HeaderRLP: MockHeaderRlp,
|
||||
BlockBody: MockBlock.Body(),
|
||||
TrxMetaData: []*ipfs.TrxMetaData{
|
||||
{
|
||||
CID: "",
|
||||
Src: senderAddr.Hex(),
|
||||
Dst: "0x0000000000000000000000000000000000000000",
|
||||
},
|
||||
{
|
||||
CID: "",
|
||||
Src: senderAddr.Hex(),
|
||||
Dst: "0x0000000000000000000000000000000000000001",
|
||||
},
|
||||
},
|
||||
ReceiptMetaData: []*ipfs.ReceiptMetaData{
|
||||
{
|
||||
CID: "",
|
||||
Topic0s: []string{
|
||||
"0x0000000000000000000000000000000000000000000000000000000000000004",
|
||||
},
|
||||
ContractAddress: "0x0000000000000000000000000000000000000000",
|
||||
},
|
||||
{
|
||||
CID: "",
|
||||
Topic0s: []string{
|
||||
"0x0000000000000000000000000000000000000000000000000000000000000005",
|
||||
},
|
||||
ContractAddress: "0x0000000000000000000000000000000000000001",
|
||||
},
|
||||
},
|
||||
StorageNodes: MockStorageNodes,
|
||||
StateNodes: MockStateNodes,
|
||||
}
|
||||
|
||||
MockCIDPayload = ipfs.CIDPayload{
|
||||
MockCIDPayload = &ipfs.CIDPayload{
|
||||
BlockNumber: "1",
|
||||
BlockHash: common.HexToHash("0x0"),
|
||||
BlockHash: MockBlock.Hash(),
|
||||
HeaderCID: "mockHeaderCID",
|
||||
UncleCIDS: make(map[common.Hash]string),
|
||||
TransactionCIDs: map[common.Hash]*ipfs.TrxMetaData{
|
||||
common.HexToHash("0x0"): {
|
||||
MockTransactions[0].Hash(): {
|
||||
CID: "mockTrxCID1",
|
||||
Dst: "mockTo1",
|
||||
Src: "mockFrom1",
|
||||
Dst: "0x0000000000000000000000000000000000000000",
|
||||
Src: senderAddr.Hex(),
|
||||
},
|
||||
common.HexToHash("0x1"): {
|
||||
MockTransactions[1].Hash(): {
|
||||
CID: "mockTrxCID2",
|
||||
Dst: "mockTo2",
|
||||
Src: "mockFrom2",
|
||||
Dst: "0x0000000000000000000000000000000000000001",
|
||||
Src: senderAddr.Hex(),
|
||||
},
|
||||
},
|
||||
ReceiptCIDs: map[common.Hash]*ipfs.ReceiptMetaData{
|
||||
common.HexToHash("0x0"): {
|
||||
CID: "mockReceiptCID1",
|
||||
Topic0s: []string{"mockTopic1"},
|
||||
MockTransactions[0].Hash(): {
|
||||
CID: "mockRctCID1",
|
||||
Topic0s: []string{"0x0000000000000000000000000000000000000000000000000000000000000004"},
|
||||
ContractAddress: "0x0000000000000000000000000000000000000000",
|
||||
},
|
||||
common.HexToHash("0x1"): {
|
||||
CID: "mockReceiptCID2",
|
||||
Topic0s: []string{"mockTopic1", "mockTopic2"},
|
||||
MockTransactions[1].Hash(): {
|
||||
CID: "mockRctCID2",
|
||||
Topic0s: []string{"0x0000000000000000000000000000000000000000000000000000000000000005"},
|
||||
ContractAddress: "0x0000000000000000000000000000000000000001",
|
||||
},
|
||||
},
|
||||
StateNodeCIDs: map[common.Hash]ipfs.StateNodeCID{
|
||||
common.HexToHash("0x0"): {
|
||||
ContractLeafKey: {
|
||||
CID: "mockStateCID1",
|
||||
Leaf: true,
|
||||
Key: "",
|
||||
},
|
||||
common.HexToHash("0x1"): {
|
||||
AnotherContractLeafKey: {
|
||||
CID: "mockStateCID2",
|
||||
Leaf: true,
|
||||
Key: "",
|
||||
},
|
||||
},
|
||||
StorageNodeCIDs: map[common.Hash][]ipfs.StorageNodeCID{
|
||||
common.HexToHash("0x0"): {
|
||||
ContractLeafKey: {
|
||||
{
|
||||
CID: "mockStorageCID1",
|
||||
Key: "0x0",
|
||||
Leaf: true,
|
||||
},
|
||||
},
|
||||
common.HexToHash("0x1"): {
|
||||
{
|
||||
CID: "mockStorageCID2",
|
||||
Key: "0x1",
|
||||
Leaf: true,
|
||||
CID: "mockStorageCID",
|
||||
Key: "0x0000000000000000000000000000000000000000000000000000000000000001",
|
||||
Leaf: true,
|
||||
StateKey: "",
|
||||
},
|
||||
},
|
||||
},
|
||||
@@ -281,7 +306,7 @@ func createTransactionsAndReceipts() (types.Transactions, types.Receipts, common
|
||||
Topics: []common.Hash{mockTopic1},
|
||||
}
|
||||
mockReceipt1.Logs = []*types.Log{mockLog1}
|
||||
mockReceipt1.TxHash = trx1.Hash()
|
||||
mockReceipt1.TxHash = signedTrx1.Hash()
|
||||
mockTopic2 := common.HexToHash("0x05")
|
||||
mockReceipt2 := types.NewReceipt(common.HexToHash("0x1").Bytes(), false, 100)
|
||||
mockReceipt2.ContractAddress = common.HexToAddress("0xaE9BEa628c4Ce503DcFD7E305CaB4e29E7476593")
|
||||
@@ -289,6 +314,6 @@ func createTransactionsAndReceipts() (types.Transactions, types.Receipts, common
|
||||
Topics: []common.Hash{mockTopic2},
|
||||
}
|
||||
mockReceipt2.Logs = []*types.Log{mockLog2}
|
||||
mockReceipt2.TxHash = trx2.Hash()
|
||||
mockReceipt2.TxHash = signedTrx2.Hash()
|
||||
return types.Transactions{signedTrx1, signedTrx2}, types.Receipts{mockReceipt1, mockReceipt2}, senderAddr
|
||||
}
|
||||
|
||||
@@ -40,11 +40,11 @@ type IPLDPublisher interface {
|
||||
|
||||
// Publisher is the underlying struct for the IPLDPublisher interface
|
||||
type Publisher struct {
|
||||
HeaderPutter *eth_block_header.BlockHeaderDagPutter
|
||||
TransactionPutter *eth_block_transactions.BlockTransactionsDagPutter
|
||||
ReceiptPutter *eth_block_receipts.EthBlockReceiptDagPutter
|
||||
StatePutter *eth_state_trie.StateTrieDagPutter
|
||||
StoragePutter *eth_storage_trie.StorageTrieDagPutter
|
||||
HeaderPutter ipfs.DagPutter
|
||||
TransactionPutter ipfs.DagPutter
|
||||
ReceiptPutter ipfs.DagPutter
|
||||
StatePutter ipfs.DagPutter
|
||||
StoragePutter ipfs.DagPutter
|
||||
}
|
||||
|
||||
// NewIPLDPublisher creates a pointer to a new Publisher which satisfies the IPLDPublisher interface
|
||||
|
||||
@@ -0,0 +1,62 @@
|
||||
// VulcanizeDB
|
||||
// Copyright © 2019 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/>.
|
||||
|
||||
package ipfs_test
|
||||
|
||||
import (
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
|
||||
"github.com/vulcanize/vulcanizedb/pkg/ipfs"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/ipfs/mocks"
|
||||
)
|
||||
|
||||
var (
|
||||
mockHeaderDagPutter *mocks.DagPutter
|
||||
mockTrxDagPutter *mocks.DagPutter
|
||||
mockRctDagPutter *mocks.DagPutter
|
||||
mockStateDagPutter *mocks.IncrementingDagPutter
|
||||
mockStorageDagPutter *mocks.IncrementingDagPutter
|
||||
)
|
||||
|
||||
var _ = Describe("Publisher", func() {
|
||||
BeforeEach(func() {
|
||||
mockHeaderDagPutter = new(mocks.DagPutter)
|
||||
mockTrxDagPutter = new(mocks.DagPutter)
|
||||
mockRctDagPutter = new(mocks.DagPutter)
|
||||
mockStateDagPutter = new(mocks.IncrementingDagPutter)
|
||||
mockStorageDagPutter = new(mocks.IncrementingDagPutter)
|
||||
})
|
||||
Describe("Publish", func() {
|
||||
It("Publishes the passed IPLDPayload objects to IPFS and returns a CIDPayload for indexing", func() {
|
||||
mockHeaderDagPutter.CIDsToReturn = []string{"mockHeaderCID"}
|
||||
mockTrxDagPutter.CIDsToReturn = []string{"mockTrxCID1", "mockTrxCID2"}
|
||||
mockRctDagPutter.CIDsToReturn = []string{"mockRctCID1", "mockRctCID2"}
|
||||
mockStateDagPutter.CIDsToReturn = []string{"mockStateCID1", "mockStateCID2"}
|
||||
mockStorageDagPutter.CIDsToReturn = []string{"mockStorageCID"}
|
||||
publisher := ipfs.Publisher{
|
||||
HeaderPutter: mockHeaderDagPutter,
|
||||
TransactionPutter: mockTrxDagPutter,
|
||||
ReceiptPutter: mockRctDagPutter,
|
||||
StatePutter: mockStateDagPutter,
|
||||
StoragePutter: mockStorageDagPutter,
|
||||
}
|
||||
cidPayload, err := publisher.Publish(mocks.MockIPLDPayload)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(*cidPayload).To(Equal(*mocks.MockCIDPayload))
|
||||
})
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user