decred.org/dcrdex@v1.0.5/tatanka/chain/chain.go (about)

     1  // This code is available on the terms of the project LICENSE.md file,
     2  // also available online at https://blueoakcouncil.org/license/1.0.0.
     3  
     4  package chain
     5  
     6  import (
     7  	"context"
     8  	"encoding/json"
     9  	"fmt"
    10  	"sync"
    11  
    12  	"decred.org/dcrdex/dex"
    13  	"decred.org/dcrdex/tatanka/tanka"
    14  )
    15  
    16  /*
    17  	The chain package describes the interfaces that must be implemented for
    18  	Tatatanka node blockchain backends.
    19  */
    20  
    21  type BadQueryError error
    22  
    23  type ChainConfig struct {
    24  	ConfigPath string
    25  }
    26  
    27  // type Query json.RawMessage
    28  type Result json.RawMessage
    29  
    30  // Chain is an interface that must be implemented by every blockchain backend.
    31  type Chain interface {
    32  	Connect(context.Context) (*sync.WaitGroup, error)
    33  	// Query may be needed if clients are the auditors, since some clients might
    34  	// not have e.g. txindex enabled for utxo-based assets.
    35  	// Query(context.Context, Query) (Result, error)
    36  	Connected() bool
    37  	CheckBond(*tanka.Bond) error
    38  	// AuditHTLC will be needed if Tatanka nodes are the auditors.
    39  	// AuditHTLC(*tanka.HTLCAudit) (bool, error)
    40  }
    41  
    42  // FeeRater is an optional interface that should be implemented by backends for
    43  // which the network transaction fee rates are variable. The Tatanka Mesh
    44  // provides a oracle service for these chains.
    45  type FeeRater interface {
    46  	FeeChannel() <-chan uint64
    47  }
    48  
    49  // ChainConstructor is a constructor for a Chain.
    50  type ChainConstructor func(config json.RawMessage, log dex.Logger, net dex.Network) (Chain, error)
    51  
    52  var chains = make(map[uint32]ChainConstructor)
    53  
    54  // RegisterChainConstructor is called by chain backends to register their
    55  // ChainConstructors.
    56  func RegisterChainConstructor(chainID uint32, c ChainConstructor) {
    57  	chains[chainID] = c
    58  }
    59  
    60  // New is used by the caller to construct a new Chain.
    61  func New(chainID uint32, cfg json.RawMessage, log dex.Logger, net dex.Network) (Chain, error) {
    62  	c, found := chains[chainID]
    63  	if !found {
    64  		return nil, fmt.Errorf("chain %d not known", chainID)
    65  	}
    66  	return c(cfg, log, net)
    67  }