Skip to content

Commit

Permalink
chore: move address and common types to sub-package (#689)
Browse files Browse the repository at this point in the history
This is in preparation for further refactoring. Additionally, it:

* adds compatability types/functions to keep existing code working
* adds NewMultiAsset function to create MultiAsset from existing data
* switches to public interface for iterating over MultiAsset in various
  places
  • Loading branch information
agaffney authored Aug 24, 2024
1 parent b7595f9 commit 3cb9204
Show file tree
Hide file tree
Showing 16 changed files with 149 additions and 73 deletions.
3 changes: 2 additions & 1 deletion ledger/allegra.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
utxorpc "github.com/utxorpc/go-codegen/utxorpc/v1alpha/cardano"

"github.com/blinklabs-io/gouroboros/cbor"
"github.com/blinklabs-io/gouroboros/ledger/common"
)

const (
Expand Down Expand Up @@ -202,7 +203,7 @@ func (t AllegraTransaction) RequiredSigners() []Blake2b224 {
return t.Body.RequiredSigners()
}

func (t AllegraTransaction) AssetMint() *MultiAsset[MultiAssetTypeMint] {
func (t AllegraTransaction) AssetMint() *common.MultiAsset[common.MultiAssetTypeMint] {
return t.Body.AssetMint()
}

Expand Down
21 changes: 12 additions & 9 deletions ledger/alonzo.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
utxorpc "github.com/utxorpc/go-codegen/utxorpc/v1alpha/cardano"

"github.com/blinklabs-io/gouroboros/cbor"
"github.com/blinklabs-io/gouroboros/ledger/common"
)

const (
Expand Down Expand Up @@ -209,10 +210,10 @@ func (o *AlonzoTransactionOutput) MarshalCBOR() ([]byte, error) {

func (o AlonzoTransactionOutput) MarshalJSON() ([]byte, error) {
tmpObj := struct {
Address Address `json:"address"`
Amount uint64 `json:"amount"`
Assets *MultiAsset[MultiAssetTypeOutput] `json:"assets,omitempty"`
DatumHash string `json:"datumHash,omitempty"`
Address Address `json:"address"`
Amount uint64 `json:"amount"`
Assets *common.MultiAsset[common.MultiAssetTypeOutput] `json:"assets,omitempty"`
DatumHash string `json:"datumHash,omitempty"`
}{
Address: o.OutputAddress,
Amount: o.OutputAmount.Amount,
Expand All @@ -232,7 +233,7 @@ func (o AlonzoTransactionOutput) Amount() uint64 {
return o.OutputAmount.Amount
}

func (o AlonzoTransactionOutput) Assets() *MultiAsset[MultiAssetTypeOutput] {
func (o AlonzoTransactionOutput) Assets() *common.MultiAsset[common.MultiAssetTypeOutput] {
return o.OutputAmount.Assets
}

Expand All @@ -247,13 +248,15 @@ func (o AlonzoTransactionOutput) Datum() *cbor.LazyValue {
func (o AlonzoTransactionOutput) Utxorpc() *utxorpc.TxOutput {
var assets []*utxorpc.Multiasset
if o.Assets() != nil {
for policyId, policyData := range o.Assets().data {
tmpAssets := o.Assets()
for _, policyId := range tmpAssets.Policies() {
var ma = &utxorpc.Multiasset{
PolicyId: policyId.Bytes(),
}
for assetName, amount := range policyData {
for _, assetName := range tmpAssets.Assets(policyId) {
amount := tmpAssets.Asset(policyId, assetName)
asset := &utxorpc.Asset{
Name: assetName.Bytes(),
Name: assetName,
OutputCoin: amount,
}
ma.Assets = append(ma.Assets, asset)
Expand Down Expand Up @@ -366,7 +369,7 @@ func (t AlonzoTransaction) RequiredSigners() []Blake2b224 {
return t.Body.RequiredSigners()
}

func (t AlonzoTransaction) AssetMint() *MultiAsset[MultiAssetTypeMint] {
func (t AlonzoTransaction) AssetMint() *common.MultiAsset[common.MultiAssetTypeMint] {
return t.Body.AssetMint()
}

Expand Down
23 changes: 13 additions & 10 deletions ledger/babbage.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
utxorpc "github.com/utxorpc/go-codegen/utxorpc/v1alpha/cardano"

"github.com/blinklabs-io/gouroboros/cbor"
"github.com/blinklabs-io/gouroboros/ledger/common"
)

const (
Expand Down Expand Up @@ -363,11 +364,11 @@ func (o *BabbageTransactionOutput) MarshalCBOR() ([]byte, error) {

func (o BabbageTransactionOutput) MarshalJSON() ([]byte, error) {
tmpObj := struct {
Address Address `json:"address"`
Amount uint64 `json:"amount"`
Assets *MultiAsset[MultiAssetTypeOutput] `json:"assets,omitempty"`
Datum *cbor.LazyValue `json:"datum,omitempty"`
DatumHash string `json:"datumHash,omitempty"`
Address Address `json:"address"`
Amount uint64 `json:"amount"`
Assets *common.MultiAsset[common.MultiAssetTypeOutput] `json:"assets,omitempty"`
Datum *cbor.LazyValue `json:"datum,omitempty"`
DatumHash string `json:"datumHash,omitempty"`
}{
Address: o.OutputAddress,
Amount: o.OutputAmount.Amount,
Expand All @@ -392,7 +393,7 @@ func (o BabbageTransactionOutput) Amount() uint64 {
return o.OutputAmount.Amount
}

func (o BabbageTransactionOutput) Assets() *MultiAsset[MultiAssetTypeOutput] {
func (o BabbageTransactionOutput) Assets() *common.MultiAsset[common.MultiAssetTypeOutput] {
return o.OutputAmount.Assets
}

Expand Down Expand Up @@ -420,13 +421,15 @@ func (o BabbageTransactionOutput) Utxorpc() *utxorpc.TxOutput {

var assets []*utxorpc.Multiasset
if o.Assets() != nil {
for policyId, policyData := range o.Assets().data {
tmpAssets := o.Assets()
for _, policyId := range tmpAssets.Policies() {
var ma = &utxorpc.Multiasset{
PolicyId: policyId.Bytes(),
}
for assetName, amount := range policyData {
for _, assetName := range tmpAssets.Assets(policyId) {
amount := tmpAssets.Asset(policyId, assetName)
asset := &utxorpc.Asset{
Name: assetName.Bytes(),
Name: assetName,
OutputCoin: amount,
}
ma.Assets = append(ma.Assets, asset)
Expand Down Expand Up @@ -540,7 +543,7 @@ func (t BabbageTransaction) RequiredSigners() []Blake2b224 {
return t.Body.RequiredSigners()
}

func (t BabbageTransaction) AssetMint() *MultiAsset[MultiAssetTypeMint] {
func (t BabbageTransaction) AssetMint() *common.MultiAsset[common.MultiAssetTypeMint] {
return t.Body.AssetMint()
}

Expand Down
5 changes: 3 additions & 2 deletions ledger/byron.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
utxorpc "github.com/utxorpc/go-codegen/utxorpc/v1alpha/cardano"

"github.com/blinklabs-io/gouroboros/cbor"
"github.com/blinklabs-io/gouroboros/ledger/common"
)

const (
Expand Down Expand Up @@ -216,7 +217,7 @@ func (t *ByronTransaction) RequiredSigners() []Blake2b224 {
return nil
}

func (t *ByronTransaction) AssetMint() *MultiAsset[MultiAssetTypeMint] {
func (t *ByronTransaction) AssetMint() *common.MultiAsset[common.MultiAssetTypeMint] {
// No asset mints in Byron
return nil
}
Expand Down Expand Up @@ -385,7 +386,7 @@ func (o ByronTransactionOutput) Amount() uint64 {
return o.OutputAmount
}

func (o ByronTransactionOutput) Assets() *MultiAsset[MultiAssetTypeOutput] {
func (o ByronTransactionOutput) Assets() *common.MultiAsset[common.MultiAssetTypeOutput] {
return nil
}

Expand Down
2 changes: 1 addition & 1 deletion ledger/address.go → ledger/common/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package ledger
package common

import (
"fmt"
Expand Down
2 changes: 1 addition & 1 deletion ledger/address_test.go → ledger/common/address_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package ledger
package common

import (
"testing"
Expand Down
10 changes: 8 additions & 2 deletions ledger/common.go → ledger/common/common.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2023 Blink Labs Software
// Copyright 2024 Blink Labs Software
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package ledger
package common

import (
"encoding/hex"
Expand Down Expand Up @@ -82,6 +82,12 @@ type MultiAsset[T MultiAssetTypeOutput | MultiAssetTypeMint] struct {
data map[Blake2b224]map[cbor.ByteString]T
}

// NewMultiAsset creates a MultiAsset with the specified data
func NewMultiAsset[T MultiAssetTypeOutput | MultiAssetTypeMint](data map[Blake2b224]map[cbor.ByteString]T) MultiAsset[T] {
return MultiAsset[T]{data: data}
}

// multiAssetJson is a convenience type for marshaling/unmarshaling MultiAsset to/from JSON
type multiAssetJson[T MultiAssetTypeOutput | MultiAssetTypeMint] struct {
Name string `json:"name"`
NameHex string `json:"nameHex"`
Expand Down
16 changes: 15 additions & 1 deletion ledger/common_test.go → ledger/common/common_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,18 @@
package ledger
// Copyright 2024 Blink Labs Software
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package common

import (
"encoding/hex"
Expand Down
56 changes: 56 additions & 0 deletions ledger/compat.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright 2024 Blink Labs Software
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package ledger

import (
"github.com/blinklabs-io/gouroboros/ledger/common"
)

// The below are compatability types and functions to keep existing code working
// after a refactor of the ledger package

// Hash types
type Blake2b224 = common.Blake2b224
type Blake2b256 = common.Blake2b256

func NewBlake2b224(data []byte) Blake2b224 {
return common.NewBlake2b224(data)
}

func NewBlake2b256(data []byte) Blake2b256 {
return common.NewBlake2b256(data)
}

// Address
type Address = common.Address
type AddrKeyHash = common.AddrKeyHash

func NewAddress(addr string) (Address, error) {
return common.NewAddress(addr)
}

// Other types
type IssuerVkey = common.IssuerVkey
type PoolId = common.PoolId

func NewPoolIdFromBech32(poolId string) (PoolId, error) {
return common.NewPoolIdFromBech32(poolId)
}

type AssetFingerprint = common.AssetFingerprint

func NewAssetFingerprint(policyId []byte, assetName []byte) AssetFingerprint {
return common.NewAssetFingerprint(policyId, assetName)
}
3 changes: 2 additions & 1 deletion ledger/conway.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
utxorpc "github.com/utxorpc/go-codegen/utxorpc/v1alpha/cardano"

"github.com/blinklabs-io/gouroboros/cbor"
"github.com/blinklabs-io/gouroboros/ledger/common"
)

const (
Expand Down Expand Up @@ -502,7 +503,7 @@ func (t ConwayTransaction) RequiredSigners() []Blake2b224 {
return t.Body.RequiredSigners()
}

func (t ConwayTransaction) AssetMint() *MultiAsset[MultiAssetTypeMint] {
func (t ConwayTransaction) AssetMint() *common.MultiAsset[common.MultiAssetTypeMint] {
return t.Body.AssetMint()
}

Expand Down
19 changes: 10 additions & 9 deletions ledger/mary.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
utxorpc "github.com/utxorpc/go-codegen/utxorpc/v1alpha/cardano"

"github.com/blinklabs-io/gouroboros/cbor"
"github.com/blinklabs-io/gouroboros/ledger/common"
)

const (
Expand Down Expand Up @@ -120,8 +121,8 @@ type MaryTransactionBody struct {
ProtocolParamUpdates map[Blake2b224]MaryProtocolParameterUpdate
Epoch uint64
} `cbor:"6,keyasint,omitempty"`
TxOutputs []MaryTransactionOutput `cbor:"1,keyasint,omitempty"`
TxMint *MultiAsset[MultiAssetTypeMint] `cbor:"9,keyasint,omitempty"`
TxOutputs []MaryTransactionOutput `cbor:"1,keyasint,omitempty"`
TxMint *common.MultiAsset[common.MultiAssetTypeMint] `cbor:"9,keyasint,omitempty"`
}

func (b *MaryTransactionBody) UnmarshalCBOR(cborData []byte) error {
Expand All @@ -145,7 +146,7 @@ func (b *MaryTransactionBody) ProtocolParametersUpdate() map[Blake2b224]any {
return updateMap
}

func (b *MaryTransactionBody) AssetMint() *MultiAsset[MultiAssetTypeMint] {
func (b *MaryTransactionBody) AssetMint() *common.MultiAsset[common.MultiAssetTypeMint] {
return b.TxMint
}

Expand Down Expand Up @@ -223,7 +224,7 @@ func (t MaryTransaction) RequiredSigners() []Blake2b224 {
return t.Body.RequiredSigners()
}

func (t MaryTransaction) AssetMint() *MultiAsset[MultiAssetTypeMint] {
func (t MaryTransaction) AssetMint() *common.MultiAsset[common.MultiAssetTypeMint] {
return t.Body.AssetMint()
}

Expand Down Expand Up @@ -312,9 +313,9 @@ type MaryTransactionOutput struct {

func (o MaryTransactionOutput) MarshalJSON() ([]byte, error) {
tmpObj := struct {
Address Address `json:"address"`
Amount uint64 `json:"amount"`
Assets *MultiAsset[MultiAssetTypeOutput] `json:"assets,omitempty"`
Address Address `json:"address"`
Amount uint64 `json:"amount"`
Assets *common.MultiAsset[common.MultiAssetTypeOutput] `json:"assets,omitempty"`
}{
Address: o.OutputAddress,
Amount: o.OutputAmount.Amount,
Expand All @@ -331,7 +332,7 @@ func (o MaryTransactionOutput) Amount() uint64 {
return o.OutputAmount.Amount
}

func (o MaryTransactionOutput) Assets() *MultiAsset[MultiAssetTypeOutput] {
func (o MaryTransactionOutput) Assets() *common.MultiAsset[common.MultiAssetTypeOutput] {
return o.OutputAmount.Assets
}

Expand All @@ -355,7 +356,7 @@ type MaryTransactionOutputValue struct {
cbor.StructAsArray
Amount uint64
// We use a pointer here to allow it to be nil
Assets *MultiAsset[MultiAssetTypeOutput]
Assets *common.MultiAsset[common.MultiAssetTypeOutput]
}

func (v *MaryTransactionOutputValue) UnmarshalCBOR(data []byte) error {
Expand Down
6 changes: 4 additions & 2 deletions ledger/mary_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,23 @@ import (

"github.com/blinklabs-io/gouroboros/cbor"
"github.com/blinklabs-io/gouroboros/internal/test"
"github.com/blinklabs-io/gouroboros/ledger/common"
)

func createMaryTransactionOutputValueAssets(
policyId []byte,
assetName []byte,
amount uint64,
) *MultiAsset[MultiAssetTypeOutput] {
) *common.MultiAsset[common.MultiAssetTypeOutput] {
data := map[Blake2b224]map[cbor.ByteString]uint64{}
policyIdKey := Blake2b224{}
copy(policyIdKey[:], policyId)
assetKey := cbor.NewByteString(assetName)
data[policyIdKey] = map[cbor.ByteString]uint64{
assetKey: amount,
}
return &MultiAsset[MultiAssetTypeOutput]{data: data}
ret := common.NewMultiAsset[common.MultiAssetTypeOutput](data)
return &ret
}

func TestMaryTransactionOutputValueEncodeDecode(t *testing.T) {
Expand Down
Loading

0 comments on commit 3cb9204

Please sign in to comment.