|
| 1 | +// Copyright 2023 Blink Labs, LLC. |
| 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 ledger |
| 16 | + |
| 17 | +import ( |
| 18 | + "encoding/hex" |
| 19 | + "reflect" |
| 20 | + "testing" |
| 21 | + |
| 22 | + "github.com/blinklabs-io/gouroboros/cbor" |
| 23 | + "github.com/blinklabs-io/gouroboros/internal/test" |
| 24 | +) |
| 25 | + |
| 26 | +func createMaryTransactionOutputValueAssets(policyId []byte, assetName []byte, amount uint64) map[Blake2b224]map[cbor.ByteString]uint64 { |
| 27 | + ret := map[Blake2b224]map[cbor.ByteString]uint64{} |
| 28 | + policyIdKey := Blake2b224{} |
| 29 | + copy(policyIdKey[:], policyId) |
| 30 | + assetKey := cbor.ByteString(assetName) |
| 31 | + ret[policyIdKey] = map[cbor.ByteString]uint64{ |
| 32 | + assetKey: amount, |
| 33 | + } |
| 34 | + return ret |
| 35 | +} |
| 36 | + |
| 37 | +func TestMaryTransactionOutputValueEncodeDecode(t *testing.T) { |
| 38 | + var tests = []struct { |
| 39 | + CborHex string |
| 40 | + Object interface{} |
| 41 | + }{ |
| 42 | + { |
| 43 | + CborHex: "1a02d71996", |
| 44 | + Object: MaryTransactionOutputValue{Amount: 47651222}, |
| 45 | + }, |
| 46 | + { |
| 47 | + CborHex: "1b0000000129d2de56", |
| 48 | + Object: MaryTransactionOutputValue{Amount: 4996652630}, |
| 49 | + }, |
| 50 | + { |
| 51 | + CborHex: "821a003d0900a1581c00000002df633853f6a47465c9496721d2d5b1291b8398016c0e87aea1476e7574636f696e01", |
| 52 | + // [4000000, {h'00000002DF633853F6A47465C9496721D2D5B1291B8398016C0E87AE': {h'6E7574636F696E': 1}}] |
| 53 | + Object: MaryTransactionOutputValue{ |
| 54 | + Amount: 4000000, |
| 55 | + Assets: createMaryTransactionOutputValueAssets( |
| 56 | + test.DecodeHexString("00000002DF633853F6A47465C9496721D2D5B1291B8398016C0E87AE"), |
| 57 | + test.DecodeHexString("6E7574636F696E"), |
| 58 | + 1, |
| 59 | + ), |
| 60 | + }, |
| 61 | + }, |
| 62 | + { |
| 63 | + CborHex: "821a004986e3a1581c3a9241cd79895e3a8d65261b40077d4437ce71e9d7c8c6c00e3f658ea1494669727374636f696e01", |
| 64 | + // [4818659, {h'3A9241CD79895E3A8D65261B40077D4437CE71E9D7C8C6C00E3F658E': {h'4669727374636F696E': 1}}] |
| 65 | + Object: MaryTransactionOutputValue{ |
| 66 | + Amount: 4818659, |
| 67 | + Assets: createMaryTransactionOutputValueAssets( |
| 68 | + test.DecodeHexString("3A9241CD79895E3A8D65261B40077D4437CE71E9D7C8C6C00E3F658E"), |
| 69 | + test.DecodeHexString("4669727374636F696E"), |
| 70 | + 1, |
| 71 | + ), |
| 72 | + }, |
| 73 | + }, |
| 74 | + } |
| 75 | + for _, test := range tests { |
| 76 | + // Test decode |
| 77 | + cborData, err := hex.DecodeString(test.CborHex) |
| 78 | + if err != nil { |
| 79 | + t.Fatalf("failed to decode CBOR hex: %s", err) |
| 80 | + } |
| 81 | + tmpObj := MaryTransactionOutputValue{} |
| 82 | + _, err = cbor.Decode(cborData, &tmpObj) |
| 83 | + if err != nil { |
| 84 | + t.Fatalf("failed to decode CBOR: %s", err) |
| 85 | + } |
| 86 | + if !reflect.DeepEqual(tmpObj, test.Object) { |
| 87 | + t.Fatalf("CBOR did not decode to expected object\n got: %#v\n wanted: %#v", tmpObj, test.Object) |
| 88 | + } |
| 89 | + // Test encode |
| 90 | + cborData, err = cbor.Encode(test.Object) |
| 91 | + if err != nil { |
| 92 | + t.Fatalf("failed to encode object to CBOR: %s", err) |
| 93 | + } |
| 94 | + cborHex := hex.EncodeToString(cborData) |
| 95 | + if cborHex != test.CborHex { |
| 96 | + t.Fatalf("object did not encode to expected CBOR\n got: %s\n wanted: %s", cborHex, test.CborHex) |
| 97 | + } |
| 98 | + } |
| 99 | +} |
0 commit comments