Class: Immudb::Store

Inherits:
Object
  • Object
show all
Defined in:
lib/immudb/store.rb

Class Method Summary collapse

Class Method Details

.digest_from(sliced_digest) ⇒ Object



66
67
68
# File 'lib/immudb/store.rb', line 66

def digest_from(sliced_digest)
  sliced_digest[0, 32]
end

.digests_from(sliced_terms) ⇒ Object



70
71
72
# File 'lib/immudb/store.rb', line 70

def digests_from(sliced_terms)
  sliced_terms.map(&:dup)
end

.encode_key(key) ⇒ Object



49
50
51
# File 'lib/immudb/store.rb', line 49

def encode_key(key)
  SET_KEY_PREFIX + key
end

.encode_kv(key, value) ⇒ Object



53
54
55
# File 'lib/immudb/store.rb', line 53

def encode_kv(key, value)
  KV.new(SET_KEY_PREFIX + key, PLAIN_VALUE_PREFIX + value)
end

.encode_reference(key, referencedKey, atTx) ⇒ Object



57
58
59
60
# File 'lib/immudb/store.rb', line 57

def encode_reference(key, referencedKey, atTx)
  refVal = REFERENCE_VALUE_PREFIX + [atTx].pack("Q>") + SET_KEY_PREFIX + referencedKey
  KV.new(SET_KEY_PREFIX + key, refVal)
end

.linear_proof_from(lp) ⇒ Object



62
63
64
# File 'lib/immudb/store.rb', line 62

def linear_proof_from(lp)
  LinearProof.new(lp.sourceTxId, lp.TargetTxId, lp.terms)
end

.tx_from(stx) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/immudb/store.rb', line 16

def tx_from(stx)
  entries = []
  stx.entries.each do |e|
    i = TXe.new
    i.h_value = digest_from(e.hValue)
    i.v_off = e.vOff
    i.value_len = e.vLen.to_i
    i.set_key(e.key)
    entries << i
  end
  tx = new_tx_with_entries(entries)
  tx.ID = stx..id
  tx.PrevAlh = digest_from(stx..prevAlh)
  tx.Ts = stx..ts
  tx.BlTxID = stx..blTxId
  tx.BlRoot = digest_from(stx..blRoot)
  tx.build_hash_tree
  tx.calc_alh
  tx
end

.tx_metadata_from(txmFrom) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/immudb/store.rb', line 37

def (txmFrom)
  txm = TxMetadata.new
  txm.iD = txmFrom.id
  txm.prevAlh = digest_from(txmFrom.prevAlh)
  txm.ts = txmFrom.ts
  txm.nEntries = txmFrom.nentries.to_i
  txm.eh = digest_from(txmFrom.eH)
  txm.blTxID = txmFrom.blTxId
  txm.blRoot = digest_from(txmFrom.blRoot)
  txm
end

.verify_dual_proof(proof, sourceTxID, targetTxID, sourceAlh, targetAlh) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/immudb/store.rb', line 94

def verify_dual_proof(proof, sourceTxID, targetTxID, sourceAlh, targetAlh)
  if proof.nil? || proof..nil? || proof..nil? || proof..iD != sourceTxID || proof..iD != targetTxID
    return false
  end
  if proof..iD == 0 || proof..iD > proof..iD
    return false
  end
  if sourceAlh != proof..alh
    return false
  end
  if targetAlh != proof..alh
    return false
  end
  if sourceTxID < proof..blTxID && !verify_inclusion_aht(proof.inclusionProof, sourceTxID, proof..blTxID, leaf_for(sourceAlh), proof..blRoot)
    return false
  end
  if proof..blTxID > 0 && !verify_consistency(proof.consistencyProof, proof..blTxID, proof..blTxID, proof..blRoot, proof..blRoot)
    return false
  end
  if proof..blTxID > 0 && !verify_last_inclusion(proof.lastInclusionProof, proof..blTxID, leaf_for(proof.targetBlTxAlh), proof..blRoot)
    return false
  end
  if sourceTxID < proof..blTxID
    verify_linear_proof(proof.linearProof, proof..blTxID, targetTxID, proof.targetBlTxAlh, targetAlh)
  else
    verify_linear_proof(proof.linearProof, sourceTxID, targetTxID, sourceAlh, targetAlh)
  end
end

.verify_inclusion(proof, digest, root) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/immudb/store.rb', line 74

def verify_inclusion(proof, digest, root)
  return false if proof.nil?
  leaf = LEAF_PREFIX + digest
  calc_root = Digest::SHA256.digest(leaf)
  i = proof.leaf
  r = proof.width - 1
  proof.terms.to_a.each do |t|
    b = NODE_PREFIX
    if i % 2 == 0 && i != r
      b = b + calc_root + t
    else
      b = b + t + calc_root
    end
    calc_root = Digest::SHA256.digest(b)
    i = i.div(2)
    r = r.div(2)
  end
  i == r && root == calc_root
end