|
8 | 8 | BLOCK_NUMBER, |
9 | 9 | EXPIRATION, |
10 | 10 | RESOURCE_ENERGY, |
| 11 | + RESOURCE_BANDWIDTH, |
11 | 12 | DELEGATION_BALANCE, |
12 | 13 | DELEGATE_RESOURCE_CONTRACT, |
13 | 14 | } from '../../resources'; |
@@ -323,4 +324,133 @@ describe('Tron DelegateResource builder', function () { |
323 | 324 | }); |
324 | 325 | }); |
325 | 326 | }); |
| 327 | + |
| 328 | + describe('BANDWIDTH serialization (protobuf3 compatibility)', () => { |
| 329 | + it('should round-trip BANDWIDTH transactions correctly', async () => { |
| 330 | + // Build a BANDWIDTH transaction |
| 331 | + const builder = (getBuilder('ttrx') as WrappedBuilder).getDelegateResourceTxBuilder(); |
| 332 | + builder |
| 333 | + .source({ address: PARTICIPANTS.from.address }) |
| 334 | + .setReceiverAddress({ address: PARTICIPANTS.to.address }) |
| 335 | + .block({ number: BLOCK_NUMBER, hash: BLOCK_HASH }) |
| 336 | + .setBalance(DELEGATION_BALANCE) |
| 337 | + .setResource(RESOURCE_BANDWIDTH); |
| 338 | + |
| 339 | + const tx = await builder.build(); |
| 340 | + const txJson = tx.toJson(); |
| 341 | + |
| 342 | + // Verify the built transaction has BANDWIDTH resource |
| 343 | + assert.equal( |
| 344 | + txJson.raw_data.contract[0].parameter.value.resource, |
| 345 | + RESOURCE_BANDWIDTH, |
| 346 | + 'Built transaction should have BANDWIDTH resource' |
| 347 | + ); |
| 348 | + |
| 349 | + // Round-trip: deserialize from broadcast format and rebuild |
| 350 | + const builder2 = getBuilder('ttrx').from(tx.toBroadcastFormat()); |
| 351 | + const tx2 = await builder2.build(); |
| 352 | + const tx2Json = tx2.toJson(); |
| 353 | + |
| 354 | + // Verify resource is preserved after round-trip |
| 355 | + assert.equal( |
| 356 | + tx2Json.raw_data.contract[0].parameter.value.resource, |
| 357 | + RESOURCE_BANDWIDTH, |
| 358 | + 'Resource should be BANDWIDTH after round-trip from broadcast format' |
| 359 | + ); |
| 360 | + |
| 361 | + // Round-trip: deserialize from JSON and rebuild |
| 362 | + const builder3 = getBuilder('ttrx').from(tx.toJson()); |
| 363 | + const tx3 = await builder3.build(); |
| 364 | + const tx3Json = tx3.toJson(); |
| 365 | + |
| 366 | + // Verify resource is preserved after round-trip from JSON |
| 367 | + assert.equal( |
| 368 | + tx3Json.raw_data.contract[0].parameter.value.resource, |
| 369 | + RESOURCE_BANDWIDTH, |
| 370 | + 'Resource should be BANDWIDTH after round-trip from JSON' |
| 371 | + ); |
| 372 | + }); |
| 373 | + |
| 374 | + it('should round-trip ENERGY transactions correctly', async () => { |
| 375 | + // Build an ENERGY transaction |
| 376 | + const builder = (getBuilder('ttrx') as WrappedBuilder).getDelegateResourceTxBuilder(); |
| 377 | + builder |
| 378 | + .source({ address: PARTICIPANTS.from.address }) |
| 379 | + .setReceiverAddress({ address: PARTICIPANTS.to.address }) |
| 380 | + .block({ number: BLOCK_NUMBER, hash: BLOCK_HASH }) |
| 381 | + .setBalance(DELEGATION_BALANCE) |
| 382 | + .setResource(RESOURCE_ENERGY); |
| 383 | + |
| 384 | + const tx = await builder.build(); |
| 385 | + const txJson = tx.toJson(); |
| 386 | + |
| 387 | + // Verify the built transaction has ENERGY resource |
| 388 | + assert.equal( |
| 389 | + txJson.raw_data.contract[0].parameter.value.resource, |
| 390 | + RESOURCE_ENERGY, |
| 391 | + 'Built transaction should have ENERGY resource' |
| 392 | + ); |
| 393 | + |
| 394 | + // Round-trip: deserialize from broadcast format and rebuild |
| 395 | + const builder2 = getBuilder('ttrx').from(tx.toBroadcastFormat()); |
| 396 | + const tx2 = await builder2.build(); |
| 397 | + const tx2Json = tx2.toJson(); |
| 398 | + |
| 399 | + // Verify resource is preserved after round-trip |
| 400 | + assert.equal( |
| 401 | + tx2Json.raw_data.contract[0].parameter.value.resource, |
| 402 | + RESOURCE_ENERGY, |
| 403 | + 'Resource should be ENERGY after round-trip from broadcast format' |
| 404 | + ); |
| 405 | + }); |
| 406 | + |
| 407 | + it('should produce consistent transaction IDs for BANDWIDTH transactions', async () => { |
| 408 | + // Build a BANDWIDTH transaction |
| 409 | + const builder = (getBuilder('ttrx') as WrappedBuilder).getDelegateResourceTxBuilder(); |
| 410 | + builder |
| 411 | + .source({ address: PARTICIPANTS.from.address }) |
| 412 | + .setReceiverAddress({ address: PARTICIPANTS.to.address }) |
| 413 | + .block({ number: BLOCK_NUMBER, hash: BLOCK_HASH }) |
| 414 | + .setBalance(DELEGATION_BALANCE) |
| 415 | + .setResource(RESOURCE_BANDWIDTH); |
| 416 | + |
| 417 | + const tx = await builder.build(); |
| 418 | + const originalTxId = tx.toJson().txID; |
| 419 | + |
| 420 | + // Round-trip and verify txID is consistent |
| 421 | + const builder2 = getBuilder('ttrx').from(tx.toBroadcastFormat()); |
| 422 | + const tx2 = await builder2.build(); |
| 423 | + const roundTripTxId = tx2.toJson().txID; |
| 424 | + |
| 425 | + assert.equal(originalTxId, roundTripTxId, 'Transaction ID should be consistent after round-trip'); |
| 426 | + }); |
| 427 | + |
| 428 | + it('should allow signing BANDWIDTH transactions after round-trip', async () => { |
| 429 | + // Build a BANDWIDTH transaction |
| 430 | + const builder = (getBuilder('ttrx') as WrappedBuilder).getDelegateResourceTxBuilder(); |
| 431 | + builder |
| 432 | + .source({ address: PARTICIPANTS.from.address }) |
| 433 | + .setReceiverAddress({ address: PARTICIPANTS.to.address }) |
| 434 | + .block({ number: BLOCK_NUMBER, hash: BLOCK_HASH }) |
| 435 | + .setBalance(DELEGATION_BALANCE) |
| 436 | + .setResource(RESOURCE_BANDWIDTH); |
| 437 | + |
| 438 | + const tx = await builder.build(); |
| 439 | + |
| 440 | + // Round-trip and sign |
| 441 | + const builder2 = getBuilder('ttrx').from(tx.toBroadcastFormat()); |
| 442 | + builder2.sign({ key: PARTICIPANTS.from.pk }); |
| 443 | + const signedTx = await builder2.build(); |
| 444 | + |
| 445 | + // Verify signature was added |
| 446 | + assert.equal(signedTx.toJson().signature.length, 1, 'Transaction should have one signature'); |
| 447 | + |
| 448 | + // Verify resource is still BANDWIDTH |
| 449 | + assert.equal( |
| 450 | + signedTx.toJson().raw_data.contract[0].parameter.value.resource, |
| 451 | + RESOURCE_BANDWIDTH, |
| 452 | + 'Resource should still be BANDWIDTH after signing' |
| 453 | + ); |
| 454 | + }); |
| 455 | + }); |
326 | 456 | }); |
0 commit comments