Skip to content

Commit fa15161

Browse files
committed
improve nonce handling
1 parent aa27ba9 commit fa15161

File tree

12 files changed

+603
-604
lines changed

12 files changed

+603
-604
lines changed

api/side/dlc/tx.pulsar.go

Lines changed: 173 additions & 99 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/side/lending/tx.pulsar.go

Lines changed: 137 additions & 286 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

proto/side/dlc/tx.proto

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ message MsgSubmitNonce {
5252

5353
string sender = 1;
5454
string nonce = 2;
55-
string signature = 3;
55+
string oracle_pubkey = 3;
56+
string signature = 4;
5657
}
5758

5859
message MsgSubmitNonceResponse {}

x/dlc/keeper/event.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package keeper
22

33
import (
4+
sdkmath "cosmossdk.io/math"
45
storetypes "cosmossdk.io/store/types"
56
sdk "github.com/cosmos/cosmos-sdk/types"
67

@@ -29,6 +30,25 @@ func (k Keeper) IncrementEventId(ctx sdk.Context) uint64 {
2930
return id
3031
}
3132

33+
// GetCurrentEventPrice gets the current event price
34+
func (k Keeper) GetCurrentEventPrice(ctx sdk.Context, pair string) int64 {
35+
store := ctx.KVStore(k.storeKey)
36+
37+
bz := store.Get(types.CurrentEventPriceKey(pair))
38+
if bz == nil {
39+
return 0
40+
}
41+
42+
return int64(sdk.BigEndianToUint64(bz))
43+
}
44+
45+
// SetCurrentEventPrice sets the current event price for the given pair
46+
func (k Keeper) SetCurrentEventPrice(ctx sdk.Context, pair string, price sdkmath.Int) {
47+
store := ctx.KVStore(k.storeKey)
48+
49+
store.Set(types.CurrentEventPriceKey(pair), sdk.Uint64ToBigEndian(price.Uint64()))
50+
}
51+
3252
// HasEvent returns true if the given event exists, false otherwise
3353
func (k Keeper) HasEvent(ctx sdk.Context, id uint64) bool {
3454
store := ctx.KVStore(k.storeKey)
@@ -47,12 +67,36 @@ func (k Keeper) GetEvent(ctx sdk.Context, id uint64) *types.DLCPriceEvent {
4767
return &event
4868
}
4969

70+
// GetEventByPrice gets the event by the given price
71+
func (k Keeper) GetEventByPrice(ctx sdk.Context, price sdkmath.Int) *types.DLCPriceEvent {
72+
store := ctx.KVStore(k.storeKey)
73+
74+
bz := store.Get(types.EventByPriceKey(price))
75+
if bz == nil {
76+
return nil
77+
}
78+
79+
return k.GetEvent(ctx, sdk.BigEndianToUint64(bz))
80+
}
81+
5082
// SetEvent sets the given event
5183
func (k Keeper) SetEvent(ctx sdk.Context, event *types.DLCPriceEvent) {
5284
store := ctx.KVStore(k.storeKey)
5385

5486
bz := k.cdc.MustMarshal(event)
5587
store.Set(types.EventKey(event.Id), bz)
88+
89+
store.Set(types.EventByPriceKey(event.TriggerPrice), sdk.Uint64ToBigEndian(event.Id))
90+
}
91+
92+
// TriggerEvent sets the given event to triggered
93+
func (k Keeper) TriggerEvent(ctx sdk.Context, event *types.DLCPriceEvent) {
94+
store := ctx.KVStore(k.storeKey)
95+
96+
event.HasTriggered = true
97+
98+
bz := k.cdc.MustMarshal(event)
99+
store.Set(types.EventKey(event.Id), bz)
56100
}
57101

58102
// GetAllEvents gets all events

x/dlc/keeper/msg_server.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func (m msgServer) SubmitNonce(goCtx context.Context, msg *types.MsgSubmitNonce)
2222

2323
ctx := sdk.UnwrapSDKContext(goCtx)
2424

25-
if err := m.Keeper.HandleNonce(ctx, msg.Sender, msg.Nonce, "", msg.Signature); err != nil {
25+
if err := m.Keeper.HandleNonce(ctx, msg.Sender, msg.Nonce, msg.OraclePubkey, msg.Signature); err != nil {
2626
return nil, err
2727
}
2828

x/dlc/keeper/nonce.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ package keeper
22

33
import (
44
"encoding/hex"
5+
"fmt"
56

67
errorsmod "cosmossdk.io/errors"
8+
sdkmath "cosmossdk.io/math"
79
storetypes "cosmossdk.io/store/types"
810
sdk "github.com/cosmos/cosmos-sdk/types"
911

@@ -33,7 +35,24 @@ func (k Keeper) HandleNonce(ctx sdk.Context, sender string, nonce string, oracle
3335
Time: ctx.BlockTime(),
3436
}
3537

38+
pair := "BTC-USD"
39+
currentEventPrice := k.GetCurrentEventPrice(ctx, pair)
40+
priceInterval := k.GetPriceInterval(ctx, pair)
41+
42+
dlcEvent := &types.DLCPriceEvent{
43+
Id: k.IncrementEventId(ctx),
44+
TriggerPrice: sdkmath.NewInt(currentEventPrice + int64(priceInterval)),
45+
Nonce: nonce,
46+
Pubkey: oraclePubKey,
47+
HasTriggered: false,
48+
PublishAt: ctx.BlockTime(),
49+
}
50+
dlcEvent.PriceDecimal = dlcEvent.TriggerPrice
51+
dlcEvent.Description = fmt.Sprintf("Liquidation event at price %s", dlcEvent.PriceDecimal)
52+
3653
k.SetNonce(ctx, dlcNonce, oracle.Id)
54+
k.SetEvent(ctx, dlcEvent)
55+
k.SetCurrentEventPrice(ctx, pair, dlcEvent.TriggerPrice)
3756

3857
return nil
3958
}

x/dlc/keeper/params.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package keeper
2+
3+
import (
4+
sdk "github.com/cosmos/cosmos-sdk/types"
5+
)
6+
7+
// GetPriceInterval gets the price interval for the given pair
8+
func (k Keeper) GetPriceInterval(ctx sdk.Context, pair string) int32 {
9+
priceIntervals := k.GetParams(ctx).PriceIntervals
10+
11+
for _, pi := range priceIntervals {
12+
if pi.PricePair == pair {
13+
return pi.Interval
14+
}
15+
}
16+
17+
return 0
18+
}

x/dlc/module/abci.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@ import (
1010
func EndBlocker(ctx sdk.Context, k keeper.Keeper) {
1111
handlePendingOracles(ctx, k)
1212
handlePendingAgencies(ctx, k)
13+
14+
handleEvents(ctx, k)
15+
}
16+
17+
// handleEvents handles the events
18+
func handleEvents(ctx sdk.Context, k keeper.Keeper) {
19+
1320
}
1421

1522
// handlePendingOracles handles the pending oracles

x/dlc/types/keys.go

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package types
22

33
import (
4+
sdkmath "cosmossdk.io/math"
45
sdk "github.com/cosmos/cosmos-sdk/types"
56
)
67

@@ -25,13 +26,17 @@ var (
2526
EventIdKey = []byte{0x04} // key for event id
2627
AttestationIdKey = []byte{0x05} // key for attestation id
2728

28-
OracleKeyPrefix = []byte{0x10} // prefix for each key to an oracle
29-
OracleByPubKeyKeyPrefix = []byte{0x11} // prefix for each key to an oracle by public key
30-
AgencyKeyPrefix = []byte{0x12} // prefix for each key to an agency
31-
NonceIndexKeyPrefix = []byte{0x13} // key prefix for the nonce index
32-
NonceKeyPrefix = []byte{0x14} // prefix for each key to a nonce
33-
EventKeyPrefix = []byte{0x15} // prefix for each key to an event
34-
AttestationKeyPrefix = []byte{0x16} // prefix for each key to an attestation
29+
OracleKeyPrefix = []byte{0x10} // prefix for each key to an oracle
30+
OracleByPubKeyKeyPrefix = []byte{0x11} // prefix for each key to an oracle by public key
31+
PendingOraclePubKeyKeyPrefix = []byte{0x12} // key prefix for the pending oracle public key
32+
AgencyKeyPrefix = []byte{0x13} // prefix for each key to an agency
33+
PendingAgencyPubKeyKeyPrefix = []byte{0x14} // key prefix for the pending agency public key
34+
NonceIndexKeyPrefix = []byte{0x15} // key prefix for the nonce index
35+
NonceKeyPrefix = []byte{0x16} // prefix for each key to a nonce
36+
EventKeyPrefix = []byte{0x17} // prefix for each key to an event
37+
EventByPriceKeyPrefix = []byte{0x18} // prefix for each key to an event by triggering price
38+
CurrentEventPriceKeyPrefix = []byte{0x19} // key prefix for the current event price
39+
AttestationKeyPrefix = []byte{0x20} // prefix for each key to an attestation
3540

3641
PriceKeyPrefix = []byte{0x20} // key prefix for the price
3742
)
@@ -44,10 +49,24 @@ func OracleByPubKeyKey(pubKey []byte) []byte {
4449
return append(OracleByPubKeyKeyPrefix, pubKey...)
4550
}
4651

52+
func PendingOraclePubKeyKey(id uint64, sender string, pubKey []byte) []byte {
53+
key := append(PendingOraclePubKeyKeyPrefix, sdk.Uint64ToBigEndian(id)...)
54+
key = append(key, []byte(sender)...)
55+
56+
return append(key, pubKey...)
57+
}
58+
4759
func AgencyKey(id uint64) []byte {
4860
return append(AgencyKeyPrefix, sdk.Uint64ToBigEndian(id)...)
4961
}
5062

63+
func PendingAgencyPubKeyKey(id uint64, sender string, pubKey []byte) []byte {
64+
key := append(PendingAgencyPubKeyKeyPrefix, sdk.Uint64ToBigEndian(id)...)
65+
key = append(key, []byte(sender)...)
66+
67+
return append(key, pubKey...)
68+
}
69+
5170
func NonceIndexKey(oracleId uint64) []byte {
5271
return append(NonceIndexKeyPrefix, sdk.Uint64ToBigEndian(oracleId)...)
5372
}
@@ -60,6 +79,14 @@ func EventKey(id uint64) []byte {
6079
return append(EventKeyPrefix, sdk.Uint64ToBigEndian(id)...)
6180
}
6281

82+
func EventByPriceKey(price sdkmath.Int) []byte {
83+
return append(EventByPriceKeyPrefix, price.BigInt().Bytes()...)
84+
}
85+
86+
func CurrentEventPriceKey(pair string) []byte {
87+
return append(CurrentEventPriceKeyPrefix, []byte(pair)...)
88+
}
89+
6390
func AttestationKey(id uint64) []byte {
6491
return append(AttestationKeyPrefix, sdk.Uint64ToBigEndian(id)...)
6592
}

x/dlc/types/msg_submit_nonce.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package types
33
import (
44
"encoding/hex"
55

6+
"github.com/btcsuite/btcd/btcec/v2"
67
"github.com/btcsuite/btcd/btcec/v2/schnorr"
78

89
errorsmod "cosmossdk.io/errors"
@@ -38,6 +39,15 @@ func (m *MsgSubmitNonce) ValidateBasic() error {
3839
return ErrInvalidNonce
3940
}
4041

42+
oraclePk, err := hex.DecodeString(m.OraclePubkey)
43+
if err != nil {
44+
return ErrInvalidPubKey
45+
}
46+
47+
if _, err := btcec.ParsePubKey(oraclePk); err != nil {
48+
return ErrInvalidPubKey
49+
}
50+
4151
sigBytes, err := hex.DecodeString(m.Signature)
4252
if err != nil {
4353
return ErrInvalidSignature

0 commit comments

Comments
 (0)