Skip to content

Commit

Permalink
refactor 3
Browse files Browse the repository at this point in the history
  • Loading branch information
smaulik13 committed Jan 16, 2025
1 parent c440472 commit eeec3c6
Show file tree
Hide file tree
Showing 5 changed files with 233 additions and 5 deletions.
146 changes: 146 additions & 0 deletions zboxcore/zboxutil/download_buffer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
package zboxutil

import (
"context"
"sync"
"time"

"github.com/0chain/gosdk/core/sys"
"github.com/valyala/bytebufferpool"
)

type DownloadBuffer interface {
RequestChunk(ctx context.Context, num int) []byte
ReleaseChunk(num int)
ClearBuffer()
}

type DownloadBufferWithChan struct {
buf []byte
length int
reqSize int
ch chan int
mu sync.Mutex
mp map[int]int
}

func NewDownloadBufferWithChan(size, numBlocks, effectiveBlockSize int) *DownloadBufferWithChan {
numBlocks++
db := &DownloadBufferWithChan{
buf: make([]byte, size*numBlocks*effectiveBlockSize),
length: size,
reqSize: effectiveBlockSize * numBlocks,
ch: make(chan int, size),
mp: make(map[int]int),
}
for i := 0; i < size; i++ {
db.ch <- i
}
return db
}

func (r *DownloadBufferWithChan) ReleaseChunk(num int) {
r.mu.Lock()
ind, ok := r.mp[num]
if !ok {
r.mu.Unlock()
return
}
delete(r.mp, num)
r.mu.Unlock()
r.ch <- ind
}

func (r *DownloadBufferWithChan) RequestChunk(ctx context.Context, num int) []byte {
select {
case <-ctx.Done():
return nil
case ind := <-r.ch:
r.mu.Lock()
r.mp[num] = ind
r.mu.Unlock()
return r.buf[ind*r.reqSize : (ind+1)*r.reqSize : (ind+1)*r.reqSize]
}
}

func (r *DownloadBufferWithChan) ClearBuffer() {
r.buf = nil
close(r.ch)
for k := range r.mp {
delete(r.mp, k)
}
r.mp = nil
}

type DownloadBufferWithMask struct {
downloadBuf []*bytebufferpool.ByteBuffer
length int
reqSize int
numBlocks int
mask uint32
mu sync.Mutex
}

func NewDownloadBufferWithMask(size, numBlocks, effectiveBlockSize int) *DownloadBufferWithMask {
numBlocks++
return &DownloadBufferWithMask{
length: size,
reqSize: effectiveBlockSize * numBlocks,
mask: (1 << size) - 1,
downloadBuf: make([]*bytebufferpool.ByteBuffer, size),
}
}

func (r *DownloadBufferWithMask) SetNumBlocks(numBlocks int) {
r.numBlocks = numBlocks
}

func (r *DownloadBufferWithMask) RequestChunk(ctx context.Context, num int) []byte {
num = num / r.numBlocks
num = num % r.length
for {
select {
case <-ctx.Done():
return nil
default:
}
r.mu.Lock()
isSet := r.mask & (1 << num)
// already assigned
if isSet == 0 {
r.mu.Unlock()
sys.Sleep(200 * time.Millisecond)
continue
}
// assign the chunk by clearing the bit
r.mask &= ^(1 << num)
if r.downloadBuf[num] == nil {
buff := BufferPool.Get()
if cap(buff.B) < r.reqSize {
buff.B = make([]byte, r.reqSize)
} else {
buff.B = buff.B[:r.reqSize]
}
r.downloadBuf[num] = buff
}
r.mu.Unlock()
return r.downloadBuf[num].B[:r.reqSize:r.reqSize]
}
}

func (r *DownloadBufferWithMask) ReleaseChunk(num int) {
num = num / r.numBlocks
num = num % r.length
r.mu.Lock()
defer r.mu.Unlock()
r.mask |= 1 << num
}

func (r *DownloadBufferWithMask) ClearBuffer() {
for _, buff := range r.downloadBuf {
if buff != nil {
BufferPool.Put(buff)
}
}
r.downloadBuf = nil
}
6 changes: 3 additions & 3 deletions zboxcore/zboxutil/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ import (
"github.com/hashicorp/golang-lru/v2/simplelru"

"github.com/0chain/errors"
"github.com/0chain/gosdk_common/core/client"
"github.com/0chain/gosdk_common/core/encryption"
"github.com/0chain/gosdk_common/core/logger"
"github.com/0chain/gosdk/core/client"
"github.com/0chain/gosdk/core/encryption"
"github.com/0chain/gosdk/core/logger"
lru "github.com/hashicorp/golang-lru/v2"
"github.com/hitenjain14/fasthttp"
)
Expand Down
19 changes: 19 additions & 0 deletions zboxcore/zboxutil/transport_wasm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//go:build js && wasm
// +build js,wasm

package zboxutil

import (
"net/http"
"time"
)

var DefaultTransport = &http.Transport{
Proxy: envProxy.Proxy,

MaxIdleConns: 100,
IdleConnTimeout: 60 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
MaxIdleConnsPerHost: 100,
}
4 changes: 2 additions & 2 deletions zboxcore/zboxutil/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import (
"errors"

thrown "github.com/0chain/errors"
"github.com/0chain/gosdk_common/zboxcore/allocationchange"
"github.com/0chain/gosdk_common/zboxcore/blockchain"
"github.com/0chain/gosdk/zboxcore/allocationchange"
"github.com/0chain/gosdk/zboxcore/blockchain"
"github.com/h2non/filetype"
"github.com/hitenjain14/fasthttp"
"github.com/lithammer/shortuuid/v3"
Expand Down
63 changes: 63 additions & 0 deletions zboxcore/zboxutil/util_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package zboxutil

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestScryptEncryption(t *testing.T) {
t.Parallel()

testCases := []struct {
name string
key string
plaintext string
wantErr bool
}{
{
name: "valid plaintext",
key: "passphrase1111111111111111111111",
plaintext: "glare mistake gun joke bid spare across diagram wrap cube swear cactus cave repeat you brave few best wild lion pitch pole original wasp",
wantErr: false,
},
{
name: "empty key",
key: "",
plaintext: "glare mistake gun joke bid spare across diagram wrap cube swear cactus cave repeat you brave few best wild lion pitch pole original wasp",
wantErr: true,
},
{
name: "empty plaintext",
key: "passphrase1111111111111111111111",
plaintext: "",
wantErr: true,
},
}

for _, tc := range testCases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()

key := []byte(tc.key)
plaintext := []byte(tc.plaintext)

// Encrypt plaintext
ciphertext, err := ScryptEncrypt(key, plaintext)

if tc.wantErr {
require.Error(t, err)
require.Nil(t, ciphertext)
} else {
require.NoError(t, err)
require.NotNil(t, ciphertext)

// Decrypt ciphertext
decryptedText, err := ScryptDecrypt(key, ciphertext)
require.NoError(t, err)
require.Equal(t, plaintext, decryptedText)
}
})
}
}

0 comments on commit eeec3c6

Please sign in to comment.