forked from cerc-io/ipld-eth-server
update to work with updated state diffing code
This commit is contained in:
+38
-14
@@ -63,8 +63,8 @@ func (pc *Converter) Convert(payload statediff.Payload) (*IPLDPayload, error) {
|
||||
TrxMetaData: make([]*TrxMetaData, 0, trxLen),
|
||||
Receipts: make(types.Receipts, 0, trxLen),
|
||||
ReceiptMetaData: make([]*ReceiptMetaData, 0, trxLen),
|
||||
StateLeafs: make(map[common.Hash][]byte),
|
||||
StorageLeafs: make(map[common.Hash]map[common.Hash][]byte),
|
||||
StateNodes: make(map[common.Hash]StateNode),
|
||||
StorageNodes: make(map[common.Hash][]StorageNode),
|
||||
}
|
||||
for gethTransactionIndex, trx := range block.Transactions() {
|
||||
// Extract to and from data from the the transactions for indexing
|
||||
@@ -105,25 +105,49 @@ func (pc *Converter) Convert(payload statediff.Payload) (*IPLDPayload, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for addr, createdAccount := range stateDiff.CreatedAccounts {
|
||||
convertedPayload.StateLeafs[addr] = createdAccount.Value
|
||||
convertedPayload.StorageLeafs[addr] = make(map[common.Hash][]byte)
|
||||
for _, createdAccount := range stateDiff.CreatedAccounts {
|
||||
hashKey := common.BytesToHash(createdAccount.Key)
|
||||
convertedPayload.StateNodes[hashKey] = StateNode{
|
||||
Value: createdAccount.Value,
|
||||
Leaf: createdAccount.Leaf,
|
||||
}
|
||||
convertedPayload.StorageNodes[hashKey] = make([]StorageNode, 0)
|
||||
for _, storageDiff := range createdAccount.Storage {
|
||||
convertedPayload.StorageLeafs[addr][common.BytesToHash(storageDiff.Key)] = storageDiff.Value
|
||||
convertedPayload.StorageNodes[hashKey] = append(convertedPayload.StorageNodes[hashKey], StorageNode{
|
||||
Key: common.BytesToHash(storageDiff.Key),
|
||||
Value: storageDiff.Value,
|
||||
Leaf: storageDiff.Leaf,
|
||||
})
|
||||
}
|
||||
}
|
||||
for addr, deletedAccount := range stateDiff.DeletedAccounts {
|
||||
convertedPayload.StateLeafs[addr] = deletedAccount.Value
|
||||
convertedPayload.StorageLeafs[addr] = make(map[common.Hash][]byte)
|
||||
for _, deletedAccount := range stateDiff.DeletedAccounts {
|
||||
hashKey := common.BytesToHash(deletedAccount.Key)
|
||||
convertedPayload.StateNodes[hashKey] = StateNode{
|
||||
Value: deletedAccount.Value,
|
||||
Leaf: deletedAccount.Leaf,
|
||||
}
|
||||
convertedPayload.StorageNodes[hashKey] = make([]StorageNode, 0)
|
||||
for _, storageDiff := range deletedAccount.Storage {
|
||||
convertedPayload.StorageLeafs[addr][common.BytesToHash(storageDiff.Key)] = storageDiff.Value
|
||||
convertedPayload.StorageNodes[hashKey] = append(convertedPayload.StorageNodes[hashKey], StorageNode{
|
||||
Key: common.BytesToHash(storageDiff.Key),
|
||||
Value: storageDiff.Value,
|
||||
Leaf: storageDiff.Leaf,
|
||||
})
|
||||
}
|
||||
}
|
||||
for addr, updatedAccount := range stateDiff.UpdatedAccounts {
|
||||
convertedPayload.StateLeafs[addr] = updatedAccount.Value
|
||||
convertedPayload.StorageLeafs[addr] = make(map[common.Hash][]byte)
|
||||
for _, updatedAccount := range stateDiff.UpdatedAccounts {
|
||||
hashKey := common.BytesToHash(updatedAccount.Key)
|
||||
convertedPayload.StateNodes[hashKey] = StateNode{
|
||||
Value: updatedAccount.Value,
|
||||
Leaf: updatedAccount.Leaf,
|
||||
}
|
||||
convertedPayload.StorageNodes[hashKey] = make([]StorageNode, 0)
|
||||
for _, storageDiff := range updatedAccount.Storage {
|
||||
convertedPayload.StorageLeafs[addr][common.BytesToHash(storageDiff.Key)] = storageDiff.Value
|
||||
convertedPayload.StorageNodes[hashKey] = append(convertedPayload.StorageNodes[hashKey], StorageNode{
|
||||
Key: common.BytesToHash(storageDiff.Key),
|
||||
Value: storageDiff.Value,
|
||||
Leaf: storageDiff.Leaf,
|
||||
})
|
||||
}
|
||||
}
|
||||
return convertedPayload, nil
|
||||
|
||||
@@ -34,9 +34,5 @@ var _ = Describe("Converter", func() {
|
||||
Expect(ipldPayload).To(Equal(&test_helpers.MockIPLDPayload))
|
||||
Expect(mockConverter.PassedStatediffPayload).To(Equal(test_helpers.MockStatediffPayload))
|
||||
})
|
||||
|
||||
It("Fails if", func() {
|
||||
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
+8
-14
@@ -24,37 +24,31 @@ import (
|
||||
"github.com/ipfs/go-cid"
|
||||
)
|
||||
|
||||
// IPLDFetcher is the interface for fetching IPLD objects from IPFS
|
||||
type IPLDFetcher interface {
|
||||
Fetch(cid cid.Cid) (blocks.Block, error)
|
||||
FetchBatch(cids []cid.Cid) []blocks.Block
|
||||
}
|
||||
|
||||
// Fetcher is the underlying struct which supports the IPLDFetcher interface
|
||||
type Fetcher struct {
|
||||
// IPLDFetcher is the underlying struct which supports a IPLD fetching interface
|
||||
type IPLDFetcher struct {
|
||||
BlockService blockservice.BlockService
|
||||
}
|
||||
|
||||
// NewIPLDFetcher creates a pointer to a new Fetcher which satisfies the IPLDFetcher interface
|
||||
func NewIPLDFetcher(ipfsPath string) (*Fetcher, error) {
|
||||
// NewIPLDFetcher creates a pointer to a new IPLDFetcher
|
||||
func NewIPLDFetcher(ipfsPath string) (*IPLDFetcher, error) {
|
||||
blockService, err := InitIPFSBlockService(ipfsPath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &Fetcher{
|
||||
return &IPLDFetcher{
|
||||
BlockService: blockService,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Fetch is used to fetch a batch of IPFS data blocks by cid
|
||||
func (f *Fetcher) Fetch(cid cid.Cid) (blocks.Block, error) {
|
||||
// Fetch is used to fetch a single block of IPFS data by cid
|
||||
func (f *IPLDFetcher) Fetch(cid cid.Cid) (blocks.Block, error) {
|
||||
return f.BlockService.GetBlock(context.Background(), cid)
|
||||
}
|
||||
|
||||
// FetchBatch is used to fetch a batch of IPFS data blocks by cid
|
||||
// There is no guarantee all are fetched, and no error in such a case, so
|
||||
// downstream we will need to confirm which CIDs were fetched in the result set
|
||||
func (f *Fetcher) FetchBatch(cids []cid.Cid) []blocks.Block {
|
||||
func (f *IPLDFetcher) FetchBatch(cids []cid.Cid) []blocks.Block {
|
||||
fetchedBlocks := make([]blocks.Block, 0, len(cids))
|
||||
blockChan := f.BlockService.GetBlocks(context.Background(), cids)
|
||||
for block := range blockChan {
|
||||
|
||||
@@ -17,10 +17,11 @@
|
||||
package ipfs_test
|
||||
|
||||
import (
|
||||
"github.com/sirupsen/logrus"
|
||||
"io/ioutil"
|
||||
"testing"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
)
|
||||
|
||||
@@ -72,8 +72,8 @@ func (i *Processor) Process(wg *sync.WaitGroup) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
wg.Add(1)
|
||||
for {
|
||||
select {
|
||||
case payload := <-i.PayloadChan:
|
||||
@@ -98,6 +98,7 @@ func (i *Processor) Process(wg *sync.WaitGroup) error {
|
||||
case err = <-sub.Err():
|
||||
log.Error(err)
|
||||
case <-i.QuitChan:
|
||||
println("quiting")
|
||||
log.Info("quiting IPFSProcessor")
|
||||
wg.Done()
|
||||
return
|
||||
|
||||
+32
-24
@@ -18,6 +18,7 @@ package ipfs_test
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/ethereum/go-ethereum/rpc"
|
||||
"github.com/ethereum/go-ethereum/statediff"
|
||||
@@ -34,36 +35,43 @@ var _ = Describe("Processor", func() {
|
||||
Describe("Process", func() {
|
||||
It("Streams StatediffPayloads, converts them to IPLDPayloads, publishes IPLDPayloads, and indexes CIDPayloads", func() {
|
||||
wg := new(sync.WaitGroup)
|
||||
payloadChan := make(chan statediff.Payload, 800)
|
||||
processor := ipfs.Processor{
|
||||
Repository: &mocks.CIDRepository{
|
||||
ReturnErr: nil,
|
||||
},
|
||||
Publisher: &mocks.IPLDPublisher{
|
||||
ReturnCIDPayload: &test_helpers.MockCIDPayload,
|
||||
ReturnErr: nil,
|
||||
},
|
||||
Streamer: &mocks.StateDiffStreamer{
|
||||
ReturnSub: &rpc.ClientSubscription{},
|
||||
StreamPayloads: []statediff.Payload{
|
||||
test_helpers.MockStatediffPayload,
|
||||
},
|
||||
ReturnErr: nil,
|
||||
WaitGroup: wg,
|
||||
},
|
||||
Converter: &mocks.PayloadConverter{
|
||||
ReturnIPLDPayload: &test_helpers.MockIPLDPayload,
|
||||
ReturnErr: nil,
|
||||
payloadChan := make(chan statediff.Payload, 1)
|
||||
quitChan := make(chan bool, 1)
|
||||
mockCidRepo := &mocks.CIDRepository{
|
||||
ReturnErr: nil,
|
||||
}
|
||||
mockPublisher := &mocks.IPLDPublisher{
|
||||
ReturnCIDPayload: &test_helpers.MockCIDPayload,
|
||||
ReturnErr: nil,
|
||||
}
|
||||
mockStreamer := &mocks.StateDiffStreamer{
|
||||
ReturnSub: &rpc.ClientSubscription{},
|
||||
StreamPayloads: []statediff.Payload{
|
||||
test_helpers.MockStatediffPayload,
|
||||
},
|
||||
ReturnErr: nil,
|
||||
}
|
||||
mockConverter := &mocks.PayloadConverter{
|
||||
ReturnIPLDPayload: &test_helpers.MockIPLDPayload,
|
||||
ReturnErr: nil,
|
||||
}
|
||||
processor := &ipfs.Processor{
|
||||
Repository: mockCidRepo,
|
||||
Publisher: mockPublisher,
|
||||
Streamer: mockStreamer,
|
||||
Converter: mockConverter,
|
||||
PayloadChan: payloadChan,
|
||||
QuitChan: quitChan,
|
||||
}
|
||||
err := processor.Process(wg)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
time.Sleep(2 * time.Second)
|
||||
quitChan <- true
|
||||
wg.Wait()
|
||||
})
|
||||
|
||||
It("Fails if", func() {
|
||||
|
||||
Expect(mockConverter.PassedStatediffPayload).To(Equal(test_helpers.MockStatediffPayload))
|
||||
Expect(mockCidRepo.PassedCIDPayload).To(Equal(&test_helpers.MockCIDPayload))
|
||||
Expect(mockPublisher.PassedIPLDPayload).To(Equal(&test_helpers.MockIPLDPayload))
|
||||
Expect(mockStreamer.PassedPayloadChan).To(Equal(payloadChan))
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
+27
-20
@@ -98,27 +98,27 @@ func (pub *Publisher) Publish(payload *IPLDPayload) (*CIDPayload, error) {
|
||||
}
|
||||
|
||||
// Process and publish state leafs
|
||||
stateLeafCids, err := pub.publishStateLeafs(payload.StateLeafs)
|
||||
stateLeafCids, err := pub.publishStateNodes(payload.StateNodes)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Process and publish storage leafs
|
||||
storageLeafCids, err := pub.publishStorageLeafs(payload.StorageLeafs)
|
||||
storageLeafCids, err := pub.publishStorageNodes(payload.StorageNodes)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Package CIDs into a single struct
|
||||
return &CIDPayload{
|
||||
BlockHash: payload.BlockHash.Hex(),
|
||||
BlockHash: payload.BlockHash,
|
||||
BlockNumber: payload.BlockNumber.String(),
|
||||
HeaderCID: headerCid,
|
||||
UncleCIDS: uncleCids,
|
||||
TransactionCIDs: transactionCids,
|
||||
ReceiptCIDs: receiptsCids,
|
||||
StateLeafCIDs: stateLeafCids,
|
||||
StorageLeafCIDs: storageLeafCids,
|
||||
StateNodeCIDs: stateLeafCids,
|
||||
StorageNodeCIDs: storageLeafCids,
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -165,34 +165,41 @@ func (pub *Publisher) publishReceipts(receipts types.Receipts, receiptMeta []*Re
|
||||
return mappedRctCids, nil
|
||||
}
|
||||
|
||||
func (pub *Publisher) publishStateLeafs(stateLeafs map[common.Hash][]byte) (map[common.Hash]string, error) {
|
||||
stateLeafCids := make(map[common.Hash]string)
|
||||
for addr, leaf := range stateLeafs {
|
||||
stateLeafCid, err := pub.StatePutter.DagPut(leaf)
|
||||
func (pub *Publisher) publishStateNodes(stateNodes map[common.Hash]StateNode) (map[common.Hash]StateNodeCID, error) {
|
||||
stateNodeCids := make(map[common.Hash]StateNodeCID)
|
||||
for addr, node := range stateNodes {
|
||||
stateNodeCid, err := pub.StatePutter.DagPut(node.Value)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(stateLeafCid) != 1 {
|
||||
if len(stateNodeCid) != 1 {
|
||||
return nil, errors.New("single CID expected to be returned for state leaf")
|
||||
}
|
||||
stateLeafCids[addr] = stateLeafCid[0]
|
||||
stateNodeCids[addr] = StateNodeCID{
|
||||
CID: stateNodeCid[0],
|
||||
Leaf: node.Leaf,
|
||||
}
|
||||
}
|
||||
return stateLeafCids, nil
|
||||
return stateNodeCids, nil
|
||||
}
|
||||
|
||||
func (pub *Publisher) publishStorageLeafs(storageLeafs map[common.Hash]map[common.Hash][]byte) (map[common.Hash]map[common.Hash]string, error) {
|
||||
storageLeafCids := make(map[common.Hash]map[common.Hash]string)
|
||||
for addr, storageTrie := range storageLeafs {
|
||||
storageLeafCids[addr] = make(map[common.Hash]string)
|
||||
for key, leaf := range storageTrie {
|
||||
storageLeafCid, err := pub.StoragePutter.DagPut(leaf)
|
||||
func (pub *Publisher) publishStorageNodes(storageNodes map[common.Hash][]StorageNode) (map[common.Hash][]StorageNodeCID, error) {
|
||||
storageLeafCids := make(map[common.Hash][]StorageNodeCID)
|
||||
for addr, storageTrie := range storageNodes {
|
||||
storageLeafCids[addr] = make([]StorageNodeCID, 0)
|
||||
for _, node := range storageTrie {
|
||||
storageNodeCid, err := pub.StoragePutter.DagPut(node.Value)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(storageLeafCid) != 1 {
|
||||
if len(storageNodeCid) != 1 {
|
||||
return nil, errors.New("single CID expected to be returned for storage leaf")
|
||||
}
|
||||
storageLeafCids[addr][key] = storageLeafCid[0]
|
||||
storageLeafCids[addr] = append(storageLeafCids[addr], StorageNodeCID{
|
||||
Key: node.Key,
|
||||
CID: storageNodeCid[0],
|
||||
Leaf: node.Leaf,
|
||||
})
|
||||
}
|
||||
}
|
||||
return storageLeafCids, nil
|
||||
|
||||
@@ -34,9 +34,5 @@ var _ = Describe("Publisher", func() {
|
||||
Expect(cidPayload).To(Equal(&test_helpers.MockCIDPayload))
|
||||
Expect(mockPublisher.PassedIPLDPayload).To(Equal(&test_helpers.MockIPLDPayload))
|
||||
})
|
||||
|
||||
It("Fails if", func() {
|
||||
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
+24
-13
@@ -43,11 +43,14 @@ func NewCIDRepository(db *postgres.DB) *Repository {
|
||||
// Index indexes a cidPayload in Postgres
|
||||
func (repo *Repository) Index(cidPayload *CIDPayload) error {
|
||||
tx, _ := repo.db.Beginx()
|
||||
headerID, err := repo.indexHeaderCID(tx, cidPayload.HeaderCID, cidPayload.BlockNumber, cidPayload.BlockHash)
|
||||
headerID, err := repo.indexHeaderCID(tx, cidPayload.HeaderCID, cidPayload.BlockNumber, cidPayload.BlockHash.Hex())
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
return err
|
||||
}
|
||||
for uncleHash, cid := range cidPayload.UncleCIDS {
|
||||
err = repo.indexUncleCID(tx, cid, cidPayload.BlockNumber, uncleHash.Hex())
|
||||
}
|
||||
err = repo.indexTransactionAndReceiptCIDs(tx, cidPayload, headerID)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
@@ -63,13 +66,20 @@ func (repo *Repository) Index(cidPayload *CIDPayload) error {
|
||||
|
||||
func (repo *Repository) indexHeaderCID(tx *sqlx.Tx, cid, blockNumber, hash string) (int64, error) {
|
||||
var headerID int64
|
||||
err := tx.QueryRowx(`INSERT INTO public.header_cids (block_number, block_hash, cid) VALUES ($1, $2, $3)
|
||||
ON CONFLICT DO UPDATE SET cid = $3
|
||||
err := tx.QueryRowx(`INSERT INTO public.header_cids (block_number, block_hash, cid, uncle) VALUES ($1, $2, $3, $4)
|
||||
ON CONFLICT DO UPDATE SET (cid, uncle) = ($3, $4)
|
||||
RETURNING id`,
|
||||
blockNumber, hash, cid).Scan(&headerID)
|
||||
blockNumber, hash, cid, false).Scan(&headerID)
|
||||
return headerID, err
|
||||
}
|
||||
|
||||
func (repo *Repository) indexUncleCID(tx *sqlx.Tx, cid, blockNumber, hash string) error {
|
||||
_, err := tx.Queryx(`INSERT INTO public.header_cids (block_number, block_hash, cid, uncle) VALUES ($1, $2, $3, $4)
|
||||
ON CONFLICT DO UPDATE SET (cid, uncle) = ($3, $4)`,
|
||||
blockNumber, hash, cid, true)
|
||||
return err
|
||||
}
|
||||
|
||||
func (repo *Repository) indexTransactionAndReceiptCIDs(tx *sqlx.Tx, payload *CIDPayload, headerID int64) error {
|
||||
for hash, trxCidMeta := range payload.TransactionCIDs {
|
||||
var txID int64
|
||||
@@ -98,17 +108,17 @@ func (repo *Repository) indexReceiptCID(tx *sqlx.Tx, cidMeta *ReceiptMetaData, t
|
||||
}
|
||||
|
||||
func (repo *Repository) indexStateAndStorageCIDs(tx *sqlx.Tx, payload *CIDPayload, headerID int64) error {
|
||||
for accountKey, stateCID := range payload.StateLeafCIDs {
|
||||
for accountKey, stateCID := range payload.StateNodeCIDs {
|
||||
var stateID int64
|
||||
err := tx.QueryRowx(`INSERT INTO public.state_cids (header_id, account_key, cid) VALUES ($1, $2, $3)
|
||||
ON CONFLICT DO UPDATE SET cid = $3
|
||||
err := tx.QueryRowx(`INSERT INTO public.state_cids (header_id, state_key, cid, leaf) VALUES ($1, $2, $3, $4)
|
||||
ON CONFLICT DO UPDATE SET (cid, leaf) = ($3, $4)
|
||||
RETURNING id`,
|
||||
headerID, accountKey.Hex(), stateCID).Scan(&stateID)
|
||||
headerID, accountKey.Hex(), stateCID.CID, stateCID.Leaf).Scan(&stateID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for storageKey, storageCID := range payload.StorageLeafCIDs[accountKey] {
|
||||
err = repo.indexStorageCID(tx, storageKey.Hex(), storageCID, stateID)
|
||||
for _, storageCID := range payload.StorageNodeCIDs[accountKey] {
|
||||
err = repo.indexStorageCID(tx, storageCID, stateID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -117,8 +127,9 @@ func (repo *Repository) indexStateAndStorageCIDs(tx *sqlx.Tx, payload *CIDPayloa
|
||||
return nil
|
||||
}
|
||||
|
||||
func (repo *Repository) indexStorageCID(tx *sqlx.Tx, key, cid string, stateID int64) error {
|
||||
_, err := repo.db.Exec(`INSERT INTO public.storage_cids (state_id, storage_key, cid) VALUES ($1, $2, $3)
|
||||
ON CONFLICT DO UPDATE SET cid = $3`, stateID, key, cid)
|
||||
func (repo *Repository) indexStorageCID(tx *sqlx.Tx, storageCID StorageNodeCID, stateID int64) error {
|
||||
_, err := repo.db.Exec(`INSERT INTO public.storage_cids (state_id, storage_key, cid, leaf) VALUES ($1, $2, $3, $4)
|
||||
ON CONFLICT DO UPDATE SET (cid, leaf) = ($3, $4)`,
|
||||
stateID, storageCID.Key, storageCID.CID, storageCID.Leaf)
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -32,9 +32,5 @@ var _ = Describe("Repository", func() {
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(mockRepo.PassedCIDPayload).To(Equal(&test_helpers.MockCIDPayload))
|
||||
})
|
||||
|
||||
It("Fails if", func() {
|
||||
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -17,8 +17,6 @@
|
||||
package ipfs_test
|
||||
|
||||
import (
|
||||
"sync"
|
||||
|
||||
"github.com/ethereum/go-ethereum/rpc"
|
||||
"github.com/ethereum/go-ethereum/statediff"
|
||||
. "github.com/onsi/ginkgo"
|
||||
@@ -31,25 +29,18 @@ import (
|
||||
var _ = Describe("Streamer", func() {
|
||||
Describe("Stream", func() {
|
||||
It("Streams StatediffPayloads from a Geth RPC subscription", func() {
|
||||
wg := new(sync.WaitGroup)
|
||||
mockStreamer := mocks.StateDiffStreamer{}
|
||||
mockStreamer.ReturnSub = &rpc.ClientSubscription{}
|
||||
mockStreamer.WaitGroup = wg
|
||||
mockStreamer.StreamPayloads = []statediff.Payload{
|
||||
test_helpers.MockStatediffPayload,
|
||||
}
|
||||
payloadChan := make(chan statediff.Payload, 1)
|
||||
sub, err := mockStreamer.Stream(payloadChan)
|
||||
wg.Wait()
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(sub).To(Equal(&rpc.ClientSubscription{}))
|
||||
Expect(mockStreamer.PassedPayloadChan).To(Equal(payloadChan))
|
||||
streamedPayload := <-payloadChan
|
||||
Expect(streamedPayload).To(Equal(test_helpers.MockStatediffPayload))
|
||||
})
|
||||
|
||||
It("Fails if", func() {
|
||||
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -17,8 +17,6 @@
|
||||
package mocks
|
||||
|
||||
import (
|
||||
"sync"
|
||||
|
||||
"github.com/ethereum/go-ethereum/rpc"
|
||||
"github.com/ethereum/go-ethereum/statediff"
|
||||
)
|
||||
@@ -29,7 +27,6 @@ type StateDiffStreamer struct {
|
||||
ReturnSub *rpc.ClientSubscription
|
||||
ReturnErr error
|
||||
StreamPayloads []statediff.Payload
|
||||
WaitGroup *sync.WaitGroup
|
||||
}
|
||||
|
||||
// Stream is the main loop for subscribing to data from the Geth state diff process
|
||||
@@ -37,11 +34,9 @@ func (sds *StateDiffStreamer) Stream(payloadChan chan statediff.Payload) (*rpc.C
|
||||
sds.PassedPayloadChan = payloadChan
|
||||
|
||||
go func() {
|
||||
sds.WaitGroup.Add(1)
|
||||
for _, payload := range sds.StreamPayloads {
|
||||
sds.PassedPayloadChan <- payload
|
||||
}
|
||||
sds.WaitGroup.Done()
|
||||
}()
|
||||
|
||||
return sds.ReturnSub, sds.ReturnErr
|
||||
|
||||
@@ -38,7 +38,7 @@ func AddressToLeafKey(address common.Address) common.Hash {
|
||||
|
||||
// Test variables
|
||||
var (
|
||||
BlockNumber = rand.Int63()
|
||||
BlockNumber = big.NewInt(rand.Int63())
|
||||
BlockHash = "0xfa40fbe2d98d98b3363a778d52f2bcd29d6790b9b3f3cab2b167fd12d3550f73"
|
||||
CodeHash = common.Hex2Bytes("0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470")
|
||||
NewNonceValue = rand.Uint64()
|
||||
@@ -65,26 +65,26 @@ var (
|
||||
CodeHash: CodeHash,
|
||||
}
|
||||
valueBytes, _ = rlp.EncodeToBytes(testAccount)
|
||||
CreatedAccountDiffs = statediff.AccountDiffsMap{
|
||||
ContractLeafKey: {
|
||||
CreatedAccountDiffs = []statediff.AccountDiff{
|
||||
{
|
||||
Key: ContractLeafKey.Bytes(),
|
||||
Value: valueBytes,
|
||||
Storage: storage,
|
||||
},
|
||||
AnotherContractLeafKey: {
|
||||
{
|
||||
Key: AnotherContractLeafKey.Bytes(),
|
||||
Value: valueBytes,
|
||||
Storage: emptyStorage,
|
||||
},
|
||||
}
|
||||
|
||||
UpdatedAccountDiffs = statediff.AccountDiffsMap{ContractLeafKey: {
|
||||
UpdatedAccountDiffs = []statediff.AccountDiff{{
|
||||
Key: ContractLeafKey.Bytes(),
|
||||
Value: valueBytes,
|
||||
Storage: storage,
|
||||
}}
|
||||
|
||||
DeletedAccountDiffs = statediff.AccountDiffsMap{ContractLeafKey: {
|
||||
DeletedAccountDiffs = []statediff.AccountDiff{{
|
||||
Key: ContractLeafKey.Bytes(),
|
||||
Value: valueBytes,
|
||||
Storage: storage,
|
||||
@@ -97,7 +97,7 @@ var (
|
||||
DeletedAccounts: DeletedAccountDiffs,
|
||||
UpdatedAccounts: UpdatedAccountDiffs,
|
||||
}
|
||||
MockStateDiffRlp, _ = rlp.EncodeToBytes(MockStateDiff)
|
||||
MockStateDiffBytes, _ = rlp.EncodeToBytes(MockStateDiff)
|
||||
|
||||
mockTransaction1 = types.NewTransaction(0, common.HexToAddress("0x0"), big.NewInt(1000), 50, big.NewInt(100), nil)
|
||||
mockTransaction2 = types.NewTransaction(1, common.HexToAddress("0x1"), big.NewInt(2000), 100, big.NewInt(200), nil)
|
||||
@@ -119,7 +119,7 @@ var (
|
||||
|
||||
MockStatediffPayload = statediff.Payload{
|
||||
BlockRlp: MockBlockRlp,
|
||||
StateDiffRlp: MockStateDiffRlp,
|
||||
StateDiffRlp: MockStateDiffBytes,
|
||||
Err: nil,
|
||||
}
|
||||
|
||||
@@ -139,7 +139,7 @@ var (
|
||||
|
||||
MockCIDPayload = ipfs.CIDPayload{
|
||||
BlockNumber: "1",
|
||||
BlockHash: "0x0",
|
||||
BlockHash: common.HexToHash("0x0"),
|
||||
HeaderCID: "mockHeaderCID",
|
||||
TransactionCIDs: map[common.Hash]*ipfs.TrxMetaData{
|
||||
common.HexToHash("0x0"): {
|
||||
@@ -163,16 +163,30 @@ var (
|
||||
Topic0s: []string{"mockTopic1", "mockTopic2"},
|
||||
},
|
||||
},
|
||||
StateLeafCIDs: map[common.Hash]string{
|
||||
common.HexToHash("0x0"): "mockStateCID1",
|
||||
common.HexToHash("0x1"): "mockStateCID2",
|
||||
},
|
||||
StorageLeafCIDs: map[common.Hash]map[common.Hash]string{
|
||||
StateNodeCIDs: map[common.Hash]ipfs.StateNodeCID{
|
||||
common.HexToHash("0x0"): {
|
||||
common.HexToHash("0x0"): "mockStorageCID1",
|
||||
CID: "mockStateCID1",
|
||||
Leaf: true,
|
||||
},
|
||||
common.HexToHash("0x1"): {
|
||||
common.HexToHash("0x1"): "mockStorageCID2",
|
||||
CID: "mockStateCID2",
|
||||
Leaf: true,
|
||||
},
|
||||
},
|
||||
StorageNodeCIDs: map[common.Hash][]ipfs.StorageNodeCID{
|
||||
common.HexToHash("0x0"): {
|
||||
{
|
||||
CID: "mockStorageCID1",
|
||||
Key: common.HexToHash("0x0"),
|
||||
Leaf: true,
|
||||
},
|
||||
},
|
||||
common.HexToHash("0x1"): {
|
||||
{
|
||||
CID: "mockStorageCID2",
|
||||
Key: common.HexToHash("0x1"),
|
||||
Leaf: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
+29
-6
@@ -17,9 +17,10 @@
|
||||
package ipfs
|
||||
|
||||
import (
|
||||
"math/big"
|
||||
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
"github.com/ethereum/go-ethereum/core/types"
|
||||
"math/big"
|
||||
)
|
||||
|
||||
// IPLDPayload is a custom type which packages ETH data for the IPFS publisher
|
||||
@@ -31,20 +32,42 @@ type IPLDPayload struct {
|
||||
TrxMetaData []*TrxMetaData
|
||||
Receipts types.Receipts
|
||||
ReceiptMetaData []*ReceiptMetaData
|
||||
StateLeafs map[common.Hash][]byte
|
||||
StorageLeafs map[common.Hash]map[common.Hash][]byte
|
||||
StateNodes map[common.Hash]StateNode
|
||||
StorageNodes map[common.Hash][]StorageNode
|
||||
}
|
||||
|
||||
type StateNode struct {
|
||||
Value []byte
|
||||
Leaf bool
|
||||
}
|
||||
|
||||
type StorageNode struct {
|
||||
Key common.Hash
|
||||
Value []byte
|
||||
Leaf bool
|
||||
}
|
||||
|
||||
// CIDPayload is a struct to hold all the CIDs and their meta data
|
||||
type CIDPayload struct {
|
||||
BlockNumber string
|
||||
BlockHash string
|
||||
BlockHash common.Hash
|
||||
HeaderCID string
|
||||
UncleCIDS map[common.Hash]string
|
||||
TransactionCIDs map[common.Hash]*TrxMetaData
|
||||
ReceiptCIDs map[common.Hash]*ReceiptMetaData
|
||||
StateLeafCIDs map[common.Hash]string
|
||||
StorageLeafCIDs map[common.Hash]map[common.Hash]string
|
||||
StateNodeCIDs map[common.Hash]StateNodeCID
|
||||
StorageNodeCIDs map[common.Hash][]StorageNodeCID
|
||||
}
|
||||
|
||||
type StateNodeCID struct {
|
||||
CID string
|
||||
Leaf bool
|
||||
}
|
||||
|
||||
type StorageNodeCID struct {
|
||||
Key common.Hash
|
||||
CID string
|
||||
Leaf bool
|
||||
}
|
||||
|
||||
// ReceiptMetaData wraps some additional data around our receipt CIDs for indexing
|
||||
|
||||
Reference in New Issue
Block a user