ssz 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: e90d345e757920b0e658c21302c92cc6bef8a8ca45dddc9cd05dfc4a979d2516
4
+ data.tar.gz: bcd166173f3156c77f882188d66ec7f53e2f978808238d3f8ba4582b55977d46
5
+ SHA512:
6
+ metadata.gz: c6e26a1d8a49db7ccfcf597e82dfcda701a6dc5e9f7a39350ea6720354c5ded63f9c325507f48157717792b0743386a3cc5c7b14ba0bb79375a3d5457ee2c8dd
7
+ data.tar.gz: 722e879df0fe33a4499f45985a2fd0070e3c60a36f125de562d359d0d8d330cce1c401feb66255e5cd1e49642aadadcee0114e709a039ff3a83ea228235ecc75
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
@@ -0,0 +1,6 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.6.5
6
+ before_install: gem install bundler -v 2.1.0.pre.2
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in ssz-rb.gemspec
4
+ gemspec
5
+
6
+ gem "rake", "~> 12.0"
7
+ gem "minitest", "~> 5.0"
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Kai Chen
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,44 @@
1
+ # SSZ
2
+
3
+ **S**imple **S**eriali**Z**e implemented in Ruby programming language.
4
+
5
+ It's a data serialization format, to replace [RLP](https://github.com/ethereum/wiki/wiki/RLP), used in [eth2](https://github.com/ethereum/eth2.0-specs).
6
+
7
+ * <https://github.com/ethereum/eth2.0-specs/blob/dev/specs/simple-serialize.md>
8
+ * <https://ethresear.ch/t/replacing-ssz-with-rlp-zip-and-sha256/5706>
9
+ * <https://rauljordan.com/2019/07/02/go-lessons-from-writing-a-serialization-library-for-ethereum.html>
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ ```ruby
16
+ gem 'ssz-rb'
17
+ ```
18
+
19
+ And then execute:
20
+
21
+ $ bundle install
22
+
23
+ Or install it yourself as:
24
+
25
+ $ gem install ssz-rb
26
+
27
+ ## Usage
28
+
29
+ TODO: Write usage instructions here
30
+
31
+ ## Development
32
+
33
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
34
+
35
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
36
+
37
+ ## Contributing
38
+
39
+ Bug reports and pull requests are welcome on GitHub at https://github.com/kaichen/ssz-rb.
40
+
41
+
42
+ ## License
43
+
44
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList["test/**/*_test.rb"]
8
+ end
9
+
10
+ task :default => :test
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "ssz/rb"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,12 @@
1
+ require "ssz/version"
2
+ require_relative './ssz/boolean'
3
+ require_relative './ssz/uint'
4
+ require_relative './ssz/bit_victor'
5
+ require_relative './ssz/bit_list'
6
+ require_relative './ssz/byte_buffer'
7
+ require_relative './ssz/victor'
8
+
9
+ module Ssz
10
+ class Error < StandardError; end
11
+ # Your code goes here...
12
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative './fixed_sized'
4
+
5
+ module Ssz
6
+ class Base
7
+ include FixedSized
8
+ end
9
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative './base'
4
+
5
+ module Ssz
6
+ class BitList < Base
7
+ def initialize(bits, capacity:)
8
+ @capacity = capacity
9
+ @bits = bits
10
+ end
11
+
12
+ def serialize
13
+ bb = ByteBuffer.from_bit_array(@bits.map{|b| b ? 1 : 0})
14
+ bb.bytes << 0 if bb.bytes.size < (@bits.size / 8) + 1 # extend buffer size
15
+ bb.bytes[@bits.size / 8] |= 1 << (@bits.size % 8)
16
+ bb.serialize
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative './base'
4
+
5
+ module Ssz
6
+ class BitVictor < Base
7
+ def initialize(bits, capacity:)
8
+ @capacity = capacity
9
+ @bits = bits
10
+ end
11
+
12
+ def serialize
13
+ ByteBuffer.from_bit_array(@bits.map{|b| b ? 1 : 0}).serialize
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative './base'
4
+
5
+ module Ssz
6
+ class Boolean < Base
7
+ def initialize(bool)
8
+ @bool = bool
9
+ end
10
+
11
+ def serialize
12
+ Array(@bool ? 1 : 0).pack("C")
13
+ end
14
+
15
+ def size
16
+ 1
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ssz
4
+ # @example:
5
+ # ByteBuffer.from_bit_array([1, 0, 0, 0]).bytes #=> "\x01"
6
+ class ByteBuffer
7
+ attr_accessor :bytes
8
+
9
+ def initialize(n = 0)
10
+ @bytes = [0] * n
11
+ end
12
+
13
+ def self.from_bit_array(bits)
14
+ byte_size = bits.size < 8 ? 1 : (bits.size / 8.0).ceil
15
+ bb = ByteBuffer.new(byte_size)
16
+ bb.bits = bits
17
+ bb
18
+ end
19
+
20
+ def bits=(bits)
21
+ byte_size = bits.size < 8 ? 1 : (bits.size / 8.0).ceil
22
+ # fill 0 to fix byte(8-bits)
23
+ while (byte_size * 8) != bits.size do
24
+ bits << 0
25
+ end
26
+ bits.each_with_index do |b, i|
27
+ @bytes[i / 8] |= b << (i % 8)
28
+ end
29
+ bits
30
+ end
31
+
32
+ def serialize
33
+ @bytes.pack("C*")
34
+ end
35
+
36
+ def length
37
+ @bytes.length
38
+ end
39
+
40
+ def bit_size
41
+ length * 8
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ssz
4
+ module FixedSized
5
+ def fixed_sized?
6
+ [Boolean, BitVictor, Victor, Uint].include? self.class
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative './base'
4
+
5
+ module Ssz
6
+ class Uint < Base
7
+ def initialize(num, length: 8)
8
+ @num = num
9
+ @data_length = length
10
+ # TODO: validate length
11
+ end
12
+
13
+ def serialize
14
+ byte_size = (@data_length / 8)
15
+ # FIXME: better way convert integer to bytes
16
+ hex = @num.to_s(16)
17
+ hex = "0#{hex}" if hex.size % 2 != 0
18
+ bytes = hex.scan(/.{2}/)
19
+ if bytes.length < byte_size
20
+ bytes = ["0"] * (byte_size - bytes.length) + bytes
21
+ end
22
+ bytes.map { |str| str.to_i(16) }.pack("C" * byte_size).force_encoding("ASCII-8BIT")
23
+ end
24
+
25
+ def size
26
+ serialize.size
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,3 @@
1
+ module Ssz
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative './base'
4
+
5
+ module Ssz
6
+ class Victor < Base
7
+ attr_reader :size
8
+
9
+ def initialize(element_type, size)
10
+ @element_type = element_type
11
+ @items = []
12
+ @size = size
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,26 @@
1
+ require_relative 'lib/ssz/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "ssz"
5
+ spec.version = Ssz::VERSION
6
+ spec.authors = ["Kai Chen"]
7
+ spec.email = ["kai@thekaiway.com"]
8
+
9
+ spec.summary = %q{Simple serialization implementation in Ruby}
10
+ spec.homepage = "https://github.com/kaichen/ssz-rb"
11
+ spec.license = "MIT"
12
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
13
+
14
+ spec.metadata["homepage_uri"] = "https://github.com/kaichen/ssz-rb"
15
+ spec.metadata["source_code_uri"] = "https://github.com/kaichen/ssz-rb"
16
+ spec.metadata["changelog_uri"] = "https://github.com/kaichen/ssz-rb/blob/master/CHANGELOG.md"
17
+
18
+ # Specify which files should be added to the gem when it is released.
19
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
20
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
21
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
22
+ end
23
+ spec.bindir = "exe"
24
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
25
+ spec.require_paths = ["lib"]
26
+ end
metadata ADDED
@@ -0,0 +1,65 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ssz
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Kai Chen
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-11-02 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email:
15
+ - kai@thekaiway.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".gitignore"
21
+ - ".travis.yml"
22
+ - Gemfile
23
+ - LICENSE.txt
24
+ - README.md
25
+ - Rakefile
26
+ - bin/console
27
+ - bin/setup
28
+ - lib/ssz.rb
29
+ - lib/ssz/base.rb
30
+ - lib/ssz/bit_list.rb
31
+ - lib/ssz/bit_victor.rb
32
+ - lib/ssz/boolean.rb
33
+ - lib/ssz/byte_buffer.rb
34
+ - lib/ssz/fixed_sized.rb
35
+ - lib/ssz/uint.rb
36
+ - lib/ssz/version.rb
37
+ - lib/ssz/victor.rb
38
+ - ssz.gemspec
39
+ homepage: https://github.com/kaichen/ssz-rb
40
+ licenses:
41
+ - MIT
42
+ metadata:
43
+ homepage_uri: https://github.com/kaichen/ssz-rb
44
+ source_code_uri: https://github.com/kaichen/ssz-rb
45
+ changelog_uri: https://github.com/kaichen/ssz-rb/blob/master/CHANGELOG.md
46
+ post_install_message:
47
+ rdoc_options: []
48
+ require_paths:
49
+ - lib
50
+ required_ruby_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: 2.3.0
55
+ required_rubygems_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: '0'
60
+ requirements: []
61
+ rubygems_version: 3.0.3
62
+ signing_key:
63
+ specification_version: 4
64
+ summary: Simple serialization implementation in Ruby
65
+ test_files: []