Skip to content

Commit

Permalink
Merge pull request #224 from kaleido-io/non-hex-filter
Browse files Browse the repository at this point in the history
Do not require the filter id to be numeric
  • Loading branch information
peterbroadhurst authored Jul 13, 2022
2 parents b75545b + bdcdf52 commit a2a305b
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 45 deletions.
4 changes: 2 additions & 2 deletions internal/events/block_confirmations.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ type blockConfirmationManager struct {
ctx context.Context
cancelFunc func()
log *log.Entry
filterID ethbinding.HexBigInt
filterID string
filterStale bool
rpc eth.RPCClient
requiredConfirmations int
Expand Down Expand Up @@ -193,7 +193,7 @@ func (bcm *blockConfirmationManager) createBlockFilter() error {
return errors.Errorf(errors.RPCCallReturnedError, "eth_newBlockFilter", err)
}
bcm.filterStale = false
bcm.log.Infof("Created block filter: %s", bcm.filterID.String())
bcm.log.Infof("Created block filter: %s", bcm.filterID)
return err
}

Expand Down
74 changes: 37 additions & 37 deletions internal/events/block_confirmations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,12 @@ func TestBlockConfirmationManagerE2ENewEvent(t *testing.T) {

// Establish the block filter
rpc.On("CallContext", mock.Anything, mock.Anything, "eth_newBlockFilter").Run(func(args mock.Arguments) {
args[1].(*hexutil.Big).ToInt().SetString("1977", 10)
*args[1].(*string) = "filter_id1"
}).Return(nil).Once()

// First poll for changes gives nothing, but we load up the event at this point for the next round
rpc.On("CallContext", mock.Anything, mock.Anything, "eth_getFilterChanges", mock.MatchedBy(func(i hexutil.Big) bool {
return i.ToInt().Int64() == int64(1977)
rpc.On("CallContext", mock.Anything, mock.Anything, "eth_getFilterChanges", mock.MatchedBy(func(f string) bool {
return f == "filter_id1"
})).Run(func(args mock.Arguments) {
*(args[1].(*[]*ethbinding.Hash)) = []*ethbinding.Hash{}

Expand All @@ -116,8 +116,8 @@ func TestBlockConfirmationManagerE2ENewEvent(t *testing.T) {
Hash: ethbind.API.HexToHash("0x64fd8179b80dd255d52ce60d7f265c0506be810e2f3df52463fadeb44bb4d2df"),
ParentHash: ethbind.API.HexToHash("0x46210d224888265c269359529618bf2f6adb2697ff52c63c10f16a2391bdd295"),
}
rpc.On("CallContext", mock.Anything, mock.Anything, "eth_getFilterChanges", mock.MatchedBy(func(i hexutil.Big) bool {
return i.ToInt().Int64() == int64(1977)
rpc.On("CallContext", mock.Anything, mock.Anything, "eth_getFilterChanges", mock.MatchedBy(func(f string) bool {
return f == "filter_id1"
})).Run(func(args mock.Arguments) {
*(args[1].(*[]*ethbinding.Hash)) = []*ethbinding.Hash{
&block1003.Hash,
Expand Down Expand Up @@ -152,8 +152,8 @@ func TestBlockConfirmationManagerE2ENewEvent(t *testing.T) {
Hash: ethbind.API.HexToHash("0xed21f4f73d150f16f922ae82b7485cd936ae1eca4c027516311b928360a347e8"),
ParentHash: ethbind.API.HexToHash("0x64fd8179b80dd255d52ce60d7f265c0506be810e2f3df52463fadeb44bb4d2df"),
}
rpc.On("CallContext", mock.Anything, mock.Anything, "eth_getFilterChanges", mock.MatchedBy(func(i hexutil.Big) bool {
return i.ToInt().Int64() == int64(1977)
rpc.On("CallContext", mock.Anything, mock.Anything, "eth_getFilterChanges", mock.MatchedBy(func(f string) bool {
return f == "filter_id1"
})).Run(func(args mock.Arguments) {
*(args[1].(*[]*ethbinding.Hash)) = []*ethbinding.Hash{
&block1004.Hash,
Expand All @@ -169,8 +169,8 @@ func TestBlockConfirmationManagerE2ENewEvent(t *testing.T) {
}).Return(nil).Once()

// Subsequent calls get nothing, and blocks until close anyway
rpc.On("CallContext", mock.Anything, mock.Anything, "eth_getFilterChanges", mock.MatchedBy(func(i hexutil.Big) bool {
return i.ToInt().Int64() == int64(1977)
rpc.On("CallContext", mock.Anything, mock.Anything, "eth_getFilterChanges", mock.MatchedBy(func(f string) bool {
return f == "filter_id1"
})).Run(func(args mock.Arguments) {
*(args[1].(*[]*ethbinding.Hash)) = []*ethbinding.Hash{}
if lastBlockDetected {
Expand Down Expand Up @@ -206,7 +206,7 @@ func TestBlockConfirmationManagerE2EFork(t *testing.T) {

// Establish the block filter
rpc.On("CallContext", mock.Anything, mock.Anything, "eth_newBlockFilter").Run(func(args mock.Arguments) {
args[1].(*hexutil.Big).ToInt().SetString("1977", 10)
*args[1].(*string) = "filter_id1"
}).Return(nil).Once()

// The next filter gives us 1002, and a first 1003 block - which will later be removed
Expand All @@ -220,8 +220,8 @@ func TestBlockConfirmationManagerE2EFork(t *testing.T) {
Hash: ethbind.API.HexToHash("0x46210d224888265c269359529618bf2f6adb2697ff52c63c10f16a2391bdd295"),
ParentHash: ethbind.API.HexToHash("0x64fd8179b80dd255d52ce60d7f265c0506be810e2f3df52463fadeb44bb4d2df"),
}
rpc.On("CallContext", mock.Anything, mock.Anything, "eth_getFilterChanges", mock.MatchedBy(func(i hexutil.Big) bool {
return i.ToInt().Int64() == int64(1977)
rpc.On("CallContext", mock.Anything, mock.Anything, "eth_getFilterChanges", mock.MatchedBy(func(f string) bool {
return f == "filter_id1"
})).Run(func(args mock.Arguments) {
*(args[1].(*[]*ethbinding.Hash)) = []*ethbinding.Hash{
&block1002.Hash,
Expand Down Expand Up @@ -250,8 +250,8 @@ func TestBlockConfirmationManagerE2EFork(t *testing.T) {
Hash: ethbind.API.HexToHash("0x110282339db2dfe4bfd13d78375f7883048cac6bc12f8393bd080a4e263d5d21"),
ParentHash: ethbind.API.HexToHash("0xed21f4f73d150f16f922ae82b7485cd936ae1eca4c027516311b928360a347e8"),
}
rpc.On("CallContext", mock.Anything, mock.Anything, "eth_getFilterChanges", mock.MatchedBy(func(i hexutil.Big) bool {
return i.ToInt().Int64() == int64(1977)
rpc.On("CallContext", mock.Anything, mock.Anything, "eth_getFilterChanges", mock.MatchedBy(func(f string) bool {
return f == "filter_id1"
})).Run(func(args mock.Arguments) {
*(args[1].(*[]*ethbinding.Hash)) = []*ethbinding.Hash{
&block1003b.Hash,
Expand All @@ -270,8 +270,8 @@ func TestBlockConfirmationManagerE2EFork(t *testing.T) {
}).Return(nil).Once()

// Subsequent calls get nothing, and blocks until close anyway
rpc.On("CallContext", mock.Anything, mock.Anything, "eth_getFilterChanges", mock.MatchedBy(func(i hexutil.Big) bool {
return i.ToInt().Int64() == int64(1977)
rpc.On("CallContext", mock.Anything, mock.Anything, "eth_getFilterChanges", mock.MatchedBy(func(f string) bool {
return f == "filter_id1"
})).Run(func(args mock.Arguments) {
*(args[1].(*[]*ethbinding.Hash)) = []*ethbinding.Hash{}
if lastBlockDetected {
Expand Down Expand Up @@ -322,12 +322,12 @@ func TestBlockConfirmationManagerE2EHistoricalEvent(t *testing.T) {

// Establish the block filter
rpc.On("CallContext", mock.Anything, mock.Anything, "eth_newBlockFilter").Run(func(args mock.Arguments) {
args[1].(*hexutil.Big).ToInt().SetString("1977", 10)
*args[1].(*string) = "filter_id1"
}).Return(nil).Once()

// We don't notify of any new blocks
rpc.On("CallContext", mock.Anything, mock.Anything, "eth_getFilterChanges", mock.MatchedBy(func(i hexutil.Big) bool {
return i.ToInt().Int64() == int64(1977)
rpc.On("CallContext", mock.Anything, mock.Anything, "eth_getFilterChanges", mock.MatchedBy(func(f string) bool {
return f == "filter_id1"
})).Run(func(args mock.Arguments) {
*(args[1].(*[]*ethbinding.Hash)) = []*ethbinding.Hash{}
}).Return(nil)
Expand Down Expand Up @@ -443,7 +443,7 @@ func TestConfirmationsListenerFailWalkingChain(t *testing.T) {
bcm.addEvent(eventToConfirm, testStream)

rpc.On("CallContext", mock.Anything, mock.Anything, "eth_newBlockFilter").Run(func(args mock.Arguments) {
args[1].(*hexutil.Big).ToInt().SetString("1977", 10)
*args[1].(*string) = "filter_id1"
bcm.cancelFunc()
}).Return(nil).Once()
rpc.On("CallContext", mock.Anything, mock.Anything, "eth_getBlockByNumber", mock.MatchedBy(func(i hexutil.Uint64) bool {
Expand All @@ -461,11 +461,11 @@ func TestConfirmationsListenerFailPollingBlocks(t *testing.T) {
bcm.done = make(chan struct{})

rpc.On("CallContext", mock.Anything, mock.Anything, "eth_newBlockFilter").Run(func(args mock.Arguments) {
args[1].(*hexutil.Big).ToInt().SetString("1977", 10)
*args[1].(*string) = "filter_id1"
bcm.cancelFunc()
}).Return(nil).Once()
rpc.On("CallContext", mock.Anything, mock.Anything, "eth_getFilterChanges", mock.MatchedBy(func(i hexutil.Big) bool {
return i.ToInt().Int64() == 1977
rpc.On("CallContext", mock.Anything, mock.Anything, "eth_getFilterChanges", mock.MatchedBy(func(f string) bool {
return f == "filter_id1"
})).Return(fmt.Errorf("pop"))

bcm.confirmationsListener()
Expand All @@ -479,14 +479,14 @@ func TestConfirmationsListenerLostFilterReestablish(t *testing.T) {
bcm.done = make(chan struct{})

rpc.On("CallContext", mock.Anything, mock.Anything, "eth_newBlockFilter").Run(func(args mock.Arguments) {
args[1].(*hexutil.Big).ToInt().SetString("1977", 10)
*args[1].(*string) = "filter_id1"
}).Return(nil).Twice()
rpc.On("CallContext", mock.Anything, mock.Anything, "eth_getFilterChanges", mock.MatchedBy(func(i hexutil.Big) bool {
return i.ToInt().Int64() == 1977
rpc.On("CallContext", mock.Anything, mock.Anything, "eth_getFilterChanges", mock.MatchedBy(func(f string) bool {
return f == "filter_id1"
})).Return(fmt.Errorf("filter not found")).Once()
rpc.On("CallContext", mock.Anything, mock.Anything, "eth_getFilterChanges", mock.MatchedBy(func(i hexutil.Big) bool {
rpc.On("CallContext", mock.Anything, mock.Anything, "eth_getFilterChanges", mock.MatchedBy(func(f string) bool {
bcm.cancelFunc()
return i.ToInt().Int64() == 1977
return f == "filter_id1"
})).Return(nil)

bcm.confirmationsListener()
Expand Down Expand Up @@ -516,10 +516,10 @@ func TestConfirmationsListenerFailWalkingChainForNewEvent(t *testing.T) {
})

rpc.On("CallContext", mock.Anything, mock.Anything, "eth_newBlockFilter").Run(func(args mock.Arguments) {
args[1].(*hexutil.Big).ToInt().SetString("1977", 10)
*args[1].(*string) = "filter_id1"
}).Return(nil).Once()
rpc.On("CallContext", mock.Anything, mock.Anything, "eth_getFilterChanges", mock.MatchedBy(func(i hexutil.Big) bool {
return i.ToInt().Int64() == int64(1977)
rpc.On("CallContext", mock.Anything, mock.Anything, "eth_getFilterChanges", mock.MatchedBy(func(f string) bool {
return f == "filter_id1"
})).Run(func(args mock.Arguments) {
*(args[1].(*[]*ethbinding.Hash)) = []*ethbinding.Hash{}
}).Return(nil)
Expand Down Expand Up @@ -557,10 +557,10 @@ func TestConfirmationsListenerStopStream(t *testing.T) {
})

rpc.On("CallContext", mock.Anything, mock.Anything, "eth_newBlockFilter").Run(func(args mock.Arguments) {
args[1].(*hexutil.Big).ToInt().SetString("1977", 10)
*args[1].(*string) = "filter_id1"
}).Return(nil)
rpc.On("CallContext", mock.Anything, mock.Anything, "eth_getFilterChanges", mock.MatchedBy(func(i hexutil.Big) bool {
return i.ToInt().Int64() == int64(1977)
rpc.On("CallContext", mock.Anything, mock.Anything, "eth_getFilterChanges", mock.MatchedBy(func(f string) bool {
return f == "filter_id1"
})).Run(func(args mock.Arguments) {
*(args[1].(*[]*ethbinding.Hash)) = []*ethbinding.Hash{}
}).Return(nil)
Expand Down Expand Up @@ -602,10 +602,10 @@ func TestConfirmationsRemoveEvent(t *testing.T) {

changeCount := 0
rpc.On("CallContext", mock.Anything, mock.Anything, "eth_newBlockFilter").Run(func(args mock.Arguments) {
args[1].(*hexutil.Big).ToInt().SetString("1977", 10)
*args[1].(*string) = "filter_id1"
}).Return(nil).Maybe()
rpc.On("CallContext", mock.Anything, mock.Anything, "eth_getFilterChanges", mock.MatchedBy(func(i hexutil.Big) bool {
return i.ToInt().Int64() == int64(1977)
rpc.On("CallContext", mock.Anything, mock.Anything, "eth_getFilterChanges", mock.MatchedBy(func(f string) bool {
return f == "filter_id1"
})).Run(func(args mock.Arguments) {
*(args[1].(*[]*ethbinding.Hash)) = []*ethbinding.Hash{}
changeCount++
Expand Down
7 changes: 3 additions & 4 deletions internal/events/eventstream_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"testing"
"time"

"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/hyperledger/firefly-ethconnect/internal/contractregistry"
"github.com/hyperledger/firefly-ethconnect/internal/errors"
"github.com/hyperledger/firefly-ethconnect/internal/eth"
Expand Down Expand Up @@ -108,10 +107,10 @@ func newTestStreamForConfirmations(t *testing.T) (*eventStream, func()) {
// Mock the RPC calls for the block confirmations manager that will just spin returning no results
rpc := sm.rpc.(*ethmocks.RPCClient)
rpc.On("CallContext", mock.Anything, mock.Anything, "eth_newBlockFilter").Run(func(args mock.Arguments) {
args[1].(*hexutil.Big).ToInt().SetString("1977", 10)
*args[1].(*string) = "filter_id1"
}).Return(nil).Maybe()
rpc.On("CallContext", mock.Anything, mock.Anything, "eth_getFilterChanges", mock.MatchedBy(func(i hexutil.Big) bool {
return i.ToInt().Int64() == int64(1977)
rpc.On("CallContext", mock.Anything, mock.Anything, "eth_getFilterChanges", mock.MatchedBy(func(f string) bool {
return f == "filter_id1"
})).Run(func(args mock.Arguments) {
*(args[1].(*[]*ethbinding.Hash)) = []*ethbinding.Hash{}
}).Return(nil).Maybe()
Expand Down
4 changes: 2 additions & 2 deletions internal/events/subscription.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ type subscription struct {
cr contractregistry.ContractResolver
lp *logProcessor
logName string
filterID ethbinding.HexBigInt
filterID string
filteredOnce bool
filterStale bool
deleting bool
Expand Down Expand Up @@ -216,7 +216,7 @@ func (s *subscription) createFilter(ctx context.Context, since *big.Int) error {
s.catchupBlock = nil // we are not in catchup mode now
s.filteredOnce = false
s.markFilterStale(ctx, false)
log.Infof("%s: created filter from block %s: %s - %+v", s.logName, since.String(), s.filterID.String(), s.info.Filter)
log.Infof("%s: created filter from block %s: %s - %+v", s.logName, since.String(), s.filterID, s.info.Filter)
return err
}

Expand Down

0 comments on commit a2a305b

Please sign in to comment.