Class: Sigma::ErgoTree

Inherits:
Object
  • Object
show all
Extended by:
FFI::Library
Defined in:
lib/sigma/ergo_tree.rb

Overview

The root of ErgoScript IR. Serialized instances of this class are self sufficient and can be passed around.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#pointerObject

Returns the value of attribute pointer.



23
24
25
# File 'lib/sigma/ergo_tree.rb', line 23

def pointer
  @pointer
end

Class Method Details

.from_base16_encoded_string(encoded_str) ⇒ ErgoTree

Decode from a base16 encoded serialized ErgoTree

Parameters:

  • encoded_str (String)

Returns:



48
49
50
51
52
53
54
# File 'lib/sigma/ergo_tree.rb', line 48

def self.from_base16_encoded_string(encoded_str)
  pointer = FFI::MemoryPointer.new(:pointer)
  error = ergo_lib_ergo_tree_from_base16_bytes(encoded_str, pointer)
  Util.check_error!(error)

  init(pointer)
end

.from_bytes(bytes) ⇒ ErgoTree

Decode from encoded serialized ErgoTree

Parameters:

  • bytes (Array<uint8>)

    Array of unsigned 8-bit integers (0-255)

Returns:



36
37
38
39
40
41
42
43
# File 'lib/sigma/ergo_tree.rb', line 36

def self.from_bytes(bytes)
  pointer = FFI::MemoryPointer.new(:pointer)
  b_ptr = FFI::MemoryPointer.new(:uint8, bytes.size)
  b_ptr.write_array_of_uint8(bytes)
  error = ergo_lib_ergo_tree_from_bytes(b_ptr, bytes.size, pointer)
  Util.check_error!(error)
  init(pointer)
end

.with_raw_pointer(unread_pointer) ⇒ ErgoTree

Note:

A user of sigma_rb generally does not need to call this function

Takes ownership of an existing ErgoTree Pointer.

Parameters:

  • pointer (FFI::MemoryPointer)

Returns:



29
30
31
# File 'lib/sigma/ergo_tree.rb', line 29

def self.with_raw_pointer(unread_pointer)
  init(unread_pointer)
end

Instance Method Details

#==(ergo_tree_two) ⇒ Object

Equality check

Parameters:



130
131
132
# File 'lib/sigma/ergo_tree.rb', line 130

def ==(ergo_tree_two)
  ergo_lib_ergo_tree_eq(self.pointer, ergo_tree_two.pointer)
end

#constants_lengthInteger

Returns the number of constants stored in the serialized “ErgoTree“ or throws error if the parsing of constants failed

Returns:

  • (Integer)


94
95
96
97
98
# File 'lib/sigma/ergo_tree.rb', line 94

def constants_length
  res = ergo_lib_ergo_tree_constants_len(self.pointer)
  Util.check_error!(res[:error])
  res[:value]
end

#get_constant(index) ⇒ Constant?

Return constant with given index (as stored in serialized ErgoTree) if it exists. Throws if constant parsing failed. Returns nil if no constant exists at given index

Parameters:

  • index (Integer)

Returns:



103
104
105
106
107
108
109
110
111
112
# File 'lib/sigma/ergo_tree.rb', line 103

def get_constant(index)
  pointer = FFI::MemoryPointer.new(:pointer)
  res = ergo_lib_ergo_tree_get_constant(self.pointer, index, pointer)
  Util.check_error!(res[:error])
  if res[:is_some]
    Sigma::Constant.with_raw_pointer(pointer)
  else
    nil
  end
end

#replace_constant(index:, constant:) ⇒ ErgoTree

Replace the constant of the “ErgoTree“ with the given ‘constant` at position `index`. Throws if no constant exists at `index`.

Parameters:

Returns:

  • (ErgoTree)

    ErgoTree with constant replaced (updates self)



118
119
120
121
122
123
124
125
126
# File 'lib/sigma/ergo_tree.rb', line 118

def replace_constant(index:, constant:)
  pointer = FFI::MemoryPointer.new(:pointer)
  error = ergo_lib_ergo_tree_with_constant(self.pointer, index, constant.pointer, pointer)
  Util.check_error!(error)

  # Replace self.pointer with new ergo_tree pointer
  # Old pointer will be deleted when out of scope by GC
  self.class.init(pointer, obj: self)
end

#to_base16_encoded_stringString

Convert to base16 encoded serialized bytes

Returns:

  • (String)


58
59
60
61
62
63
64
65
66
# File 'lib/sigma/ergo_tree.rb', line 58

def to_base16_encoded_string
  s_ptr = FFI::MemoryPointer.new(:pointer, 1)
  error = ergo_lib_ergo_tree_to_base16_bytes(self.pointer, s_ptr)
  Util.check_error!(error)
  s_ptr = s_ptr.read_pointer()
  str = s_ptr.read_string().force_encoding('UTF-8')
  Util.ergo_lib_delete_string(s_ptr)
  str
end

#to_bytesArray<uint8>

Convert to serialized bytes

Returns:

  • (Array<uint8>)

    Array of 8-bit integers (0-255)



70
71
72
73
74
75
76
77
78
# File 'lib/sigma/ergo_tree.rb', line 70

def to_bytes
  res = ergo_lib_ergo_tree_bytes_len(self.pointer)
  Util.check_error!(res[:error])
  bytes_length = res[:value]
  bytes_ptr = FFI::MemoryPointer.new(:uint8, bytes_length)
  error = ergo_lib_ergo_tree_to_bytes(self.pointer, bytes_ptr)
  Util.check_error!(error)
  bytes_ptr.read_array_of_uint8(bytes_length)
end

#to_template_bytesArray<uint8>

Serialized proposition expression of SigmaProp type with ConstantPlaceholder nodes instead of Constant nodes.

Returns:

  • (Array<uint8>)

    Array of 8-bit integers (0-255)



82
83
84
85
86
87
88
89
90
# File 'lib/sigma/ergo_tree.rb', line 82

def to_template_bytes
  res = ergo_lib_ergo_tree_template_bytes_len(self.pointer)
  Util.check_error!(res[:error])
  bytes_length = res[:value]
  bytes_ptr = FFI::MemoryPointer.new(:uint8, bytes_length)
  error = ergo_lib_ergo_tree_template_bytes(self.pointer, bytes_ptr)
  Util.check_error!(error)
  bytes_ptr.read_array_of_uint8(bytes_length)
end