-
Notifications
You must be signed in to change notification settings - Fork 109
/
accumulator.go
167 lines (146 loc) · 4.71 KB
/
accumulator.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package state
import (
"context"
"fmt"
"github.com/oasisprotocol/oasis-core/go/common/quantity"
abciAPI "github.com/oasisprotocol/oasis-core/go/consensus/tendermint/api"
staking "github.com/oasisprotocol/oasis-core/go/staking/api"
)
// StakeAccumulatorCache is a thin wrapper around an underlying escrow accounts which caches some
// values (like the threshold map) between operations.
type StakeAccumulatorCache struct {
ctx context.Context
// state is the mutable state.
state *MutableState
// accounts is a map of staking accounts that we are changing.
accounts map[staking.Address]*staking.Account
// thresholds is a cache of the threshold map.
thresholds map[staking.ThresholdKind]quantity.Quantity
}
func (c *StakeAccumulatorCache) getAccount(addr staking.Address) (*staking.Account, error) {
if c.accounts == nil {
c.accounts = make(map[staking.Address]*staking.Account)
}
if a := c.accounts[addr]; a != nil {
return a, nil
}
a, err := c.state.Account(c.ctx, addr)
if err != nil {
return nil, err
}
c.accounts[addr] = a
return a, nil
}
// CheckStakeClaims checks whether the escrow account balance satisfies all the stake claims.
func (c *StakeAccumulatorCache) CheckStakeClaims(addr staking.Address) error {
acct, err := c.getAccount(addr)
if err != nil {
return err
}
return acct.Escrow.CheckStakeClaims(c.thresholds)
}
// AddStakeClaim attempts to add a stake claim to the given escrow account.
//
// In case there is insufficient stake to cover the claim or an error occurs, no modifications are
// made to the stake accumulator.
func (c *StakeAccumulatorCache) AddStakeClaim(
addr staking.Address,
claim staking.StakeClaim,
thresholds []staking.StakeThreshold,
) error {
acct, err := c.getAccount(addr)
if err != nil {
return err
}
return acct.Escrow.AddStakeClaim(c.thresholds, claim, thresholds)
}
// RemoveStakeClaim removes a given stake claim.
//
// It is an error if the stake claim does not exist.
func (c *StakeAccumulatorCache) RemoveStakeClaim(
addr staking.Address,
claim staking.StakeClaim,
) error {
acct, err := c.getAccount(addr)
if err != nil {
return err
}
return acct.Escrow.RemoveStakeClaim(claim)
}
// GetEscrowBalance returns a given account's escrow balance.
func (c *StakeAccumulatorCache) GetEscrowBalance(addr staking.Address) (*quantity.Quantity, error) {
acct, err := c.getAccount(addr)
if err != nil {
return nil, err
}
return acct.Escrow.Active.Balance.Clone(), nil
}
// Commit commits the stake accumulator changes. The caller must ensure that this does not overwrite
// any outstanding account updates.
func (c *StakeAccumulatorCache) Commit() error {
for addr, acct := range c.accounts {
if err := c.state.SetAccount(c.ctx, addr, acct); err != nil {
return fmt.Errorf("failed to set account %s: %w", addr, err)
}
}
return nil
}
// Discard discards any stake accumulator changes.
func (c *StakeAccumulatorCache) Discard() {
c.accounts = nil
}
// NewStakeAccumulatorCache creates a new stake accumulator cache.
func NewStakeAccumulatorCache(ctx *abciAPI.Context) (*StakeAccumulatorCache, error) {
state := NewMutableState(ctx.State())
thresholds, err := state.Thresholds(ctx)
if err != nil {
return nil, fmt.Errorf("staking/tendermint: failed to query thresholds: %w", err)
}
return &StakeAccumulatorCache{
ctx: ctx,
state: state,
thresholds: thresholds,
}, nil
}
// AddStakeClaim is a convenience function for adding a single stake claim to an entity.
//
// In case there is no errors, the added claim is automatically committed. The caller must ensure
// that this does not overwrite any outstanding account updates.
func AddStakeClaim(
ctx *abciAPI.Context,
addr staking.Address,
claim staking.StakeClaim,
thresholds []staking.StakeThreshold,
) error {
sa, err := NewStakeAccumulatorCache(ctx)
if err != nil {
return err
}
if err = sa.AddStakeClaim(addr, claim, thresholds); err != nil {
return err
}
return sa.Commit()
}
// RemoveStakeClaim is a convenience function for removing a single stake claim from an entity.
//
// In case there is no errors, the removed claim is automatically committed. The caller must ensure
// that this does not overwrite any outstanding account updates.
func RemoveStakeClaim(ctx *abciAPI.Context, addr staking.Address, claim staking.StakeClaim) error {
sa, err := NewStakeAccumulatorCache(ctx)
if err != nil {
return err
}
if err = sa.RemoveStakeClaim(addr, claim); err != nil {
return err
}
return sa.Commit()
}
// CheckStakeClaims is a convenience function for checking a single entity's stake claims.
func CheckStakeClaims(ctx *abciAPI.Context, addr staking.Address) error {
sa, err := NewStakeAccumulatorCache(ctx)
if err != nil {
return err
}
defer sa.Discard()
return sa.CheckStakeClaims(addr)
}