Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

identity deserialization #628

Merged
merged 8 commits into from
May 21, 2024
Merged

identity deserialization #628

merged 8 commits into from
May 21, 2024

Conversation

adecaro
Copy link
Contributor

@adecaro adecaro commented May 10, 2024

This PR does the following:

  • It introduces an alias of view.Identity from the smart client in the Token and Driver API. In future PR, the alias will be used everywhere and then the dependency to the smart client removed.
  • It refactors the identity deserialization infrastructure to be more customizable. The identity type tells which deserializer to use in all circumstances.

Side node: This PR leaves a TODO to be addressed in another PR. The auditor of the zkatdlog driver needs to be updated to better handle the htlc identities and their relative audit information.

Signed-off-by: Angelo De Caro <adc@zurich.ibm.com>
Signed-off-by: Angelo De Caro <adc@zurich.ibm.com>
Signed-off-by: Angelo De Caro <adc@zurich.ibm.com>
Signed-off-by: Angelo De Caro <adc@zurich.ibm.com>
Signed-off-by: Angelo De Caro <adc@zurich.ibm.com>
Signed-off-by: Angelo De Caro <adc@zurich.ibm.com>
@adecaro adecaro changed the title F identity refactor identity deserialization May 10, 2024
Signed-off-by: Angelo De Caro <adc@zurich.ibm.com>
@adecaro adecaro self-assigned this May 10, 2024
@@ -94,7 +93,7 @@ func (v *Validator[P, T, TA, IA]) VerifyTokenRequestFromRaw(getState driver.GetS
return nil, nil, errors.Wrap(err, "failed to marshal signed token request")
}

v.Logger.Debugf("cc tx-id [%s][%s]", hash.Hashable(raqRaw).String(), anchor)
v.Logger.Debugf("cc tx-id [%s][%s]", Hashable(raqRaw).String(), anchor)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoid to call String() in debug logs. It is called lazily

@@ -125,14 +136,15 @@ func (s *TransferService) Transfer(txID string, wallet driver.OwnerWallet, ids [
}

metadata := &driver.TransferMetadata{
Outputs: outputs,
Copy link
Contributor

@alexandrosfilios alexandrosfilios May 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would give more clarity to have a struct:

type OutToken struct {
 TokenID
 Sender
 Output
 Metadata
 AuditInfo
 Receiver
 ReceiverAuditInfo
 ReceiverIsSender
}

Then the transferMetadata would contain a []OutToken slice.

This way it's not necessary to read the code to figure out which slice elements are related, and we don't need to check that the lengths are correct.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

}

type TypedVerifierDeserializerMultiplex struct {
deserializers map[string]TypedVerifierDeserializer
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could use an alias to link this value to the Type property and even provide constants for it

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

func (a *AuditDeserializer) DeserializeAuditInfo(bytes []byte) (deserializer.AuditInfo, error) {
si := &ScriptInfo{}
err := json.Unmarshal(bytes, si)
if err == nil && (len(si.Sender) != 0 || len(si.Recipient) != 0) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could inverse the condition to reduce nesting.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

}
return nil, errors.Errorf("no recipient defined")
}
return nil, errors.Errorf("ivalid audit info, failed unmarshal [%s]", string(bytes))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it makes sense to include also the length of sender/recipient for better debugging.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

return nil, errors.Errorf("failed signer deserialization [%v]", errs)
}

func (d *multipler) Info(raw []byte, auditInfo []byte) (string, error) {
Copy link
Contributor

@alexandrosfilios alexandrosfilios May 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To avoid duplication we could have a method:

func deserialize[V](extractor func(driver.Deserializer) (V, err)) {
	var errs []error

	copyDeserial := d.threadSafeCopyDeserializers()
	for _, des := range copyDeserial {
		if logger.IsEnabledFor(zapcore.DebugLevel) {
			logger.Debugf("trying signer deserialization with [%s]", des)
		}
		v, err := extractor()
		if err == nil {
			if logger.IsEnabledFor(zapcore.DebugLevel) {
				logger.Debugf("trying signer deserialization with [%s] succeeded", des)
			}
			return v, nil
		}

		if logger.IsEnabledFor(zapcore.DebugLevel) {
			logger.Debugf("trying signer deserialization with [%s] failed [%s]", des, err)
		}
		errs = append(errs, err)
	}

	return nil, errors.Errorf("failed signer deserialization [%v]", errs)
}

Then your three methods are almost the same:

func (d *multipler) DeserializeSigner(raw []byte) (driver.Signer, error) {
 return deserialize(d.threadSafeCopyDeserializers(), func(des driver.Deserializer) (driver.Signer, error) {return des.DeserializeSigner(raw)})
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

beautiful 👍

Copy link
Contributor

@alexandrosfilios alexandrosfilios left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM modulo comments

Signed-off-by: Angelo De Caro <adc@zurich.ibm.com>
@adecaro adecaro merged commit fd5f50a into main May 21, 2024
38 checks passed
@adecaro adecaro deleted the f-identity-refactor branch May 21, 2024 06:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants