2019-08-23 21:34:54 +00:00
|
|
|
// 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/>.
|
|
|
|
|
2020-01-17 23:16:01 +00:00
|
|
|
package eth_test
|
2019-08-23 21:34:54 +00:00
|
|
|
|
|
|
|
import (
|
2020-02-20 22:12:52 +00:00
|
|
|
"bytes"
|
2019-08-23 21:34:54 +00:00
|
|
|
"math/big"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2020-03-11 18:41:59 +00:00
|
|
|
"github.com/ethereum/go-ethereum/statediff"
|
2019-08-23 21:34:54 +00:00
|
|
|
"github.com/ipfs/go-block-format"
|
|
|
|
. "github.com/onsi/ginkgo"
|
|
|
|
. "github.com/onsi/gomega"
|
|
|
|
|
2020-03-11 18:41:59 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/ipfs"
|
2020-02-03 18:22:29 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/ipfs/mocks"
|
2020-01-17 23:16:01 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/super_node/eth"
|
2019-08-23 21:34:54 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
mockHeaderData = []byte{0, 1, 2, 3, 4}
|
|
|
|
mockUncleData = []byte{1, 2, 3, 4, 5}
|
|
|
|
mockTrxData = []byte{2, 3, 4, 5, 6}
|
|
|
|
mockReceiptData = []byte{3, 4, 5, 6, 7}
|
|
|
|
mockStateData = []byte{4, 5, 6, 7, 8}
|
|
|
|
mockStorageData = []byte{5, 6, 7, 8, 9}
|
|
|
|
mockStorageData2 = []byte{6, 7, 8, 9, 1}
|
|
|
|
mockHeaderBlock = blocks.NewBlock(mockHeaderData)
|
|
|
|
mockUncleBlock = blocks.NewBlock(mockUncleData)
|
|
|
|
mockTrxBlock = blocks.NewBlock(mockTrxData)
|
|
|
|
mockReceiptBlock = blocks.NewBlock(mockReceiptData)
|
|
|
|
mockStateBlock = blocks.NewBlock(mockStateData)
|
|
|
|
mockStorageBlock1 = blocks.NewBlock(mockStorageData)
|
|
|
|
mockStorageBlock2 = blocks.NewBlock(mockStorageData2)
|
|
|
|
mockBlocks = []blocks.Block{mockHeaderBlock, mockUncleBlock, mockTrxBlock, mockReceiptBlock, mockStateBlock, mockStorageBlock1, mockStorageBlock2}
|
|
|
|
mockBlockService *mocks.MockIPFSBlockService
|
2020-01-17 23:16:01 +00:00
|
|
|
mockCIDWrapper = ð.CIDWrapper{
|
|
|
|
BlockNumber: big.NewInt(9000),
|
2020-02-23 23:14:29 +00:00
|
|
|
Header: eth.HeaderModel{
|
2020-02-27 21:07:33 +00:00
|
|
|
TotalDifficulty: "1337",
|
|
|
|
CID: mockHeaderBlock.Cid().String(),
|
2020-01-17 23:16:01 +00:00
|
|
|
},
|
2020-01-26 19:55:26 +00:00
|
|
|
Uncles: []eth.UncleModel{
|
2020-01-17 23:16:01 +00:00
|
|
|
{
|
|
|
|
CID: mockUncleBlock.Cid().String(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Transactions: []eth.TxModel{
|
|
|
|
{
|
|
|
|
CID: mockTrxBlock.Cid().String(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Receipts: []eth.ReceiptModel{
|
|
|
|
{
|
|
|
|
CID: mockReceiptBlock.Cid().String(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
StateNodes: []eth.StateNodeModel{{
|
|
|
|
CID: mockStateBlock.Cid().String(),
|
2020-03-11 18:41:59 +00:00
|
|
|
NodeType: 2,
|
2019-08-23 21:34:54 +00:00
|
|
|
StateKey: "0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470",
|
2020-01-17 23:16:01 +00:00
|
|
|
}},
|
|
|
|
StorageNodes: []eth.StorageNodeWithStateKeyModel{{
|
|
|
|
CID: mockStorageBlock1.Cid().String(),
|
2020-03-11 18:41:59 +00:00
|
|
|
NodeType: 2,
|
2020-01-17 23:16:01 +00:00
|
|
|
StateKey: "0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470",
|
|
|
|
StorageKey: "0000000000000000000000000000000000000000000000000000000000000001",
|
2019-08-23 21:34:54 +00:00
|
|
|
},
|
|
|
|
{
|
2020-01-17 23:16:01 +00:00
|
|
|
CID: mockStorageBlock2.Cid().String(),
|
2020-03-11 18:41:59 +00:00
|
|
|
NodeType: 2,
|
2020-01-17 23:16:01 +00:00
|
|
|
StateKey: "0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470",
|
|
|
|
StorageKey: "0000000000000000000000000000000000000000000000000000000000000002",
|
2019-08-23 21:34:54 +00:00
|
|
|
}},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2020-04-30 21:26:32 +00:00
|
|
|
var _ = Describe("IPLDFetcher", func() {
|
|
|
|
Describe("Fetch", func() {
|
2019-08-23 21:34:54 +00:00
|
|
|
BeforeEach(func() {
|
|
|
|
mockBlockService = new(mocks.MockIPFSBlockService)
|
|
|
|
err := mockBlockService.AddBlocks(mockBlocks)
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
Expect(len(mockBlockService.Blocks)).To(Equal(7))
|
|
|
|
})
|
|
|
|
|
|
|
|
It("Fetches and returns IPLDs for the CIDs provided in the CIDWrapper", func() {
|
2020-01-17 23:16:01 +00:00
|
|
|
fetcher := new(eth.IPLDFetcher)
|
2019-08-23 21:34:54 +00:00
|
|
|
fetcher.BlockService = mockBlockService
|
2020-01-17 23:16:01 +00:00
|
|
|
i, err := fetcher.Fetch(mockCIDWrapper)
|
2019-08-23 21:34:54 +00:00
|
|
|
Expect(err).ToNot(HaveOccurred())
|
2020-02-20 22:12:52 +00:00
|
|
|
iplds, ok := i.(eth.IPLDs)
|
2020-01-17 23:16:01 +00:00
|
|
|
Expect(ok).To(BeTrue())
|
2020-02-27 21:07:33 +00:00
|
|
|
Expect(iplds.TotalDifficulty).To(Equal(big.NewInt(1337)))
|
2019-08-23 21:34:54 +00:00
|
|
|
Expect(iplds.BlockNumber).To(Equal(mockCIDWrapper.BlockNumber))
|
2020-02-23 23:14:29 +00:00
|
|
|
Expect(iplds.Header).To(Equal(ipfs.BlockModel{
|
2020-02-20 22:13:19 +00:00
|
|
|
Data: mockHeaderBlock.RawData(),
|
|
|
|
CID: mockHeaderBlock.Cid().String(),
|
|
|
|
}))
|
2019-08-23 21:34:54 +00:00
|
|
|
Expect(len(iplds.Uncles)).To(Equal(1))
|
2020-02-20 22:13:19 +00:00
|
|
|
Expect(iplds.Uncles[0]).To(Equal(ipfs.BlockModel{
|
|
|
|
Data: mockUncleBlock.RawData(),
|
|
|
|
CID: mockUncleBlock.Cid().String(),
|
|
|
|
}))
|
2019-08-23 21:34:54 +00:00
|
|
|
Expect(len(iplds.Transactions)).To(Equal(1))
|
2020-02-20 22:13:19 +00:00
|
|
|
Expect(iplds.Transactions[0]).To(Equal(ipfs.BlockModel{
|
|
|
|
Data: mockTrxBlock.RawData(),
|
|
|
|
CID: mockTrxBlock.Cid().String(),
|
|
|
|
}))
|
2019-08-23 21:34:54 +00:00
|
|
|
Expect(len(iplds.Receipts)).To(Equal(1))
|
2020-02-20 22:13:19 +00:00
|
|
|
Expect(iplds.Receipts[0]).To(Equal(ipfs.BlockModel{
|
|
|
|
Data: mockReceiptBlock.RawData(),
|
|
|
|
CID: mockReceiptBlock.Cid().String(),
|
|
|
|
}))
|
2019-08-23 21:34:54 +00:00
|
|
|
Expect(len(iplds.StateNodes)).To(Equal(1))
|
2020-03-11 18:41:59 +00:00
|
|
|
Expect(iplds.StateNodes[0].StateLeafKey).To(Equal(common.HexToHash("0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470")))
|
|
|
|
Expect(iplds.StateNodes[0].Type).To(Equal(statediff.Leaf))
|
2020-02-20 22:13:19 +00:00
|
|
|
Expect(iplds.StateNodes[0].IPLD).To(Equal(ipfs.BlockModel{
|
|
|
|
Data: mockStateBlock.RawData(),
|
|
|
|
CID: mockStateBlock.Cid().String(),
|
|
|
|
}))
|
2020-02-20 22:12:52 +00:00
|
|
|
Expect(len(iplds.StorageNodes)).To(Equal(2))
|
|
|
|
for _, storage := range iplds.StorageNodes {
|
2020-03-11 18:41:59 +00:00
|
|
|
Expect(storage.Type).To(Equal(statediff.Leaf))
|
|
|
|
Expect(storage.StateLeafKey).To(Equal(common.HexToHash("0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470")))
|
|
|
|
if bytes.Equal(storage.StorageLeafKey.Bytes(), common.HexToHash("0000000000000000000000000000000000000000000000000000000000000001").Bytes()) {
|
2020-02-20 22:13:19 +00:00
|
|
|
Expect(storage.IPLD).To(Equal(ipfs.BlockModel{
|
|
|
|
Data: mockStorageBlock1.RawData(),
|
|
|
|
CID: mockStorageBlock1.Cid().String(),
|
|
|
|
}))
|
2020-02-20 22:12:52 +00:00
|
|
|
}
|
2020-03-11 18:41:59 +00:00
|
|
|
if bytes.Equal(storage.StorageLeafKey.Bytes(), common.HexToHash("0000000000000000000000000000000000000000000000000000000000000002").Bytes()) {
|
2020-02-20 22:13:19 +00:00
|
|
|
Expect(storage.IPLD).To(Equal(ipfs.BlockModel{
|
|
|
|
Data: mockStorageBlock2.RawData(),
|
|
|
|
CID: mockStorageBlock2.Cid().String(),
|
|
|
|
}))
|
2020-02-20 22:12:52 +00:00
|
|
|
}
|
|
|
|
}
|
2019-08-23 21:34:54 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|