interface.go raw

   1  package chainclient
   2  
   3  import (
   4  	"github.com/p9c/p9/pkg/btcaddr"
   5  	"time"
   6  	
   7  	"github.com/p9c/p9/pkg/chainhash"
   8  	"github.com/p9c/p9/pkg/waddrmgr"
   9  	"github.com/p9c/p9/pkg/wire"
  10  	"github.com/p9c/p9/pkg/wtxmgr"
  11  )
  12  
  13  // BackEnds returns a list of the available back ends.
  14  // TODO: Refactor each into a driver and use dynamic registration.
  15  func BackEnds() []string {
  16  	return []string{
  17  		"bitcoind",
  18  		"pod",
  19  		"neutrino",
  20  	}
  21  }
  22  
  23  // Interface allows more than one backing blockchain source, such as a pod RPC chain server, or an SPV library, as long
  24  // as we write a driver for it.
  25  type Interface interface {
  26  	Start() error
  27  	Stop()
  28  	WaitForShutdown()
  29  	GetBestBlock() (*chainhash.Hash, int32, error)
  30  	GetBlock(*chainhash.Hash) (*wire.Block, error)
  31  	GetBlockHash(int64) (*chainhash.Hash, error)
  32  	GetBlockHeader(*chainhash.Hash) (*wire.BlockHeader, error)
  33  	FilterBlocks(*FilterBlocksRequest) (*FilterBlocksResponse, error)
  34  	BlockStamp() (*waddrmgr.BlockStamp, error)
  35  	SendRawTransaction(*wire.MsgTx, bool) (*chainhash.Hash, error)
  36  	Rescan(*chainhash.Hash, []btcaddr.Address, map[wire.OutPoint]btcaddr.Address) error
  37  	NotifyReceived([]btcaddr.Address) error
  38  	NotifyBlocks() error
  39  	Notifications() <-chan interface{}
  40  	BackEnd() string
  41  }
  42  
  43  // Notification types.
  44  //
  45  // These are defined here and processed from from reading a notificationChan to avoid handling these notifications
  46  // directly in rpcclient callbacks, which isn't very Go-like and doesn't allow blocking client calls.
  47  type (
  48  	// ClientConnected is a notification for when a client connection is opened or reestablished to the chain server.
  49  	ClientConnected struct{}
  50  	// BlockConnected is a notification for a newly-attached block to the best chain.
  51  	BlockConnected wtxmgr.BlockMeta
  52  	// FilteredBlockConnected is an alternate notification that contains both block and relevant transaction information
  53  	// in one struct, which allows atomic updates.
  54  	FilteredBlockConnected struct {
  55  		Block       *wtxmgr.BlockMeta
  56  		RelevantTxs []*wtxmgr.TxRecord
  57  	}
  58  	// FilterBlocksRequest specifies a range of blocks and the set of internal and external addresses of interest,
  59  	// indexed by corresponding scoped-index of the child address. A global set of watched outpoints is also included to
  60  	// monitor for spends.
  61  	FilterBlocksRequest struct {
  62  		Blocks           []wtxmgr.BlockMeta
  63  		ExternalAddrs    map[waddrmgr.ScopedIndex]btcaddr.Address
  64  		InternalAddrs    map[waddrmgr.ScopedIndex]btcaddr.Address
  65  		WatchedOutPoints map[wire.OutPoint]btcaddr.Address
  66  	}
  67  	// FilterBlocksResponse reports the set of all internal and external addresses found in response to a
  68  	// FilterBlockRequest, any outpoints found that correspond to those addresses, as well as the relevant transactions
  69  	// that can modify the wallet's balance. The index of the block within the FilterBlocksRequest is returned, such
  70  	// that the caller can reinitiate a request for the subsequent block after updating the addresses of interest.
  71  	FilterBlocksResponse struct {
  72  		BatchIndex         uint32
  73  		BlockMeta          wtxmgr.BlockMeta
  74  		FoundExternalAddrs map[waddrmgr.KeyScope]map[uint32]struct{}
  75  		FoundInternalAddrs map[waddrmgr.KeyScope]map[uint32]struct{}
  76  		FoundOutPoints     map[wire.OutPoint]btcaddr.Address
  77  		RelevantTxns       []*wire.MsgTx
  78  	}
  79  	// BlockDisconnected is a notifcation that the block described by the BlockStamp was reorganized out of the best
  80  	// chain.
  81  	BlockDisconnected wtxmgr.BlockMeta
  82  	// RelevantTx is a notification for a transaction which spends wallet inputs or pays to a watched address.
  83  	RelevantTx struct {
  84  		TxRecord *wtxmgr.TxRecord
  85  		Block    *wtxmgr.BlockMeta // nil if unmined
  86  	}
  87  	// RescanProgress is a notification describing the current status of an in-progress rescan.
  88  	RescanProgress struct {
  89  		Hash   *chainhash.Hash
  90  		Height int32
  91  		Time   time.Time
  92  	}
  93  	// RescanFinished is a notification that a previous rescan request has finished.
  94  	RescanFinished struct {
  95  		Hash   *chainhash.Hash
  96  		Height int32
  97  		Time   time.Time
  98  	}
  99  )
 100