Skip to content

Commit

Permalink
Merge pull request #1692 from 0chain/feat/callback-hash
Browse files Browse the repository at this point in the history
Add lookuphash in callback
  • Loading branch information
dabasov authored Nov 30, 2024
2 parents dea91ce + 134920b commit 5a8de55
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
5 changes: 4 additions & 1 deletion wasmsdk/blobber.go
Original file line number Diff line number Diff line change
Expand Up @@ -1037,7 +1037,10 @@ func downloadBlocks(allocId, remotePath, authTicket, lookupHash, writeChunkFuncN
fh = mf
defer sys.Files.Remove(pathHash) //nolint
} else {
fh = jsbridge.NewFileCallbackWriter(writeChunkFuncName)
fh = jsbridge.NewFileCallbackWriter(writeChunkFuncName, lookupHash)
if fh == nil {
return nil, fmt.Errorf("could not create file writer, callback function not found")
}
}

wg.Add(1)
Expand Down
11 changes: 8 additions & 3 deletions wasmsdk/jsbridge/file_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,14 +168,19 @@ type FileCallbackWriter struct {
writeChunk js.Value
buf []byte
offset int64
lookupHash string
}

const bufCallbackCap = 4 * 1024 * 1024 //4MB

func NewFileCallbackWriter(writeChunkFuncName string) *FileCallbackWriter {
func NewFileCallbackWriter(writeChunkFuncName, lookupHash string) *FileCallbackWriter {
writeChunk := js.Global().Get(writeChunkFuncName)
if !writeChunk.Truthy() {
return nil
}
return &FileCallbackWriter{
writeChunk: writeChunk,
lookupHash: lookupHash,
}
}

Expand All @@ -190,7 +195,7 @@ func (wc *FileCallbackWriter) Write(p []byte) (int, error) {
if len(wc.buf)+len(p) > cap(wc.buf) {
uint8Array := js.Global().Get("Uint8Array").New(len(wc.buf))
js.CopyBytesToJS(uint8Array, wc.buf)
_, err := Await(wc.writeChunk.Invoke(uint8Array, wc.offset))
_, err := Await(wc.writeChunk.Invoke(wc.lookupHash, uint8Array, wc.offset))
if len(err) > 0 && !err[0].IsNull() {
return 0, errors.New("file_writer: " + err[0].String())
}
Expand All @@ -205,7 +210,7 @@ func (wc *FileCallbackWriter) Close() error {
if len(wc.buf) > 0 {
uint8Array := js.Global().Get("Uint8Array").New(len(wc.buf))
js.CopyBytesToJS(uint8Array, wc.buf)
_, err := Await(wc.writeChunk.Invoke(uint8Array, wc.offset))
_, err := Await(wc.writeChunk.Invoke(wc.lookupHash, uint8Array, wc.offset))
if len(err) > 0 && !err[0].IsNull() {
return errors.New("file_writer: " + err[0].String())
}
Expand Down

0 comments on commit 5a8de55

Please sign in to comment.