Skip to content

Commit

Permalink
refactor: each bid written to a separate node
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris-Hibbert committed Jun 27, 2023
1 parent 0cbe9ab commit ed21579
Show file tree
Hide file tree
Showing 6 changed files with 270 additions and 306 deletions.
52 changes: 30 additions & 22 deletions packages/inter-protocol/src/auction/auctionBook.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import '@agoric/zoe/exported.js';
import '@agoric/zoe/src/contracts/exported.js';

import { AmountMath } from '@agoric/ertp';
import { mustMatch } from '@agoric/store';
import { M, prepareExoClassKit } from '@agoric/vat-data';
import { mustMatch, makeScalarMapStore } from '@agoric/store';
import { M, prepareExoClassKit, provide } from '@agoric/vat-data';

import { assertAllDefined, makeTracer } from '@agoric/internal';
import {
Expand All @@ -18,6 +18,7 @@ import {
} from '@agoric/zoe/src/contractSupport/index.js';
import { E } from '@endo/captp';
import { observeNotifier } from '@agoric/notifier';
import { ToFarFunction } from '@endo/marshal';

import { makeNatAmountShape } from '../contractSupport.js';
import { preparePriceBook, prepareScaledBidBook } from './offerBook.js';
Expand Down Expand Up @@ -106,15 +107,15 @@ export const makeOfferSpecShape = (bidBrand, collateralBrand) => {
*
* @property {Ratio} bidScaling
* @property {Amount<'nat'>} wanted
* @property {Boolean} exitAfterBuy
* @property {boolean} exitAfterBuy
*/

/**
* @typedef {object} PricedBidData
*
* @property {Ratio} price
* @property {Amount<'nat'>} wanted
* @property {Boolean} exitAfterBuy
* @property {boolean} exitAfterBuy
*/

/**
Expand All @@ -130,8 +131,11 @@ export const makeOfferSpecShape = (bidBrand, collateralBrand) => {
* @param {import('@agoric/zoe/src/contractSupport/recorder.js').MakeRecorderKit} makeRecorderKit
*/
export const prepareAuctionBook = (baggage, zcf, makeRecorderKit) => {
const makeScaledBidBook = prepareScaledBidBook(baggage);
const makePriceBook = preparePriceBook(baggage);
const bidDataKits = provide(baggage, 'bidDataKits', () =>
makeScalarMapStore('BidDataKits'),
);
const makeScaledBidBook = prepareScaledBidBook(baggage, makeRecorderKit);
const makePriceBook = preparePriceBook(baggage, makeRecorderKit);

const AuctionBookStateShape = harden({
collateralBrand: M.any(),
Expand All @@ -143,7 +147,8 @@ export const prepareAuctionBook = (baggage, zcf, makeRecorderKit) => {
priceAuthority: M.any(),
updatingOracleQuote: M.any(),
bookDataKit: M.any(),
bidDataKit: M.any(),
bidDataKits: M.any(),
bidsDataKit: M.any(),
priceBook: M.any(),
scaledBidBook: M.any(),
startCollateral: M.any(),
Expand All @@ -161,7 +166,7 @@ export const prepareAuctionBook = (baggage, zcf, makeRecorderKit) => {
* @param {Brand<'nat'>} bidBrand
* @param {Brand<'nat'>} collateralBrand
* @param {PriceAuthority} pAuthority
* @param {Array<StorageNode>} nodes
* @param {Array<ERef<StorageNode>>} nodes
*/
(bidBrand, collateralBrand, pAuthority, nodes) => {
assertAllDefined({ bidBrand, collateralBrand, pAuthority });
Expand All @@ -177,27 +182,35 @@ export const prepareAuctionBook = (baggage, zcf, makeRecorderKit) => {
// returned to the funders.
const { zcfSeat: collateralSeat } = zcf.makeEmptySeatKit();
const { zcfSeat: bidHoldingSeat } = zcf.makeEmptySeatKit();
const [scheduleNode, bidsNode] = nodes;

const bidAmountShape = makeNatAmountShape(bidBrand);
const collateralAmountShape = makeNatAmountShape(collateralBrand);
const makeBidNode = ToFarFunction('makeBidNode', bidId =>
E(bidsNode).makeChildNode(`bids${bidId}`),
);

const scaledBidBook = makeScaledBidBook(
makeBrandedRatioPattern(bidAmountShape, bidAmountShape),
collateralBrand,
makeBidNode,
);

const priceBook = makePriceBook(
makeBrandedRatioPattern(bidAmountShape, collateralAmountShape),
collateralBrand,
makeBidNode,
);

const [scheduleNode, bidsNode] = nodes;
const bookDataKit = makeRecorderKit(
// @ts-expect-error ERef<ScheduleNodes> should be acceptable
scheduleNode,
/** @type {import('@agoric/zoe/src/contractSupport/recorder.js').TypedMatcher<BookDataNotification>} */ (
M.any()
),
);
const bidDataKit = makeRecorderKit(
const bidsDataKit = makeRecorderKit(
// @ts-expect-error ERef<ScheduleNodes> should be acceptable
bidsNode,
/** @type {import('@agoric/zoe/src/contractSupport/recorder.js').TypedMatcher<BidDataNotification>} */ (
M.any()
Expand All @@ -216,7 +229,8 @@ export const prepareAuctionBook = (baggage, zcf, makeRecorderKit) => {
updatingOracleQuote: zeroRatio,

bookDataKit,
bidDataKit,
bidDataKits,
bidsDataKit,

priceBook,
scaledBidBook,
Expand Down Expand Up @@ -479,19 +493,15 @@ export const prepareAuctionBook = (baggage, zcf, makeRecorderKit) => {
exitAfterBuy,
timestamp,
);
helper.publishBidData();
void helper.publishBidData();
}

void helper.publishBookData();
},
publishBidData() {
const { state } = this;
// XXX should this be compressed somewhat? lots of redundant brands.
state.bidDataKit.recorder.write({
scaledBids: state.scaledBidBook.publishOffers(),
// @ts-expect-error how to convince TS these ratios are non-null?
pricedBids: state.priceBook.publishOffers(),
});
state.scaledBidBook.publishOffers();
state.priceBook.publishOffers();
},
publishBookData() {
const { state } = this;
Expand All @@ -511,6 +521,7 @@ export const prepareAuctionBook = (baggage, zcf, makeRecorderKit) => {
collateralAvailable,
currentPriceLevel: state.curAuctionPrice,
});

return state.bookDataKit.recorder.write(bookData);
},
},
Expand Down Expand Up @@ -761,16 +772,13 @@ export const prepareAuctionBook = (baggage, zcf, makeRecorderKit) => {
getDataUpdates() {
return this.state.bookDataKit.subscriber;
},
getBidDataUpdates() {
return this.state.bidDataKit.subscriber;
},
getPublicTopics() {
return {
bookData: makeRecorderTopic(
'Auction schedule',
this.state.bookDataKit,
),
bids: makeRecorderTopic('Auction Bids', this.state.bidDataKit),
bids: makeRecorderTopic('Auction Bids', this.state.bidsDataKit),
};
},
},
Expand Down
14 changes: 5 additions & 9 deletions packages/inter-protocol/src/auction/auctioneer.js
Original file line number Diff line number Diff line change
Expand Up @@ -650,9 +650,6 @@ export const start = async (zcf, privateArgs, baggage) => {
getBookDataUpdates(brand) {
return books.get(brand).getDataUpdates();
},
getBidDataUpdates(brand) {
return books.get(brand).getBidDataUpdates();
},
getPublicTopics(brand) {
if (brand) {
return books.get(brand).getPublicTopics();
Expand Down Expand Up @@ -693,17 +690,16 @@ export const start = async (zcf, privateArgs, baggage) => {

const bookId = `book${bookCounter}`;
bookCounter += 1;
const bNode = E(privateArgs.storageNode).makeChildNode(bookId);
const nodes = await Promise.all([
E(bNode).makeChildNode('schedule'),
E(bNode).makeChildNode('bids'),
]);

const bookNode = E(privateArgs.storageNode).makeChildNode(bookId);
const scheduleNode = E(bookNode).makeChildNode('schedule');
const bidsNode = E(bookNode).makeChildNode('bids');

const newBook = await makeAuctionBook(
brands.Bid,
brand,
priceAuthority,
nodes,
[scheduleNode, bidsNode],
);

// These three store.init() calls succeed or fail atomically
Expand Down
Loading

0 comments on commit ed21579

Please sign in to comment.