forked from btcsuite/btcwallet
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request btcsuite#822 from MStreet3/bug/rescan-data-race
chain: fix NeutrinoClient segfault on NotifyReceived call
- Loading branch information
Showing
6 changed files
with
508 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,4 @@ vendor | |
.idea | ||
coverage.txt | ||
*.swp | ||
.vscode |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package chain | ||
|
||
import ( | ||
"github.com/dcrlabs/ltcwallet/spv" | ||
"github.com/dcrlabs/ltcwallet/spv/banman" | ||
"github.com/dcrlabs/ltcwallet/spv/headerfs" | ||
"github.com/ltcsuite/ltcd/chaincfg" | ||
"github.com/ltcsuite/ltcd/chaincfg/chainhash" | ||
"github.com/ltcsuite/ltcd/ltcutil" | ||
"github.com/ltcsuite/ltcd/ltcutil/gcs" | ||
"github.com/ltcsuite/ltcd/wire" | ||
) | ||
|
||
// NeutrinoChainService is an interface that encapsulates all the public | ||
// methods of a *neutrino.ChainService | ||
type NeutrinoChainService interface { | ||
Start() error | ||
GetBlock(chainhash.Hash, ...spv.QueryOption) (*ltcutil.Block, error) | ||
GetBlockHeight(*chainhash.Hash) (int32, error) | ||
BestBlock() (*headerfs.BlockStamp, error) | ||
GetBlockHash(int64) (*chainhash.Hash, error) | ||
GetBlockHeader(*chainhash.Hash) (*wire.BlockHeader, error) | ||
IsCurrent() bool | ||
SendTransaction(*wire.MsgTx) error | ||
GetCFilter(chainhash.Hash, wire.FilterType, | ||
...spv.QueryOption) (*gcs.Filter, error) | ||
GetUtxo(...spv.RescanOption) (*spv.SpendReport, error) | ||
BanPeer(string, banman.Reason) error | ||
IsBanned(addr string) bool | ||
AddPeer(*spv.ServerPeer) | ||
AddBytesSent(uint64) | ||
AddBytesReceived(uint64) | ||
NetTotals() (uint64, uint64) | ||
UpdatePeerHeights(*chainhash.Hash, int32, *spv.ServerPeer) | ||
ChainParams() chaincfg.Params | ||
Stop() error | ||
PeerByAddr(string) *spv.ServerPeer | ||
} | ||
|
||
var _ NeutrinoChainService = (*spv.ChainService)(nil) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
package chain | ||
|
||
import ( | ||
"container/list" | ||
"errors" | ||
|
||
"github.com/dcrlabs/ltcwallet/spv" | ||
"github.com/dcrlabs/ltcwallet/spv/banman" | ||
"github.com/dcrlabs/ltcwallet/spv/headerfs" | ||
"github.com/ltcsuite/ltcd/chaincfg" | ||
"github.com/ltcsuite/ltcd/chaincfg/chainhash" | ||
"github.com/ltcsuite/ltcd/ltcutil" | ||
"github.com/ltcsuite/ltcd/ltcutil/gcs" | ||
"github.com/ltcsuite/ltcd/wire" | ||
) | ||
|
||
var ( | ||
errNotImplemented = errors.New("not implemented") | ||
testBestBlock = &headerfs.BlockStamp{ | ||
Height: 42, | ||
} | ||
) | ||
|
||
var ( | ||
_ rescanner = (*mockRescanner)(nil) | ||
_ NeutrinoChainService = (*mockChainService)(nil) | ||
) | ||
|
||
// newMockNeutrinoClient constructs a neutrino client with a mock chain | ||
// service implementation and mock rescanner interface implementation. | ||
func newMockNeutrinoClient() *NeutrinoClient { | ||
// newRescanFunc returns a mockRescanner | ||
newRescanFunc := func(ro ...spv.RescanOption) rescanner { | ||
return &mockRescanner{ | ||
updateArgs: list.New(), | ||
} | ||
} | ||
|
||
return &NeutrinoClient{ | ||
CS: &mockChainService{}, | ||
newRescan: newRescanFunc, | ||
} | ||
} | ||
|
||
// mockRescanner is a mock implementation of a rescanner interface for use in | ||
// tests. Only the Update method is implemented. | ||
type mockRescanner struct { | ||
updateArgs *list.List | ||
} | ||
|
||
func (m *mockRescanner) Update(opts ...spv.UpdateOption) error { | ||
m.updateArgs.PushBack(opts) | ||
return nil | ||
} | ||
|
||
func (m *mockRescanner) Start() <-chan error { | ||
return nil | ||
} | ||
|
||
func (m *mockRescanner) WaitForShutdown() { | ||
// no-op | ||
} | ||
|
||
// mockChainService is a mock implementation of a chain service for use in | ||
// tests. Only the Start, GetBlockHeader and BestBlock methods are implemented. | ||
type mockChainService struct { | ||
} | ||
|
||
func (m *mockChainService) Start() error { | ||
return nil | ||
} | ||
|
||
func (m *mockChainService) BestBlock() (*headerfs.BlockStamp, error) { | ||
return testBestBlock, nil | ||
} | ||
|
||
func (m *mockChainService) GetBlockHeader( | ||
*chainhash.Hash) (*wire.BlockHeader, error) { | ||
|
||
return &wire.BlockHeader{}, nil | ||
} | ||
|
||
func (m *mockChainService) GetBlock(chainhash.Hash, | ||
...spv.QueryOption) (*ltcutil.Block, error) { | ||
|
||
return nil, errNotImplemented | ||
} | ||
|
||
func (m *mockChainService) GetBlockHeight(*chainhash.Hash) (int32, error) { | ||
return 0, errNotImplemented | ||
} | ||
|
||
func (m *mockChainService) GetBlockHash(int64) (*chainhash.Hash, error) { | ||
return nil, errNotImplemented | ||
} | ||
|
||
func (m *mockChainService) IsCurrent() bool { | ||
return false | ||
} | ||
|
||
func (m *mockChainService) SendTransaction(*wire.MsgTx) error { | ||
return errNotImplemented | ||
} | ||
|
||
func (m *mockChainService) GetCFilter(chainhash.Hash, | ||
wire.FilterType, ...spv.QueryOption) (*gcs.Filter, error) { | ||
|
||
return nil, errNotImplemented | ||
} | ||
|
||
func (m *mockChainService) GetUtxo( | ||
_ ...spv.RescanOption) (*spv.SpendReport, error) { | ||
|
||
return nil, errNotImplemented | ||
} | ||
|
||
func (m *mockChainService) BanPeer(string, banman.Reason) error { | ||
return errNotImplemented | ||
} | ||
|
||
func (m *mockChainService) IsBanned(addr string) bool { | ||
panic(errNotImplemented) | ||
} | ||
|
||
func (m *mockChainService) AddPeer(*spv.ServerPeer) { | ||
panic(errNotImplemented) | ||
} | ||
|
||
func (m *mockChainService) AddBytesSent(uint64) { | ||
panic(errNotImplemented) | ||
} | ||
|
||
func (m *mockChainService) AddBytesReceived(uint64) { | ||
panic(errNotImplemented) | ||
} | ||
|
||
func (m *mockChainService) NetTotals() (uint64, uint64) { | ||
panic(errNotImplemented) | ||
} | ||
|
||
func (m *mockChainService) UpdatePeerHeights(*chainhash.Hash, | ||
int32, *spv.ServerPeer, | ||
) { | ||
panic(errNotImplemented) | ||
} | ||
|
||
func (m *mockChainService) ChainParams() chaincfg.Params { | ||
panic(errNotImplemented) | ||
} | ||
|
||
func (m *mockChainService) Stop() error { | ||
panic(errNotImplemented) | ||
} | ||
|
||
func (m *mockChainService) PeerByAddr(string) *spv.ServerPeer { | ||
panic(errNotImplemented) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.