Skip to content

Commit 1aace8d

Browse files
jkawanJenita
andauthored
test: added tests for utxorpc block functions (#1116)
* test: added tests for utxorpc block functions Signed-off-by: Jenita <[email protected]> * test: added tests for utxorpc block functions Signed-off-by: Jenita <[email protected]> --------- Signed-off-by: Jenita <[email protected]> Co-authored-by: Jenita <[email protected]>
1 parent 1c7eca1 commit 1aace8d

File tree

7 files changed

+316
-1
lines changed

7 files changed

+316
-1
lines changed

ledger/allegra/block_test.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222

2323
"github.com/blinklabs-io/gouroboros/cbor"
2424
"github.com/blinklabs-io/gouroboros/ledger/allegra"
25+
"github.com/stretchr/testify/assert"
2526
)
2627

2728
// https://cexplorer.io/block/8115134ab013f6a5fd88fd2a10825177a2eedcde31cb2f1f35e492df469cf9a8
@@ -88,3 +89,50 @@ func TestAllegraBlock_CborRoundTrip_UsingCborEncode(t *testing.T) {
8889
}
8990
}
9091
}
92+
93+
func TestAllegraUtxorpcBlock(t *testing.T) {
94+
// Decode the block hex to bytes
95+
blockCbor, err := hex.DecodeString(allegraBlockHex)
96+
assert.NoError(t, err, "Failed to decode block hex")
97+
98+
// Parse the block
99+
block, err := allegra.NewAllegraBlockFromCbor(blockCbor)
100+
assert.NoError(t, err, "Failed to parse block")
101+
assert.NotNil(t, block, "Parsed block is nil")
102+
103+
expectedHash := block.Hash().String()
104+
expectedHeight := block.BlockNumber()
105+
expectedSlot := block.SlotNumber()
106+
107+
// Convert to UTxO RPC format
108+
utxorpcBlock, err := block.Utxorpc()
109+
assert.NoError(t, err, "Utxorpc() conversion failed")
110+
assert.NotNil(t, utxorpcBlock, "RPC block is nil")
111+
112+
t.Run("BlockHeader", func(t *testing.T) {
113+
assert.Equal(t, expectedHash, hex.EncodeToString(utxorpcBlock.Header.Hash),
114+
"Block hash mismatch")
115+
116+
assert.Equal(t, expectedHeight, utxorpcBlock.Header.Height,
117+
"Block height mismatch")
118+
119+
assert.Equal(t, expectedSlot, utxorpcBlock.Header.Slot,
120+
"Block slot mismatch")
121+
})
122+
123+
t.Run("Transactions", func(t *testing.T) {
124+
originalTxs := block.Transactions()
125+
rpcTxs := utxorpcBlock.Body.Tx
126+
127+
assert.Equal(t, len(originalTxs), len(rpcTxs),
128+
"Transaction count mismatch")
129+
130+
if len(rpcTxs) > 0 {
131+
firstRpcTx := rpcTxs[0]
132+
assert.NotEmpty(t, firstRpcTx.Hash, "Transaction hash should not be empty")
133+
assert.Greater(t, len(firstRpcTx.Inputs), 0, "Transaction should have inputs")
134+
assert.Greater(t, len(firstRpcTx.Outputs), 0, "Transaction should have outputs")
135+
assert.Greater(t, firstRpcTx.Fee, uint64(0), "Transaction fee should be positive")
136+
}
137+
})
138+
}

ledger/alonzo/alonzo.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -426,12 +426,18 @@ func (o AlonzoTransactionOutput) Utxorpc() (*utxorpc.TxOutput, error) {
426426
return nil, fmt.Errorf("failed to get address bytes: %w", err)
427427
}
428428

429+
// Datum hash handling
430+
var datumHash []byte
431+
if o.OutputDatumHash != nil {
432+
datumHash = o.OutputDatumHash.Bytes()
433+
}
434+
429435
return &utxorpc.TxOutput{
430436
Address: addressBytes,
431437
Coin: o.Amount(),
432438
Assets: assets,
433439
Datum: &utxorpc.Datum{
434-
Hash: o.OutputDatumHash.Bytes(),
440+
Hash: datumHash,
435441
},
436442
},
437443
nil

ledger/alonzo/block_test.go

Lines changed: 37 additions & 0 deletions
Large diffs are not rendered by default.

ledger/byron/block_test.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,54 @@
11
package byron_test
22

3+
import (
4+
"encoding/hex"
5+
"testing"
6+
7+
"github.com/blinklabs-io/gouroboros/ledger/byron"
8+
"github.com/stretchr/testify/assert"
9+
)
10+
311
// https://cexplorer.io/block/1451a0dbf16cfeddf4991a838961df1b08a68f43a19c0eb3b36cc4029c77a2d8
412
//
513
//slot:4471207
614
//hash:1451a0dbf16cfeddf4991a838961df1b08a68f43a19c0eb3b36cc4029c77a2d8
715
var byronBlockHex = "83851a2d964a09582025df38df102b89ec25a432a2972993d2fa8cc1f597a73e6260b2f07e79501eb084830258200f284bc22f5b96228ee0687b7bb87c56132f77df4235c78a1595729ccfce2001582019fb988d02ec920a6de5ac71c5d5e75f8b73d7ed8e8abea7773e28859983206e82035820d36a2619a672494604e11bb447cbcf5231e9f2ba25c2169177edc941bd50ad6c5820afc0da64183bf2664f3d4eec7238d524ba607faeeab24fc100eb861dba69971b58204e66280cd94d591072349bec0a3090a53aa945562efb6d08d56e53654b0e4098848218cf0758401bc97a2fe02c297880ce8ecfd997fe4c1ec09ee10feeee9f686760166b05281d6283468ffd93becb0c956ccddd642df9b1244c915911185fa49355f6f22bfab9811a004430ed820282840058401bc97a2fe02c297880ce8ecfd997fe4c1ec09ee10feeee9f686760166b05281d6283468ffd93becb0c956ccddd642df9b1244c915911185fa49355f6f22bfab9584061261a95b7613ee6bf2067dad77b70349729b0c50d57bc1cf30de0db4a1e73a885d0054af7c23fc6c37919dba41c602a57e2d0f9329a7954b867338d6fb2c9455840e03e62f083df5576360e60a32e22bbb07b3c8df4fcab8079f1d6f61af3954d242ba8a06516c395939f24096f3df14e103a7d9c2b80a68a9363cf1f27c7a4e3075840325068a2307397703c4eebb1de1ecab0b23c24a5e80c985e0f7546bb6571ee9eb94069708fc25ec67a4a5753a0d49ab5e536131c19c7f9dd4fd32532fd0f71028483010000826a63617264616e6f2d736c01a058204ba92aa320c60acc9ad7b9a64f2eda55c4d2ec28e604faf186708b4f0c4e8edf849f82839f8200d8185824825820b0a7782d21f37e9d98f4cbdc23bf2677d93eca1ac0fb3f79923863a698d53f8f018200d81858248258205bd3e8385d2ecdd17d3b602263e8a5e7aa0edb4dd00221f369c2720f7d85940d008200d81858248258201e4a77f8375548e5bc409a518dbcb4a8437b539682f4e840f4a1056f01cea566008200d81858248258205e83b53253f705c214d904f65fdaaa2f153db59a229a9cee1da6c329b543236100ff9f8282d818584283581ca1932430cb1ad6482a1b67964d778c18b574674fda151cdfa73c63cda101581e581cfc8a0b5477e819a27a34910e6c174b50b871192e95cca1a711bbceb3001abcb52f6d1b000000013446d5718282d818584283581c093916f7e775fba80eaa65cded085d985f7f9e4982cddd2bb7c476aea101581e581c83d3e2df30edf90acf198b85a7327d964f9d92fd739d0c986a914f6c001a27a611b61a000c48cbffa0848200d8185885825840a781c060f2b32d116ef79bb1823d4a25ea36f6c130b3a182713739ea1819e32d261db3dce30b15c29db81d1c284d3fe350d12241e8ccc65cdf08adba90e0ad4558408eb4c9549a6a044d687d6c04fdee2240994f43966ef113ebb3e76a756e39472badb137c3e0268d34ce6042f76c2534220cc1e061a1a29cce065faf486184cf078200d818588582584085dc150754227f68d1640887f8fa57c93e4cad3499f2cb7b5e8258b0b367dcceaa42bf9ea1cfff73fd0fab44d9e0a36ef61bc5d0f294365316a4e0ed12b40a135840f1233519fa85f3ecbb2deaa9dff2d7e943156d49a7a33603381f2c1779b7f65ea0d39a8dcdd227f5d69b9355ab35df0c43c2abb751c6dd24b107a2c7ac51f5088200d81858858258403559467e9b4a4e47af0388e7224358197e5d39c57c71c391db4a7d480f297d8b86b0746de21dc5dfca2bd8b8fa817c1fa1c3bd3eeaddbfd7a6b270564e416d0c5840b0e33544dcb1895b592a612f5be81242a88226d0612da76099b653f89ce7c5641af14fad696ccd44b58744915291240224fd83a26f103c0717752ea256b4af0b8200d8185885825840572c3ea039ded80f19b0d6841e9ad0d0d1b73242ac98538affbec6e7356192f48eba0291ea1b174f9c42e139ba85ce75656a036ba0993dda605d5a62956dba6558406257e3a27a896268cade4d5371537ed606d3004d6269f87ebe6056b6eff737a2a9ef82d27ba1f9b642ffc622ec27b38e69ed41e272d3de0767cad860d50fa10d82839f8200d8185824825820779a319e0d64b80eaff5ed13d08062b8672fc71ac27e7b30574c1c7972764de202ff9f8282d818582183581c2c0dd53d4e6001e006729fc09d74c5a799d5f93c9f4b74748412a823a0001a1abd89081a769cfd808282d818582183581c05b073f36ee030589a31148838cd47e8d8c8f82fec9fe091c7d53cd8a0001a0c5f26b11b00000016bc0c4c47ffa0818200d8185885825840f129f07bbfd87fd1d3ff5fb32e9a5566e02208f89518e9994048add22074f433424e682a392581268c7544e34e9c54378a8820bdcf7dddce30490bbb2d363b4b5840709a2e70d3803554a15d788235bf56c9567407102be375be5071fa81d4c137047743b5f5abefdbab6b2781822474995dff917213c962ecd111619d75b8534f0aff8203d90102809fff82809fff81a0"
16+
17+
func TestByronTransactionUtxorpc(t *testing.T) {
18+
// Decode the hex string to bytes
19+
blockBytes, err := hex.DecodeString(byronBlockHex)
20+
assert.NoError(t, err)
21+
22+
block, err := byron.NewByronMainBlockFromCbor(blockBytes)
23+
assert.NoError(t, err)
24+
assert.NotNil(t, block)
25+
26+
txs := block.Transactions()
27+
assert.Greater(t, len(txs), 0, "Expected at least one transaction in the block")
28+
29+
for _, tx := range txs {
30+
byronTx, ok := tx.(*byron.ByronTransaction)
31+
assert.True(t, ok, "Transaction should be of type *ByronTransaction")
32+
33+
utxoTx, err := byronTx.Utxorpc()
34+
assert.NoError(t, err)
35+
assert.NotNil(t, utxoTx, "Utxorpc() should not return nil")
36+
37+
txHash := byronTx.Hash()
38+
assert.NotEmpty(t, txHash.String(), "Transaction hash should not be empty")
39+
40+
inputs := byronTx.Inputs()
41+
assert.Equal(t, len(inputs), len(byronTx.Consumed()), "Consumed inputs should match Inputs() length")
42+
43+
produced := byronTx.Produced()
44+
assert.Equal(t, len(produced), len(byronTx.Outputs()), "Produced should match Outputs() length")
45+
46+
for _, utxo := range produced {
47+
rpcOut, err := utxo.Output.Utxorpc()
48+
assert.NoError(t, err)
49+
assert.NotNil(t, rpcOut, "Utxorpc output should not be nil")
50+
assert.Greater(t, rpcOut.Coin, uint64(0), "Coin amount should be greater than 0")
51+
assert.NotEmpty(t, rpcOut.Address, "Address bytes should not be empty")
52+
}
53+
}
54+
}

ledger/conway/block_test.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,76 @@ func TestConwayBlock_CborRoundTrip_UsingCborEncode(t *testing.T) {
8888
}
8989
}
9090
}
91+
92+
func TestConwayBlockUtxorpc(t *testing.T) {
93+
blockCbor, err := hex.DecodeString(conwayBlockHex)
94+
if err != nil {
95+
t.Fatalf("failed to decode test block hex: %v", err)
96+
}
97+
98+
block, err := conway.NewConwayBlockFromCbor(blockCbor)
99+
if err != nil {
100+
t.Fatalf("failed to parse Conway block: %v", err)
101+
}
102+
103+
utxoBlock, err := block.Utxorpc()
104+
if err != nil {
105+
t.Fatalf("failed to convert Conway block to utxorpc: %v", err)
106+
}
107+
108+
if utxoBlock.Header == nil {
109+
t.Fatal("block header is nil")
110+
}
111+
112+
expectedHash := block.Hash().Bytes()
113+
if !compareByteSlices(utxoBlock.Header.Hash, expectedHash) {
114+
t.Errorf("block hash mismatch:\nexpected: %x\nactual: %x", expectedHash, utxoBlock.Header.Hash)
115+
}
116+
117+
if utxoBlock.Header.Height != block.BlockNumber() {
118+
t.Errorf("block height mismatch: expected %d, got %d", block.BlockNumber(), utxoBlock.Header.Height)
119+
}
120+
121+
if utxoBlock.Header.Slot != block.SlotNumber() {
122+
t.Errorf("block slot mismatch: expected %d, got %d", block.SlotNumber(), utxoBlock.Header.Slot)
123+
}
124+
125+
if utxoBlock.Body == nil {
126+
t.Fatal("block body is nil")
127+
}
128+
129+
expectedTxCount := len(block.TransactionBodies)
130+
if len(utxoBlock.Body.Tx) != expectedTxCount {
131+
t.Errorf("transaction count mismatch: expected %d, got %d", expectedTxCount, len(utxoBlock.Body.Tx))
132+
}
133+
134+
if expectedTxCount > 0 {
135+
firstTx := block.Transactions()[0]
136+
utxoFirstTx := utxoBlock.Body.Tx[0]
137+
138+
expectedTxHash := firstTx.Hash().Bytes()
139+
if !compareByteSlices(utxoFirstTx.Hash, expectedTxHash) {
140+
t.Errorf("first tx hash mismatch:\nexpected: %x\nactual: %x", expectedTxHash, utxoFirstTx.Hash)
141+
}
142+
143+
if len(utxoFirstTx.Inputs) != len(firstTx.Inputs()) {
144+
t.Errorf("first tx input count mismatch: expected %d, got %d", len(firstTx.Inputs()), len(utxoFirstTx.Inputs))
145+
}
146+
147+
if len(utxoFirstTx.Outputs) != len(firstTx.Outputs()) {
148+
t.Errorf("first tx output count mismatch: expected %d, got %d", len(firstTx.Outputs()), len(utxoFirstTx.Outputs))
149+
}
150+
}
151+
}
152+
153+
func compareByteSlices(a, b []byte) bool {
154+
if len(a) != len(b) {
155+
return false
156+
}
157+
for i := range a {
158+
if a[i] != b[i] {
159+
return false
160+
}
161+
}
162+
return true
163+
}

0 commit comments

Comments
 (0)