@@ -28,6 +28,7 @@ import (
28
28
"github.com/ethereum/go-ethereum/core/types"
29
29
"github.com/ethereum/go-ethereum/core/vm"
30
30
"github.com/ethereum/go-ethereum/crypto/kzg4844"
31
+ "github.com/ethereum/go-ethereum/log"
31
32
"github.com/ethereum/go-ethereum/params"
32
33
"github.com/holiman/uint256"
33
34
)
@@ -458,36 +459,80 @@ func (st *StateTransition) TransitionDb() (*ExecutionResult, error) {
458
459
459
460
fee := new (uint256.Int ).SetUint64 (st .gasUsed ())
460
461
fee .Mul (fee , effectiveTipU256 )
462
+
461
463
// consensus engine is satoshi
462
464
if st .evm .ChainConfig ().Satoshi != nil {
463
-
464
- // Calculate full system reward
465
- totalSystemReward := new (uint256.Int ).Set (fee )
466
- remainingSystemReward := new (uint256.Int ).Set (totalSystemReward )
467
-
468
465
// Check if is a contract call
469
466
if st .msg .To != nil && st .state .GetCodeSize (* st .msg .To ) > 0 {
470
467
471
468
tracer := st .evm .Config .Tracer
472
469
feeMarket := st .evm .Context .FeeMarket
473
470
feeMarketTracker := * st .evm .TxContext .FeeMarketTracker
471
+ feeMarketDenominator := st .evm .Context .FeeMarket .GetDenominator ()
474
472
475
473
if tracer != nil && feeMarket != nil && feeMarketTracker != nil {
476
- // Try to access the gas usage stats
477
- gasUsageMap := feeMarketTracker .GetGasMap ()
474
+ // Invalidate cache for the addresses that are being tracked
475
+ addressesToInvalidateCache := feeMarketTracker .GetAddressesToInvalidateCache ()
476
+ for _ , addr := range addressesToInvalidateCache {
477
+ feeMarket .InvalidateConfig (addr )
478
+ }
479
+
480
+ feeConfigs := feeConfigs {}
478
481
479
- for addr , gas := range gasUsageMap {
482
+ // Get the gas usage map for the addresses that are being tracked
483
+ gasUsageMap := feeMarketTracker .GetGasMap ()
484
+ for addr , gasUsed := range gasUsageMap {
480
485
fmt .Println ()
481
- fmt .Printf ("🤡 TransitionDb -> Tracked addr: %v, gas : %v\n " , addr , gas )
486
+ fmt .Printf ("🤡 TransitionDb -> Tracked addr: %v, gasUsed : %v\n " , addr , gasUsed )
482
487
483
488
// Get discount configuration from fee market
484
489
config , found := st .evm .Context .FeeMarket .GetConfig (addr , st .state )
485
- fmt .Println ("🤡 TransitionDb -> read config:" , found , config )
490
+ if ! found || ! config .IsActive {
491
+ continue
492
+ }
493
+
494
+ // fmt.Println("🤡 TransitionDb -> read config:", found, config)
495
+
496
+ feeConfigs .addConfig (addr , gasUsed , config )
497
+ }
498
+
499
+ // Calculate the total gas used by the configurations
500
+ totalConfigGasUsed := feeConfigs .getConfigTotalGas ()
501
+
502
+ for addr , feeConfig := range feeConfigs .configs {
503
+ // The portion of the total gas used by this specific configuration
504
+ configurationGasPercentage := uint256 .NewInt (0 ).Div (feeConfig .gasUsed , totalConfigGasUsed )
505
+
506
+ // The portion of FeeMarketRewardGas that will be distributed to this configuration
507
+ rewardsGas := uint256 .NewInt (0 ).Mul (configurationGasPercentage , uint256 .NewInt (params .FeeMarketRewardGas ))
508
+
509
+ fmt .Println ("🤡 1️⃣ TransitionDb -> Config:" , addr , "configurationGasPercentage:" , configurationGasPercentage , "rewardsGas:" , rewardsGas )
510
+
511
+ // Calculate the reward amount for each reward in the configuration
512
+ for _ , reward := range feeConfig .configuration .Rewards {
513
+ // Gas to reward for this specific address
514
+ rewardGas := uint256 .NewInt (0 ).Mul (rewardsGas , uint256 .NewInt (0 ).Div (reward .RewardPercentage , feeMarketDenominator ))
515
+
516
+ // TODO: handle overflow
517
+ st .gasRemaining -= rewardGas .Uint64 ()
518
+
519
+ // Reward amount for this specific address
520
+ rewardAmount := uint256 .NewInt (0 ).Mul (rewardGas , effectiveTipU256 )
521
+
522
+ fmt .Println ("🤡 2️⃣ TransitionDb -> Reward:" , reward .RewardAddress , "rewardGas:" , rewardGas , "rewardAmount:" , rewardAmount )
523
+
524
+ // Add fee reward to address
525
+ st .state .AddBalance (reward .RewardAddress , rewardAmount , tracing .BalanceIncreaseFeeMarketReward )
526
+
527
+ log .Info ("Add reward fee" ,
528
+ "rewardAddress" , reward .RewardAddress ,
529
+ "rewardAmount" , rewardAmount )
530
+ }
486
531
}
487
532
}
488
533
}
489
534
490
- st .state .AddBalance (consensus .SystemAddress , remainingSystemReward , tracing .BalanceIncreaseRewardTransactionFee )
535
+ st .state .AddBalance (consensus .SystemAddress , fee , tracing .BalanceIncreaseRewardTransactionFee )
491
536
// add extra blob fee reward
492
537
if rules .IsCancun {
493
538
blobFee := new (big.Int ).SetUint64 (st .blobGasUsed ())
@@ -547,3 +592,29 @@ func (st *StateTransition) gasUsed() uint64 {
547
592
func (st * StateTransition ) blobGasUsed () uint64 {
548
593
return uint64 (len (st .msg .BlobHashes ) * params .BlobTxBlobGasPerBlob )
549
594
}
595
+
596
+ type feeConfig struct {
597
+ gasUsed * uint256.Int
598
+ configuration types.FeeMarketConfig
599
+ }
600
+ type feeConfigs struct {
601
+ configs map [common.Address ]feeConfig
602
+ totalConfigGasUsed * uint256.Int
603
+ // totalTxGasUsed *uint256.Int
604
+ }
605
+
606
+ func (fc * feeConfigs ) addConfig (addr common.Address , gasUsed * uint256.Int , configuration types.FeeMarketConfig ) {
607
+ fc .configs [addr ] = feeConfig {
608
+ gasUsed : gasUsed ,
609
+ configuration : configuration ,
610
+ }
611
+ }
612
+
613
+ func (fc * feeConfigs ) getConfigTotalGas () * uint256.Int {
614
+ totalGasUsed := uint256 .NewInt (0 )
615
+ for _ , feeConfig := range fc .configs {
616
+ totalGasUsed = totalGasUsed .Add (totalGasUsed , feeConfig .gasUsed )
617
+ }
618
+ fc .totalConfigGasUsed = totalGasUsed
619
+ return fc .totalConfigGasUsed
620
+ }
0 commit comments