-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
233 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}) | ||
} | ||
} |