kaspad/cmd/txgen/client.go
Ori Newman c88fa1492e [NOD-375] Move to pkg/errors (#447)
* [NOD-375] Move to pkg/errors

* [NOD-375] Fix tests

* [NOD-375] Make AreErrorsEqual a shared function
2019-11-04 11:24:12 +02:00

46 lines
1.1 KiB
Go

package main
import (
"github.com/daglabs/btcd/rpcclient"
"github.com/daglabs/btcd/util"
"github.com/daglabs/btcd/wire"
"github.com/pkg/errors"
)
type txgenClient struct {
*rpcclient.Client
onBlockAdded chan *blockAddedMsg
}
type blockAddedMsg struct {
chainHeight uint64
header *wire.BlockHeader
txs []*util.Tx
}
func newTxgenClient(connCfg *rpcclient.ConnConfig) (*txgenClient, error) {
client := &txgenClient{
onBlockAdded: make(chan *blockAddedMsg),
}
notificationHandlers := &rpcclient.NotificationHandlers{
OnFilteredBlockAdded: func(height uint64, header *wire.BlockHeader,
txs []*util.Tx) {
client.onBlockAdded <- &blockAddedMsg{
chainHeight: height,
header: header,
txs: txs,
}
},
}
var err error
client.Client, err = rpcclient.New(connCfg, notificationHandlers)
if err != nil {
return nil, errors.Errorf("Error connecting to address %s: %s", connCfg.Host, err)
}
if err = client.NotifyBlocks(); err != nil {
return nil, errors.Errorf("Error while registering client %s for block notifications: %s", client.Host(), err)
}
return client, nil
}