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

feat(lib/erasure): implement Go binding over rust for erasure coding #3447

Merged
merged 22 commits into from
Aug 24, 2023
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .github/workflows/unit-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,11 @@ jobs:
go test -timeout=10m ./... && \
cd ..

- name: generate a shared library file for erasure
run: make compile-erasure

- name: Run unit tests
run: go test -coverprofile=coverage.out -covermode=atomic -timeout=45m ./...
run: LD_LIBRARY_PATH=${PWD}/lib/erasure go test -coverprofile=coverage.out -covermode=atomic -timeout=45m ./...
axaysagathiya marked this conversation as resolved.
Show resolved Hide resolved

- name: Trie memory test
run: |
Expand Down
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,8 @@ tmp
# node_modules used by polkadot.js/api tests
tests/polkadotjs_test/node_modules
!tests/polkadotjs_test/test/*.wasm

# Ignore rust target dir
lib/erasure/rustlib/target

*.so
9 changes: 8 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -146,4 +146,11 @@ endif
chmod a+x $(GOPATH)/bin/zombienet

zombienet-test: install install-zombienet
zombienet test -p native zombienet_tests/functional/0001-basic-network.zndsl
zombienet test -p native zombienet_tests/functional/0001-basic-network.zndsl

compile-erasure:
cd lib/erasure/rustlib/ && \
axaysagathiya marked this conversation as resolved.
Show resolved Hide resolved
cargo build --release --target x86_64-unknown-linux-gnu && \
cd .. && \
cp ./rustlib/target/x86_64-unknown-linux-gnu/release/liberasure_coding_gorust.so ./liberasure.so && \
axaysagathiya marked this conversation as resolved.
Show resolved Hide resolved
cd ../../
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ require (
github.com/ipfs/go-ds-badger2 v0.1.3
github.com/jpillora/ipfilter v1.2.9
github.com/klauspost/compress v1.16.7
github.com/klauspost/reedsolomon v1.11.8
github.com/libp2p/go-libp2p v0.27.7
github.com/libp2p/go-libp2p-kad-dht v0.24.2
github.com/minio/sha256-simd v1.0.1
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -431,8 +431,6 @@ github.com/klauspost/compress v1.16.7 h1:2mk3MPGNzKyxErAw8YaohYh69+pa4sIQSC0fPGC
github.com/klauspost/compress v1.16.7/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE=
github.com/klauspost/cpuid/v2 v2.2.5 h1:0E5MSMDEoAulmXNFquVs//DdoomxaoTY1kUhbc/qbZg=
github.com/klauspost/cpuid/v2 v2.2.5/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws=
github.com/klauspost/reedsolomon v1.11.8 h1:s8RpUW5TK4hjr+djiOpbZJB4ksx+TdYbRH7vHQpwPOY=
github.com/klauspost/reedsolomon v1.11.8/go.mod h1:4bXRN+cVzMdml6ti7qLouuYi32KHJ5MGv0Qd8a47h6A=
github.com/koron/go-ssdp v0.0.0-20191105050749-2e1c40ed0b5d/go.mod h1:5Ky9EC2xfoUKUor0Hjgi2BJhCSXJfMOFlmyYrVKGQMk=
github.com/koron/go-ssdp v0.0.4 h1:1IDwrghSKYM7yLf7XCzbByg2sJ/JcNOZRXS2jczTwz0=
github.com/koron/go-ssdp v0.0.4/go.mod h1:oDXq+E5IL5q0U8uSBcoAXzTzInwy5lEgC91HoKtbmZk=
Expand Down
12 changes: 12 additions & 0 deletions lib/erasure/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Building Rust code Binary and Setting Environment

- Generate rust binary
```
cd lib/erasure/rustlib/ && cargo build --release --target x86_64-unknown-linux-gnu && cd ..
axaysagathiya marked this conversation as resolved.
Show resolved Hide resolved
cp ./rustlib/target/x86_64-unknown-linux-gnu/release/liberasure_coding_gorust.so ./liberasure.so && cd ../../
axaysagathiya marked this conversation as resolved.
Show resolved Hide resolved
```

- set a path to .so file
```
export LD_LIBRARY_PATH=${PWD}/lib/erasure
```
axaysagathiya marked this conversation as resolved.
Show resolved Hide resolved
126 changes: 79 additions & 47 deletions lib/erasure/erasure.go
Original file line number Diff line number Diff line change
@@ -1,70 +1,102 @@
// Copyright 2021 ChainSafe Systems (ON)
// Copyright 2023 ChainSafe Systems (ON)
// SPDX-License-Identifier: LGPL-3.0-only

package erasure
axaysagathiya marked this conversation as resolved.
Show resolved Hide resolved

// #cgo CFLAGS: -I.
// #cgo LDFLAGS: -L. -lerasure
EclesioMeloJunior marked this conversation as resolved.
Show resolved Hide resolved
// #include "./rustlib.h"
axaysagathiya marked this conversation as resolved.
Show resolved Hide resolved
import (
axaysagathiya marked this conversation as resolved.
Show resolved Hide resolved
"C"
)
import (
"bytes"
"errors"
"fmt"

"github.com/klauspost/reedsolomon"
"unsafe"
)

// ErrNotEnoughValidators cannot encode something for zero or one validator
var ErrNotEnoughValidators = errors.New("expected at least 2 validators")
var (
ErrZeroSizedData = errors.New("data can't be zero sized")
ErrZeroSizedChunks = errors.New("chunks can't be zero sized")
)

// ObtainChunks obtains erasure-coded chunks, divides data into number of validatorsQty chunks and
// creates parity chunks for reconstruction
func ObtainChunks(validatorsQty int, data []byte) ([][]byte, error) {
recoveryThres, err := recoveryThreshold(validatorsQty)
if err != nil {
return nil, err
}
enc, err := reedsolomon.New(validatorsQty, recoveryThres)
if err != nil {
return nil, fmt.Errorf("creating new reed solomon failed: %w", err)
// ObtainChunks obtains erasure-coded chunks, one for each validator.
// This works only up to 65536 validators, and `n_validators` must be non-zero and accepts
// number of validators and scale encoded data.
func ObtainChunks(nValidators uint, data []byte) ([][]byte, error) {
if len(data) == 0 {
return nil, ErrZeroSizedData
}
shards, err := enc.Split(data)
if err != nil {
return nil, err

var cFlattenedChunks *C.uchar
var cFlattenedChunksLen C.size_t

cnValidators := C.size_t(nValidators)
cData := (*C.uchar)(unsafe.Pointer(&data[0]))
cLen := C.size_t(len(data))

cErr := C.obtain_chunks(cnValidators, cData, cLen, &cFlattenedChunks, &cFlattenedChunksLen)
errStr := C.GoString(cErr)
C.free(unsafe.Pointer(cErr))

if len(errStr) > 0 {
return nil, errors.New(errStr)
}
err = enc.Encode(shards)
if err != nil {
return nil, err

resData := C.GoBytes(unsafe.Pointer(cFlattenedChunks), C.int(cFlattenedChunksLen))
C.free(unsafe.Pointer(cFlattenedChunks))

chunkSize := uint(len(resData)) / nValidators
chunks := make([][]byte, nValidators)

start := uint(0)
for i := start; i < nValidators; i++ {
end := start + chunkSize
chunks[i] = resData[start:end]
start = end
}

return shards, nil
return chunks, nil
}

// Reconstruct the missing data from a set of chunks
func Reconstruct(validatorsQty, originalDataLen int, chunks [][]byte) ([]byte, error) {
recoveryThres, err := recoveryThreshold(validatorsQty)
if err != nil {
return nil, err
// Reconstruct decodable data from a set of chunks.
//
// Provide an iterator containing chunk data and the corresponding index.
// The indices of the present chunks must be indicated. If too few chunks
// are provided, recovery is not possible.
//
// Works only up to 65536 validators, and `n_validators` must be non-zero
func Reconstruct(nValidators uint, chunks [][]byte) ([]byte, error) {
if len(chunks) == 0 {
return nil, ErrZeroSizedChunks
}

enc, err := reedsolomon.New(validatorsQty, recoveryThres)
if err != nil {
return nil, err
}
err = enc.Reconstruct(chunks)
if err != nil {
return nil, err
var cReconstructedData *C.uchar
var cReconstructedDataLen C.size_t
var flattenedChunks []byte

for _, chunk := range chunks {
flattenedChunks = append(flattenedChunks, chunk...)
}
buf := new(bytes.Buffer)
err = enc.Join(buf, chunks, originalDataLen)
return buf.Bytes(), err
}

// recoveryThreshold gives the max number of shards/chunks that we can afford to lose and still construct
// the full initial data. Total number of chunks will be validatorQty + recoveryThreshold
func recoveryThreshold(validators int) (int, error) {
if validators <= 1 {
return 0, ErrNotEnoughValidators
cChunkSize := C.size_t(len(chunks[0]))
cFlattenedChunks := (*C.uchar)(unsafe.Pointer(&flattenedChunks[0]))
cFlattenedChunksLen := C.size_t(len(flattenedChunks))

cErr := C.reconstruct(
C.size_t(nValidators),
cFlattenedChunks, cFlattenedChunksLen,
cChunkSize,
&cReconstructedData, &cReconstructedDataLen,
)
errStr := C.GoString(cErr)
C.free(unsafe.Pointer(cErr))

if len(errStr) > 0 {
return nil, errors.New(errStr)
}

needed := (validators - 1) / 3
res := C.GoBytes(unsafe.Pointer(cReconstructedData), C.int(cReconstructedDataLen))
C.free(unsafe.Pointer(cReconstructedData))

return needed + 1, nil
return res, nil
}
Loading
Loading