Skip to content

Commit 252cbfe

Browse files
committed
optimize attestation
1 parent 14ba21d commit 252cbfe

File tree

5 files changed

+65
-12
lines changed

5 files changed

+65
-12
lines changed

x/dlc/keeper/attestation.go

+17
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ func (k Keeper) HandleAttestation(ctx sdk.Context, sender string, eventId uint64
2222
}
2323

2424
event := k.GetEvent(ctx, eventId)
25+
if !event.HasTriggered {
26+
return types.ErrEventNotTriggered
27+
}
2528

2629
pubKeyBytes, _ := hex.DecodeString(event.Pubkey)
2730
sigBytes, _ := hex.DecodeString(signature)
@@ -84,6 +87,20 @@ func (k Keeper) SetAttestation(ctx sdk.Context, attestation *types.DLCAttestatio
8487

8588
bz := k.cdc.MustMarshal(attestation)
8689
store.Set(types.AttestationKey(attestation.Id), bz)
90+
91+
store.Set(types.AttestationByEventKey(attestation.EventId), sdk.Uint64ToBigEndian(attestation.Id))
92+
}
93+
94+
// GetAttestationByEvent gets the attestation by the given event
95+
func (k Keeper) GetAttestationByEvent(ctx sdk.Context, eventId uint64) *types.DLCAttestation {
96+
store := ctx.KVStore(k.storeKey)
97+
98+
bz := store.Get(types.AttestationByEventKey(eventId))
99+
if bz == nil {
100+
return nil
101+
}
102+
103+
return k.GetAttestation(ctx, sdk.BigEndianToUint64(bz))
87104
}
88105

89106
// GetAttestations gets attestations

x/dlc/types/errors.go

+9-8
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,15 @@ var (
1313
ErrOracleDoesNotExist = errorsmod.Register(ModuleName, 1104, "oracle does not exist")
1414
ErrAgencyDoesNotExist = errorsmod.Register(ModuleName, 1105, "agency does not exist")
1515
ErrEventDoesNotExist = errorsmod.Register(ModuleName, 1106, "event does not exist")
16-
ErrInvalidParticipants = errorsmod.Register(ModuleName, 1107, "invalid participants")
17-
ErrUnauthorizedParticipant = errorsmod.Register(ModuleName, 1108, "unauthorized participant")
18-
ErrInvalidThreshold = errorsmod.Register(ModuleName, 1109, "invalid threshold")
19-
ErrPendingOraclePubKeyExists = errorsmod.Register(ModuleName, 1110, "pending oracle public key already exists")
20-
ErrInvalidOracleStatus = errorsmod.Register(ModuleName, 1111, "invalid oracle status")
21-
ErrPendingAgencyPubKeyExists = errorsmod.Register(ModuleName, 1112, "pending agency public key already exists")
22-
ErrInvalidAgencyStatus = errorsmod.Register(ModuleName, 1113, "invalid agency status")
23-
ErrDKGTimedOut = errorsmod.Register(ModuleName, 1114, "dkg timed out")
16+
ErrEventNotTriggered = errorsmod.Register(ModuleName, 1107, "event not triggered")
17+
ErrInvalidParticipants = errorsmod.Register(ModuleName, 1108, "invalid participants")
18+
ErrUnauthorizedParticipant = errorsmod.Register(ModuleName, 1109, "unauthorized participant")
19+
ErrInvalidThreshold = errorsmod.Register(ModuleName, 1110, "invalid threshold")
20+
ErrPendingOraclePubKeyExists = errorsmod.Register(ModuleName, 1111, "pending oracle public key already exists")
21+
ErrInvalidOracleStatus = errorsmod.Register(ModuleName, 1112, "invalid oracle status")
22+
ErrPendingAgencyPubKeyExists = errorsmod.Register(ModuleName, 1113, "pending agency public key already exists")
23+
ErrInvalidAgencyStatus = errorsmod.Register(ModuleName, 1114, "invalid agency status")
24+
ErrDKGTimedOut = errorsmod.Register(ModuleName, 1115, "dkg timed out")
2425

2526
ErrInvalidParams = errorsmod.Register(ModuleName, 2100, "invalid params")
2627
)

x/dlc/types/keys.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,9 @@ var (
3737
EventByPriceKeyPrefix = []byte{0x18} // prefix for each key to an event by triggering price
3838
CurrentEventPriceKeyPrefix = []byte{0x19} // key prefix for the current event price
3939
AttestationKeyPrefix = []byte{0x20} // prefix for each key to an attestation
40+
AttestationByEventKeyPrefix = []byte{0x21} // prefix for each key to an attestation by event
4041

41-
PriceKeyPrefix = []byte{0x20} // key prefix for the price
42+
PriceKeyPrefix = []byte{0x30} // key prefix for the price
4243
)
4344

4445
func OracleKey(id uint64) []byte {
@@ -91,6 +92,10 @@ func AttestationKey(id uint64) []byte {
9192
return append(AttestationKeyPrefix, sdk.Uint64ToBigEndian(id)...)
9293
}
9394

95+
func AttestationByEventKey(eventId uint64) []byte {
96+
return append(AttestationByEventKeyPrefix, sdk.Uint64ToBigEndian(eventId)...)
97+
}
98+
9499
func PriceKey(pair string) []byte {
95100
return append(PriceKeyPrefix, []byte(pair)...)
96101
}

x/lending/module/abci.go

+31-3
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,25 @@
11
package lending
22

33
import (
4+
"encoding/hex"
45
"fmt"
56

67
sdk "github.com/cosmos/cosmos-sdk/types"
78

9+
"github.com/sideprotocol/side/crypto/adaptor"
810
auctiontypes "github.com/sideprotocol/side/x/auction/types"
911
"github.com/sideprotocol/side/x/lending/keeper"
1012
"github.com/sideprotocol/side/x/lending/types"
1113
)
1214

1315
// EndBlocker called at every block
1416
func EndBlocker(ctx sdk.Context, k keeper.Keeper) {
15-
handleLoans(ctx, k)
17+
handleActiveLoans(ctx, k)
18+
handleLiquidatedLoans(ctx, k)
1619
}
1720

18-
// handleLoans handles loans
19-
func handleLoans(ctx sdk.Context, k keeper.Keeper) {
21+
// handleActiveLoans handles active loans
22+
func handleActiveLoans(ctx sdk.Context, k keeper.Keeper) {
2023
// get all active loans
2124
loans := k.GetLoans(ctx, types.LoanStatus_Disburse)
2225

@@ -64,3 +67,28 @@ func handleLoans(ctx sdk.Context, k keeper.Keeper) {
6467
}
6568
}
6669
}
70+
71+
// handleLiquidatedLoans handles liquidated loans
72+
func handleLiquidatedLoans(ctx sdk.Context, k keeper.Keeper) {
73+
// get all liquidated loans
74+
loans := k.GetLoans(ctx, types.LoanStatus_Liquidate)
75+
76+
for _, loan := range loans {
77+
// check if the event attestation has been submitted
78+
attestation := k.DLCKeeper().GetAttestationByEvent(ctx, loan.EventId)
79+
if attestation == nil {
80+
continue
81+
}
82+
83+
adaptorSecret, _ := hex.DecodeString(attestation.Signature)
84+
85+
// TODO: get borrower adaptor signature from CET
86+
adaptorSignature := []byte{}
87+
88+
// decrypt the adaptor signature
89+
adaptedSig := adaptor.Adapt(adaptorSignature, adaptorSecret)
90+
91+
// TODO: set CET
92+
_ = adaptedSig
93+
}
94+
}

x/lending/types/expected_keepers.go

+2
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ type AuctionKeeper interface {
5252
// DLCKeeper defines the expected DLC keeper interface
5353
type DLCKeeper interface {
5454
GetEvent(ctx sdk.Context, id uint64) *dlctypes.DLCPriceEvent
55+
GetAttestationByEvent(ctx sdk.Context, eventId uint64) *dlctypes.DLCAttestation
56+
5557
TriggerEvent(ctx sdk.Context, id uint64)
5658
}
5759

0 commit comments

Comments
 (0)