Sha256: 9e446156cdfe10936634b02e54f65807743ea604c2ce494f711fce13ace3065c

Contents?: true

Size: 1.17 KB

Versions: 5

Compression:

Stored size: 1.17 KB

Contents

module Dcha
  class Peer
    # :nodoc:
    module HasTrie
      def trie
        @retry = false
        @trie ||= MPT::Trie.new
      end

      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

      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

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

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

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

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
dcha-0.1.4 lib/dcha/peer/has_trie.rb
dcha-0.1.3 lib/dcha/peer/has_trie.rb
dcha-0.1.2 lib/dcha/peer/has_trie.rb
dcha-0.1.1 lib/dcha/peer/has_trie.rb
dcha-0.1.0 lib/dcha/peer/has_trie.rb