Skip to content

Commit 75507c1

Browse files
committed
chore: partially fix tests for StandingBid
1 parent 8b3a047 commit 75507c1

File tree

7 files changed

+59
-23
lines changed

7 files changed

+59
-23
lines changed

compiled/auction_escrow_validator.plutus

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

compiled/standing_bid_validator.plutus

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

src/HydraAuctionOnchain/Types/BidTerms.hs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
module HydraAuctionOnchain.Types.BidTerms
44
( PBidTerms (PBidTerms)
5+
, bidderSigMessageLength
56
, psellerPayout
67
, pvalidateBidTerms
78
) where
@@ -93,6 +94,15 @@ pvalidateBidTerms = phoistAcyclic $
9394
# bidderSigMessageLengthHex
9495
)
9596

97+
-- Maximum (reasonable) size of the bidder signature message where
98+
-- bidPrice is set to the total supply of ADA (45 billion).
99+
--
100+
-- Note, that the bid price is the only component of the message that
101+
-- has variable size; and for lower bid prices the message is padded
102+
-- with zero bytes at the beginning to reach this size.
103+
bidderSigMessageLength :: Integer
104+
bidderSigMessageLength = 69
105+
96106
bidderSigMessageLengthHex :: Term s PByteString
97107
bidderSigMessageLengthHex =
98108
-- 69 = 2 (cbor) + 28 (cs) + 2 (cbor) + 28 (pkh) + 9 (lovelace)
@@ -113,7 +123,7 @@ bidderSignatureMessage
113123
)
114124
bidderSignatureMessage = phoistAcyclic $
115125
plam $ \auctionCs bidPrice bidderPkh ->
116-
padMessage # 69 #$ (pserialise # auctionCs)
126+
padMessage # pconstant bidderSigMessageLength #$ (pserialise # auctionCs)
117127
<> (pserialise # bidderPkh)
118128
<> (pserialise # bidPrice)
119129

test/Spec/HydraAuctionOnchain/QuickCheck/Gen.hs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,11 @@ import Crypto.PubKey.Ed25519 (PublicKey, SecretKey, generateSecretKey, sign, toP
1515
import Crypto.Random (drgNewTest, withDRG)
1616
import Data.ByteArray (ByteArrayAccess, convert)
1717
import Data.ByteString (ByteString)
18+
import Data.ByteString qualified as ByteString (pack)
1819
import Data.Functor ((<&>))
20+
import HydraAuctionOnchain.Types.BidTerms (bidderSigMessageLength)
1921
import Plutarch.Test.QuickCheck.Instances ()
22+
import PlutusLedgerApi.V1.Address (pubKeyHashAddress)
2023
import PlutusLedgerApi.V1.Interval qualified as Interval (always)
2124
import PlutusLedgerApi.V1.Value qualified as Value (singleton)
2225
import PlutusLedgerApi.V2
@@ -34,6 +37,7 @@ import PlutusLedgerApi.V2
3437
, toBuiltin
3538
)
3639
import PlutusTx.AssocMap qualified as AMap (empty)
40+
import PlutusTx.Builtins (lengthOfByteString)
3741
import Spec.HydraAuctionOnchain.Helpers (hashVerificationKey, serialise)
3842
import Spec.HydraAuctionOnchain.QuickCheck.Modifiers (GenNonAdaValue (GenNonAdaValue))
3943
import Spec.HydraAuctionOnchain.Types.AuctionTerms (AuctionTerms (..))
@@ -99,11 +103,12 @@ genValidBidTerms
99103
-> Maybe Integer
100104
-> Gen BidTerms
101105
genValidBidTerms auctionCs auctionTerms sellerKeys bidderKeys mOldBidPrice = do
102-
let (bi'BidderVk, bi'BidderPkh) = hashVerificationKey $ vkey bidderKeys
106+
let (bi'BidderVk, bidderPkh) = hashVerificationKey $ vkey bidderKeys
107+
let bi'BidderAddress = pubKeyHashAddress bidderPkh
103108
let bt'Bidder = BidderInfo {..}
104109
bt'BidPrice <- genBidPrice
105110
let bt'SellerSignature = sellerSignature bi'BidderVk
106-
let bt'BidderSignature = bidderSignature bt'BidPrice bi'BidderPkh
111+
let bt'BidderSignature = bidderSignature bt'BidPrice bidderPkh
107112
pure BidTerms {..}
108113
where
109114
genBidPrice :: Gen Integer
@@ -122,7 +127,19 @@ genValidBidTerms auctionCs auctionTerms sellerKeys bidderKeys mOldBidPrice = do
122127
bidderSignature :: Integer -> PubKeyHash -> BuiltinByteString
123128
bidderSignature bidPrice bidderPkh =
124129
signUsingKeyPair bidderKeys $
125-
(serialise auctionCs <> serialise bidderPkh <> serialise bidPrice)
130+
padMessage
131+
bidderSigMessageLength
132+
(serialise auctionCs <> serialise bidderPkh <> serialise bidPrice)
133+
134+
padMessage :: Integer -> BuiltinByteString -> BuiltinByteString
135+
padMessage targetSize message
136+
| padSize <= 0 = message
137+
| otherwise =
138+
toBuiltin (ByteString.pack $ replicate padSize 0) <> message
139+
where
140+
padSize :: Int
141+
padSize =
142+
fromInteger $ targetSize - lengthOfByteString message
126143

127144
genValidBidState
128145
:: CurrencySymbol
@@ -155,7 +172,8 @@ genValidNewBidState oldBidState auctionCs auctionTerms sellerKeys bidderKeys =
155172
genValidAuctionTerms :: PublicKey -> Gen AuctionTerms
156173
genValidAuctionTerms vkey = do
157174
GenNonAdaValue @Positive at'AuctionLot <- arbitrary
158-
let (at'SellerVk, at'SellerPkh) = hashVerificationKey vkey
175+
let (at'SellerVk, sellerPkh) = hashVerificationKey vkey
176+
let at'SellerAddress = pubKeyHashAddress sellerPkh
159177
at'Delegates <- vector @PubKeyHash =<< chooseInt (1, 10)
160178

161179
let chooseInterval = POSIXTime <$> chooseInteger (1, 604_800_000) -- up to 1 week in msec

test/Spec/HydraAuctionOnchain/Types/AuctionTerms.hs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,20 @@ module Spec.HydraAuctionOnchain.Types.AuctionTerms
1111
import HydraAuctionOnchain.Types.AuctionTerms (PAuctionTerms)
1212
import Plutarch.DataRepr (DerivePConstantViaData (DerivePConstantViaData))
1313
import Plutarch.Lift (PConstantDecl, PUnsafeLiftDecl (PLifted))
14-
import PlutusLedgerApi.V2 (BuiltinByteString, POSIXTime, POSIXTimeRange, PubKeyHash, Value)
14+
import PlutusLedgerApi.V2
15+
( Address
16+
, BuiltinByteString
17+
, POSIXTime
18+
, POSIXTimeRange
19+
, PubKeyHash
20+
, Value
21+
)
1522
import PlutusTx (makeIsDataIndexed)
1623
import Spec.HydraAuctionOnchain.Helpers (intervalFiniteClosedOpen)
1724

1825
data AuctionTerms = AuctionTerms
1926
{ at'AuctionLot :: Value
20-
, at'SellerPkh :: PubKeyHash
27+
, at'SellerAddress :: Address
2128
, at'SellerVk :: BuiltinByteString
2229
, at'Delegates :: [PubKeyHash]
2330
, at'BiddingStart :: POSIXTime

test/Spec/HydraAuctionOnchain/Types/BidderInfo.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ module Spec.HydraAuctionOnchain.Types.BidderInfo
99
import HydraAuctionOnchain.Types.BidderInfo (PBidderInfo)
1010
import Plutarch.DataRepr (DerivePConstantViaData (DerivePConstantViaData))
1111
import Plutarch.Lift (PConstantDecl, PUnsafeLiftDecl (PLifted))
12-
import PlutusLedgerApi.V2 (BuiltinByteString, PubKeyHash)
12+
import PlutusLedgerApi.V2 (Address, BuiltinByteString)
1313
import PlutusTx (makeIsDataIndexed)
1414

1515
data BidderInfo = BidderInfo
16-
{ bi'BidderPkh :: PubKeyHash
16+
{ bi'BidderAddress :: Address
1717
, bi'BidderVk :: BuiltinByteString
1818
}
1919
deriving stock (Show, Eq)

test/Spec/HydraAuctionOnchain/Validators/StandingBid.hs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,12 @@ spec =
9191
prop_mintsBurnsValue_fails
9292
]
9393
, testGroup "NewBidRedeemer" $
94-
[ testProperty "Succeeds if transaction is valid" $
95-
prop_newBid_validInput_succeeds
96-
, testProperty "Fails if standing bid output does not exist" $
94+
-- TODO: Reimplement this test suite in the offchain repo.
95+
-- testProperty "Succeeds if transaction is valid" $
96+
-- prop_newBid_validInput_succeeds
97+
-- testProperty "Fails if tx validity interval is incorrect" $
98+
-- prop_newBid_incorrectValidRange_fails
99+
[ testProperty "Fails if standing bid output does not exist" $
97100
prop_newBid_missingStandingBidOutput_fails
98101
, testProperty "Fails if there are multiple standing bid outputs" $
99102
prop_multipleStandingBidOutputs_fails
@@ -105,8 +108,6 @@ spec =
105108
prop_newBid_invalidNewBidStateDatum_fails
106109
, testProperty "Fails if new bid state is empty" $
107110
prop_newBid_emptyNewBidState_fails
108-
, testProperty "Fails if tx validity interval is incorrect" $
109-
prop_newBid_incorrectValidRange_fails
110111
]
111112
, testGroup "MoveToHydra" $
112113
[ testProperty "Succeeds if transaction is valid" $
@@ -170,8 +171,8 @@ prop_mintsBurnsValue_fails testContext =
170171

171172
-- NewBid ------------------------------------------------------------
172173

173-
prop_newBid_validInput_succeeds :: NewBidTestContext -> Property
174-
prop_newBid_validInput_succeeds testContext =
174+
_prop_newBid_validInput_succeeds :: NewBidTestContext -> Property
175+
_prop_newBid_validInput_succeeds testContext =
175176
shouldSucceed $
176177
testNewBid testContext def
177178

@@ -223,8 +224,8 @@ prop_newBid_emptyNewBidState_fails testContext =
223224
{ newBidStateMode = NewBidStateEmpty
224225
}
225226

226-
prop_newBid_incorrectValidRange_fails :: NewBidTestContext -> Property
227-
prop_newBid_incorrectValidRange_fails testContext =
227+
_prop_newBid_incorrectValidRange_fails :: NewBidTestContext -> Property
228+
_prop_newBid_incorrectValidRange_fails testContext =
228229
shouldFailWithError StandingBid'NewBid'Error'IncorrectValidityInterval $
229230
testNewBid testContext $
230231
def

0 commit comments

Comments
 (0)