-
Notifications
You must be signed in to change notification settings - Fork 0
/
mutable_state.go
76 lines (67 loc) · 2.43 KB
/
mutable_state.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
package price_encoder
import (
"fmt"
"sync"
"github.com/jinxprotocol/v4-chain/protocol/daemons/pricefeed/client/types"
)
// mutableState stores all mutable state for the price encoder. Unlike the price fetcher, the
// price encoder needs access to multiple parts of the mutable market state, so we store it all.
type mutableState struct {
sync.Mutex
mutableExchangeConfig *types.MutableExchangeMarketConfig
marketToMutableConfig map[types.MarketId]*types.MutableMarketConfig
}
// Update updates all fields in the mutableState atomically. Updates are required
// to be atomic in order to keep the mutableState consistent. This method expects
// validation to occur in the PriceEncoder. This method is synchronized.
func (ms *mutableState) Update(
mutableExchangeConfig *types.MutableExchangeMarketConfig,
marketToMutableConfig map[types.MarketId]*types.MutableMarketConfig,
) {
ms.Lock()
defer ms.Unlock()
ms.mutableExchangeConfig = mutableExchangeConfig
ms.marketToMutableConfig = marketToMutableConfig
}
// GetPriceConversionDetailsForMarket returns the price conversion details for the given market.
func (ms *mutableState) GetPriceConversionDetailsForMarket(id types.MarketId) (priceConversionDetails, error) {
ms.Lock()
defer ms.Unlock()
marketConfig, ok := ms.mutableExchangeConfig.MarketToMarketConfig[id]
if !ok {
return priceConversionDetails{}, fmt.Errorf(
"market config for market %v not found on exchange '%v'",
id,
ms.mutableExchangeConfig.Id,
)
}
var adjustDetails *adjustByMarketDetails
if marketConfig.AdjustByMarket != nil {
adjustByMarketConfig, ok := ms.marketToMutableConfig[*marketConfig.AdjustByMarket]
if !ok {
return priceConversionDetails{}, fmt.Errorf(
"mutable market config for adjust-by market %v not found on exchange '%v'",
*marketConfig.AdjustByMarket,
ms.mutableExchangeConfig.Id,
)
}
adjustDetails = &adjustByMarketDetails{
MarketId: *marketConfig.AdjustByMarket,
Exponent: adjustByMarketConfig.Exponent,
MinExchanges: adjustByMarketConfig.MinExchanges,
}
}
mutableMarketConfig, ok := ms.marketToMutableConfig[id]
if !ok {
return priceConversionDetails{}, fmt.Errorf(
"mutable market config for market %v not found on exchange '%v'",
id,
ms.mutableExchangeConfig.Id,
)
}
return priceConversionDetails{
Invert: marketConfig.Invert,
Exponent: mutableMarketConfig.Exponent,
AdjustByMarketDetails: adjustDetails,
}, nil
}