Skip to content

Commit

Permalink
fix double heartbeat.heartbeat field in transaction object
Browse files Browse the repository at this point in the history
  • Loading branch information
algorandskiy committed Jan 9, 2025
1 parent a1aa79a commit 43c5288
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 19 deletions.
34 changes: 27 additions & 7 deletions src/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import {
HeartbeatTransactionParams,
} from './types/transactions/base.js';
import { StateProof, StateProofMessage } from './stateproof.js';
import { Heartbeat } from './heartbeat.js';
import { Heartbeat, HeartbeatProof } from './heartbeat.js';
import * as utils from './utils/utils.js';

const ALGORAND_TRANSACTION_LENGTH = 52;
Expand Down Expand Up @@ -251,7 +251,11 @@ export interface StateProofTransactionFields {
}

export interface HeartbeatTransactionFields {
readonly heartbeat: Heartbeat;
readonly address: Address;
readonly proof: HeartbeatProof;
readonly seed: Uint8Array;
readonly voteID: Uint8Array;
readonly keyDilution: bigint;
}

/**
Expand Down Expand Up @@ -712,9 +716,13 @@ export class Transaction implements encoding.Encodable {
}

if (params.heartbeatParams) {
this.heartbeat = {
heartbeat: params.heartbeatParams.heartbeat,
};
this.heartbeat = new Heartbeat({
address: params.heartbeatParams.address,
proof: params.heartbeatParams.proof,
seed: params.heartbeatParams.seed,
voteID: params.heartbeatParams.voteID,
keyDilution: params.heartbeatParams.keyDilution,
});
}

// Determine fee
Expand Down Expand Up @@ -859,7 +867,14 @@ export class Transaction implements encoding.Encodable {
}

if (this.heartbeat) {
data.set('hb', this.heartbeat.heartbeat.toEncodingData());
const heartbeat = new Heartbeat({
address: this.heartbeat.address,
proof: this.heartbeat.proof,
seed: this.heartbeat.seed,
voteID: this.heartbeat.voteID,
keyDilution: this.heartbeat.keyDilution,
});
data.set('hb', heartbeat.toEncodingData());
return data;
}

Expand Down Expand Up @@ -1028,8 +1043,13 @@ export class Transaction implements encoding.Encodable {
};
params.stateProofParams = stateProofParams;
} else if (params.type === TransactionType.hb) {
const heartbeat = Heartbeat.fromEncodingData(data.get('hb'));
const heartbeatParams: HeartbeatTransactionParams = {
heartbeat: Heartbeat.fromEncodingData(data.get('hb')),
address: heartbeat.address,
proof: heartbeat.proof,
seed: heartbeat.seed,
voteID: heartbeat.voteID,
keyDilution: heartbeat.keyDilution,
};
params.heartbeatParams = heartbeatParams;
} else {
Expand Down
26 changes: 23 additions & 3 deletions src/types/transactions/base.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Address } from '../../encoding/address.js';
import { StateProof, StateProofMessage } from '../../stateproof.js';
import { Heartbeat } from '../../heartbeat.js';
import { HeartbeatProof } from '../../heartbeat.js';

/**
* Enum for application transaction types.
Expand Down Expand Up @@ -478,9 +478,29 @@ export interface StateProofTransactionParams {
*/
export interface HeartbeatTransactionParams {
/*
* Heartbeat transaction fields
* Account address this txn is proving onlineness for
*/
heartbeat: Heartbeat;
address: Address;

/**
* Signature using HeartbeatAddress's partkey, thereby showing it is online.
*/
proof: HeartbeatProof;

/**
* The block seed for the this transaction's firstValid block.
*/
seed: Uint8Array;

/**
* Must match the hbAddress account's current VoteID
*/
voteID: Uint8Array;

/**
* Must match hbAddress account's current KeyDilution.
*/
keyDilution: bigint;
}

/**
Expand Down
6 changes: 3 additions & 3 deletions tests/5.Transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1158,7 +1158,7 @@ describe('Sign', () => {
assert.deepStrictEqual(reencRep, encRep);
});

it.only('should correctly serialize and deserialize heartbeat transaction', () => {
it('should correctly serialize and deserialize heartbeat transaction', () => {
const golden = algosdk.base64ToBytes(
'gqRsc2lngaFsxAYLMSAyAxKjdHhuhqJmdmqiZ2jEIP9SQzAGyec/v8omzEOW3/GIM+a7bvPaU5D/ohX7qjFtomhihaFhxCBsU6oqjVx2U65owbsX9/6N7/YCmul+O3liZ0fO2L75/KJrZGSjcHJmhaFwxCAM1TyIrIbgm+yPLT9so6VDI3rKl33t4c4RSGJv6G12eaNwMXPEQBETln14zJzQ1Mb/SNjmDNl0fyQ4DPBQZML8iTEbhqBj+YDAgpNSEduWj7OuVkCSQMq4N/Er/+2HfKUHu//spgOicDLEIB9c5n7WgG+5aOdjfBmuxH3z4TYiQzDVYKjBLhv4IkNfo3Ayc8RAeKpQ+o/GJyGCH0I4f9luN0i7BPXlMlaJAuXLX5Ng8DTN0vtZtztjqYfkwp1cVOYPu+Fce3aIdJHVoUDaJaMIDqFzxEBQN41y5zAZhYHQWf2wWF6CGboqQk6MxDcQ76zXHvVtzrAPUWXZDt4IB8Ha1z+54Hc6LmEoG090pk0IYs+jLN8HonNkxCCPVPjiD5O7V0c3P/SVsHmED7slwllta7c92WiKwnvgoqN2aWTEIHBy8sOi/V0YKXJw8VtW40MbqhtUyO9HC9m/haf84xiGomx2dKNzbmTEIDAp2wPDnojyy8tTgb3sMH++26D5+l7nHZmyRvzFfLsOpHR5cGWiaGI='
);
Expand All @@ -1176,10 +1176,10 @@ describe('Sign', () => {

assert.deepStrictEqual(decTxn.txn.type, algosdk.TransactionType.hb);
assert.deepStrictEqual(
decTxn.txn.heartbeat?.heartbeat.address.toString(),
decTxn.txn.heartbeat?.address.toString(),
hbAddress
);
assert.deepStrictEqual(decTxn.txn.heartbeat?.heartbeat.keyDilution, 100n);
assert.deepStrictEqual(decTxn.txn.heartbeat?.keyDilution, 100n);
});

it('reserializes correctly no genesis ID', () => {
Expand Down
8 changes: 2 additions & 6 deletions tests/cucumber/steps/steps.js
Original file line number Diff line number Diff line change
Expand Up @@ -2327,16 +2327,12 @@ module.exports = function getSteps(options) {
Then(
'the parsed Get Block response should have heartbeat address {string}',
(hbAddress) => {
// console.log(anyBlockResponse.block.payset[0].signedTxn.signedTxn.txn.heartbeat);
// console.log(anyBlockResponse.block.payset[0].signedTxn.signedTxn.txn.heartbeat.heartbeat.address);
// console.log(anyBlockResponse.block.payset[0].signedTxn.signedTxn.txn.heartbeat.heartbeat.proof);
// console.log(anyBlockResponse.block.payset[0].signedTxn.signedTxn.txn.heartbeat.heartbeat.seed);
assert.ok(
anyBlockResponse.block.payset[0].signedTxn.signedTxn.txn.heartbeat
.heartbeat.address instanceof algosdk.Address
.address instanceof algosdk.Address
);
const hbAddressString =
anyBlockResponse.block.payset[0].signedTxn.signedTxn.txn.heartbeat.heartbeat.address.toString();
anyBlockResponse.block.payset[0].signedTxn.signedTxn.txn.heartbeat.address.toString();
assert.strictEqual(hbAddress, hbAddressString);
}
);
Expand Down

0 comments on commit 43c5288

Please sign in to comment.