github.com/cgcardona/r-subnet-evm@v0.1.5/consensus/dummy/consensus_test.go (about)

     1  // (c) 2019-2020, Ava Labs, Inc. All rights reserved.
     2  // See the file LICENSE for licensing terms.
     3  
     4  package dummy
     5  
     6  import (
     7  	"math"
     8  	"math/big"
     9  	"testing"
    10  
    11  	"github.com/cgcardona/r-subnet-evm/core/types"
    12  	"github.com/cgcardona/r-subnet-evm/params"
    13  	"github.com/ethereum/go-ethereum/common"
    14  )
    15  
    16  var testBlockGasCostStep = big.NewInt(50_000)
    17  
    18  func TestVerifyBlockFee(t *testing.T) {
    19  	tests := map[string]struct {
    20  		baseFee                 *big.Int
    21  		parentBlockGasCost      *big.Int
    22  		parentTime, currentTime uint64
    23  		txs                     []*types.Transaction
    24  		receipts                []*types.Receipt
    25  		shouldErr               bool
    26  	}{
    27  		"tx only base fee": {
    28  			baseFee:            big.NewInt(100),
    29  			parentBlockGasCost: big.NewInt(0),
    30  			parentTime:         10,
    31  			currentTime:        10,
    32  			txs: []*types.Transaction{
    33  				types.NewTransaction(0, common.HexToAddress("7ef5a6135f1fd6a02593eedc869c6d41d934aef8"), big.NewInt(0), 100, big.NewInt(100), nil),
    34  			},
    35  			receipts: []*types.Receipt{
    36  				{GasUsed: 1000},
    37  			},
    38  			shouldErr: true,
    39  		},
    40  		"tx covers exactly block fee": {
    41  			baseFee:            big.NewInt(100),
    42  			parentBlockGasCost: big.NewInt(0),
    43  			parentTime:         10,
    44  			currentTime:        10,
    45  			txs: []*types.Transaction{
    46  				types.NewTransaction(0, common.HexToAddress("7ef5a6135f1fd6a02593eedc869c6d41d934aef8"), big.NewInt(0), 100_000, big.NewInt(200), nil),
    47  			},
    48  			receipts: []*types.Receipt{
    49  				{GasUsed: 100_000},
    50  			},
    51  			shouldErr: false,
    52  		},
    53  		"txs share block fee": {
    54  			baseFee:            big.NewInt(100),
    55  			parentBlockGasCost: big.NewInt(0),
    56  			parentTime:         10,
    57  			currentTime:        10,
    58  			txs: []*types.Transaction{
    59  				types.NewTransaction(0, common.HexToAddress("7ef5a6135f1fd6a02593eedc869c6d41d934aef8"), big.NewInt(0), 100_000, big.NewInt(200), nil),
    60  				types.NewTransaction(1, common.HexToAddress("7ef5a6135f1fd6a02593eedc869c6d41d934aef8"), big.NewInt(0), 100_000, big.NewInt(100), nil),
    61  			},
    62  			receipts: []*types.Receipt{
    63  				{GasUsed: 100_000},
    64  				{GasUsed: 100_000},
    65  			},
    66  			shouldErr: false,
    67  		},
    68  		"txs split block fee": {
    69  			baseFee:            big.NewInt(100),
    70  			parentBlockGasCost: big.NewInt(0),
    71  			parentTime:         10,
    72  			currentTime:        10,
    73  			txs: []*types.Transaction{
    74  				types.NewTransaction(0, common.HexToAddress("7ef5a6135f1fd6a02593eedc869c6d41d934aef8"), big.NewInt(0), 100_000, big.NewInt(150), nil),
    75  				types.NewTransaction(1, common.HexToAddress("7ef5a6135f1fd6a02593eedc869c6d41d934aef8"), big.NewInt(0), 100_000, big.NewInt(150), nil),
    76  			},
    77  			receipts: []*types.Receipt{
    78  				{GasUsed: 100_000},
    79  				{GasUsed: 100_000},
    80  			},
    81  			shouldErr: false,
    82  		},
    83  		"tx only base fee after full time window": {
    84  			baseFee:            big.NewInt(100),
    85  			parentBlockGasCost: big.NewInt(500_000),
    86  			parentTime:         10,
    87  			currentTime:        22, // 2s target + 10
    88  			txs: []*types.Transaction{
    89  				types.NewTransaction(0, common.HexToAddress("7ef5a6135f1fd6a02593eedc869c6d41d934aef8"), big.NewInt(0), 100, big.NewInt(100), nil),
    90  			},
    91  			receipts: []*types.Receipt{
    92  				{GasUsed: 1000},
    93  			},
    94  			shouldErr: false,
    95  		},
    96  		"tx only base fee after large time window": {
    97  			baseFee:            big.NewInt(100),
    98  			parentBlockGasCost: big.NewInt(100_000),
    99  			parentTime:         0,
   100  			currentTime:        math.MaxUint64,
   101  			txs: []*types.Transaction{
   102  				types.NewTransaction(0, common.HexToAddress("7ef5a6135f1fd6a02593eedc869c6d41d934aef8"), big.NewInt(0), 100, big.NewInt(100), nil),
   103  			},
   104  			receipts: []*types.Receipt{
   105  				{GasUsed: 1000},
   106  			},
   107  			shouldErr: false,
   108  		},
   109  		"parent time > current time": {
   110  			baseFee:            big.NewInt(100),
   111  			parentBlockGasCost: big.NewInt(0),
   112  			parentTime:         11,
   113  			currentTime:        10,
   114  			txs:                nil,
   115  			receipts:           nil,
   116  			shouldErr:          true,
   117  		},
   118  	}
   119  
   120  	for name, test := range tests {
   121  		t.Run(name, func(t *testing.T) {
   122  			blockGasCost := calcBlockGasCost(
   123  				params.DefaultFeeConfig.TargetBlockRate,
   124  				params.DefaultFeeConfig.MinBlockGasCost,
   125  				params.DefaultFeeConfig.MaxBlockGasCost,
   126  				testBlockGasCostStep,
   127  				test.parentBlockGasCost,
   128  				test.parentTime, test.currentTime,
   129  			)
   130  			engine := NewFaker()
   131  			if err := engine.verifyBlockFee(test.baseFee, blockGasCost, test.txs, test.receipts); err != nil {
   132  				if !test.shouldErr {
   133  					t.Fatalf("Unexpected error: %s", err)
   134  				}
   135  			} else {
   136  				if test.shouldErr {
   137  					t.Fatal("Should have failed verification")
   138  				}
   139  			}
   140  		})
   141  	}
   142  }