This repository has been archived by the owner on Apr 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
/
chain_vm.go
131 lines (114 loc) · 3.25 KB
/
chain_vm.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// Copyright (C) 2019-2021, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package vm
import (
"github.com/ava-labs/avalanchego/database"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/utils/set"
log "github.com/inconshreveable/log15"
"github.com/ava-labs/spacesvm/chain"
)
func (vm *VM) Genesis() *chain.Genesis {
return vm.genesis
}
func (vm *VM) IsBootstrapped() bool {
return vm.bootstrapped.GetValue()
}
func (vm *VM) State() database.Database {
return vm.db
}
func (vm *VM) Mempool() chain.Mempool {
return vm.mempool
}
func (vm *VM) Verified(b *chain.StatelessBlock) {
vm.verifiedBlocks[b.ID()] = b
for _, tx := range b.Txs {
_ = vm.mempool.Remove(tx.ID())
}
log.Debug("verified block", "id", b.ID(), "parent", b.Prnt)
}
func (vm *VM) Rejected(b *chain.StatelessBlock) {
delete(vm.verifiedBlocks, b.ID())
for _, tx := range b.Txs {
vm.mempool.Add(tx)
}
log.Debug("rejected block", "id", b.ID())
}
func (vm *VM) Accepted(b *chain.StatelessBlock) {
vm.blocks.Put(b.ID(), b)
delete(vm.verifiedBlocks, b.ID())
vm.lastAccepted = b
log.Debug("accepted block", "blkID", b.ID())
if vm.config.ActivityCacheSize == 0 {
return
}
cs := uint64(vm.config.ActivityCacheSize)
for _, tx := range b.Txs {
activity := tx.Activity()
activity.Tmstmp = b.Tmstmp
vm.activityCache[vm.activityCacheCursor%cs] = activity
vm.activityCacheCursor++
if reward, ok := b.Winners[tx.ID()]; ok {
vm.activityCache[vm.activityCacheCursor%cs] = reward
vm.activityCacheCursor++
}
}
}
func (vm *VM) ExecutionContext(currTime int64, lastBlock *chain.StatelessBlock) (*chain.Context, error) {
g := vm.genesis
recentBlockIDs := set.Set[ids.ID]{}
recentTxIDs := set.Set[ids.ID]{}
recentUnits := uint64(0)
prices := []uint64{}
costs := []uint64{}
err := vm.lookback(currTime, lastBlock.ID(), func(b *chain.StatelessBlock) (bool, error) {
recentBlockIDs.Add(b.ID())
for _, tx := range b.StatefulBlock.Txs {
recentTxIDs.Add(tx.ID())
recentUnits += tx.LoadUnits(g)
}
prices = append(prices, b.Price)
costs = append(costs, b.Cost)
return true, nil
})
if err != nil {
return nil, err
}
// compute new block cost
secondsSinceLast := currTime - lastBlock.Tmstmp
nextCost := lastBlock.Cost
if secondsSinceLast < g.TargetBlockRate {
nextCost += uint64(g.TargetBlockRate - secondsSinceLast)
} else {
possibleDiff := uint64(secondsSinceLast - g.TargetBlockRate)
if nextCost >= chain.MinBlockCost && possibleDiff < nextCost-chain.MinBlockCost {
nextCost -= possibleDiff
} else {
nextCost = chain.MinBlockCost
}
}
if !g.BlockCostEnabled {
nextCost = lastBlock.Cost
}
// compute new min price
nextPrice := lastBlock.Price
if recentUnits > vm.targetRangeUnits {
nextPrice++
} else if recentUnits < vm.targetRangeUnits {
elapsedWindows := uint64(secondsSinceLast/g.LookbackWindow) + 1 // account for current window being less
if nextPrice >= g.MinPrice && elapsedWindows < nextPrice-g.MinPrice {
nextPrice -= elapsedWindows
} else {
nextPrice = g.MinPrice
}
}
return &chain.Context{
RecentBlockIDs: recentBlockIDs,
RecentTxIDs: recentTxIDs,
RecentLoadUnits: recentUnits,
Prices: prices,
Costs: costs,
NextPrice: nextPrice,
NextCost: nextCost,
}, nil
}