Skip to content

Commit

Permalink
improve nonce handling
Browse files Browse the repository at this point in the history
  • Loading branch information
keithsue committed Jan 3, 2025
1 parent aa27ba9 commit fa15161
Show file tree
Hide file tree
Showing 12 changed files with 603 additions and 604 deletions.
272 changes: 173 additions & 99 deletions api/side/dlc/tx.pulsar.go

Large diffs are not rendered by default.

423 changes: 137 additions & 286 deletions api/side/lending/tx.pulsar.go

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion proto/side/dlc/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ message MsgSubmitNonce {

string sender = 1;
string nonce = 2;
string signature = 3;
string oracle_pubkey = 3;
string signature = 4;
}

message MsgSubmitNonceResponse {}
Expand Down
44 changes: 44 additions & 0 deletions x/dlc/keeper/event.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package keeper

import (
sdkmath "cosmossdk.io/math"
storetypes "cosmossdk.io/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"

Expand Down Expand Up @@ -29,6 +30,25 @@ func (k Keeper) IncrementEventId(ctx sdk.Context) uint64 {
return id
}

// GetCurrentEventPrice gets the current event price
func (k Keeper) GetCurrentEventPrice(ctx sdk.Context, pair string) int64 {
store := ctx.KVStore(k.storeKey)

bz := store.Get(types.CurrentEventPriceKey(pair))
if bz == nil {
return 0
}

return int64(sdk.BigEndianToUint64(bz))
}

// SetCurrentEventPrice sets the current event price for the given pair
func (k Keeper) SetCurrentEventPrice(ctx sdk.Context, pair string, price sdkmath.Int) {
store := ctx.KVStore(k.storeKey)

store.Set(types.CurrentEventPriceKey(pair), sdk.Uint64ToBigEndian(price.Uint64()))
}

// HasEvent returns true if the given event exists, false otherwise
func (k Keeper) HasEvent(ctx sdk.Context, id uint64) bool {
store := ctx.KVStore(k.storeKey)
Expand All @@ -47,12 +67,36 @@ func (k Keeper) GetEvent(ctx sdk.Context, id uint64) *types.DLCPriceEvent {
return &event
}

// GetEventByPrice gets the event by the given price
func (k Keeper) GetEventByPrice(ctx sdk.Context, price sdkmath.Int) *types.DLCPriceEvent {
store := ctx.KVStore(k.storeKey)

bz := store.Get(types.EventByPriceKey(price))
if bz == nil {
return nil
}

return k.GetEvent(ctx, sdk.BigEndianToUint64(bz))
}

// SetEvent sets the given event
func (k Keeper) SetEvent(ctx sdk.Context, event *types.DLCPriceEvent) {
store := ctx.KVStore(k.storeKey)

bz := k.cdc.MustMarshal(event)
store.Set(types.EventKey(event.Id), bz)

store.Set(types.EventByPriceKey(event.TriggerPrice), sdk.Uint64ToBigEndian(event.Id))
}

// TriggerEvent sets the given event to triggered
func (k Keeper) TriggerEvent(ctx sdk.Context, event *types.DLCPriceEvent) {
store := ctx.KVStore(k.storeKey)

event.HasTriggered = true

bz := k.cdc.MustMarshal(event)
store.Set(types.EventKey(event.Id), bz)
}

// GetAllEvents gets all events
Expand Down
2 changes: 1 addition & 1 deletion x/dlc/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func (m msgServer) SubmitNonce(goCtx context.Context, msg *types.MsgSubmitNonce)

ctx := sdk.UnwrapSDKContext(goCtx)

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

Expand Down
19 changes: 19 additions & 0 deletions x/dlc/keeper/nonce.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package keeper

import (
"encoding/hex"
"fmt"

errorsmod "cosmossdk.io/errors"
sdkmath "cosmossdk.io/math"
storetypes "cosmossdk.io/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"

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

pair := "BTC-USD"
currentEventPrice := k.GetCurrentEventPrice(ctx, pair)
priceInterval := k.GetPriceInterval(ctx, pair)

dlcEvent := &types.DLCPriceEvent{
Id: k.IncrementEventId(ctx),
TriggerPrice: sdkmath.NewInt(currentEventPrice + int64(priceInterval)),
Nonce: nonce,
Pubkey: oraclePubKey,
HasTriggered: false,
PublishAt: ctx.BlockTime(),
}
dlcEvent.PriceDecimal = dlcEvent.TriggerPrice
dlcEvent.Description = fmt.Sprintf("Liquidation event at price %s", dlcEvent.PriceDecimal)

k.SetNonce(ctx, dlcNonce, oracle.Id)
k.SetEvent(ctx, dlcEvent)
k.SetCurrentEventPrice(ctx, pair, dlcEvent.TriggerPrice)

return nil
}
Expand Down
18 changes: 18 additions & 0 deletions x/dlc/keeper/params.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package keeper

import (
sdk "github.com/cosmos/cosmos-sdk/types"
)

// GetPriceInterval gets the price interval for the given pair
func (k Keeper) GetPriceInterval(ctx sdk.Context, pair string) int32 {
priceIntervals := k.GetParams(ctx).PriceIntervals

for _, pi := range priceIntervals {
if pi.PricePair == pair {
return pi.Interval
}
}

return 0
}
7 changes: 7 additions & 0 deletions x/dlc/module/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ import (
func EndBlocker(ctx sdk.Context, k keeper.Keeper) {
handlePendingOracles(ctx, k)
handlePendingAgencies(ctx, k)

handleEvents(ctx, k)
}

// handleEvents handles the events
func handleEvents(ctx sdk.Context, k keeper.Keeper) {

}

// handlePendingOracles handles the pending oracles
Expand Down
41 changes: 34 additions & 7 deletions x/dlc/types/keys.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package types

import (
sdkmath "cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
)

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

OracleKeyPrefix = []byte{0x10} // prefix for each key to an oracle
OracleByPubKeyKeyPrefix = []byte{0x11} // prefix for each key to an oracle by public key
AgencyKeyPrefix = []byte{0x12} // prefix for each key to an agency
NonceIndexKeyPrefix = []byte{0x13} // key prefix for the nonce index
NonceKeyPrefix = []byte{0x14} // prefix for each key to a nonce
EventKeyPrefix = []byte{0x15} // prefix for each key to an event
AttestationKeyPrefix = []byte{0x16} // prefix for each key to an attestation
OracleKeyPrefix = []byte{0x10} // prefix for each key to an oracle
OracleByPubKeyKeyPrefix = []byte{0x11} // prefix for each key to an oracle by public key
PendingOraclePubKeyKeyPrefix = []byte{0x12} // key prefix for the pending oracle public key
AgencyKeyPrefix = []byte{0x13} // prefix for each key to an agency
PendingAgencyPubKeyKeyPrefix = []byte{0x14} // key prefix for the pending agency public key
NonceIndexKeyPrefix = []byte{0x15} // key prefix for the nonce index
NonceKeyPrefix = []byte{0x16} // prefix for each key to a nonce
EventKeyPrefix = []byte{0x17} // prefix for each key to an event
EventByPriceKeyPrefix = []byte{0x18} // prefix for each key to an event by triggering price
CurrentEventPriceKeyPrefix = []byte{0x19} // key prefix for the current event price
AttestationKeyPrefix = []byte{0x20} // prefix for each key to an attestation

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

func PendingOraclePubKeyKey(id uint64, sender string, pubKey []byte) []byte {
key := append(PendingOraclePubKeyKeyPrefix, sdk.Uint64ToBigEndian(id)...)
key = append(key, []byte(sender)...)

return append(key, pubKey...)
}

func AgencyKey(id uint64) []byte {
return append(AgencyKeyPrefix, sdk.Uint64ToBigEndian(id)...)
}

func PendingAgencyPubKeyKey(id uint64, sender string, pubKey []byte) []byte {
key := append(PendingAgencyPubKeyKeyPrefix, sdk.Uint64ToBigEndian(id)...)
key = append(key, []byte(sender)...)

return append(key, pubKey...)
}

func NonceIndexKey(oracleId uint64) []byte {
return append(NonceIndexKeyPrefix, sdk.Uint64ToBigEndian(oracleId)...)
}
Expand All @@ -60,6 +79,14 @@ func EventKey(id uint64) []byte {
return append(EventKeyPrefix, sdk.Uint64ToBigEndian(id)...)
}

func EventByPriceKey(price sdkmath.Int) []byte {
return append(EventByPriceKeyPrefix, price.BigInt().Bytes()...)
}

func CurrentEventPriceKey(pair string) []byte {
return append(CurrentEventPriceKeyPrefix, []byte(pair)...)
}

func AttestationKey(id uint64) []byte {
return append(AttestationKeyPrefix, sdk.Uint64ToBigEndian(id)...)
}
Expand Down
10 changes: 10 additions & 0 deletions x/dlc/types/msg_submit_nonce.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package types
import (
"encoding/hex"

"github.com/btcsuite/btcd/btcec/v2"
"github.com/btcsuite/btcd/btcec/v2/schnorr"

errorsmod "cosmossdk.io/errors"
Expand Down Expand Up @@ -38,6 +39,15 @@ func (m *MsgSubmitNonce) ValidateBasic() error {
return ErrInvalidNonce
}

oraclePk, err := hex.DecodeString(m.OraclePubkey)
if err != nil {
return ErrInvalidPubKey
}

if _, err := btcec.ParsePubKey(oraclePk); err != nil {
return ErrInvalidPubKey
}

sigBytes, err := hex.DecodeString(m.Signature)
if err != nil {
return ErrInvalidSignature
Expand Down
Loading

0 comments on commit fa15161

Please sign in to comment.