package commands import ( "context" "math" "math/big" "testing" "github.com/holiman/uint256" "github.com/ledgerwatch/erigon-lib/kv" "github.com/ledgerwatch/erigon-lib/kv/kvcache" "github.com/ledgerwatch/erigon/common" "github.com/ledgerwatch/erigon/core" "github.com/ledgerwatch/erigon/core/types" "github.com/ledgerwatch/erigon/crypto" "github.com/ledgerwatch/erigon/params" "github.com/ledgerwatch/erigon/turbo/snapshotsync" "github.com/ledgerwatch/erigon/turbo/stages" ) func TestGasPrice(t *testing.T) { cases := []struct { description string chainSize int expectedPrice *big.Int }{ { description: "standard settings 60 blocks", chainSize: 60, expectedPrice: big.NewInt(params.GWei * int64(36)), }, { description: "standard settings 30 blocks", chainSize: 30, expectedPrice: big.NewInt(params.GWei * int64(18)), }, } for _, testCase := range cases { t.Run(testCase.description, func(t *testing.T) { db := createGasPriceTestKV(t, testCase.chainSize) defer db.Close() stateCache := kvcache.New(kvcache.DefaultCoherentConfig) base := NewBaseApi(nil, stateCache, snapshotsync.NewBlockReader(), nil, nil, false) eth := NewEthAPI(base, db, nil, nil, nil, 5000000) ctx := context.Background() result, err := eth.GasPrice(ctx) if err != nil { t.Fatalf("error getting gas price: %s", err) } if testCase.expectedPrice.Cmp(result.ToInt()) != 0 { t.Fatalf("gas price mismatch, want %d, got %d", testCase.expectedPrice, result.ToInt()) } }) } } func createGasPriceTestKV(t *testing.T, chainSize int) kv.RwDB { var ( key, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291") addr = crypto.PubkeyToAddress(key.PublicKey) gspec = &core.Genesis{ Config: params.TestChainConfig, Alloc: core.GenesisAlloc{addr: {Balance: big.NewInt(math.MaxInt64)}}, } signer = types.LatestSigner(gspec.Config) ) m := stages.MockWithGenesis(t, gspec, key, false) // Generate testing blocks chain, err := core.GenerateChain(m.ChainConfig, m.Genesis, m.Engine, m.DB, chainSize, func(i int, b *core.BlockGen) { b.SetCoinbase(common.Address{1}) tx, txErr := types.SignTx(types.NewTransaction(b.TxNonce(addr), common.HexToAddress("deadbeef"), uint256.NewInt(100), 21000, uint256.NewInt(uint64(int64(i+1)*params.GWei)), nil), *signer, key) if txErr != nil { t.Fatalf("failed to create tx: %v", txErr) } b.AddTx(tx) }, false) if err != nil { t.Error(err) } // Construct testing chain if err = m.InsertChain(chain); err != nil { t.Error(err) } return m.DB }