Skip to content

Commit 730e558

Browse files
committed
Fix collateral selection sort
1 parent fb164d8 commit 730e558

File tree

6 files changed

+39
-14
lines changed

6 files changed

+39
-14
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
## v3.1.1 : Collateral Selection Fix
2+
3+
### Fixes
4+
- Collateral selection logic now prefers 5Ada inputs
5+
- Multiple collateral inputs if collateral is not sufficient with 1 utxo.
6+
17
## v3.1.0 : Stable Conway Era Support
28
This stable release supports adds full support for new conway governance features.
39

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ Kuber can be stared easily with [docker-compose.yml](./docker-compose.yml) file.
2626

2727
```bash
2828
git clone https://github.com/dquadrant/kuber.git
29-
git checkout 3.1.0
29+
git checkout 3.1.1
3030
docker-compose up -d
3131
```
3232

docker-compose.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ volumes:
44
node-db:
55
services:
66
cardano-node:
7-
image: inputoutput/cardano-node:${CARDANO_NODE_VERSION:-9.1.0}
7+
image: inputoutput/cardano-node:${CARDANO_NODE_VERSION:-9.2.0}
88
environment:
99
NETWORK: ${NETWORK:-preprod}
1010
volumes:
@@ -16,7 +16,7 @@ services:
1616
max-size: "2M"
1717
max-file: "10"
1818
kuber:
19-
image: dquadrant/kuber:${KUBER_VERSION:-3.1.0}
19+
image: dquadrant/kuber:${KUBER_VERSION:-3.1.1}
2020
environment:
2121
NETWORK: ${NETWORK:- preprod}
2222
volumes:

kuber-server/kuber-server.cabal

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
cabal-version: 2.4
22
name: kuber-server
3-
version: 3.1.0.0
3+
version: 3.1.1.0
44

55
-- A short (one-line) description of the package.
66
-- synopsis:

kuber.cabal

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
cabal-version: 2.4
22
name: kuber
3-
version: 3.1.0.0
3+
version: 3.1.1.0
44

55
-- A short (one-line) description of the package.
66
-- synopsis:

src/Cardano/Kuber/Core/TxFramework.hs

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import Data.Functor ((<&>))
3030

3131
import Data.Set (Set)
3232
import Data.Maybe (mapMaybe, fromMaybe, fromJust)
33-
import Data.List (sortBy, minimumBy, find)
33+
import Data.List (sortBy,find, intercalate)
3434
import qualified Data.Foldable as Foldable
3535
import PlutusLedgerApi.V2 (PubKeyHash(PubKeyHash), fromBuiltin)
3636
import Cardano.Kuber.Core.TxBuilder
@@ -313,7 +313,7 @@ txBuilderToTxBody network pParam systemStart eraHistory
313313
Just tis -> pure tis
314314
)
315315
else pure []
316-
316+
Debug.traceM $ "collaterals: " ++ intercalate "\n" ( map show collaterals)
317317
(txCerts,cDeposits) <- ( if null certs
318318
then pure $ (TxCertificatesNone,zeroValue)
319319
else inEonForEra
@@ -622,9 +622,26 @@ txBuilderToTxBody network pParam systemStart eraHistory
622622
maybeCollaterals = case txContextCollaterals of
623623
[] -> case mapMaybe canBeCollateral $ Map.toList spendableUtxos of
624624
[] -> Nothing
625-
v -> let (tin,pkh,_) =minimumBy sortingFunc v in Just [(tin,pkh)]
625+
v -> let sorted = sortBy collateralSortingFunc v
626+
selected = collectCollateral 0 sorted []
627+
in
628+
Debug.trace ("sorted: " ++ ( intercalate "\n" $ map show sorted )
629+
++ "selected: " ++ ( intercalate "\n" $ map show selected)) $ Just $ selected
630+
626631
v-> Just v
627632
where
633+
collectCollateral _ [] selected = selected
634+
collectCollateral amount (v@(ti,to,quantity):rest) selected =
635+
let
636+
newAmount=quantity +amount
637+
in
638+
if length selected > 2
639+
then selected
640+
else if newAmount > 8
641+
then (ti,to):selected
642+
else collectCollateral newAmount rest selected
643+
644+
628645
canBeCollateral :: (IsShelleyBasedEra era) => (TxIn , TxOut ctx era) -> Maybe (TxIn, Hash PaymentKey, Integer)
629646
canBeCollateral v@(ti, to@(TxOut addr val mDatumHash _)) = case mDatumHash of
630647
TxOutDatumNone -> case val of
@@ -636,14 +653,16 @@ txBuilderToTxBody network pParam systemStart eraHistory
636653
Just pkh -> Just ( ti,pkh,case snd $ head _list of { Quantity n -> n } )
637654
else Nothing
638655
_ -> Nothing
639-
filterCollateral = mapMaybe canBeCollateral $ Map.toList spendableUtxos
640656

641657
-- sort based on following conditions => Utxos having >4ada come eariler and the lesser ones come later.
642-
sortingFunc :: (TxIn,a,Integer) -> (TxIn,a,Integer)-> Ordering
643-
sortingFunc (_,_,v1) (_,_,v2)
644-
| v1 < 5 = if v2 < 5 then v1 `compare` v2 else GT
645-
| v2 < 5 = LT
646-
| otherwise = v1 `compare` v2
658+
collateralSortingFunc :: (TxIn,a,Integer) -> (TxIn,a,Integer)-> Ordering
659+
collateralSortingFunc (_,_,v1) (_,_,v2)=
660+
Debug.trace (show v1 ++ " compare "++ show v2 ++ "=" ++ show doCompare ) doCompare
661+
where
662+
doCompare
663+
| v1 <= 5_000_000 = if v2 <= 5_000_000 then v2 `compare` v1 else GT
664+
| v2 <= 5_000_000 = LT
665+
| otherwise = v1 `compare` v2
647666

648667
txContextCollaterals =foldl getCollaterals [] _collaterals
649668
getCollaterals accum x = case x of

0 commit comments

Comments
 (0)