Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

An atomicized list of code refactors #5

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
13 changes: 7 additions & 6 deletions src/common/address.cr
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
# For more information, please refer to <http://unlicense.org>

require "./types"
require "./string"

require "big"

Expand All @@ -38,9 +39,9 @@ module Pampero
# To obtain a workable object, `check_format` needs to be over-
# loaded to check that the address' format is correct.
abstract struct Address
getter :bytes
getter :to_i
getter :little
getter bytes
getter to_i
getter little

@str : String = ""

Expand All @@ -56,7 +57,7 @@ module Pampero
# order
def initialize(str : String, @little : Bool = false)
# Remove the header, not supported by BigInt.new
str = str[2..] if str[0] == '0' && (str[1] == 'x' || str[1] == 'X')
str = str.hexstring

@to_i = BigInt.new(str, 16)
@bytes = Array(UInt8).new(str.size.as(Int) >> 1, 0u8)
Expand All @@ -80,13 +81,13 @@ module Pampero
end

def to_s
hex_str()
hex_str
end

def from_bytes(bytes : Array(UInt8))
@bytes = bytes

if check_format()
if check_format
# Calculates the integral version
@to_i = BigInt.new(0)
(@little ? @bytes.reverse : @bytes).each do |b|
Expand Down
13 changes: 13 additions & 0 deletions src/common/string.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class String
def hex? : Bool
self.size > 2 && self[0] == '0' && self[1].downcase == 'x'
end

def hexstring : String
if self.hex?
self[2..]
else
self
end
end
beta-ziliani marked this conversation as resolved.
Show resolved Hide resolved
end
28 changes: 13 additions & 15 deletions src/common/types.cr
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,11 @@ module Pampero

def initialize(str : String)
@data = uninitialized StaticArray(UInt8, 20)
str = str[2..] if str[0] == '0' && (str[1] == 'x' || str[1] == 'X')
if str.size != 40
raise "Invalid format"
end
str = str.hexstring
raise "Invalid format: string has #{str.size} size, but must be 40" if str.size != 40

20.times do |i|
@data[i] = str[2*i..2*i+1].to_u8(16)
@data[i] = str[2*i..2*i + 1].to_u8(16)
end
end
end
Expand Down Expand Up @@ -47,19 +46,18 @@ module Pampero

def initialize(str : String)
@data = uninitialized StaticArray(UInt8, 32)
str = str[2..] if str[0] == '0' && (str[1] == 'x' || str[1] == 'X')
if str.size != 64
raise "Invalid format"
end
str = str.hexstring
raise "Invalid format: string has #{str.size} size, but must be 64" if str.size != 64

32.times do |i|
@data[i] = str[2*i..2*i+1].to_u8(16)
@data[i] = str[2*i..2*i + 1].to_u8(16)
end
end

def to_uint256 : UInt256
result = BigInt.new 0
32.times do |i|
result = result * 256 + @data[31-i]
result = result * 256 + @data[31 - i]
end
result
end
Expand All @@ -75,19 +73,19 @@ module Pampero
end

def to_s : String
to_hex()
to_hex
end

def inspect : String
to_hex()
to_hex
end

def to_json(json : JSON::Builder) : Nil
json.string(to_hex())
json.string(to_hex)
end

def to_json_object_key : String
to_hex()
to_hex
end
end

Expand Down
35 changes: 12 additions & 23 deletions src/core/verkle_state.cr
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,15 @@ module Pampero
state_diff.suffix_diffs.map do |suffix_diff|
suffix = suffix_diff.suffix
if suffix.is_a?(String)
is_hex = suffix[0] == '0' && (suffix[1] == 'x' || suffix[1] == 'X')
suffix = if is_hex
suffix[2..].to_i(16)
else
suffix.to_i
end
is_hex = suffix[0] == '0' && (suffix[1].downcase == 'x')
suffix = is_hex ? suffix[2..].to_i(16) : suffix.to_i
end
key = Bytes32.new sprintf("%s%02x", stem, suffix)

current_value = get_leaf_value(suffix_diff.current_value)
if current_value
@state[key] = current_value
else
@state.delete key
end
current_value = suffix_diff.current_value.try { |x| get_leaf_value x }
write_key key, current_value

new_value = get_leaf_value(suffix_diff.new_value)
new_value = suffix_diff.new_value.try { |x| get_leaf_value x }
if new_value
@pos_state[key] = new_value
else
Expand All @@ -39,17 +31,14 @@ module Pampero
end
end

def get_leaf_value(leaf : String | LeafValue?) : Bytes32?
result = nil
if leaf.is_a?(String)
result = Bytes32.new(leaf)
elsif leaf.is_a?(LeafValue)
value = leaf.value
if value
result = Bytes32.new(value)
end
def get_leaf_value(leaf : String) : Bytes32
Bytes32.new leaf
end

def get_leaf_value(leaf : LeafValue) : Bytes32?
if value = leaf.value
Bytes32.new(value)
end
result
end

def read_key(key : Bytes32) : Bytes32?
Expand Down
36 changes: 11 additions & 25 deletions src/core/verkle_state_manager.cr
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ module Pampero
VERKLE_NODE_WIDTH = BigInt.new(256)

HEADER_STORAGE_OFFSET = BigInt.new(64)
MAIN_STORAGE_OFFSET = VERKLE_NODE_WIDTH^31
MAIN_STORAGE_OFFSET = VERKLE_NODE_WIDTH ^ 31

CHUNK_LENGTH = BigInt.new(31)

Expand All @@ -32,18 +32,14 @@ module Pampero
def get_account(address : Address20) : Account?
stem = get_stem(address, UInt256.new(0))

result = read_account(stem)

version = result[:version]
balance = result[:balance]
nonce = result[:nonce]
code_hash = result[:code_hash]
code_size = result[:code_size]
version = read_version(stem)
balance = read_balance(stem)
nonce = read_nonce(stem)
code_hash = read_code_hash(stem)
code_size = read_code_size(stem)

# If at least one of the fields is not nil we assume the account exists
if version.nil? && balance.nil? && nonce.nil? && code_hash.nil? && code_size.nil?
return nil
end
return nil if version.nil? && balance.nil? && nonce.nil? && code_hash.nil? && code_size.nil?

account = Account.new
account.version = version
Expand Down Expand Up @@ -131,16 +127,6 @@ module Pampero
get_tree_key address32, index, 0_u8
end

def read_account(stem : Bytes32)
{
version: read_version(stem),
balance: read_balance(stem),
nonce: read_nonce(stem),
code_hash: read_code_hash(stem),
code_size: read_code_size(stem),
}
end

def get_tree_key(address : Address32, treeIndex : Bytes32, subIndex : UInt8) : Bytes32
data = Bytes64.new address, treeIndex.@data
key = Pampero::Crypto.hash data
Expand All @@ -150,10 +136,10 @@ module Pampero

def get_tree_storage_key(address : Address20, storage_key : UInt256) : Bytes32
position = if storage_key < CODE_OFFSET - HEADER_STORAGE_OFFSET
HEADER_STORAGE_OFFSET + storage_key
else
MAIN_STORAGE_OFFSET + storage_key
end
HEADER_STORAGE_OFFSET + storage_key
else
MAIN_STORAGE_OFFSET + storage_key
end

calc_tree_key(address, position)
end
Expand Down