github.com/hyperledger/burrow@v0.34.5-0.20220512172541-77f09336001d/vent/chain/chain.go (about)

     1  // Abstracts over a Burrow GRPC connection and Ethereum json-rpc web3 connection for the purposes of vent
     2  
     3  package chain
     4  
     5  import (
     6  	"context"
     7  	"time"
     8  
     9  	"github.com/hyperledger/burrow/binary"
    10  	"github.com/hyperledger/burrow/crypto"
    11  	"github.com/hyperledger/burrow/event/query"
    12  	"github.com/hyperledger/burrow/execution/errors"
    13  	"github.com/hyperledger/burrow/rpc/rpcevents"
    14  	"github.com/hyperledger/burrow/vent/types"
    15  	"google.golang.org/grpc/connectivity"
    16  )
    17  
    18  const (
    19  	// Infura has free tier usage of 100,000 req/day
    20  	defaultMaxRequests       = 99_990
    21  	defaultTimeBase          = time.Hour * 24
    22  	defaultMaxRetires        = 5
    23  	defaultBackoffBase       = time.Second
    24  	defaultMaxBlockBatchSize = 100
    25  )
    26  
    27  type Chain interface {
    28  	GetChainID() string
    29  	GetVersion() string
    30  	ConsumeBlocks(ctx context.Context, in *rpcevents.BlockRange, consumer func(Block) error) error
    31  	StatusMessage(ctx context.Context, lastProcessedHeight uint64) []interface{}
    32  	Connectivity() connectivity.State
    33  	GetABI(ctx context.Context, address crypto.Address) (string, error)
    34  	Close() error
    35  }
    36  
    37  type Block interface {
    38  	GetHeight() uint64
    39  	GetTxs() []Transaction
    40  	GetMetadata(columns types.SQLColumnNames) (map[string]interface{}, error)
    41  }
    42  
    43  type Transaction interface {
    44  	GetHash() binary.HexBytes
    45  	GetIndex() uint64
    46  	GetEvents() []Event
    47  	GetException() *errors.Exception
    48  	GetOrigin() *Origin
    49  	GetMetadata(columns types.SQLColumnNames) (map[string]interface{}, error)
    50  }
    51  
    52  type Event interface {
    53  	query.Tagged
    54  	GetIndex() uint64
    55  	GetTransactionHash() binary.HexBytes
    56  	GetAddress() crypto.Address
    57  	GetTopics() []binary.Word256
    58  	GetData() []byte
    59  }
    60  
    61  type Filter struct {
    62  	Addresses []crypto.Address
    63  	Topics    []binary.Word256
    64  }
    65  
    66  type Origin struct {
    67  	// The original ChainID from for this transaction
    68  	ChainID string
    69  	// The original height at which this transaction was committed
    70  	Height uint64
    71  	// The original index in the block
    72  	Index uint64
    73  }
    74  
    75  // Client-side block consumer configuration. Requests are retried subject to backoff if a non-fatal error is detected
    76  type BlockConsumerConfig struct {
    77  	// The maximum number of requests to make per TimeBase before throttling requests
    78  	MaxRequests int
    79  	// The base duration over which to count requests to check for overage of MaxRequests
    80  	TimeBase time.Duration
    81  	// The base backoff - we wait this amount of time between each batch and we increase the backoff exponentially
    82  	// until we reach MaxRetries from BaseBackoffDuration
    83  	BaseBackoffDuration time.Duration
    84  	// The maximum number of retries before failing
    85  	MaxRetries uint64
    86  	// The default and maximum batch size for block requests, we will reduce it logarithmically to a single block
    87  	// when backing off
    88  	MaxBlockBatchSize uint64
    89  }
    90  
    91  func (config *BlockConsumerConfig) Complete() {
    92  	if config.MaxRequests == 0 {
    93  		config.MaxRequests = defaultMaxRequests
    94  	}
    95  	if config.TimeBase == 0 {
    96  		config.TimeBase = defaultTimeBase
    97  	}
    98  	if config.MaxBlockBatchSize == 0 {
    99  		config.MaxBlockBatchSize = defaultMaxBlockBatchSize
   100  	}
   101  	if config.BaseBackoffDuration == 0 {
   102  		config.BaseBackoffDuration = defaultBackoffBase
   103  	}
   104  	if config.MaxRetries == 0 {
   105  		config.MaxRetries = defaultMaxRetires
   106  	}
   107  }