// Copyright 2015 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.

package core

import (
	"errors"
	"fmt"

	"github.com/ethereum/go-ethereum/common"
	"github.com/ethereum/go-ethereum/consensus"
	"github.com/ethereum/go-ethereum/core/state"
	"github.com/ethereum/go-ethereum/core/types"
	"github.com/ethereum/go-ethereum/log"
	"github.com/ethereum/go-ethereum/params"
	"github.com/ethereum/go-ethereum/trie"
)

// BlockValidator is responsible for validating block headers, uncles and
// processed state.
//
// BlockValidator implements Validator.
type BlockValidator struct {
	config *params.ChainConfig // Chain configuration options
	bc     *BlockChain         // Canonical block chain
}

// NewBlockValidator returns a new block validator which is safe for re-use
func NewBlockValidator(config *params.ChainConfig, blockchain *BlockChain) *BlockValidator {
	validator := &BlockValidator{
		config: config,
		bc:     blockchain,
	}
	return validator
}

// ValidateBody validates the given block's uncles and verifies the block
// header's transaction and uncle roots. The headers are assumed to be already
// validated at this point.
func (v *BlockValidator) ValidateBody(block *types.Block) error {
	// check EIP 7934 RLP-encoded block size cap
	if v.config.IsOsaka(block.Number(), block.Time()) && block.Size() > params.MaxBlockSize {
		return ErrBlockOversized
	}
	// Check whether the block is already imported.
	if v.bc.HasBlockAndState(block.Hash(), block.NumberU64()) {
		return ErrKnownBlock
	}

	// Header validity is known at this point. Here we verify that uncles, transactions
	// and withdrawals given in the block body match the header.
	header := block.Header()
	if err := v.bc.engine.VerifyUncles(v.bc, block); err != nil {
		return err
	}
	if hash := types.CalcUncleHash(block.Uncles()); hash != header.UncleHash {
		return fmt.Errorf("uncle root hash mismatch (header value %x, calculated %x)", header.UncleHash, hash)
	}
	if hash := types.DeriveSha(block.Transactions(), trie.NewStackTrie(nil)); hash != header.TxHash {
		return fmt.Errorf("transaction root hash mismatch (header value %x, calculated %x)", header.TxHash, hash)
	}

	// Withdrawals are present after the Shanghai fork.
	if header.WithdrawalsHash != nil {
		// Withdrawals list must be present in body after Shanghai.
		if block.Withdrawals() == nil {
			return errors.New("missing withdrawals in block body")
		}
		if hash := types.DeriveSha(block.Withdrawals(), trie.NewStackTrie(nil)); hash != *header.WithdrawalsHash {
			return fmt.Errorf("withdrawals root hash mismatch (header value %x, calculated %x)", *header.WithdrawalsHash, hash)
		}
	} else if block.Withdrawals() != nil {
		// Withdrawals are not allowed prior to Shanghai fork
		return errors.New("withdrawals present in block body")
	}

	// Blob transactions may be present after the Cancun fork.
	var blobs int
	for i, tx := range block.Transactions() {
		// Count the number of blobs to validate against the header's blobGasUsed
		blobs += len(tx.BlobHashes())

		// If the tx is a blob tx, it must NOT have a sidecar attached to be valid in a block.
		if tx.BlobTxSidecar() != nil {
			return fmt.Errorf("unexpected blob sidecar in transaction at index %d", i)
		}

		// The individual checks for blob validity (version-check + not empty)
		// happens in state transition.
	}

	// Check blob gas usage.
	if header.BlobGasUsed != nil {
		if want := *header.BlobGasUsed / params.BlobTxBlobGasPerBlob; uint64(blobs) != want { // div because the header is surely good vs the body might be bloated
			return fmt.Errorf("blob gas used mismatch (header %v, calculated %v)", *header.BlobGasUsed, blobs*params.BlobTxBlobGasPerBlob)
		}
	} else {
		if blobs > 0 {
			return errors.New("data blobs present in block body")
		}
	}

	// Ancestor block must be known.
	// XDC v114: If the parent block is the trusted checkpoint block (sync-from-block),
	// allow unknown parent since the checkpoint is a trusted anchor and the
	// pre-checkpoint chain is intentionally skipped.
	parentNum := block.NumberU64() - 1
	parentHash := block.ParentHash()
	parentIsTrustedCheckpoint := false
	if v.config.XDPoS != nil {
		for _, cp := range v.config.TrustedSyncCheckpoints {
			if cp != nil && cp.Number == parentNum && cp.Hash == parentHash {
				parentIsTrustedCheckpoint = true
				break
			}
		}
	}
	if !parentIsTrustedCheckpoint && !v.bc.HasBlockAndState(parentHash, parentNum) {
		// XDC: During checkpoint sync without state, the parent state won't exist
		// until state is built incrementally. Skip the state check if we're in
		// checkpoint sync mode.
		checkpointNoState := v.bc.checkpointSyncNoState.Load()
		if checkpointNoState {
			// Parent state missing during checkpoint sync — this is expected
			log.Debug("ValidateBody: checkpointSyncNoState=true, skipping state check", "block", block.NumberU64(), "parent", parentHash.Hex()[:16])
		} else if !v.bc.HasBlock(parentHash, parentNum) {
			log.Warn("ValidateBody ErrUnknownAncestor", "number", block.NumberU64(), "parent", parentHash.Hex(), "parentNum", parentNum)
			return consensus.ErrUnknownAncestor
		} else {
			// During genesis sync with legacy peers (no snap sync), parent state
			// may not be available even though the block exists. Within the first
			// 10000 blocks from genesis, allow missing state and let ProcessBlock
			// create empty state incrementally.
			if v.config.XDPoS != nil && parentNum < 10000 {
				log.Debug("ValidateBody: genesis sync early block, allowing missing parent state",
					"block", block.NumberU64(), "parent", parentHash.Hex()[:16], "parentNum", parentNum)
				// Don't return ErrPrunedAncestor; let insertChain proceed.
				// ProcessBlock will handle missing state via empty state fallback.
			} else {
				log.Warn("ValidateBody ErrPrunedAncestor", "block", block.NumberU64(), "parent", parentHash.Hex()[:16], "checkpointNoState", checkpointNoState)
				return consensus.ErrPrunedAncestor
			}
		}
	}

	// XDC Network: Cache signing transactions for reward calculation.
	// During initial sync, blocks aren't written to DB before Finalize is called.
	// We cache signing txs during body validation so they're available for
	// reward calculation at checkpoint blocks (e.g., block 1800).
	if v.config.ChainID != nil {
		chainID := v.config.ChainID.Uint64()
		if chainID == 50 || chainID == 51 || chainID == 5551 {
			if engine, ok := v.bc.engine.(interface{ CacheSigningTxsFromBlock(*types.Block) }); ok {
				engine.CacheSigningTxsFromBlock(block)
			}
		}
	}

	return nil
}

// ValidateState validates the various changes that happen after a state transition,
// such as amount of used gas, the receipt roots and the state root itself.
func (v *BlockValidator) ValidateState(block *types.Block, statedb *state.StateDB, res *ProcessResult, stateless bool) error {
	if res == nil {
		return errors.New("nil ProcessResult value")
	}
	header := block.Header()
	// XDC: During checkpoint sync, parent state may be missing which causes
	// gas execution to differ from the canonical chain. Skip gas validation
	// when checkpointSyncNoState is active and we are below the trusted checkpoint.
	if block.GasUsed() != res.GasUsed {
		checkpointNoState := v.bc.checkpointSyncNoState.Load()
		if checkpointNoState && v.config.XDPoS != nil {
			log.Warn("Gas mismatch during checkpoint sync, allowing", "block", block.NumberU64(), "remote", block.GasUsed(), "local", res.GasUsed)
		} else {
			return fmt.Errorf("invalid gas used (remote: %d local: %d)", block.GasUsed(), res.GasUsed)
		}
	}
	// Validate the received block's bloom with the one derived from the generated receipts.
	// For valid blocks this should always validate to true.
	//
	// Receipts must go through MakeReceipt to calculate the receipt's bloom
	// already. Merge the receipt's bloom together instead of recalculating
	// everything.
	rbloom := types.MergeBloom(res.Receipts)
	if rbloom != header.Bloom {
		return fmt.Errorf("invalid bloom (remote: %x  local: %x)", header.Bloom, rbloom)
	}
	// In stateless mode, return early because the receipt and state root are not
	// provided through the witness, rather the cross validator needs to return it.
	if stateless {
		return nil
	}
	// The receipt Trie's root (R = (Tr [[H1, R1], ... [Hn, Rn]]))
	receiptSha := types.DeriveSha(res.Receipts, trie.NewStackTrie(nil))
	if receiptSha != header.ReceiptHash {
		return fmt.Errorf("invalid receipt root hash (remote: %x local: %x)", header.ReceiptHash, receiptSha)
	}
	// Validate the parsed requests match the expected header value.
	if header.RequestsHash != nil {
		reqhash := types.CalcRequestsHash(res.Requests)
		if reqhash != *header.RequestsHash {
			return fmt.Errorf("invalid requests hash (remote: %x local: %x)", *header.RequestsHash, reqhash)
		}
	} else if res.Requests != nil {
		return errors.New("block has requests before prague fork")
	}
	// Validate the state root against the received state root and throw
	// an error if they don't match.
	root := statedb.IntermediateRoot(v.config.IsEIP158(header.Number))
	if header.Root != root {
		return fmt.Errorf("invalid merkle root (remote: %x local: %x) dberr: %w", header.Root, root, statedb.Error())
	}
	return nil
}

// xdcStateRootMismatchCounter tracks how many state root mismatches we've logged
var xdcStateRootMismatchCounter uint64

// logXDCStateRootMismatch logs state root mismatches for XDC chains.
// First 10 mismatches are logged at INFO level, then reduced to DEBUG.
func logXDCStateRootMismatch(blockNum uint64, remoteRoot, localRoot common.Hash) {
	if xdcStateRootMismatchCounter < 10 {
		log.Info("XDC state root bypass",
			"block", blockNum,
			"remote", remoteRoot.Hex(),
			"local", localRoot.Hex(),
			"count", xdcStateRootMismatchCounter)
	} else {
		log.Debug("XDC state root bypass",
			"block", blockNum,
			"remote", remoteRoot.Hex(),
			"local", localRoot.Hex())
	}
	xdcStateRootMismatchCounter++
}

// CalcGasLimit computes the gas limit of the next block after parent. It aims
// to keep the baseline gas close to the provided target, and increase it towards
// the target if the baseline gas is lower.
func CalcGasLimit(parentGasLimit, desiredLimit uint64) uint64 {
	delta := parentGasLimit/params.GasLimitBoundDivisor - 1
	limit := parentGasLimit
	if desiredLimit < params.MinGasLimit {
		desiredLimit = params.MinGasLimit
	}
	// If we're outside our allowed gas range, we try to hone towards them
	if limit < desiredLimit {
		limit = parentGasLimit + delta
		if limit > desiredLimit {
			limit = desiredLimit
		}
		return limit
	}
	if limit > desiredLimit {
		limit = parentGasLimit - delta
		if limit < desiredLimit {
			limit = desiredLimit
		}
	}
	return limit
}
