github.com/TrueBlocks/trueblocks-core/src/apps/chifra@v0.0.0-20241022031540-b362680128f7/pkg/rpc/get_withdrawal.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/logger"
    10  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/types"
    11  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/walk"
    12  )
    13  
    14  // GetMinerAndWithdrawals returns the miner and withdrawals for a block
    15  func (conn *Connection) GetMinerAndWithdrawals(bn base.Blknum) ([]types.Withdrawal, base.Address, error) {
    16  	if bn < base.KnownBlock(conn.Chain, base.Merge) {
    17  		return []types.Withdrawal{}, base.ZeroAddr, nil
    18  	}
    19  
    20  	if block, err := conn.GetBlockHeaderByNumber(bn); err != nil {
    21  		return []types.Withdrawal{}, base.ZeroAddr, nil
    22  	} else {
    23  		if withdrawals, err := conn.GetWithdrawalsByNumber(bn); err != nil {
    24  			return []types.Withdrawal{}, base.ZeroAddr, nil
    25  		} else {
    26  			return withdrawals, block.Miner, nil
    27  		}
    28  	}
    29  }
    30  
    31  // GetWithdrawalsByNumber returns all withdrawals in a block
    32  func (conn *Connection) GetWithdrawalsByNumber(bn base.Blknum) ([]types.Withdrawal, error) {
    33  	if bn < base.KnownBlock(conn.Chain, base.Shanghai) {
    34  		return []types.Withdrawal{}, nil
    35  	}
    36  
    37  	if conn.StoreReadable() {
    38  		// walk.Cache_Withdrawals
    39  		withdrawalGroup := &types.WithdrawalGroup{
    40  			BlockNumber:      bn,
    41  			TransactionIndex: base.NOPOSN,
    42  		}
    43  		if err := conn.Store.Read(withdrawalGroup, nil); err == nil {
    44  			return withdrawalGroup.Withdrawals, nil
    45  		}
    46  	}
    47  
    48  	if withdrawals, ts, err := conn.getWithdrawals(bn); err != nil {
    49  		return withdrawals, err
    50  	} else {
    51  		isFinal := base.IsFinal(conn.LatestBlockTimestamp, ts)
    52  		if isFinal && conn.StoreWritable() && conn.EnabledMap[walk.Cache_Withdrawals] {
    53  			withdrawalGroup := &types.WithdrawalGroup{
    54  				BlockNumber:      bn,
    55  				TransactionIndex: base.NOPOSN,
    56  				Withdrawals:      withdrawals,
    57  			}
    58  			if err = conn.Store.Write(withdrawalGroup, nil); err != nil {
    59  				logger.Warn("Failed to write withdrawals to cache", err)
    60  			}
    61  		}
    62  
    63  		return withdrawals, err
    64  	}
    65  }
    66  
    67  // getWithdrawals fetches the withdrawals from a block
    68  func (conn *Connection) getWithdrawals(bn base.Blknum) ([]types.Withdrawal, base.Timestamp, error) {
    69  	if block, err := conn.GetBlockHeaderByNumber(bn); err != nil {
    70  		return []types.Withdrawal{}, base.NOPOSI, err
    71  	} else {
    72  		return block.Withdrawals, block.Timestamp, nil
    73  	}
    74  }