github.com/TrueBlocks/trueblocks-core/src/apps/chifra@v0.0.0-20241022031540-b362680128f7/pkg/rpc/get_block_parts.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  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/base"
     9  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/walk"
    10  )
    11  
    12  // GetBlockTimestamp returns the timestamp associated with a given block
    13  func (conn *Connection) GetBlockTimestamp(bn base.Blknum) base.Timestamp {
    14  	block, _ := conn.GetBlockHeaderByNumber(bn)
    15  	return block.Timestamp
    16  }
    17  
    18  // GetBlockHashByHash returns a block's hash if it's a valid block
    19  func (conn *Connection) GetBlockHashByHash(hash string) (base.Hash, error) {
    20  	if block, err := conn.getLightBlockFromRpc(base.NOPOSN, base.HexToHash(hash)); err != nil {
    21  		return base.Hash{}, err
    22  	} else {
    23  		isFinal := base.IsFinal(conn.LatestBlockTimestamp, block.Timestamp)
    24  		if isFinal && conn.StoreWritable() && conn.EnabledMap[walk.Cache_Blocks] {
    25  			_ = conn.Store.Write(block, nil)
    26  		}
    27  		return block.Hash, nil
    28  	}
    29  }
    30  
    31  // GetBlockNumberByHash returns a block's hash if it's a valid block
    32  func (conn *Connection) GetBlockNumberByHash(hash string) (base.Blknum, error) {
    33  	if block, err := conn.getLightBlockFromRpc(base.NOPOSN, base.HexToHash(hash)); err != nil {
    34  		return 0, err
    35  	} else {
    36  		isFinal := base.IsFinal(conn.LatestBlockTimestamp, block.Timestamp)
    37  		if isFinal && conn.StoreWritable() && conn.EnabledMap[walk.Cache_Blocks] {
    38  			_ = conn.Store.Write(block, nil)
    39  		}
    40  		return block.BlockNumber, nil
    41  	}
    42  }
    43  
    44  // GetBlockHashByNumber returns a block's hash if it's a valid block
    45  func (conn *Connection) GetBlockHashByNumber(bn base.Blknum) (base.Hash, error) {
    46  	if block, err := conn.GetBlockHeaderByNumber(bn); err != nil {
    47  		return base.Hash{}, err
    48  	} else {
    49  		return block.Hash, err
    50  	}
    51  }