Skip to content

Commit

Permalink
additional refactoring
Browse files Browse the repository at this point in the history
Signed-off-by: Angelo De Caro <[email protected]>
  • Loading branch information
adecaro committed Nov 18, 2024
1 parent 6e9c0b5 commit 4305ba0
Show file tree
Hide file tree
Showing 13 changed files with 581 additions and 97 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,21 @@ Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/

package common
package htlc

import (
"encoding/json"
"time"

"github.com/hyperledger-labs/fabric-token-sdk/token/core/common"
"github.com/hyperledger-labs/fabric-token-sdk/token/driver"
"github.com/hyperledger-labs/fabric-token-sdk/token/services/identity"
"github.com/hyperledger-labs/fabric-token-sdk/token/services/interop/htlc"
"github.com/pkg/errors"
)

// TransferHTLCValidate checks the validity of the HTLC scripts, if any
func TransferHTLCValidate[P driver.PublicParameters, T driver.Output, TA driver.TransferAction, IA driver.IssueAction](ctx *Context[P, T, TA, IA]) error {
func TransferHTLCValidate[P driver.PublicParameters, T driver.Output, TA driver.TransferAction, IA driver.IssueAction](ctx *common.Context[P, T, TA, IA]) error {
now := time.Now()

for i, in := range ctx.InputTokens {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,29 @@ Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/

package common
package pledge

import (
"bytes"
"encoding/json"
"fmt"
"time"

"github.com/hyperledger-labs/fabric-token-sdk/token/core/common"
"github.com/hyperledger-labs/fabric-token-sdk/token/driver"
"github.com/hyperledger-labs/fabric-token-sdk/token/services/identity"
"github.com/hyperledger-labs/fabric-token-sdk/token/services/interop/pledge"
"github.com/pkg/errors"
)

func IssuePledgeValidate[P driver.PublicParameters, T driver.Output, TA driver.TransferAction, IA driver.IssueAction](ctx *Context[P, T, TA, IA]) error {
func IssuePledgeValidate[P driver.PublicParameters, T driver.Output, TA driver.TransferAction, IA driver.IssueAction](ctx *common.Context[P, T, TA, IA]) error {
for k := range ctx.IssueAction.GetMetadata() {
ctx.CountMetadataKey(k)
}
return nil
}

func TransferPledgeValidate[P driver.PublicParameters, T driver.Output, TA driver.TransferAction, IA driver.IssueAction](ctx *Context[P, T, TA, IA]) error {
func TransferPledgeValidate[P driver.PublicParameters, T driver.Output, TA driver.TransferAction, IA driver.IssueAction](ctx *common.Context[P, T, TA, IA]) error {
for _, in := range ctx.InputTokens {
id, err := identity.UnmarshalTypedIdentity(in.GetOwner())
if err != nil {
Expand Down
16 changes: 8 additions & 8 deletions token/core/fabtoken/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,23 @@ func (m *OutputMetadata) Serialize() ([]byte, error) {
}

// Output carries the output of an action
type Output struct {
Output *token.Token
}
type Output token.Token

// Serialize marshals a Output
func (t *Output) Serialize() ([]byte, error) {
return json.Marshal(t.Output)
return json.Marshal(t)
}

// IsRedeem returns true if the owner of a Output is empty
// todo update interface to account for nil t.Output.Owner and nil t.Output
// IsRedeem returns true if the owner of a Owner is empty
func (t *Output) IsRedeem() bool {
return len(t.Output.Owner.Raw) == 0
return t.Owner == nil || len(t.Owner.Raw) == 0
}

func (t *Output) GetOwner() []byte {
return t.Output.Owner.Raw
if t.Owner != nil {
return t.Owner.Raw
}
return nil
}

// IssueAction encodes a fabtoken Issue
Expand Down
4 changes: 2 additions & 2 deletions token/core/fabtoken/driver/interop/state/fabric/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ type StateQueryExecutor struct {

func NewStateQueryExecutor(
Logger logging.Logger,
relayProvider fabric3.RelayProvider,
RelayProvider fabric3.RelayProvider,
targetNetworkURL string,
relaySelector *fabric.NetworkService,
) (*StateQueryExecutor, error) {
Expand All @@ -53,7 +53,7 @@ func NewStateQueryExecutor(
}
return &StateQueryExecutor{
Logger: Logger,
RelayProvider: relayProvider,
RelayProvider: RelayProvider,
TargetNetworkURL: targetNetworkURL,
RelaySelector: relaySelector,
}, nil
Expand Down
10 changes: 4 additions & 6 deletions token/core/fabtoken/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,11 @@ func (s *IssueService) Issue(ctx context.Context, issuerIdentity driver.Identity
return nil, nil, errors.Wrapf(err, "failed to convert [%d] to quantity of precision [%d]", v, precision)
}
outs = append(outs, &Output{
Output: &token2.Token{
Owner: &token2.Owner{
Raw: owners[i],
},
Type: tokenType,
Quantity: q.Hex(),
Owner: &token2.Owner{
Raw: owners[i],
},
Type: tokenType,
Quantity: q.Hex(),
})

meta := &OutputMetadata{
Expand Down
31 changes: 20 additions & 11 deletions token/core/fabtoken/transfer.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,14 @@ func NewTransferService(

// Transfer returns a TransferAction as a function of the passed arguments
// It also returns the corresponding TransferMetadata
func (s *TransferService) Transfer(ctx context.Context, txID string, wallet driver.OwnerWallet, tokenIDs []*token.ID, Outputs []*token.Token, opts *driver.TransferOptions) (driver.TransferAction, *driver.TransferMetadata, error) {
func (s *TransferService) Transfer(
ctx context.Context,
txID string,
wallet driver.OwnerWallet,
tokenIDs []*token.ID,
outputs []*token.Token,
opts *driver.TransferOptions,
) (driver.TransferAction, *driver.TransferMetadata, error) {
// select inputs
inputTokens, err := s.TokenLoader.GetTokens(tokenIDs)
if err != nil {
Expand All @@ -59,9 +66,11 @@ func (s *TransferService) Transfer(ctx context.Context, txID string, wallet driv
// prepare outputs
var outs []*Output
var outputsMetadata [][]byte
for _, output := range Outputs {
for _, output := range outputs {
outs = append(outs, &Output{
Output: output,
Owner: output.Owner,
Type: output.Type,
Quantity: output.Quantity,
})
meta := &OutputMetadata{}
metaRaw, err := meta.Serialize()
Expand All @@ -84,22 +93,22 @@ func (s *TransferService) Transfer(ctx context.Context, txID string, wallet driv
var receivers []driver.Identity
var outputAuditInfos [][]byte
for i, output := range outs {
if output.Output == nil || output.Output.Owner == nil {
if output == nil || output.Owner == nil {
return nil, nil, errors.Errorf("failed to transfer: invalid output at index %d", i)
}
if len(output.Output.Owner.Raw) == 0 { // redeem
receivers = append(receivers, output.Output.Owner.Raw)
if len(output.Owner.Raw) == 0 { // redeem
receivers = append(receivers, output.Owner.Raw)
outputAuditInfos = append(outputAuditInfos, []byte{})
continue
}
recipients, err := s.Deserializer.Recipients(output.Output.Owner.Raw)
recipients, err := s.Deserializer.Recipients(output.Owner.Raw)
if err != nil {
return nil, nil, errors.Wrap(err, "failed getting recipients")
}
receivers = append(receivers, recipients...)
auditInfo, err := s.Deserializer.GetOwnerAuditInfo(output.Output.Owner.Raw, ws)
auditInfo, err := s.Deserializer.GetOwnerAuditInfo(output.Owner.Raw, ws)
if err != nil {
return nil, nil, errors.Wrapf(err, "failed getting audit info for sender identity [%s]", driver.Identity(output.Output.Owner.Raw).String())
return nil, nil, errors.Wrapf(err, "failed getting audit info for sender identity [%s]", driver.Identity(output.Owner.Raw).String())
}
outputAuditInfos = append(outputAuditInfos, auditInfo...)
}
Expand All @@ -126,7 +135,7 @@ func (s *TransferService) Transfer(ctx context.Context, txID string, wallet driv
s.Logger.Debugf("receiver audit info for [%s] is [%s]", receiver, auditInfo)
receiverAuditInfos = append(receiverAuditInfos, auditInfo...)
}
outputs, err := transfer.GetSerializedOutputs()
serializedOutputs, err := transfer.GetSerializedOutputs()
if err != nil {
return nil, nil, errors.Wrapf(err, "failed getting serialized outputs")
}
Expand All @@ -141,7 +150,7 @@ func (s *TransferService) Transfer(ctx context.Context, txID string, wallet driv
TokenIDs: tokenIDs,
Senders: senders,
SenderAuditInfos: senderAuditInfos,
Outputs: outputs,
Outputs: serializedOutputs,
OutputsMetadata: outputsMetadata,
OutputAuditInfos: outputAuditInfos,
Receivers: receivers,
Expand Down
19 changes: 10 additions & 9 deletions token/core/fabtoken/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ package fabtoken

import (
"github.com/hyperledger-labs/fabric-token-sdk/token/core/common"
"github.com/hyperledger-labs/fabric-token-sdk/token/core/common/interop/htlc"
"github.com/hyperledger-labs/fabric-token-sdk/token/core/common/interop/pledge"
"github.com/hyperledger-labs/fabric-token-sdk/token/core/common/logging"
"github.com/hyperledger-labs/fabric-token-sdk/token/driver"
"github.com/hyperledger-labs/fabric-token-sdk/token/token"
)

type ValidateTransferFunc = common.ValidateTransferFunc[*PublicParams, *token.Token, *TransferAction, *IssueAction]
type ValidateTransferFunc = common.ValidateTransferFunc[*PublicParams, *Output, *TransferAction, *IssueAction]

type ValidateIssueFunc = common.ValidateIssueFunc[*PublicParams, *token.Token, *TransferAction, *IssueAction]
type ValidateIssueFunc = common.ValidateIssueFunc[*PublicParams, *Output, *TransferAction, *IssueAction]

type ActionDeserializer struct{}

Expand All @@ -41,25 +42,25 @@ func (a *ActionDeserializer) DeserializeActions(tr *driver.TokenRequest) ([]*Iss
return issueActions, transferActions, nil
}

type Context = common.Context[*PublicParams, *token.Token, *TransferAction, *IssueAction]
type Context = common.Context[*PublicParams, *Output, *TransferAction, *IssueAction]

type Validator = common.Validator[*PublicParams, *token.Token, *TransferAction, *IssueAction]
type Validator = common.Validator[*PublicParams, *Output, *TransferAction, *IssueAction]

func NewValidator(logger logging.Logger, pp *PublicParams, deserializer driver.Deserializer, extraValidators ...ValidateTransferFunc) *Validator {
transferValidators := []ValidateTransferFunc{
TransferSignatureValidate,
TransferBalanceValidate,
common.TransferHTLCValidate[*PublicParams, *token.Token, *TransferAction, *IssueAction],
common.TransferPledgeValidate[*PublicParams, *token.Token, *TransferAction, *IssueAction],
htlc.TransferHTLCValidate[*PublicParams, *Output, *TransferAction, *IssueAction],
pledge.TransferPledgeValidate[*PublicParams, *Output, *TransferAction, *IssueAction],
}
transferValidators = append(transferValidators, extraValidators...)

issueValidators := []ValidateIssueFunc{
IssueValidate,
common.IssuePledgeValidate[*PublicParams, *token.Token, *TransferAction, *IssueAction],
pledge.IssuePledgeValidate[*PublicParams, *Output, *TransferAction, *IssueAction],
}

return common.NewValidator[*PublicParams, *token.Token, *TransferAction, *IssueAction](
return common.NewValidator[*PublicParams, *Output, *TransferAction, *IssueAction](
logger,
pp,
deserializer,
Expand Down
2 changes: 1 addition & 1 deletion token/core/fabtoken/validator_issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func IssueValidate(ctx *Context) error {
return errors.Errorf("there is no output")
}
for _, output := range action.GetOutputs() {
out := output.(*Output).Output
out := output.(*Output)
q, err := token.ToQuantity(out.Quantity, ctx.PP.QuantityPrecision)
if err != nil {
return errors.Wrapf(err, "failed parsing quantity [%s]", out.Quantity)
Expand Down
8 changes: 4 additions & 4 deletions token/core/fabtoken/validator_transfer.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func TransferBalanceValidate(ctx *Context) error {
}
}
for _, output := range ctx.TransferAction.GetOutputs() {
out := output.(*Output).Output
out := output.(*Output)
q, err := token.ToQuantity(out.Quantity, ctx.PP.QuantityPrecision)
if err != nil {
return errors.Wrapf(err, "failed parsing quantity [%s]", out.Quantity)
Expand All @@ -87,8 +87,8 @@ func TransferBalanceValidate(ctx *Context) error {
}

// RetrieveInputsFromTransferAction retrieves from the passed ledger the inputs identified in TransferAction
func RetrieveInputsFromTransferAction(t *TransferAction, ledger driver.Ledger) ([]*token.Token, error) {
var inputTokens []*token.Token
func RetrieveInputsFromTransferAction(t *TransferAction, ledger driver.Ledger) ([]*Output, error) {
var inputTokens []*Output
inputs, err := t.GetInputs()
if err != nil {
return nil, errors.Wrap(err, "failed to retrieve input IDs")
Expand All @@ -101,7 +101,7 @@ func RetrieveInputsFromTransferAction(t *TransferAction, ledger driver.Ledger) (
if len(bytes) == 0 {
return nil, errors.Errorf("input to spend [%s] does not exists", in)
}
tok := &token.Token{}
tok := &Output{}
err = json.Unmarshal(bytes, tok)
if err != nil {
return nil, errors.Wrapf(err, "failed to deserialize input to spend [%s]", in)
Expand Down
8 changes: 5 additions & 3 deletions token/core/zkatdlog/crypto/validator/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ package validator

import (
"github.com/hyperledger-labs/fabric-token-sdk/token/core/common"
"github.com/hyperledger-labs/fabric-token-sdk/token/core/common/interop/htlc"
"github.com/hyperledger-labs/fabric-token-sdk/token/core/common/interop/pledge"
"github.com/hyperledger-labs/fabric-token-sdk/token/core/common/logging"
"github.com/hyperledger-labs/fabric-token-sdk/token/core/zkatdlog/crypto"
"github.com/hyperledger-labs/fabric-token-sdk/token/core/zkatdlog/crypto/issue"
Expand Down Expand Up @@ -52,14 +54,14 @@ func New(logger logging.Logger, pp *crypto.PublicParams, deserializer driver.Des
transferValidators := []ValidateTransferFunc{
TransferSignatureValidate,
TransferZKProofValidate,
common.TransferHTLCValidate[*crypto.PublicParams, *token.Token, *transfer.Action, *issue.IssueAction],
common.TransferPledgeValidate[*crypto.PublicParams, *token.Token, *transfer.Action, *issue.IssueAction],
htlc.TransferHTLCValidate[*crypto.PublicParams, *token.Token, *transfer.Action, *issue.IssueAction],
pledge.TransferPledgeValidate[*crypto.PublicParams, *token.Token, *transfer.Action, *issue.IssueAction],
}
transferValidators = append(transferValidators, extraValidators...)

issueValidators := []ValidateIssueFunc{
IssueValidate,
common.IssuePledgeValidate[*crypto.PublicParams, *token.Token, *transfer.Action, *issue.IssueAction],
pledge.IssuePledgeValidate[*crypto.PublicParams, *token.Token, *transfer.Action, *issue.IssueAction],
}

return common.NewValidator[*crypto.PublicParams, *token.Token, *transfer.Action, *issue.IssueAction](
Expand Down
Loading

0 comments on commit 4305ba0

Please sign in to comment.