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

runtime: Bump oasis-cbor to 0.3.0 #4784

Merged
merged 1 commit into from
Jun 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .changelog/4784.internal.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
runtime: Bump oasis-cbor to 0.3.0
12 changes: 6 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ edition = "2018"

[dependencies]
oasis-core-runtime = { path = "../runtime" }
cbor = { version = "0.2.1", package = "oasis-cbor" }
cbor = { version = "0.3.0", package = "oasis-cbor" }

# Third party.
anyhow = "1.0"
Expand Down
40 changes: 38 additions & 2 deletions go/common/node/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package node

import (
"encoding/base64"
"fmt"
"net"
"testing"

Expand Down Expand Up @@ -271,6 +270,43 @@ func TestNodeForTestSerialization(t *testing.T) {
var t map[string]interface{}
err = cbor.Unmarshal(enc, &t)
require.NoError(err, "Unamarshal inter")
fmt.Println(t)
}
}

func TestNodeDeserialization(t *testing.T) {
require := require.New(t)

var runtimeID common.Namespace
require.NoError(runtimeID.UnmarshalHex("8000000000000000000000000000000000000000000000000000000000000010"), "runtime id")

// NOTE: These cases should be synced with tests in runtime/src/consensus/registry.rs.
for _, tc := range []struct {
rawBase64 string
expectedNode Node
}{
{
"qmF2AmJpZFggAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABjcDJwomJpZFggAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABpYWRkcmVzc2Vz9mN0bHOjZ3B1Yl9rZXlYIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAaWFkZHJlc3Nlc/ZsbmV4dF9wdWJfa2V5WCAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAGVyb2xlcwBocnVudGltZXP2aWNvbnNlbnN1c6JiaWRYIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAaWFkZHJlc3Nlc/ZpZW50aXR5X2lkWCAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAGpleHBpcmF0aW9uAHBzb2Z0d2FyZV92ZXJzaW9u9g==",
Node{Versioned: cbor.NewVersioned(LatestNodeDescriptorVersion)},
},
{
"qWF2AmJpZFggAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABjcDJwomJpZFggAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABpYWRkcmVzc2Vz9mN0bHOjZ3B1Yl9rZXlYIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAaWFkZHJlc3Nlc/ZsbmV4dF9wdWJfa2V5WCAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAGVyb2xlcwBocnVudGltZXOBomJpZFgggAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABBndmVyc2lvbvZpY29uc2Vuc3VzomJpZFggAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABpYWRkcmVzc2Vz9mllbnRpdHlfaWRYIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAamV4cGlyYXRpb24A",
Node{
Versioned: cbor.NewVersioned(LatestNodeDescriptorVersion),
Runtimes: []*Runtime{
{
ID: runtimeID,
Version: version.FromU64(0),
},
},
},
},
} {
raw, err := base64.StdEncoding.DecodeString(tc.rawBase64)
require.NoError(err, "DecodeString")

var dec Node
err = cbor.Unmarshal(raw, &dec)
require.NoError(err, "Unmarshal")
require.EqualValues(tc.expectedNode, dec, "Node serialization should round-trip")
}
}
13 changes: 13 additions & 0 deletions go/staking/api/address_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,22 @@ import (
"github.com/stretchr/testify/require"

"github.com/oasisprotocol/oasis-core/go/common"
"github.com/oasisprotocol/oasis-core/go/common/cbor"
"github.com/oasisprotocol/oasis-core/go/common/crypto/signature"
)

func TestAddressDeserialization(t *testing.T) {
require := require.New(t)

var addr Address
err := cbor.Unmarshal([]byte{0xF6}, &addr)
require.NoError(err, "cbor.Unmarshal")
require.EqualValues("oasis1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq0ltrq9", addr.String())

raw, _ := addr.MarshalBinary()
require.EqualValues([]byte{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, raw)
}

func TestReserved(t *testing.T) {
require := require.New(t)

Expand Down
2 changes: 1 addition & 1 deletion keymanager-api-common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ edition = "2018"

[dependencies]
oasis-core-runtime = { path = "../runtime" }
cbor = { version = "0.2.1", package = "oasis-cbor" }
cbor = { version = "0.3.0", package = "oasis-cbor" }

base64 = "0.13.0"
rustc-hex = "2.0.1"
Expand Down
24 changes: 12 additions & 12 deletions keymanager-api-common/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ impl AsRef<[u8]> for MasterSecret {
}

/// Key manager initialization request.
#[derive(Clone, cbor::Encode, cbor::Decode)]
#[derive(Clone, Default, cbor::Encode, cbor::Decode)]
pub struct InitRequest {
/// Checksum for validating replication.
pub checksum: Vec<u8>,
Expand All @@ -62,7 +62,7 @@ pub struct InitRequest {
}

/// Key manager initialization response.
#[derive(Clone, cbor::Encode, cbor::Decode)]
#[derive(Clone, Default, cbor::Encode, cbor::Decode)]
pub struct InitResponse {
/// True iff the key manager thinks it's running in a secure mode.
pub is_secure: bool,
Expand All @@ -76,7 +76,7 @@ pub struct InitResponse {
pub const INIT_RESPONSE_CONTEXT: &[u8] = b"oasis-core/keymanager: init response";

/// Signed InitResponse.
#[derive(Clone, cbor::Encode, cbor::Decode)]
#[derive(Clone, Default, cbor::Encode, cbor::Decode)]
pub struct SignedInitResponse {
/// InitResponse.
pub init_response: InitResponse,
Expand All @@ -85,19 +85,19 @@ pub struct SignedInitResponse {
}

/// Key manager replication request.
#[derive(Clone, cbor::Encode, cbor::Decode)]
#[derive(Clone, Default, cbor::Encode, cbor::Decode)]
pub struct ReplicateRequest {
// Empty.
}

/// Key manager replication response.
#[derive(Clone, cbor::Encode, cbor::Decode)]
#[derive(Clone, Default, cbor::Encode, cbor::Decode)]
pub struct ReplicateResponse {
pub master_secret: MasterSecret,
}

/// Request runtime/key pair id tuple.
#[derive(Clone, cbor::Encode, cbor::Decode)]
#[derive(Clone, Default, cbor::Encode, cbor::Decode)]
pub struct RequestIds {
/// Runtime ID.
pub runtime_id: Namespace,
Expand All @@ -121,7 +121,7 @@ impl RequestIds {
}

/// A key pair managed by the key manager.
#[derive(Clone, cbor::Encode, cbor::Decode)]
#[derive(Clone, Default, cbor::Encode, cbor::Decode)]
pub struct KeyPair {
/// Input key pair (pk, sk)
pub input_keypair: InputKeyPair,
Expand Down Expand Up @@ -171,7 +171,7 @@ impl KeyPair {
}
}

#[derive(Clone, cbor::Encode, cbor::Decode)]
#[derive(Clone, Default, cbor::Encode, cbor::Decode)]
pub struct InputKeyPair {
/// Public key.
pub pk: PublicKey,
Expand All @@ -183,7 +183,7 @@ pub struct InputKeyPair {
pub const PUBLIC_KEY_CONTEXT: [u8; 8] = *b"EkKmPubK";

/// Signed public key.
#[derive(Clone, Debug, PartialEq, Eq, cbor::Encode, cbor::Decode)]
#[derive(Clone, Debug, Default, PartialEq, Eq, cbor::Encode, cbor::Decode)]
pub struct SignedPublicKey {
/// Public key.
pub key: PublicKey,
Expand Down Expand Up @@ -221,22 +221,22 @@ pub enum KeyManagerError {
}

/// Key manager access control policy.
#[derive(Clone, Debug, cbor::Encode, cbor::Decode)]
#[derive(Clone, Debug, Default, cbor::Encode, cbor::Decode)]
pub struct PolicySGX {
pub serial: u32,
pub id: Namespace,
pub enclaves: HashMap<EnclaveIdentity, EnclavePolicySGX>,
}

/// Per enclave key manager access control policy.
#[derive(Clone, Debug, cbor::Encode, cbor::Decode)]
#[derive(Clone, Debug, Default, cbor::Encode, cbor::Decode)]
pub struct EnclavePolicySGX {
pub may_query: HashMap<Namespace, Vec<EnclaveIdentity>>,
pub may_replicate: Vec<EnclaveIdentity>,
}

/// Signed key manager access control policy.
#[derive(Clone, Debug, cbor::Encode, cbor::Decode)]
#[derive(Clone, Debug, Default, cbor::Encode, cbor::Decode)]
pub struct SignedPolicySGX {
pub policy: PolicySGX,
pub signatures: Vec<SignatureBundle>,
Expand Down
2 changes: 1 addition & 1 deletion keymanager-client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ edition = "2018"
oasis-core-client = { path = "../client" }
oasis-core-runtime = { path = "../runtime" }
oasis-core-keymanager-api-common = { path = "../keymanager-api-common" }
cbor = { version = "0.2.1", package = "oasis-cbor" }
cbor = { version = "0.3.0", package = "oasis-cbor" }

# Third party.
futures = "0.3.17"
Expand Down
2 changes: 1 addition & 1 deletion keymanager-lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ edition = "2018"
oasis-core-runtime = { path = "../runtime" }
oasis-core-keymanager-api-common = { path = "../keymanager-api-common" }
oasis-core-keymanager-client = { path = "../keymanager-client" }
cbor = { version = "0.2.1", package = "oasis-cbor" }
cbor = { version = "0.3.0", package = "oasis-cbor" }

# Third party.
anyhow = "1.0"
Expand Down
2 changes: 1 addition & 1 deletion runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ authors = ["Oasis Protocol Foundation <[email protected]>"]
edition = "2018"

[dependencies]
cbor = { version = "0.2.1", package = "oasis-cbor" }
cbor = { version = "0.3.0", package = "oasis-cbor" }

# Third party.
log = "0.4"
Expand Down
13 changes: 13 additions & 0 deletions runtime/src/common/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,10 @@ macro_rules! impl_bytes {
// Deserialization.

impl $crate::cbor::Decode for $name {
fn try_default() -> Result<Self, $crate::cbor::DecodeError> {
Ok(Default::default())
}

fn try_from_cbor_value(
value: $crate::cbor::Value,
) -> Result<Self, $crate::cbor::DecodeError> {
Expand Down Expand Up @@ -201,4 +205,13 @@ mod tests {
let new_test_key: TestKey = cbor::from_slice(&test_key_vec).unwrap();
assert_eq!(new_test_key, test_key);
}

#[test]
fn test_cbor_null() {
let test_key: TestKey = cbor::from_slice(&[0xF6]).unwrap();
assert_eq!(
test_key,
"0000000000000000000000000000000000000000000000000000000000000000".into()
);
}
}
8 changes: 8 additions & 0 deletions runtime/src/common/quantity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,10 @@ impl fmt::Display for Quantity {
}

impl cbor::Encode for Quantity {
fn is_empty(&self) -> bool {
self.0.is_zero()
}

fn into_cbor_value(self) -> cbor::Value {
if self.0.is_zero() {
cbor::Value::ByteString(vec![])
Expand All @@ -211,6 +215,10 @@ impl cbor::Encode for Quantity {
}

impl cbor::Decode for Quantity {
fn try_default() -> Result<Self, cbor::DecodeError> {
Ok(Default::default())
}

fn try_from_cbor_value(value: cbor::Value) -> Result<Self, cbor::DecodeError> {
match value {
cbor::Value::ByteString(data) => Ok(Quantity(BigUint::from_bytes_be(&data))),
Expand Down
11 changes: 2 additions & 9 deletions runtime/src/common/sgx/avr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ impl QuoteBody {
}

/// Attestation verification report.
#[derive(Debug, Clone, cbor::Encode, cbor::Decode)]
#[derive(Debug, Default, Clone, cbor::Encode, cbor::Decode)]
pub struct AVR {
pub body: Vec<u8>,
pub signature: Vec<u8>,
Expand Down Expand Up @@ -481,20 +481,13 @@ pub(crate) fn timestamp_is_fresh(now: i64, timestamp: i64) -> bool {
}

/// Enclave identity.
#[derive(Debug, Clone, Hash, Eq, PartialEq, cbor::Encode, cbor::Decode)]
#[derive(Debug, Default, Clone, Hash, Eq, PartialEq, cbor::Encode, cbor::Decode)]
pub struct EnclaveIdentity {
pub mr_enclave: MrEnclave,
pub mr_signer: MrSigner,
}

impl EnclaveIdentity {
pub fn default() -> Self {
Self {
mr_enclave: MrEnclave::default(),
mr_signer: MrSigner::default(),
}
}

pub fn current() -> Option<Self> {
#[cfg(target_env = "sgx")]
{
Expand Down
6 changes: 0 additions & 6 deletions runtime/src/common/version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,12 @@
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, cbor::Encode, cbor::Decode)]
pub struct Version {
#[cbor(optional)]
#[cbor(default)]
#[cbor(skip_serializing_if = "num_traits::Zero::is_zero")]
pub major: u16,

#[cbor(optional)]
#[cbor(default)]
#[cbor(skip_serializing_if = "num_traits::Zero::is_zero")]
pub minor: u16,

#[cbor(optional)]
#[cbor(default)]
#[cbor(skip_serializing_if = "num_traits::Zero::is_zero")]
pub patch: u16,
}

Expand Down
13 changes: 13 additions & 0 deletions runtime/src/consensus/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,10 @@ impl cbor::Encode for Address {
}

impl cbor::Decode for Address {
fn try_default() -> Result<Self, cbor::DecodeError> {
Ok(Default::default())
}

fn try_from_cbor_value(value: cbor::Value) -> Result<Self, cbor::DecodeError> {
match value {
cbor::Value::ByteString(data) => Ok(Address(
Expand Down Expand Up @@ -196,4 +200,13 @@ mod test {
"oasis1qpllh99nhwzrd56px4txvl26atzgg4f3a58jzzad"
);
}

#[test]
fn test_deserialization() {
let addr: Address = cbor::from_slice(&[0xF6]).unwrap();
assert_eq!(
addr.to_bech32(),
"oasis1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq0ltrq9"
);
}
}
2 changes: 1 addition & 1 deletion runtime/src/consensus/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub mod verifier;
pub const HEIGHT_LATEST: u64 = 0;

/// Light consensus block.
#[derive(Clone, Debug, cbor::Encode, cbor::Decode)]
#[derive(Clone, Default, Debug, cbor::Encode, cbor::Decode)]
pub struct LightBlock {
pub height: u64,
pub meta: Vec<u8>,
Expand Down
Loading