1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
//! This is a planned API for v0.5.0 that will help structuring RGB validation
//! into a more formal process
/*
/// This simple trait MUST be used by all parties implementing client-side
/// validation paradigm. The core concept of this paradigm is that a client
/// must have a complete and uniform set of data, which can be represented
/// or accessed through a single structure; and MUST be able to deterministically
/// validate this set giving an external validation function, that is able to
/// provide validator with
pub trait ClientSideValidate<TR> where TR: TrustResolver {
type ClientData: ClientData;
type ValidationError: FromTrustProblem<TR> + FromInternalInconsistency<TR>;
fn new() -> Self;
fn client_side_validate(client_data: Self::ClientData, trust_resolver: TR) -> Result<(), Self::ValidationError> {
let validator = Self::new();
client_data.validate_internal_consistency()?;
client_data.validation_iter().try_for_each(|item| {
trust_resolver.resolve_trust(item, validator.get_context_for_atom(item))?;
item.client_side_validate()
})
}
fn get_context_for_item<C: TrustContext>(&self, data_item: Self::ClientData::ValidationItem) -> C;
}
pub trait ClientData {
type ValidationItem: ClientData;
}
pub trait TrustContext {
}
/// Trust resolver for a given client data type MUST work with a single type
/// of `TrustContext`, defined by an associated type. Trust resolution MUST
/// always produce a singular success type (defined by `()`) or fail with a
/// well-defined type of `TrustProblem`.
///
/// Trust resolved may have an internal state (represented by `self` reference)
/// and it does not require to produce a deterministic result for the same
/// given data piece and context: the trust resolver may depend on previous
/// operation history and depend on type and other external parameters.
pub trait TrustResolver<T: ClientData> {
type TrustProblem: std::error::Error;
type Context: TrustContext;
fn resolve_trust(&self, data_piece: &T, context: &Self::Context) -> Result<(), Self::TrustProblem>;
}
mod test {
struct BlockchainValidator;
impl ClientSideValidate for BlockchainValidator {
type ClientData = Blockchain;
fn new() -> Self { Self }
fn get_context_for_item(&self, data_item: Block) -> Difficulty { }
}
fn test() {
}
}
*/