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

     1  package native
     2  
     3  import (
     4  	"context"
     5  	"math/big"
     6  
     7  	"github.com/unicornultrafoundation/go-helios/hash"
     8  	go_u2u "github.com/unicornultrafoundation/go-u2u"
     9  	"github.com/unicornultrafoundation/go-u2u/common"
    10  	"github.com/unicornultrafoundation/go-u2u/common/hexutil"
    11  
    12  	"github.com/unicornultrafoundation/go-u2u/native"
    13  )
    14  
    15  // GetEvent returns Hashgraph event by hash or short ID.
    16  func (ec *Client) GetEvent(ctx context.Context, h hash.Event) (e native.EventI, err error) {
    17  	var raw map[string]interface{}
    18  	err = ec.c.CallContext(ctx, &raw, "dag_getEvent", h.Hex())
    19  	if err != nil {
    20  		return
    21  	} else if len(raw) == 0 {
    22  		err = go_u2u.NotFound
    23  		return
    24  	}
    25  
    26  	e = native.RPCUnmarshalEvent(raw)
    27  	return
    28  }
    29  
    30  // GetEvent returns Hashgraph event by hash or short ID.
    31  func (ec *Client) GetEventPayload(ctx context.Context, h hash.Event, inclTx bool) (e native.EventI, txs []common.Hash, err error) {
    32  	var raw map[string]interface{}
    33  	err = ec.c.CallContext(ctx, &raw, "dag_getEventPayload", h.Hex(), inclTx)
    34  	if err != nil {
    35  		return
    36  	} else if len(raw) == 0 {
    37  		err = go_u2u.NotFound
    38  		return
    39  	}
    40  
    41  	e = native.RPCUnmarshalEvent(raw)
    42  
    43  	if inclTx {
    44  		vv := raw["transactions"].([]interface{})
    45  		txs = make([]common.Hash, len(vv))
    46  		for i, v := range vv {
    47  			txs[i] = common.HexToHash(v.(string))
    48  		}
    49  	}
    50  
    51  	return
    52  }
    53  
    54  // GetHeads returns IDs of all the epoch events with no descendants.
    55  // * When epoch is -2 the heads for latest epoch are returned.
    56  // * When epoch is -1 the heads for latest sealed epoch are returned.
    57  func (ec *Client) GetHeads(ctx context.Context, epoch *big.Int) (hash.Events, error) {
    58  	var raw []interface{}
    59  	err := ec.c.CallContext(ctx, &raw, "dag_getHeads", toBlockNumArg(epoch))
    60  	if err != nil {
    61  		return nil, err
    62  	}
    63  
    64  	return native.HexToEventIDs(raw), nil
    65  }
    66  
    67  // GetEpochStats returns epoch statistics.
    68  // * When epoch is -2 the statistics for latest epoch is returned.
    69  // * When epoch is -1 the statistics for latest sealed epoch is returned.
    70  func (ec *Client) GetEpochStats(ctx context.Context, epoch *big.Int) (map[string]interface{}, error) {
    71  	var raw map[string]interface{}
    72  	err := ec.c.CallContext(ctx, &raw, "dag_getEpochStats", toBlockNumArg(epoch))
    73  	if err != nil {
    74  		return nil, err
    75  	} else if len(raw) == 0 {
    76  		return nil, go_u2u.NotFound
    77  	}
    78  
    79  	return raw, nil
    80  }
    81  
    82  func toBlockNumArg(number *big.Int) string {
    83  	if number == nil {
    84  		return "latest"
    85  	}
    86  	pending := big.NewInt(-1)
    87  	if number.Cmp(pending) == 0 {
    88  		return "pending"
    89  	}
    90  	return hexutil.EncodeBig(number)
    91  }