Skip to content

Commit

Permalink
Add metadata typing for libra transactions
Browse files Browse the repository at this point in the history
Signed-off-by: Nassim Eddequiouaq <nass@fb.com>
  • Loading branch information
Nassim Eddequiouaq committed Jul 13, 2020
1 parent d703cb0 commit 21a3c06
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 0 deletions.
4 changes: 4 additions & 0 deletions testsuite/generate-format/src/libra.rs
Expand Up @@ -55,6 +55,10 @@ pub fn get_registry() -> Result<Registry> {
// 2. Trace the main entry point(s) + every enum separately.
tracer.trace_type::<contract_event::ContractEvent>(&samples)?;
tracer.trace_type::<language_storage::TypeTag>(&samples)?;
tracer.trace_type::<transaction::MetadataType>(&samples)?;
tracer.trace_type::<transaction::GeneralMetadataV0>(&samples)?;
tracer.trace_type::<transaction::TravelRuleMetadataV0>(&samples)?;
tracer.trace_type::<transaction::UnstructuredStringMetadata>(&samples)?;
tracer.trace_type::<transaction::Transaction>(&samples)?;
tracer.trace_type::<transaction::TransactionArgument>(&samples)?;
tracer.trace_type::<transaction::TransactionPayload>(&samples)?;
Expand Down
44 changes: 44 additions & 0 deletions testsuite/generate-format/tests/staged/libra.yaml
Expand Up @@ -47,10 +47,40 @@ Ed25519Signature:
NEWTYPESTRUCT: BYTES
EventKey:
NEWTYPESTRUCT: BYTES
GeneralMetadata:
ENUM:
0:
GeneralMetadataVersion0:
NEWTYPE:
TYPENAME: GeneralMetadataV0
GeneralMetadataV0:
STRUCT:
- to_subaddress:
OPTION: BYTES
- from_subaddress:
OPTION: BYTES
- referenced_event:
OPTION: U64
HashValue:
NEWTYPESTRUCT: BYTES
Identifier:
NEWTYPESTRUCT: STR
MetadataType:
ENUM:
0:
Undefined: UNIT
1:
GeneralMetadataType:
NEWTYPE:
TYPENAME: GeneralMetadata
2:
TravelRuleMetadataType:
NEWTYPE:
TYPENAME: TravelRuleMetadata
3:
UnstructuredStringMetadataType:
NEWTYPE:
TYPENAME: UnstructuredStringMetadata
Module:
STRUCT:
- code: BYTES
Expand Down Expand Up @@ -160,6 +190,16 @@ TransactionPayload:
Module:
NEWTYPE:
TYPENAME: Module
TravelRuleMetadata:
ENUM:
0:
TravelRuleMetadataVersion0:
NEWTYPE:
TYPENAME: TravelRuleMetadataV0
TravelRuleMetadataV0:
STRUCT:
- off_chain_reference_id:
OPTION: STR
TypeTag:
ENUM:
0:
Expand All @@ -182,6 +222,10 @@ TypeTag:
Struct:
NEWTYPE:
TYPENAME: StructTag
UnstructuredStringMetadata:
STRUCT:
- metadata:
OPTION: STR
WriteOp:
ENUM:
0:
Expand Down
51 changes: 51 additions & 0 deletions types/src/transaction/metadata.rs
@@ -0,0 +1,51 @@
// Copyright (c) The Libra Core Contributors
// SPDX-License-Identifier: Apache-2.0

use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub enum MetadataType {
Undefined,
GeneralMetadataType(GeneralMetadata),
TravelRuleMetadataType(TravelRuleMetadata),
UnstructuredStringMetadataType(UnstructuredStringMetadata),
}

// Used for versioning of general metadata
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub enum GeneralMetadata {
GeneralMetadataVersion0(GeneralMetadataV0),
}

#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub struct GeneralMetadataV0 {
// Subaddress to which the funds are being sent
#[serde(with = "serde_bytes")]
to_subaddress: Option<Vec<u8>>,

// Subaddress from which the funds are being sent
#[serde(with = "serde_bytes")]
from_subaddress: Option<Vec<u8>>,

// Event sequence number of referenced payment
referenced_event: Option<u64>,
}

// Used for versioning of travel rule metadata
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub enum TravelRuleMetadata {
TravelRuleMetadataVersion0(TravelRuleMetadataV0),
}

#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub struct TravelRuleMetadataV0 {
// Off-chain reference_id. Used when off-chain APIs are used.
// Specifies the off-chain reference ID that was agreed upon in off-chain APIs.
off_chain_reference_id: Option<String>,
}

#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub struct UnstructuredStringMetadata {
// Unstructured string metadata
metadata: Option<String>,
}
2 changes: 2 additions & 0 deletions types/src/transaction/mod.rs
Expand Up @@ -36,11 +36,13 @@ use std::{
pub mod authenticator;
mod change_set;
pub mod helpers;
mod metadata;
mod module;
mod script;
mod transaction_argument;

pub use change_set::ChangeSet;
pub use metadata::*;
pub use module::Module;
pub use script::{ArgumentABI, Script, ScriptABI, TypeArgumentABI, SCRIPT_HASH_LENGTH};

Expand Down

0 comments on commit 21a3c06

Please sign in to comment.