Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nazrhom/fee escrow #297

Merged
merged 8 commits into from
May 16, 2023
Merged

Nazrhom/fee escrow #297

merged 8 commits into from
May 16, 2023

Conversation

nazrhom
Copy link
Collaborator

@nazrhom nazrhom commented May 2, 2023

Part of #267

@nazrhom nazrhom requested a review from a team as a code owner May 2, 2023 10:05
@nazrhom nazrhom force-pushed the nazrhom/fee-escrow branch from cfecf29 to 739504c Compare May 2, 2023 10:07
@nazrhom nazrhom linked an issue May 2, 2023 that may be closed by this pull request
2 tasks
@@ -67,8 +69,8 @@ instance ToJSON AuctionTermsDynamic
instance FromJSON AuctionTermsDynamic

-- | Stub for tests not checking MoveToHyda. Something not existent.
nonExistentHeadIdStub :: CurrencySymbol
nonExistentHeadIdStub = "DEADBEEF"
nonExistentHeadIdStub :: HeadId
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks 👍

@@ -46,12 +46,12 @@ config =
, configDiffVoucherExpiry = 8
, configDiffCleanup = 10
, configAuctionFeePerDelegate = fromJust $ intToNatural 4_000_000
, configStartingBid = fromJust $ intToNatural 8_000_000
, configMinimumBidIncrement = fromJust $ intToNatural 8_000_000
, configStartingBid = fromJust $ intToNatural 15_000_000
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why?

-- Prelude imports
import PlutusTx.Prelude

-- import Prelude (quot)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
-- import Prelude (quot)

feeEscrowUtxo <- scriptUtxos FeeEscrow terms

feeEscrowValue <- case UTxO.pairs feeEscrowUtxo of
[(_, txOut)] -> pure $ txOutValue txOut
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a scriptSingleUtxo function for that.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it work as nicely in this case, as I want both the reference to feeEscrowUtxo and to look at its value

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It returns Maybe (TxIn, TxOut CtxUTxO) which seems the same that you use.

Probably the name ..Utxo is confusing, but I still do not found how to name (TxIn, TxOut) pair better. Utxo term being overloaded is awful, and fixing better vocabluary would be cool, but I do not know how to name it better.

cc @GeorgeFlerovsky

adaValueOf v = valueOf v adaSymbol adaToken

expectedValuePerDelegate :: Integer
expectedValuePerDelegate = adaValueOf singleFeeInputValue `quotient` length (delegates terms)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just auctionFeePerDelegate. This cannot have a reminder, cuz Escrow ensures that calculateTotalFee terms is emitted. If it was not so amount should be checked anyway.

So no need for all this stuff.

spread 0 xs = xs
spread k (x : xs) = (x + 1) : spread (k - 1) xs

distributedLovelace = spread remainder (replicate delegateNumber quotient)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not needed because of auctionFeePerDelegate too

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or is all this code due to #298 ? In that case please put comments for spread function and write about this motivation in spec.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we leave this kind of logic, it should have property-based unit test matching it with on-chain check.

So I think it may be simpler to just reuse this function in on-chain too. That way there is no need to test that they match each over. And it seems like there is no use for indeterminacy of remainder spreading anyway.

While this needs unit-test or property-based test anyway.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or is all this code due to #298 ? I

No, this is only distributing ADA to the delegates in a balanced way.

If we leave this kind of logic, it should have property-based unit test matching it with on-chain check.

So I think it may be simpler to just reuse this function in on-chain too. That way there is no need to test that they match each over. And it seems like there is no use for indeterminacy of remainder spreading anyway.

While this needs unit-test or property-based test anyway.

I don't think we really need unit tests for this helper function, its quite a trivial implementation, we already have on and offchain tests for this anyway, so we are testing it.

Also, I don't think it would be nice to use this on-chain, this gives slightly more ADA to the delegates appearing first in the list. In the on-chain we should only verify that each delegates receives >= auctionFeePerDelegate.
The off-chain has to actually consume the utxo, so in case there is more ada than auctionFeePerDelegate * length delegates it can decide how to distribute the extra ada

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, this is only distributing ADA to the delegates in a balanced way.

I mean that if we do are not concerned with #298 then this ADA separation code is not needed and just auctionFeePerDelegate can be used.

I don't think we really need unit tests for this helper function, its quite a trivial implementation

Integer division and such logic is exactly the kind of stuff where people get errors like off-by-one or something like that. Also we already got off-by-one errors in intervals helpers code.

we already have on and offchain tests for this anyway, so we are testing it.

Our tests do not test ADA amount at all and this is an accident waiting to be happen.

this gives slightly more ADA to the delegates appearing first in the list

Yes, and your specification says that this is okay. So why accept different remainder distributions then if spec says that any of them are good?

The off-chain has to actually consume the utxo, so in case there is more ada than auctionFeePerDelegate * length delegates it can decide how to distribute the extra ada

What is off-chain? Why where can be extra ADA? If it is winning lot, then this does not interfere with delegate ADA distribution, cuz bidder cannot be an delegate.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@GeorgeFlerovsky Can delegate be a bidder too?

@uhbif19 uhbif19 self-requested a review May 3, 2023 21:40
TxOutDatumNone
ReferenceScriptNone

zipDistributingValue :: Integer -> [PubKeyHash] -> [(PubKeyHash, Value)]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
zipDistributingValue :: Integer -> [PubKeyHash] -> [(PubKeyHash, Value)]
zipDistributingValue :: Lovelace -> [PubKeyHash] -> [(PubKeyHash, Value)]

Copy link
Contributor

@uhbif19 uhbif19 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I now noticed that now this code actually uses delegates list. But there is not way to actually get it now. It should be returned by delegate server I gues.

@nazrhom
Copy link
Collaborator Author

nazrhom commented May 8, 2023

@nazrhom nazrhom requested a review from uhbif19 May 8, 2023 09:15
@uhbif19
Copy link
Contributor

uhbif19 commented May 10, 2023

this is currently hardcoded to be hydraNodeActors

I understand. I meant real CLI. We can separate CLI fixing for that stuff in another issue, which probably requires delegates list returining, and merge this.

@@ -105,8 +104,8 @@ distributeFee terms = do
TxOutDatumNone
ReferenceScriptNone

zipDistributingValue :: Integer -> [PubKeyHash] -> [(PubKeyHash, Value)]
zipDistributingValue lovelaceAmt delegatePKHs = zip delegatePKHs (lovelaceToValue . Lovelace <$> distributedLovelace)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lovelace seems to implement divMod, so (un)wrapping is not really necessary.

image

-- Every delegate is payed at least the expected proportion of fee
all receivesProportionOfFee (delegates terms)
-- There is one input spent from the fee escrow validator with enough ADA to pay the fees
adaValueOf singleFeeInputValue >= expectedFeePerDelegeate * length (delegates terms)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But if all delegates get their fee, then everything is okay by construction, no?

@nazrhom nazrhom enabled auto-merge May 16, 2023 09:19
@nazrhom nazrhom merged commit a373e2d into staging May 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Implement fee escrow on-chain and tx constuction
2 participants