Skip to content

Commit

Permalink
fix: ethClient.SubscribeNewDeposit should accept StateData channel (e…
Browse files Browse the repository at this point in the history
…thereum#44)

* ethClient.SubscribeNewDeposit should accept StateData channel

* remove redundant function

* Fix compilation error
  • Loading branch information
atvanguard committed Apr 6, 2020
1 parent 260243c commit 7183e21
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 15 deletions.
3 changes: 3 additions & 0 deletions accounts/abi/bind/backends/simulated.go
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,9 @@ func (fb *filterBackend) SubscribeRemovedLogsEvent(ch chan<- core.RemovedLogsEve
func (fb *filterBackend) SubscribeLogsEvent(ch chan<- []*types.Log) event.Subscription {
return fb.bc.SubscribeLogsEvent(ch)
}
func (fb *filterBackend) SubscribeStateEvent(ch chan<- core.NewStateChangeEvent) event.Subscription {
return fb.bc.SubscribeStateEvent(ch)
}

func (fb *filterBackend) BloomStatus() (uint64, uint64) { return 4096, 0 }
func (fb *filterBackend) ServiceFilter(ctx context.Context, ms *bloombits.MatcherSession) {
Expand Down
2 changes: 0 additions & 2 deletions consensus/bor/bor.go
Original file line number Diff line number Diff line change
Expand Up @@ -1161,7 +1161,6 @@ func (c *Bor) CommitStates(
header *types.Header,
chain core.ChainContext,
) error {
fmt.Println("comminting state")
// get pending state proposals
stateIds, err := c.GetPendingStateProposals(header.Number.Uint64() - 1)
if err != nil {
Expand Down Expand Up @@ -1240,7 +1239,6 @@ func (c *Bor) CommitStates(
return nil
}


// SubscribeStateEvent registers a subscription of ChainSideEvent.
func (c *Bor) SubscribeStateEvent(ch chan<- core.NewStateChangeEvent) event.Subscription {
return c.scope.Track(c.stateDataFeed.Subscribe(ch))
Expand Down
8 changes: 7 additions & 1 deletion core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"sync/atomic"
"time"

"github.com/hashicorp/golang-lru"
"github.com/maticnetwork/bor/common"
"github.com/maticnetwork/bor/common/mclock"
"github.com/maticnetwork/bor/common/prque"
Expand All @@ -42,7 +43,6 @@ import (
"github.com/maticnetwork/bor/params"
"github.com/maticnetwork/bor/rlp"
"github.com/maticnetwork/bor/trie"
"github.com/hashicorp/golang-lru"
)

var (
Expand Down Expand Up @@ -142,6 +142,7 @@ type BlockChain struct {
chainHeadFeed event.Feed
logsFeed event.Feed
blockProcFeed event.Feed
stateDataFeed event.Feed
scope event.SubscriptionScope
genesisBlock *types.Block

Expand Down Expand Up @@ -2177,6 +2178,11 @@ func (bc *BlockChain) SubscribeLogsEvent(ch chan<- []*types.Log) event.Subscript
return bc.scope.Track(bc.logsFeed.Subscribe(ch))
}

// SubscribeStateEvent registers a subscription of ChainSideEvent.
func (bc *BlockChain) SubscribeStateEvent(ch chan<- NewStateChangeEvent) event.Subscription {
return bc.scope.Track(bc.stateDataFeed.Subscribe(ch))
}

// SubscribeBlockProcessingEvent registers a subscription of bool where true means
// block processing has started while false means it has stopped.
func (bc *BlockChain) SubscribeBlockProcessingEvent(ch chan<- bool) event.Subscription {
Expand Down
4 changes: 0 additions & 4 deletions core/types/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,6 @@ type txdataMarshaling struct {
S *hexutil.Big
}

func (sd *StateData) StateData() *StateData {
return sd
}

func NewTransaction(nonce uint64, to common.Address, amount *big.Int, gasLimit uint64, gasPrice *big.Int, data []byte) *Transaction {
return newTransaction(nonce, &to, amount, gasLimit, gasPrice, data)
}
Expand Down
3 changes: 2 additions & 1 deletion eth/filters/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package filters

import (
"bytes"
"context"
"encoding/json"
"errors"
Expand Down Expand Up @@ -247,7 +248,7 @@ func (api *PublicFilterAPI) NewDeposits(ctx context.Context, crit ethereum.Filte
for {
select {
case h := <-stateData:
if crit.Did == h.Did || crit.Contract == h.Contract ||
if crit.Did == h.Did || bytes.Compare(crit.Contract.Bytes(), h.Contract.Bytes()) == 0 ||
(crit.Did == 0 && crit.Contract == common.Address{}) {
notifier.Notify(rpcSub.ID, h)
}
Expand Down
5 changes: 2 additions & 3 deletions eth/filters/filter_system.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,8 +311,7 @@ func (es *EventSystem) SubscribeNewHeads(headers chan *types.Header) *Subscripti
return es.subscribe(sub)
}

// SubscribeNewHeads creates a subscription that writes the header of a block that is
// imported in the chain.
// SubscribeNewDeposits creates a subscription that writes details about the new state sync events (from mainchain to Bor)
func (es *EventSystem) SubscribeNewDeposits(stateData chan *types.StateData) *Subscription {
sub := &subscription{
id: rpc.NewID(),
Expand Down Expand Up @@ -387,7 +386,7 @@ func (es *EventSystem) broadcast(filters filterIndex, ev interface{}) {
}
case core.NewStateChangeEvent:
for _, f := range filters[StateSubscription] {
f.stateData <- e.StateData.StateData()
f.stateData <- e.StateData
}
case core.ChainEvent:
for _, f := range filters[BlocksSubscription] {
Expand Down
7 changes: 3 additions & 4 deletions ethclient/ethclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,10 +324,9 @@ func (ec *Client) SubscribeNewHead(ctx context.Context, ch chan<- *types.Header)
return ec.c.EthSubscribe(ctx, ch, "newHeads")
}

// SubscribeNewHead subscribes to notifications about the current blockchain head
// on the given channel.
func (ec *Client) SubscribeNewDeposit(ctx context.Context, ch chan<- *types.Header) (ethereum.Subscription, error) {
return ec.c.EthSubscribe(ctx, ch, "newDeposits")
// SubscribeNewDeposit subscribes to new state sync events
func (ec *Client) SubscribeNewDeposit(ctx context.Context, ch chan<- *types.StateData) (ethereum.Subscription, error) {
return ec.c.EthSubscribe(ctx, ch, "newDeposits", nil)
}

// State Access
Expand Down

0 comments on commit 7183e21

Please sign in to comment.