Skip to content
Merged
2 changes: 0 additions & 2 deletions vms/saevm/blocks/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ go_library(
"invariants.go",
"settlement.go",
"snow.go",
"time.go",
],
importpath = "github.com/ava-labs/avalanchego/vms/saevm/blocks",
visibility = ["//visibility:public"],
Expand Down Expand Up @@ -51,7 +50,6 @@ go_test(
"block_test.go",
"execution_test.go",
"settlement_test.go",
"time_test.go",
],
data = glob(["testdata/**"]),
embed = [":blocks"],
Expand Down
3 changes: 1 addition & 2 deletions vms/saevm/blocks/execution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"github.com/ava-labs/avalanchego/vms/components/gas"
"github.com/ava-labs/avalanchego/vms/saevm/cmputils"
"github.com/ava-labs/avalanchego/vms/saevm/gastime"
"github.com/ava-labs/avalanchego/vms/saevm/hook"
"github.com/ava-labs/avalanchego/vms/saevm/saetest"

saetypes "github.com/ava-labs/avalanchego/vms/saevm/types"
Expand Down Expand Up @@ -174,7 +173,7 @@ type selfAsHasher common.Hash

func (h selfAsHasher) Hash() common.Hash { return common.Hash(h) }

func mustNewGasTime(tb testing.TB, at time.Time, target, excess gas.Gas, gasPriceConfig hook.GasPriceConfig) *gastime.Time {
func mustNewGasTime(tb testing.TB, at time.Time, target, excess gas.Gas, gasPriceConfig gastime.GasPriceConfig) *gastime.Time {
tb.Helper()
tm, err := gastime.New(at, target, excess, gasPriceConfig)
require.NoError(tb, err, "gastime.New()")
Expand Down
4 changes: 2 additions & 2 deletions vms/saevm/blocks/settlement.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func (b *Block) MarkSynchronous(hooks hook.Points, db ethdb.Database, xdb types.
// would also require them to be received as an argument to MarkSynchronous.
target, cfg := hooks.GasConfigAfter(b.Header())
execTime, err := gastime.New(
PreciseTime(hooks, b.Header()),
hooks.BlockTime(b.Header()),
// Target, excess, and config _after_ are a requirement of
// [Block.MarkExecuted].
target,
Expand Down Expand Up @@ -283,7 +283,7 @@ func LastToSettleAt(hooks hook.Points, settleAt time.Time, parent *Block) (b *Bl
return block, known, nil
}

if startsNoEarlierThan := PreciseTime(hooks, block.Header()); startsNoEarlierThan.Compare(settleAt) > 0 {
if startsNoEarlierThan := hooks.BlockTime(block.Header()); startsNoEarlierThan.Compare(settleAt) > 0 {
known = true
continue
}
Expand Down
2 changes: 1 addition & 1 deletion vms/saevm/blocks/settlement_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func TestSettlementInvariants(t *testing.T) {
db := rawdb.NewMemoryDatabase()
xdb := saetest.NewExecutionResultsDB()
for _, b := range []*Block{b, parent, lastSettled} {
tm := mustNewGasTime(t, preciseTime(b.Header(), 0), 1, 0, gastime.DefaultGasPriceConfig())
tm := mustNewGasTime(t, time.Unix(int64(b.Header().Time), 0), 1, 0, gastime.DefaultGasPriceConfig()) //#nosec G115 -- block time is hard-coded above.
b.markExecutedForTests(t, db, xdb, tm)
}

Expand Down
39 changes: 0 additions & 39 deletions vms/saevm/blocks/time.go
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Confirming my understanding: PreciseTime() is replaced by hook.Points.BlockTime() and GasTime() is just proxytime.Of(hook.Points.BlockTime())?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PreciseTime is replaced by the hook 👍.
GasTime was never used.

This file was deleted.

95 changes: 0 additions & 95 deletions vms/saevm/blocks/time_test.go

This file was deleted.

5 changes: 0 additions & 5 deletions vms/saevm/gastime/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,8 @@ go_library(
visibility = ["//visibility:public"],
deps = [
"//vms/components/gas",
"//vms/saevm/hook",
"//vms/saevm/intmath",
"//vms/saevm/proxytime",
"@com_github_ava_labs_libevm//core/types",
"@com_github_google_go_cmp//cmp",
"@com_github_google_go_cmp//cmp/cmpopts",
"@com_github_holiman_uint256//:uint256",
Expand All @@ -36,12 +34,9 @@ go_test(
embed = [":gastime"],
deps = [
"//vms/components/gas",
"//vms/saevm/hook",
"//vms/saevm/hook/hookstest",
"//vms/saevm/intmath",
"//vms/saevm/proxytime",
"@com_github_arr4n_shed//testerr",
"@com_github_ava_labs_libevm//core/types",
"@com_github_ava_labs_libevm//params",
"@com_github_google_go_cmp//cmp",
"@com_github_google_go_cmp//cmp/cmpopts",
Expand Down
47 changes: 28 additions & 19 deletions vms/saevm/gastime/acp176.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,37 @@ package gastime
import (
"fmt"
"math"

"github.com/ava-labs/libevm/core/types"
"time"

"github.com/ava-labs/avalanchego/vms/components/gas"
"github.com/ava-labs/avalanchego/vms/saevm/hook"
"github.com/ava-labs/avalanchego/vms/saevm/intmath"
)

// BeforeBlock is intended to be called before processing a block, with the
// timestamp sourced from [hook.Points] and [types.Header].
func (tm *Time) BeforeBlock(hooks hook.Points, h *types.Header) {
tm.FastForwardTo(
h.Time,
SubSecond(hooks, h, tm.Rate()),
// BeforeBlock is intended to be called before processing a block with the
// provided time. The gastime is advanced to be no earlier than the block time.
func (tm *Time) BeforeBlock(bTime time.Time) {
Comment thread
alarso16 marked this conversation as resolved.
Outdated
s, ns := bTime.Unix(), bTime.Nanosecond()
// g = ceil(ns * rate / time.Second)
g, _, err := intmath.MulDivCeil(
gas.Gas(ns), //#nosec G115 -- ns is in [0, time.Second)
tm.Rate(),
gas.Gas(time.Second),
)
if err != nil {
// [time.Time.Nanosecond] is documented as only returning values in the
// range [0, time.Second). So either Nanosecond returned an incorrect
// value, or [intmath.MulDivCeil] incorrectly returned an error.
// Regardless, this failure MUST be detected in tests, hence not just
// dropping the error.
panic(fmt.Sprintf("broken invariant: %v", err))
}
tm.FastForwardTo(uint64(s), g) //#nosec G115 -- known non-negative.
}

// AfterBlock is intended to be called after processing a block, with the
// target and gas configuration sourced from [hook.Points] and [types.Header].
func (tm *Time) AfterBlock(used gas.Gas, hooks hook.Points, h *types.Header) error {
// target and gas configuration provided.
func (tm *Time) AfterBlock(used gas.Gas, target gas.Gas, cfg GasPriceConfig) error {
tm.Tick(used)
target, hookCfg := hooks.GasConfigAfter(h)
// Although [Time.SetTarget] scales the excess by the same factor as the
// change in target, it rounds when necessary, which might alter the price
// by a negligible amount. We therefore take a price snapshot beforehand
Expand All @@ -35,9 +45,8 @@ func (tm *Time) AfterBlock(used gas.Gas, hooks hook.Points, h *types.Header) err
p := tm.Price()
tm.SetTarget(target)

cfg, err := newConfig(hookCfg)
if err != nil {
return fmt.Errorf("%T.newConfig() after block: %w", tm, err)
if err := cfg.Validate(); err != nil {
return fmt.Errorf("%T.Validate() after block: %w", cfg, err)
}
if cfg.equals(tm.config) {
return nil
Expand All @@ -49,8 +58,8 @@ func (tm *Time) AfterBlock(used gas.Gas, hooks hook.Points, h *types.Header) err
}

// findExcessForPrice uses binary search over uint64 to find the smallest excess
// value that produces targetPrice with the current [config]. This maintains
// price continuity under a change in [config], with the following scenarios:
// value that produces targetPrice with the current [GasPriceConfig]. This maintains
// price continuity under a change in [GasPriceConfig], with the following scenarios:
//
// - K changes (via TargetToExcessScaling): Scale excess to maintain current price
// - StaticPricing is true: Set excess to 0, enabling fixed price mode
Expand All @@ -61,7 +70,7 @@ func (tm *Time) findExcessForPrice(targetPrice gas.Price) gas.Gas {
// We return 0 in case targetPrice < minPrice because we should at least maintain the minimum price
// by setting the excess to 0. ( P = M * e^(0 / K) = M )
// Note: Even though we return 0 for excess it won't avoid accumulating excess in the long run.
if targetPrice <= tm.config.minPrice || tm.config.staticPricing {
if targetPrice <= tm.config.MinPrice || tm.config.StaticPricing {
return 0
}

Expand All @@ -71,7 +80,7 @@ func (tm *Time) findExcessForPrice(targetPrice gas.Price) gas.Gas {
lo, hi := gas.Gas(0), gas.Gas(math.MaxUint64)
for lo < hi {
mid := lo + (hi-lo)/2
if gas.CalculatePrice(tm.config.minPrice, mid, k) >= targetPrice {
if gas.CalculatePrice(tm.config.MinPrice, mid, k) >= targetPrice {
hi = mid
} else {
lo = mid + 1
Expand Down
Loading
Loading