Class: TonSdkRuby::Cell

Inherits:
Object
  • Object
show all
Defined in:
lib/ton-sdk-ruby/boc/cell.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Cell

Returns a new instance of Cell.



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/ton-sdk-ruby/boc/cell.rb', line 183

def initialize(options = {})
  options = { bits: [], refs: [], type: CellType::Ordinary }.merge(options)

  mapper = get_mapper(options[:type])
  validate = mapper[:validate]
  mask = mapper[:mask]

  validate.call(options[:bits], options[:refs])
  @mask = mask.call(options[:bits], options[:refs])
  @type = options[:type]
  @bits = options[:bits]
  @refs = options[:refs]
  @depths = {}
  @hashes = {}

  init()
end

Instance Attribute Details

#bitsObject

Get current Cell instance bits



202
203
204
# File 'lib/ton-sdk-ruby/boc/cell.rb', line 202

def bits
  @bits
end

#maskObject

Get current Cell instance Mask (that includes level, hashes count, etc…)



212
213
214
# File 'lib/ton-sdk-ruby/boc/cell.rb', line 212

def mask
  @mask
end

#refsObject

Get current Cell instance refs



207
208
209
# File 'lib/ton-sdk-ruby/boc/cell.rb', line 207

def refs
  @refs
end

#typeObject

Get current Cell instance CellType



217
218
219
# File 'lib/ton-sdk-ruby/boc/cell.rb', line 217

def type
  @type
end

Class Method Details

.get_depth_descriptor(depth) ⇒ Object

Calculate depth descriptor



227
228
229
230
# File 'lib/ton-sdk-ruby/boc/cell.rb', line 227

def self.get_depth_descriptor(depth)
  descriptor = [(depth / 256).floor, depth % 256].pack('C*')
  bytes_to_bits(descriptor)
end

Instance Method Details

#depth(level = 3) ⇒ Object

Get cell’s depth (max level by default)



270
271
272
273
274
275
276
277
278
279
280
281
282
# File 'lib/ton-sdk-ruby/boc/cell.rb', line 270

def depth(level = 3)
  return @depths[@mask.apply(level).hash_index] if @type != CellType::PrunedBranch

  hash_index = @mask.apply(level).hash_index
  this_hash_index = @mask.hash_index
  skip = 16 + this_hash_index * HASH_BITS + hash_index * DEPTH_BITS

  if hash_index != this_hash_index
    bits_to_int_uint(@bits[skip...(skip + DEPTH_BITS)], type: 'uint')
  else
    @depths[0]
  end
end

#eq(cell) ⇒ Object

Checks Cell equality by comparing cell hashes



307
308
309
# File 'lib/ton-sdk-ruby/boc/cell.rb', line 307

def eq(cell)
  hash == cell.hash
end

#exoticObject

Check if current Cell instance is exotic type



222
223
224
# File 'lib/ton-sdk-ruby/boc/cell.rb', line 222

def exotic
  @type != CellType::Ordinary
end

#get_augmented_bitsObject

Get current Cell instance augmented bits



250
251
252
# File 'lib/ton-sdk-ruby/boc/cell.rb', line 250

def get_augmented_bits
  augment(@bits)
end

#get_bits_descriptorObject

Get current Cell instance bits descriptor



243
244
245
246
247
# File 'lib/ton-sdk-ruby/boc/cell.rb', line 243

def get_bits_descriptor
  value = (@bits.length / 8.0).ceil + (@bits.length / 8.0).floor
  descriptor = [value].pack('C')
  bytes_to_bits(descriptor)
end

#get_refs_descriptor(mask = nil) ⇒ Object

Get current Cell instance refs descriptor



233
234
235
236
237
238
239
240
# File 'lib/ton-sdk-ruby/boc/cell.rb', line 233

def get_refs_descriptor(mask = nil)
  value = @refs.length +
    (exotic ? 8 : 0) +
    ((mask ? mask.value : @mask.value) * 32)

  descriptor = [value].pack('C')
  bytes_to_bits(descriptor)
end

#hash(level = 3) ⇒ Object

Get cell’s hash in hex (max level by default)



255
256
257
258
259
260
261
262
263
264
265
266
267
# File 'lib/ton-sdk-ruby/boc/cell.rb', line 255

def hash(level = 3)
  return @hashes[@mask.apply(level).hash_index] if @type != CellType::PrunedBranch

  hash_index = @mask.apply(level).hash_index
  this_hash_index = @mask.hash_index
  skip = 16 + hash_index * HASH_BITS

  if hash_index != this_hash_index
    bits_to_hex(@bits[skip...(skip + HASH_BITS)])
  else
    @hashes[0]
  end
end

#parseObject

Get Slice from current instance



285
286
287
# File 'lib/ton-sdk-ruby/boc/cell.rb', line 285

def parse
  Slice.parse(self)
end

Print cell as fift-hex



290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
# File 'lib/ton-sdk-ruby/boc/cell.rb', line 290

def print(indent = 1, size = 0)
  # TODO: fix this logic

  bits = @bits.dup
  are_divisible = bits.length % 4 == 0
  augmented = are_divisible ? bits : augment(bits, 4)
  fift_hex = "#{bits_to_hex(augmented).upcase}#{are_divisible ? '' : '_'}"
  output = ["#{' ' * (indent * size)}x{#{fift_hex}}\n"]

  @refs.each do |ref|
    output.push(ref.print(indent, size + 1))
  end

  output.join('')
end