github.com/TrueBlocks/trueblocks-core/src/apps/chifra@v0.0.0-20241022031540-b362680128f7/pkg/rpc/get_uncle.go (about)

     1  // Copyright 2021 The TrueBlocks Authors. All rights reserved.
     2  // Use of this source code is governed by a license that can
     3  // be found in the LICENSE file.
     4  
     5  package rpc
     6  
     7  import (
     8  	"fmt"
     9  
    10  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/base"
    11  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/rpc/query"
    12  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/types"
    13  )
    14  
    15  // GetUncleBodiesByNumber returns the number of uncles in a block. (search: FromRpc)
    16  func (conn *Connection) GetUncleBodiesByNumber(bn base.Blknum) ([]types.Block, error) {
    17  	if count, err := conn.GetUnclesCountInBlock(bn); err != nil {
    18  		return nil, err
    19  
    20  	} else if count == 0 {
    21  		return []types.Block{}, nil
    22  
    23  	} else {
    24  		ret := make([]types.Block, 0, count)
    25  		for i := uint64(0); i < count; i++ {
    26  			method := "eth_getUncleByBlockNumberAndIndex"
    27  			params := query.Params{
    28  				fmt.Sprintf("0x%x", bn),
    29  				fmt.Sprintf("0x%x", i),
    30  			}
    31  
    32  			if uncle, err := query.Query[types.Block](conn.Chain, method, params); err != nil {
    33  				return ret, err
    34  			} else {
    35  				uncle.BlockNumber = uncle.Number
    36  				ret = append(ret, *uncle)
    37  			}
    38  		}
    39  		// TODO: BOGUS - avoid copy
    40  		return ret, nil
    41  	}
    42  }
    43  
    44  // GetUnclesCountInBlock returns the number of uncles in a block. (search: FromRpc)
    45  func (conn *Connection) GetUnclesCountInBlock(bn base.Blknum) (uint64, error) {
    46  	if bn >= base.KnownBlock(conn.Chain, base.Merge) {
    47  		return 0, nil
    48  	}
    49  
    50  	method := "eth_getUncleCountByBlockNumber"
    51  	params := query.Params{fmt.Sprintf("0x%x", bn)}
    52  
    53  	if count, err := query.Query[base.Value](conn.Chain, method, params); err != nil {
    54  		return 0, err
    55  	} else {
    56  		return uint64(*count), nil
    57  	}
    58  }