// Copyright 2021 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 eip1559

import (
	"errors"
	"fmt"
	"math/big"

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

// VerifyEIP1559Header verifies some header attributes which were changed in EIP-1559,
// - gas limit check
// - basefee check
//
// XDC-specific: uses config.IsEIP1559() instead of config.IsLondon() to support
// the XDC mainnet configuration where London and EIP-1559 activate at different blocks.
// Reference: XinFinOrg/XDPoSChain v2.6.8 params/config.go IsEIP1559()
func VerifyEIP1559Header(config *params.ChainConfig, parent, header *types.Header) error {
	// XDC-specific: If EIP-1559 is not active for this block, only check that
	// baseFee is nil (as expected for pre-EIP-1559 blocks), then skip all
	// further validation. This matches XinFinOrg/XDPoSChain dev-upgrade behavior.
	if !config.IsEIP1559(header.Number) {
		if header.BaseFee != nil {
			return fmt.Errorf("invalid baseFee: have %s, want <nil>", header.BaseFee)
		}
		return nil
	}

	// Verify that the gas limit remains within allowed bounds
	//
	// XDC-specific: On XDC networks (Apothem, mainnet), the gas limit does NOT
	// double at the EIP-1559 fork boundary. The actual chain keeps the same gas
	// limit (e.g., 420M on Apothem). The standard Ethereum behavior of doubling
	// via ElasticityMultiplier is only valid for pure Ethereum chains.
	//
	// Reference: XinFinOrg/XDPoSChain v2.6.8 does not apply elasticity
	// multiplier to parent gas limit at the fork.
	parentGasLimit := parent.GasLimit
	if !config.IsEIP1559(parent.Number) {
		// XDC: skip elasticity multiplier — gas limit stays constant across fork
		parentGasLimit = parent.GasLimit
	}
	if err := misc.VerifyGaslimit(parentGasLimit, header.GasLimit); err != nil {
		return err
	}
	// Verify the header is not malformed
	if header.BaseFee == nil {
		return errors.New("header is missing baseFee")
	}
	// XDC-specific: During checkpoint sync, the parent block may have BaseFee=nil
	// even though the canonical chain established a baseFee before the EIP-1559
	// fork. Skip validation in that exact narrow case (XDC chain, parent is
	// pre-fork, parent.BaseFee is missing). The skip is gated on
	// !config.IsEIP1559(parent.Number) so we never mask post-fork DB corruption.
	// We still require header.BaseFee > 0 to retain a minimum sanity check.
	// Reference: Issue #541, PR #542.
	if config.XDPoS != nil && parent.BaseFee == nil && !config.IsEIP1559(parent.Number) {
		if header.BaseFee.Sign() <= 0 {
			return fmt.Errorf("invalid baseFee at fork boundary: have %s, want positive value", header.BaseFee)
		}
		return nil
	}
	// v158 FIX: During checkpoint sync, the checkpoint header may have GasUsed=0
	// (from ancient store insertion) while the canonical block had GasUsed=25M.
	// When the first post-checkpoint block maintains the same baseFee (because
	// real GasUsed was at target), skip validation. The checkpoint is trusted.
	if config.XDPoS != nil && parent.GasUsed == 0 && parent.BaseFee != nil &&
		header.BaseFee.Cmp(parent.BaseFee) == 0 {
		log.Debug("[eip1559] Skipping baseFee validation at checkpoint boundary",
			"block", header.Number.Uint64(), "parentGasUsed", parent.GasUsed,
			"baseFee", header.BaseFee)
		return nil
	}
	expectedBaseFee := CalcBaseFee(config, parent)
	if header.BaseFee.Cmp(expectedBaseFee) != 0 {
		return fmt.Errorf("invalid baseFee: have %s, want %s, parentBaseFee %s, parentGasUsed %d",
			header.BaseFee, expectedBaseFee, parent.BaseFee, parent.GasUsed)
	}
	return nil
}

// CalcBaseFee calculates the basefee of the header.
//
// XDC-specific: uses config.IsEIP1559() instead of config.IsLondon() to support
// XDC mainnet where EIP-1559 is disabled (Eip1559Block = 9999999999).
//
// For XDC chains (config.XDPoS != nil), EIP-1559 is implemented with a *fixed*
// base fee rather than the dynamic Ethereum formula. XDPoSChain v2.6.8
// (consensus/misc/eip1559/eip1559.go + common/gas.go) returns
// common.BaseFee = 12,500,000,000 for every post-fork block, ignoring all
// parent gas inputs. The standard Ethereum formula below applies only to
// non-XDC chains.
func CalcBaseFee(config *params.ChainConfig, parent *types.Header) *big.Int {
	// XDC: fixed base fee post-fork.
	if config.XDPoS != nil {
		// Post-fork: every block carries XDCBaseFee.
		// Pre-fork: no base fee at all (header.BaseFee is nil on canonical
		// pre-fork blocks). Returning XDCBaseFee here keeps callers like the
		// txpool and miner non-nil even when consulted for a pre-fork parent —
		// preserves the pre-existing fallback contract while making the
		// post-fork answer match the canonical chain.
		return new(big.Int).SetUint64(params.XDCBaseFee)
	}

	// Standard Ethereum dynamic EIP-1559 formula below for non-XDC chains.

	// If the current block is the first EIP-1559 block, return the parent's baseFee.
	if !config.IsEIP1559(parent.Number) {
		if parent.BaseFee != nil {
			return new(big.Int).Set(parent.BaseFee)
		}
		return new(big.Int).SetUint64(params.InitialBaseFee)
	}
	// Defensive: a post-fork parent should always have BaseFee.
	if parent.BaseFee == nil {
		return new(big.Int).SetUint64(params.InitialBaseFee)
	}

	parentGasTarget := parent.GasLimit / config.ElasticityMultiplier()
	// If the parent gasUsed is the same as the target, the baseFee remains unchanged.
	if parent.GasUsed == parentGasTarget {
		return new(big.Int).Set(parent.BaseFee)
	}

	var (
		num   = new(big.Int)
		denom = new(big.Int)
	)

	if parent.GasUsed > parentGasTarget {
		// If the parent block used more gas than its target, the baseFee should increase.
		// max(1, parentBaseFee * gasUsedDelta / parentGasTarget / baseFeeChangeDenominator)
		num.SetUint64(parent.GasUsed - parentGasTarget)
		num.Mul(num, parent.BaseFee)
		num.Div(num, denom.SetUint64(parentGasTarget))
		num.Div(num, denom.SetUint64(config.BaseFeeChangeDenominator()))
		if num.Cmp(common.Big1) < 0 {
			return num.Add(parent.BaseFee, common.Big1)
		}
		return num.Add(parent.BaseFee, num)
	} else {
		// Otherwise if the parent block used less gas than its target, the baseFee should decrease.
		// max(0, parentBaseFee * gasUsedDelta / parentGasTarget / baseFeeChangeDenominator)
		num.SetUint64(parentGasTarget - parent.GasUsed)
		num.Mul(num, parent.BaseFee)
		num.Div(num, denom.SetUint64(parentGasTarget))
		num.Div(num, denom.SetUint64(config.BaseFeeChangeDenominator()))

		baseFee := num.Sub(parent.BaseFee, num)
		if baseFee.Cmp(common.Big0) < 0 {
			baseFee = common.Big0
		}
		return baseFee
	}
}
