Module: Dcha::Peer::HasTrie

Included in:
Dcha::Peer
Defined in:
lib/dcha/peer/has_trie.rb

Overview

:nodoc:

Instance Method Summary collapse

Instance Method Details

#read(key) ⇒ Object



42
43
44
45
46
47
48
49
50
51
# File 'lib/dcha/peer/has_trie.rb', line 42

def read(key)
  trie[key]
rescue Store::DataUnavailableError => e
  transmit action: :store_get, params: [
    trie.root_hash,
    e.message,
    ipaddr.ip_address
  ]
  '[SYNCING]'
end

#reset(root_hash) ⇒ Object



10
11
12
13
14
15
16
17
18
19
# File 'lib/dcha/peer/has_trie.rb', line 10

def reset(root_hash)
  return if root_hash == trie.root_hash
  @trie = MPT::Trie.new(root_hash)
rescue Store::DataUnavailableError => e
  transmit action: :store_get, params: [
    root_hash,
    e.message,
    ipaddr.ip_address
  ]
end

#store_get(root_hash, key, _address) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/dcha/peer/has_trie.rb', line 21

def store_get(root_hash, key, _address)
  return if root_hash != trie.root_hash
  changed
  transmit action: :store_set, params: [
    root_hash,
    key,
    Config.store[key]
  ]
rescue Store::DataUnavailableError
  nil
end

#store_set(root_hash, key, value) ⇒ Object



33
34
35
36
# File 'lib/dcha/peer/has_trie.rb', line 33

def store_set(root_hash, key, value)
  Config.store[key] = value
  reset(root_hash) if root_hash != trie.root_hash
end

#trieObject



5
6
7
8
# File 'lib/dcha/peer/has_trie.rb', line 5

def trie
  @retry = false
  @trie ||= MPT::Trie.new
end

#write(key, value) ⇒ Object



38
39
40
# File 'lib/dcha/peer/has_trie.rb', line 38

def write(key, value)
  trie[key] = value
end