Skip to content

Commit

Permalink
fix(datastore): broker not updating settlement
Browse files Browse the repository at this point in the history
  • Loading branch information
blakebyrnes committed Oct 26, 2024
1 parent afed957 commit efbf866
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 27 deletions.
12 changes: 6 additions & 6 deletions datastore/core/db/DatastoreChannelHoldsDb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,12 @@ export default class DatastoreChannelHoldsDb {
if (channelHold.remaining < payment.microgons) {
throw new Error('This channelHold does not have enough remaining funds.');
}
if (
Math.ceil((channelHold.allocated - channelHold.remaining + payment.microgons) / 1000) >
Number(payment.channelHold.settledMilligons)
) {
const neededMilligons = Math.ceil(
(channelHold.allocated - channelHold.remaining + payment.microgons) / 1000,
);
if (neededMilligons > Number(payment.channelHold.settledMilligons)) {
throw new Error(
`This channelHold needs a larger settlement to debit. Current settledMilligons=${payment.channelHold.settledMilligons}, New spentMicrogons=${channelHold.allocated - channelHold.remaining + payment.microgons} (ceiling to nearest milligon)`,
`This channelHold needs a larger settlement to debit. Current settledMilligons=${payment.channelHold.settledMilligons}, New spentMicrogons=${neededMilligons} (ceiling to nearest milligon)`,
);
}
throw new Error('Could not debit the channelHold.');
Expand All @@ -162,7 +162,7 @@ export default class DatastoreChannelHoldsDb {
const result = this.finalizeStatement.run({ id: channelHoldId, microgons: adjustment });
if (!result.changes || result.changes < 1) {
throw new Error(
`Could not finalize the payment. ${channelHoldId} -> adjustment: ${adjustment}`,
`Could not finalize the payment. ${channelHoldId} -> adjustment: ${adjustment} (entry: ${entry.microgons}, final: ${finalMicrogons})`,
);
}
}
Expand Down
2 changes: 2 additions & 0 deletions datastore/main/payments/BrokerChannelHoldSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ export default class BrokerChannelHoldSource implements IChannelHoldSource {
const bytes = BalanceChangeBuilder.toSigningMessage(json);
const signature = this.keyring.getPairs()[0].sign(bytes, { withType: true });
balanceChange.signature = Buffer.from(signature);
channelHold.settledSignature = balanceChange.signature;
channelHold.settledMilligons = updatedSettlement;
}

public static createSignatureMessage(
Expand Down
43 changes: 24 additions & 19 deletions end-to-end/lib/TestDatabroker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,29 +100,34 @@ export default class TestDatabroker {
Helpers.onClose(() => adminConnection.disconnect());
await adminConnection.connect();
await new Promise(setImmediate);
console.log('connected');
const { id } = await adminConnection.sendRequest({
command: 'Organization.create',
args: [
{
name: 'Test Organization',
balance: amount,
},
],
}, 10e3);
const { id } = await adminConnection.sendRequest(
{
command: 'Organization.create',
args: [
{
name: 'Test Organization',
balance: amount,
},
],
},
10e3,
);

const identity = Identity.loadFromFile(identityPath).bech32;

console.log('[DATABROKER] Registering user', identity);
await adminConnection.sendRequest({
command: 'User.create',
args: [
{
identity,
organizationId: id,
},
],
}, 10e3);
await adminConnection.sendRequest(
{
command: 'User.create',
args: [
{
identity,
organizationId: id,
},
],
},
10e3,
);
console.log('[DATABROKER] Registered user', identity);
await adminConnection.disconnect();
}
Expand Down
4 changes: 2 additions & 2 deletions end-to-end/test/payments-broker.e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,8 @@ async function setupDatastore(
1,
{
[version]: mainchainClient.createType('ArgonPrimitivesDomainVersionHost', {
datastoreId: mainchainClient.createType('Bytes', datastoreId),
host: mainchainClient.createType('Bytes', `ws://127.0.0.1:${cloudAddress.split(':')[1]}`),
datastoreId,
host: `ws://127.0.0.1:${cloudAddress.split(':')[1]}`,
}),
},
);
Expand Down

0 comments on commit efbf866

Please sign in to comment.