|
| 1 | +// Copyright 2025 Blink Labs Software |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package alonzo |
| 16 | + |
| 17 | +import ( |
| 18 | + "math/big" |
| 19 | + "reflect" |
| 20 | + "testing" |
| 21 | + |
| 22 | + "github.com/blinklabs-io/gouroboros/cbor" |
| 23 | + "github.com/blinklabs-io/gouroboros/internal/test" |
| 24 | + "github.com/blinklabs-io/gouroboros/ledger/common" |
| 25 | + "github.com/blinklabs-io/gouroboros/ledger/mary" |
| 26 | + "github.com/blinklabs-io/plutigo/pkg/data" |
| 27 | +) |
| 28 | + |
| 29 | +func TestAlonzoTransactionOutputToPlutusDataCoinOnly(t *testing.T) { |
| 30 | + testAddr := "addr_test1vqg3zyg3zyg3zyg3zyg3zyg3zyg3zyg3zyg3zyg3zyg3zygxrcya6" |
| 31 | + var testAmount uint64 = 123_456_789 |
| 32 | + testTxOut := AlonzoTransactionOutput{ |
| 33 | + OutputAddress: func() common.Address { foo, _ := common.NewAddress(testAddr); return foo }(), |
| 34 | + OutputAmount: mary.MaryTransactionOutputValue{ |
| 35 | + Amount: testAmount, |
| 36 | + }, |
| 37 | + } |
| 38 | + expectedData := data.NewConstr( |
| 39 | + 0, |
| 40 | + // Address |
| 41 | + data.NewConstr( |
| 42 | + 0, |
| 43 | + data.NewConstr( |
| 44 | + 0, |
| 45 | + data.NewConstr( |
| 46 | + 0, |
| 47 | + data.NewByteString( |
| 48 | + []byte{ |
| 49 | + 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, |
| 50 | + }, |
| 51 | + ), |
| 52 | + ), |
| 53 | + ), |
| 54 | + data.NewConstr( |
| 55 | + 1, |
| 56 | + ), |
| 57 | + ), |
| 58 | + // Value |
| 59 | + data.NewMap( |
| 60 | + [][2]data.PlutusData{ |
| 61 | + { |
| 62 | + data.NewByteString(nil), |
| 63 | + data.NewMap( |
| 64 | + [][2]data.PlutusData{ |
| 65 | + { |
| 66 | + data.NewByteString(nil), |
| 67 | + data.NewInteger(big.NewInt(int64(testAmount))), |
| 68 | + }, |
| 69 | + }, |
| 70 | + ), |
| 71 | + }, |
| 72 | + }, |
| 73 | + ), |
| 74 | + // TODO: empty datum option |
| 75 | + data.NewConstr(0), |
| 76 | + // TODO: empty script ref |
| 77 | + data.NewConstr(1), |
| 78 | + ) |
| 79 | + tmpData := testTxOut.ToPlutusData() |
| 80 | + if !reflect.DeepEqual(tmpData, expectedData) { |
| 81 | + t.Fatalf("did not get expected PlutusData\n got: %#v\n wanted: %#v", tmpData, expectedData) |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +func TestAlonzoTransactionOutputToPlutusDataCoinAssets(t *testing.T) { |
| 86 | + testAddr := "addr_test1vqg3zyg3zyg3zyg3zyg3zyg3zyg3zyg3zyg3zyg3zyg3zygxrcya6" |
| 87 | + var testAmount uint64 = 123_456_789 |
| 88 | + testAssets := common.NewMultiAsset[common.MultiAssetTypeOutput]( |
| 89 | + map[common.Blake2b224]map[cbor.ByteString]common.MultiAssetTypeOutput{ |
| 90 | + common.NewBlake2b224(test.DecodeHexString("29a8fb8318718bd756124f0c144f56d4b4579dc5edf2dd42d669ac61")): { |
| 91 | + cbor.NewByteString(test.DecodeHexString("6675726e697368613239686e")): 123456, |
| 92 | + }, |
| 93 | + common.NewBlake2b224(test.DecodeHexString("eaf8042c1d8203b1c585822f54ec32c4c1bb4d3914603e2cca20bbd5")): { |
| 94 | + cbor.NewByteString(test.DecodeHexString("426f7764757261436f6e63657074733638")): 234567, |
| 95 | + }, |
| 96 | + }, |
| 97 | + ) |
| 98 | + testTxOut := AlonzoTransactionOutput{ |
| 99 | + OutputAddress: func() common.Address { foo, _ := common.NewAddress(testAddr); return foo }(), |
| 100 | + OutputAmount: mary.MaryTransactionOutputValue{ |
| 101 | + Amount: testAmount, |
| 102 | + Assets: &testAssets, |
| 103 | + }, |
| 104 | + } |
| 105 | + expectedData := data.NewConstr( |
| 106 | + 0, |
| 107 | + // Address |
| 108 | + data.NewConstr( |
| 109 | + 0, |
| 110 | + data.NewConstr( |
| 111 | + 0, |
| 112 | + data.NewConstr( |
| 113 | + 0, |
| 114 | + data.NewByteString( |
| 115 | + []byte{ |
| 116 | + 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, |
| 117 | + }, |
| 118 | + ), |
| 119 | + ), |
| 120 | + ), |
| 121 | + data.NewConstr( |
| 122 | + 1, |
| 123 | + ), |
| 124 | + ), |
| 125 | + // Value |
| 126 | + data.NewMap( |
| 127 | + [][2]data.PlutusData{ |
| 128 | + { |
| 129 | + data.NewByteString(nil), |
| 130 | + data.NewMap( |
| 131 | + [][2]data.PlutusData{ |
| 132 | + { |
| 133 | + data.NewByteString(nil), |
| 134 | + data.NewInteger(big.NewInt(int64(testAmount))), |
| 135 | + }, |
| 136 | + }, |
| 137 | + ), |
| 138 | + }, |
| 139 | + { |
| 140 | + data.NewByteString(test.DecodeHexString("29a8fb8318718bd756124f0c144f56d4b4579dc5edf2dd42d669ac61")), |
| 141 | + data.NewMap( |
| 142 | + [][2]data.PlutusData{ |
| 143 | + { |
| 144 | + data.NewByteString(test.DecodeHexString("6675726e697368613239686e")), |
| 145 | + data.NewInteger(big.NewInt(123456)), |
| 146 | + }, |
| 147 | + }, |
| 148 | + ), |
| 149 | + }, |
| 150 | + { |
| 151 | + data.NewByteString(test.DecodeHexString("eaf8042c1d8203b1c585822f54ec32c4c1bb4d3914603e2cca20bbd5")), |
| 152 | + data.NewMap( |
| 153 | + [][2]data.PlutusData{ |
| 154 | + { |
| 155 | + data.NewByteString(test.DecodeHexString("426f7764757261436f6e63657074733638")), |
| 156 | + data.NewInteger(big.NewInt(234567)), |
| 157 | + }, |
| 158 | + }, |
| 159 | + ), |
| 160 | + }, |
| 161 | + }, |
| 162 | + ), |
| 163 | + // Empty datum option |
| 164 | + data.NewConstr(0), |
| 165 | + // Empty script ref |
| 166 | + data.NewConstr(1), |
| 167 | + ) |
| 168 | + tmpData := testTxOut.ToPlutusData() |
| 169 | + if !reflect.DeepEqual(tmpData, expectedData) { |
| 170 | + t.Fatalf("did not get expected PlutusData\n got: %#v\n wanted: %#v", tmpData, expectedData) |
| 171 | + } |
| 172 | +} |
0 commit comments