2020-03-06 12:33:47 +03:00
|
|
|
// Copyright 2020 The Grin Developers
|
2019-02-13 18:05:19 +03:00
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
|
|
|
//! This module contains old slate versions and conversions to the newest slate version
|
|
|
|
//! Used for serialization and deserialization of slates in a backwards compatible way.
|
2019-06-27 12:41:05 +03:00
|
|
|
//! Versions earlier than V2 are removed for the 2.0.0 release, but versioning code
|
|
|
|
//! remains for future needs
|
|
|
|
|
2019-04-23 13:29:59 +03:00
|
|
|
use crate::slate::Slate;
|
2019-11-20 14:01:38 +03:00
|
|
|
use crate::slate_versions::v3::{CoinbaseV3, SlateV3};
|
2020-03-06 12:33:47 +03:00
|
|
|
use crate::slate_versions::v4::{CoinbaseV4, SlateV4};
|
2020-05-19 13:19:03 +03:00
|
|
|
use crate::slate_versions::v4_bin::SlateV4Bin;
|
2019-08-20 12:05:21 +03:00
|
|
|
use crate::types::CbData;
|
2020-05-19 13:19:03 +03:00
|
|
|
use crate::{Error, ErrorKind};
|
2020-03-10 21:19:27 +03:00
|
|
|
use std::convert::TryFrom;
|
2019-04-23 13:29:59 +03:00
|
|
|
|
2019-11-20 14:01:38 +03:00
|
|
|
pub mod ser;
|
|
|
|
|
2019-03-12 19:48:14 +03:00
|
|
|
#[allow(missing_docs)]
|
2019-11-20 14:01:38 +03:00
|
|
|
pub mod v3;
|
2020-03-06 12:33:47 +03:00
|
|
|
#[allow(missing_docs)]
|
|
|
|
pub mod v4;
|
2020-05-19 13:19:03 +03:00
|
|
|
#[allow(missing_docs)]
|
|
|
|
pub mod v4_bin;
|
2019-04-23 13:29:59 +03:00
|
|
|
|
|
|
|
/// The most recent version of the slate
|
2020-03-06 12:33:47 +03:00
|
|
|
pub const CURRENT_SLATE_VERSION: u16 = 4;
|
2019-04-23 13:29:59 +03:00
|
|
|
|
2019-05-31 10:33:23 +03:00
|
|
|
/// The grin block header this slate is intended to be compatible with
|
2019-12-02 16:54:57 +03:00
|
|
|
pub const GRIN_BLOCK_HEADER_VERSION: u16 = 3;
|
2019-05-31 10:33:23 +03:00
|
|
|
|
2019-04-23 13:29:59 +03:00
|
|
|
/// Existing versions of the slate
|
2019-05-30 11:34:04 +03:00
|
|
|
#[derive(EnumIter, Serialize, Deserialize, Clone, Debug, PartialEq, PartialOrd, Eq, Ord)]
|
2019-04-23 13:29:59 +03:00
|
|
|
pub enum SlateVersion {
|
2020-03-06 12:33:47 +03:00
|
|
|
/// V4 (most current)
|
|
|
|
V4,
|
|
|
|
/// V3 (3.0.0 - 4.0.0)
|
2019-11-20 14:01:38 +03:00
|
|
|
V3,
|
2019-04-23 13:29:59 +03:00
|
|
|
}
|
|
|
|
|
2019-09-02 18:03:35 +03:00
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
2019-04-23 13:29:59 +03:00
|
|
|
#[serde(untagged)]
|
|
|
|
/// Versions are ordered newest to oldest so serde attempts to
|
|
|
|
/// deserialize newer versions first, then falls back to older versions.
|
|
|
|
pub enum VersionedSlate {
|
2020-03-06 12:33:47 +03:00
|
|
|
/// Current (4.0.0 Onwards )
|
|
|
|
V4(SlateV4),
|
|
|
|
/// V2 (2.0.0 - 3.0.0)
|
2019-11-20 14:01:38 +03:00
|
|
|
V3(SlateV3),
|
2019-04-23 13:29:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
impl VersionedSlate {
|
|
|
|
/// Return slate version
|
|
|
|
pub fn version(&self) -> SlateVersion {
|
|
|
|
match *self {
|
2020-03-06 12:33:47 +03:00
|
|
|
VersionedSlate::V4(_) => SlateVersion::V4,
|
2019-11-20 14:01:38 +03:00
|
|
|
VersionedSlate::V3(_) => SlateVersion::V3,
|
2019-04-23 13:29:59 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// convert this slate type to a specified older version
|
2020-03-10 21:19:27 +03:00
|
|
|
pub fn into_version(slate: Slate, version: SlateVersion) -> Result<VersionedSlate, Error> {
|
2019-04-23 13:29:59 +03:00
|
|
|
match version {
|
2020-03-10 21:19:27 +03:00
|
|
|
SlateVersion::V4 => Ok(VersionedSlate::V4(slate.into())),
|
2020-03-06 12:33:47 +03:00
|
|
|
SlateVersion::V3 => {
|
|
|
|
let s = SlateV4::from(slate);
|
2020-03-10 21:19:27 +03:00
|
|
|
let s = SlateV3::try_from(&s)?;
|
|
|
|
Ok(VersionedSlate::V3(s))
|
2019-12-04 14:52:42 +03:00
|
|
|
}
|
2019-04-23 13:29:59 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<VersionedSlate> for Slate {
|
|
|
|
fn from(slate: VersionedSlate) -> Slate {
|
|
|
|
match slate {
|
2020-03-06 12:33:47 +03:00
|
|
|
VersionedSlate::V4(s) => Slate::from(s),
|
|
|
|
VersionedSlate::V3(s) => {
|
|
|
|
let s = SlateV4::from(s);
|
2019-12-04 14:52:42 +03:00
|
|
|
Slate::from(s)
|
|
|
|
}
|
2019-04-23 13:29:59 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-08-20 12:05:21 +03:00
|
|
|
|
2020-05-19 13:19:03 +03:00
|
|
|
#[derive(Deserialize, Serialize)]
|
|
|
|
#[serde(untagged)]
|
|
|
|
/// Binary versions, can only be parsed 1:1 into the appropriate
|
|
|
|
/// version, and VersionedSlate can up/downgrade from there
|
|
|
|
pub enum VersionedBinSlate {
|
|
|
|
/// Version 4, binary
|
|
|
|
V4(SlateV4Bin),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TryFrom<VersionedSlate> for VersionedBinSlate {
|
|
|
|
type Error = Error;
|
|
|
|
fn try_from(slate: VersionedSlate) -> Result<VersionedBinSlate, Error> {
|
|
|
|
match slate {
|
|
|
|
VersionedSlate::V4(s) => Ok(VersionedBinSlate::V4(SlateV4Bin(s))),
|
|
|
|
VersionedSlate::V3(_) => {
|
|
|
|
return Err(
|
|
|
|
ErrorKind::Compatibility("V3 Slate does not support binary".to_owned()).into(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<VersionedBinSlate> for VersionedSlate {
|
|
|
|
fn from(slate: VersionedBinSlate) -> VersionedSlate {
|
|
|
|
match slate {
|
|
|
|
VersionedBinSlate::V4(s) => VersionedSlate::V4(s.0),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-20 12:05:21 +03:00
|
|
|
#[derive(Deserialize, Serialize)]
|
|
|
|
#[serde(untagged)]
|
|
|
|
/// Versions are ordered newest to oldest so serde attempts to
|
|
|
|
/// deserialize newer versions first, then falls back to older versions.
|
|
|
|
pub enum VersionedCoinbase {
|
|
|
|
/// Current supported coinbase version.
|
2020-03-06 12:33:47 +03:00
|
|
|
V4(CoinbaseV4),
|
|
|
|
/// Previous supported coinbase version.
|
2019-11-20 14:01:38 +03:00
|
|
|
V3(CoinbaseV3),
|
2019-08-20 12:05:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
impl VersionedCoinbase {
|
|
|
|
/// convert this coinbase data to a specific versioned representation for the json api.
|
|
|
|
pub fn into_version(cb: CbData, version: SlateVersion) -> VersionedCoinbase {
|
|
|
|
match version {
|
2020-03-06 12:33:47 +03:00
|
|
|
SlateVersion::V4 => VersionedCoinbase::V4(cb.into()),
|
2019-11-20 14:01:38 +03:00
|
|
|
SlateVersion::V3 => VersionedCoinbase::V3(cb.into()),
|
2019-08-20 12:05:21 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|