github.com/unicornultrafoundation/go-u2u@v1.0.0-rc1.0.20240205080301-e74a83d3fadc/ethapi/dag_api.go (about)

     1  package ethapi
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  	"fmt"
     7  	"math/big"
     8  
     9  	"github.com/unicornultrafoundation/go-u2u/common/hexutil"
    10  	"github.com/unicornultrafoundation/go-u2u/log"
    11  	"github.com/unicornultrafoundation/go-u2u/rpc"
    12  
    13  	"github.com/unicornultrafoundation/go-u2u/native"
    14  )
    15  
    16  // PublicDAGChainAPI provides an API to access the directed acyclic graph chain.
    17  // It offers only methods that operate on public data that is freely available to anyone.
    18  type PublicDAGChainAPI struct {
    19  	b Backend
    20  }
    21  
    22  // NewPublicDAGChainAPI creates a new DAG chain API.
    23  func NewPublicDAGChainAPI(b Backend) *PublicDAGChainAPI {
    24  	return &PublicDAGChainAPI{b}
    25  }
    26  
    27  // GetEvent returns the Hashgraph event header by hash or short ID.
    28  func (s *PublicDAGChainAPI) GetEvent(ctx context.Context, shortEventID string) (map[string]interface{}, error) {
    29  	header, err := s.b.GetEvent(ctx, shortEventID)
    30  	if err != nil {
    31  		return nil, err
    32  	}
    33  	if header == nil {
    34  		return nil, fmt.Errorf("event %s not found", shortEventID)
    35  	}
    36  	return native.RPCMarshalEvent(header), nil
    37  }
    38  
    39  // GetEventPayload returns Hashgraph event by hash or short ID.
    40  func (s *PublicDAGChainAPI) GetEventPayload(ctx context.Context, shortEventID string, inclTx bool) (map[string]interface{}, error) {
    41  	event, err := s.b.GetEventPayload(ctx, shortEventID)
    42  	if err != nil {
    43  		return nil, err
    44  	}
    45  	if event == nil {
    46  		return nil, fmt.Errorf("event %s not found", shortEventID)
    47  	}
    48  	return native.RPCMarshalEventPayload(event, inclTx, false)
    49  }
    50  
    51  // GetHeads returns IDs of all the epoch events with no descendants.
    52  // * When epoch is -2 the heads for latest epoch are returned.
    53  // * When epoch is -1 the heads for latest sealed epoch are returned.
    54  func (s *PublicDAGChainAPI) GetHeads(ctx context.Context, epoch rpc.BlockNumber) ([]hexutil.Bytes, error) {
    55  	res, err := s.b.GetHeads(ctx, epoch)
    56  
    57  	if err != nil {
    58  		return nil, err
    59  	}
    60  
    61  	return native.EventIDsToHex(res), nil
    62  }
    63  
    64  // GetEpochStats returns epoch statistics.
    65  // * When epoch is -2 the statistics for latest epoch is returned.
    66  // * When epoch is -1 the statistics for latest sealed epoch is returned.
    67  func (s *PublicBlockChainAPI) GetEpochStats(ctx context.Context, requestedEpoch rpc.BlockNumber) (map[string]interface{}, error) {
    68  	log.Warn("GetEpochStats API call is deprecated. Consider retrieving data from SFC v3 contract.")
    69  	if requestedEpoch != rpc.LatestBlockNumber && requestedEpoch != rpc.BlockNumber(s.b.CurrentEpoch(ctx))-1 {
    70  		return nil, errors.New("getEpochStats API call doesn't support retrieving previous sealed epochs")
    71  	}
    72  	start, end := s.b.SealedEpochTiming(ctx)
    73  	return map[string]interface{}{
    74  		"epoch":                 hexutil.Uint64(s.b.CurrentEpoch(ctx) - 1),
    75  		"start":                 hexutil.Uint64(start),
    76  		"end":                   hexutil.Uint64(end),
    77  		"totalFee":              (*hexutil.Big)(new(big.Int)),
    78  		"totalBaseRewardWeight": (*hexutil.Big)(new(big.Int)),
    79  		"totalTxRewardWeight":   (*hexutil.Big)(new(big.Int)),
    80  	}, nil
    81  }