forked from OffchainLabs/nitro
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathactivate_test.go
108 lines (87 loc) · 2.9 KB
/
activate_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// Copyright 2021-2024, Offchain Labs, Inc.
// For license information, see https://github.com/nitro/blob/master/LICENSE
package arbos
import (
"math/rand"
"testing"
"time"
"github.com/ethereum/go-ethereum/common"
"github.com/offchainlabs/nitro/arbos/arbosState"
"github.com/offchainlabs/nitro/arbos/programs"
"github.com/offchainlabs/nitro/util/arbmath"
"github.com/offchainlabs/nitro/util/colors"
"github.com/offchainlabs/nitro/util/testhelpers"
)
func TestActivationDataFee(t *testing.T) {
rand.Seed(time.Now().UTC().UnixNano())
state, _ := arbosState.NewArbosMemoryBackedArbOSState()
pricer := state.Programs().DataPricer()
// #nosec G115
time := uint64(time.Now().Unix())
assert := func(cond bool) {
t.Helper()
if !cond {
Fail(t, "assertion failed")
}
}
hour := uint64(60 * 60)
commonSize := uint32(5 * 1024 * 1024)
fee, _ := pricer.UpdateModel(0, time)
assert(fee.Uint64() == 0)
firstHourlyFee, _ := pricer.UpdateModel(commonSize, time)
assert(firstHourlyFee.Uint64() > 0)
capacity := uint32(programs.InitialHourlyBytes)
usage := uint32(0)
lastFee := common.Big0
totalFees := common.Big0
reset := func() {
capacity = uint32(programs.InitialHourlyBytes)
usage = uint32(0)
lastFee = common.Big0
totalFees = common.Big0
}
reset()
for usage < capacity {
bytes := uint32(5 * 1024 * 1024)
fee, _ := pricer.UpdateModel(bytes, time+hour)
assert(arbmath.BigGreaterThan(fee, lastFee))
totalFees = arbmath.BigAdd(totalFees, fee)
usage += bytes
lastFee = fee
}
// ensure the chain made enough money
minimumTotal := arbmath.UintToBig(uint64(capacity))
minimumTotal = arbmath.BigMulByUint(minimumTotal, 59/10*1e9)
colors.PrintBlue("total ", totalFees.String(), " ", minimumTotal.String())
assert(arbmath.BigGreaterThan(totalFees, minimumTotal))
// advance a bit past an hour to reset the pricer
fee, _ = pricer.UpdateModel(commonSize, time+2*hour+60)
assert(arbmath.BigEquals(fee, firstHourlyFee))
// buy all the capacity at once
fee, _ = pricer.UpdateModel(capacity, time+3*hour)
colors.PrintBlue("every ", fee.String(), " ", minimumTotal.String())
assert(arbmath.BigGreaterThan(fee, minimumTotal))
reset()
for usage < capacity {
bytes := uint32(10 * 1024)
fee, _ := pricer.UpdateModel(bytes, time+5*hour)
assert(arbmath.BigGreaterThanOrEqual(fee, lastFee))
totalFees = arbmath.BigAdd(totalFees, fee)
usage += bytes
lastFee = fee
}
// check small programs
colors.PrintBlue("small ", totalFees.String(), " ", minimumTotal.String())
assert(arbmath.BigGreaterThan(totalFees, minimumTotal))
reset()
for usage < capacity {
bytes := testhelpers.RandomUint32(1, 1024*1024)
fee, _ := pricer.UpdateModel(bytes, time+7*hour)
totalFees = arbmath.BigAdd(totalFees, fee)
usage += bytes
lastFee = fee
}
// check random programs
colors.PrintBlue("rands ", totalFees.String(), " ", minimumTotal.String())
assert(arbmath.BigGreaterThan(totalFees, minimumTotal))
}