From a60e482ab78ddc0f362c9904226fbfde619804a0 Mon Sep 17 00:00:00 2001 From: Hitenjain14 Date: Thu, 14 Mar 2024 13:50:38 +0530 Subject: [PATCH 01/32] wait for write on error --- zboxcore/sdk/downloadworker.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/zboxcore/sdk/downloadworker.go b/zboxcore/sdk/downloadworker.go index 3fbbafd24..f7eb1f108 100644 --- a/zboxcore/sdk/downloadworker.go +++ b/zboxcore/sdk/downloadworker.go @@ -747,8 +747,9 @@ func (req *DownloadRequest) processDownload() { } if err := eg.Wait(); err != nil { writeCancel() - req.errorCB(err, remotePathCB) close(blocks) + wg.Wait() + req.errorCB(err, remotePathCB) return } From 9e121eca51ff3de8bce85e898043d19c64c63bb7 Mon Sep 17 00:00:00 2001 From: Hitenjain14 Date: Mon, 18 Mar 2024 15:52:11 +0530 Subject: [PATCH 02/32] increase read and write timeout --- zboxcore/sdk/blockdownloadworker.go | 2 +- zboxcore/sdk/chunked_upload.go | 2 +- zboxcore/zboxutil/http.go | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/zboxcore/sdk/blockdownloadworker.go b/zboxcore/sdk/blockdownloadworker.go index 24a98c017..1ac298641 100644 --- a/zboxcore/sdk/blockdownloadworker.go +++ b/zboxcore/sdk/blockdownloadworker.go @@ -157,7 +157,7 @@ func (req *BlockDownloadRequest) downloadBlobberBlock(hostClient *fasthttp.HostC err = func() error { now := time.Now() - statuscode, respBuf, err := hostClient.GetWithRequestTimeout(httpreq, req.respBuf, 30*time.Second) + statuscode, respBuf, err := hostClient.GetWithRequestTimeout(httpreq, req.respBuf, 45*time.Second) fasthttp.ReleaseRequest(httpreq) timeTaken := time.Since(now).Milliseconds() if err != nil { diff --git a/zboxcore/sdk/chunked_upload.go b/zboxcore/sdk/chunked_upload.go index a8d906074..c9ef5f0ac 100644 --- a/zboxcore/sdk/chunked_upload.go +++ b/zboxcore/sdk/chunked_upload.go @@ -33,7 +33,7 @@ import ( ) const ( - DefaultUploadTimeOut = 45 * time.Second + DefaultUploadTimeOut = 90 * time.Second ) var ( diff --git a/zboxcore/zboxutil/http.go b/zboxcore/zboxutil/http.go index 92e02a004..107dc0b10 100644 --- a/zboxcore/zboxutil/http.go +++ b/zboxcore/zboxutil/http.go @@ -189,8 +189,8 @@ func init() { Concurrency: 4096, DNSCacheDuration: time.Hour, }).Dial, - ReadTimeout: 60 * time.Second, - WriteTimeout: 60 * time.Second, + ReadTimeout: 90 * time.Second, + WriteTimeout: 90 * time.Second, } envProxy.initialize() log.Init(logger.DEBUG, "0box-sdk") From 3a65afa809478d72a9ea8846fa9ebe0f611f576f Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Thu, 21 Mar 2024 23:07:56 +0530 Subject: [PATCH 03/32] Added get version --- wasmsdk/sdk.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/wasmsdk/sdk.go b/wasmsdk/sdk.go index b616d48ee..0521c8857 100644 --- a/wasmsdk/sdk.go +++ b/wasmsdk/sdk.go @@ -49,6 +49,10 @@ func initSDKs(chainID, blockWorker, signatureScheme string, return nil } +func getVersion() string { + return sdk.GetVersion() +} + var sdkLogger *logger.Logger var zcnLogger *logger.Logger var logEnabled = false From bc0662f3334544920cdd3ff2ddf0e191597515f1 Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Fri, 22 Mar 2024 01:06:54 +0530 Subject: [PATCH 04/32] Debug --- wasmsdk/smartcontract.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/wasmsdk/smartcontract.go b/wasmsdk/smartcontract.go index e0421dfb1..9d0b4b06c 100644 --- a/wasmsdk/smartcontract.go +++ b/wasmsdk/smartcontract.go @@ -3,6 +3,7 @@ package main import ( "encoding/json" "fmt" + "github.com/0chain/gosdk/zboxcore/sdk" "sync" "github.com/0chain/gosdk/core/transaction" @@ -13,6 +14,10 @@ func faucet(methodName, input string, token float64) (*transaction.Transaction, return executeSmartContract(zcncore.FaucetSmartContractAddress, methodName, input, zcncore.ConvertToValue(token)) } +func getVersion() string { + return sdk.GetVersion() +} + func executeSmartContract(address, methodName, input string, value uint64) (*transaction.Transaction, error) { wg := &sync.WaitGroup{} cb := &transactionCallback{wg: wg} From 5385f7fc3fe287a1b06af94fc22399668ea53d11 Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Fri, 22 Mar 2024 01:09:17 +0530 Subject: [PATCH 05/32] Debug --- wasmsdk/sdk.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/wasmsdk/sdk.go b/wasmsdk/sdk.go index 0521c8857..044a1b1fd 100644 --- a/wasmsdk/sdk.go +++ b/wasmsdk/sdk.go @@ -49,9 +49,9 @@ func initSDKs(chainID, blockWorker, signatureScheme string, return nil } -func getVersion() string { - return sdk.GetVersion() -} +//func getVersion() string { +// return sdk.GetVersion() +//} var sdkLogger *logger.Logger var zcnLogger *logger.Logger From a66d15d02319422c0a2ac51c68b53c0edb1d9012 Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Fri, 22 Mar 2024 01:15:08 +0530 Subject: [PATCH 06/32] Fix --- wasmsdk/proxy.go | 1 + wasmsdk/sdk.go | 6 +++--- wasmsdk/smartcontract.go | 5 ----- 3 files changed, 4 insertions(+), 8 deletions(-) diff --git a/wasmsdk/proxy.go b/wasmsdk/proxy.go index 7ba983926..697287f4f 100644 --- a/wasmsdk/proxy.go +++ b/wasmsdk/proxy.go @@ -160,6 +160,7 @@ func main() { "showLogs": showLogs, "getUSDRate": getUSDRate, "isWalletID": isWalletID, + "getVersion": getVersion, "getLookupHash": getLookupHash, "createThumbnail": createThumbnail, "makeSCRestAPICall": makeSCRestAPICall, diff --git a/wasmsdk/sdk.go b/wasmsdk/sdk.go index 044a1b1fd..0521c8857 100644 --- a/wasmsdk/sdk.go +++ b/wasmsdk/sdk.go @@ -49,9 +49,9 @@ func initSDKs(chainID, blockWorker, signatureScheme string, return nil } -//func getVersion() string { -// return sdk.GetVersion() -//} +func getVersion() string { + return sdk.GetVersion() +} var sdkLogger *logger.Logger var zcnLogger *logger.Logger diff --git a/wasmsdk/smartcontract.go b/wasmsdk/smartcontract.go index 9d0b4b06c..e0421dfb1 100644 --- a/wasmsdk/smartcontract.go +++ b/wasmsdk/smartcontract.go @@ -3,7 +3,6 @@ package main import ( "encoding/json" "fmt" - "github.com/0chain/gosdk/zboxcore/sdk" "sync" "github.com/0chain/gosdk/core/transaction" @@ -14,10 +13,6 @@ func faucet(methodName, input string, token float64) (*transaction.Transaction, return executeSmartContract(zcncore.FaucetSmartContractAddress, methodName, input, zcncore.ConvertToValue(token)) } -func getVersion() string { - return sdk.GetVersion() -} - func executeSmartContract(address, methodName, input string, value uint64) (*transaction.Transaction, error) { wg := &sync.WaitGroup{} cb := &transactionCallback{wg: wg} From 186f010d0502b5fa38943f96f3ce6c487c764a4f Mon Sep 17 00:00:00 2001 From: Hitenjain14 Date: Fri, 22 Mar 2024 18:10:25 +0530 Subject: [PATCH 07/32] increase buffer size and update package --- go.mod | 2 +- go.sum | 2 ++ zboxcore/zboxutil/util.go | 5 +++-- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index e3ef0afec..309e2b0f9 100644 --- a/go.mod +++ b/go.mod @@ -13,7 +13,7 @@ require ( github.com/google/uuid v1.3.0 github.com/gorilla/handlers v1.5.1 github.com/gorilla/mux v1.8.0 - github.com/h2non/filetype v1.1.3 + github.com/h2non/filetype v1.1.4-0.20231228185113-6469358c2bcb github.com/hashicorp/go-retryablehttp v0.7.2 github.com/hashicorp/golang-lru/v2 v2.0.1 github.com/herumi/bls-go-binary v1.31.0 diff --git a/go.sum b/go.sum index ada166f42..8bc6a4b81 100644 --- a/go.sum +++ b/go.sum @@ -281,6 +281,8 @@ github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWm github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= github.com/h2non/filetype v1.1.3 h1:FKkx9QbD7HR/zjK1Ia5XiBsq9zdLi5Kf3zGyFTAFkGg= github.com/h2non/filetype v1.1.3/go.mod h1:319b3zT68BvV+WRj7cwy856M2ehB3HqNOt6sy1HndBY= +github.com/h2non/filetype v1.1.4-0.20231228185113-6469358c2bcb h1:GlQyMv2C48qmfPItvAXFoyN341Swxp9JNVeUZxnmbJw= +github.com/h2non/filetype v1.1.4-0.20231228185113-6469358c2bcb/go.mod h1:319b3zT68BvV+WRj7cwy856M2ehB3HqNOt6sy1HndBY= github.com/hashicorp/go-bexpr v0.1.10 h1:9kuI5PFotCboP3dkDYFr/wi0gg0QVbSNz5oFRpxn4uE= github.com/hashicorp/go-bexpr v0.1.10/go.mod h1:oxlubA2vC/gFVfX1A6JGp7ls7uCDlfJn732ehYYg+g0= github.com/hashicorp/go-cleanhttp v0.5.2 h1:035FKYIWjmULyFRBKPs8TBQoi0x6d9G4xc9neXJWAZQ= diff --git a/zboxcore/zboxutil/util.go b/zboxcore/zboxutil/util.go index dbff87480..65e3bfd66 100644 --- a/zboxcore/zboxutil/util.go +++ b/zboxcore/zboxutil/util.go @@ -66,13 +66,14 @@ func (b *lazybuf) string() string { } func GetFileContentType(out io.ReadSeeker) (string, error) { - buffer := make([]byte, 261) - _, err := out.Read(buffer) + buffer := make([]byte, 10240) + n, err := out.Read(buffer) defer out.Seek(0, 0) //nolint if err != nil && err != io.EOF { return "", err } + buffer = buffer[:n] kind, _ := filetype.Match(buffer) if kind == filetype.Unknown { From 98b04a94d0f569c048a26700ce7a47b792f68670 Mon Sep 17 00:00:00 2001 From: Hitenjain14 Date: Fri, 22 Mar 2024 18:45:18 +0530 Subject: [PATCH 08/32] run mod tidy --- go.mod | 2 +- go.sum | 8 -------- 2 files changed, 1 insertion(+), 9 deletions(-) diff --git a/go.mod b/go.mod index 309e2b0f9..c3366e95f 100644 --- a/go.mod +++ b/go.mod @@ -46,7 +46,6 @@ require ( require ( github.com/hashicorp/golang-lru v0.5.5-0.20210104140557-80c98217689d github.com/minio/sha256-simd v1.0.1 - github.com/valyala/fasthttp v1.51.0 ) require ( @@ -116,6 +115,7 @@ require ( github.com/tklauser/numcpus v0.6.0 // indirect github.com/urfave/cli/v2 v2.17.2-0.20221006022127-8f469abc00aa // indirect github.com/valyala/bytebufferpool v1.0.0 // indirect + github.com/valyala/fasthttp v1.51.0 // indirect github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect go.dedis.ch/fixbuf v1.0.3 // indirect go.opencensus.io v0.24.0 // indirect diff --git a/go.sum b/go.sum index 8bc6a4b81..ef78fa0a0 100644 --- a/go.sum +++ b/go.sum @@ -279,8 +279,6 @@ github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI= github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So= github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc= github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= -github.com/h2non/filetype v1.1.3 h1:FKkx9QbD7HR/zjK1Ia5XiBsq9zdLi5Kf3zGyFTAFkGg= -github.com/h2non/filetype v1.1.3/go.mod h1:319b3zT68BvV+WRj7cwy856M2ehB3HqNOt6sy1HndBY= github.com/h2non/filetype v1.1.4-0.20231228185113-6469358c2bcb h1:GlQyMv2C48qmfPItvAXFoyN341Swxp9JNVeUZxnmbJw= github.com/h2non/filetype v1.1.4-0.20231228185113-6469358c2bcb/go.mod h1:319b3zT68BvV+WRj7cwy856M2ehB3HqNOt6sy1HndBY= github.com/hashicorp/go-bexpr v0.1.10 h1:9kuI5PFotCboP3dkDYFr/wi0gg0QVbSNz5oFRpxn4uE= @@ -301,12 +299,6 @@ github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4= github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= github.com/herumi/bls-go-binary v1.31.0 h1:L1goQ2tMtGgpXCg5AwHAdJQpLs/pfnWWEc3Wog6OhmI= github.com/herumi/bls-go-binary v1.31.0/go.mod h1:O4Vp1AfR4raRGwFeQpr9X/PQtncEicMoOe6BQt1oX0Y= -github.com/hitenjain14/fasthttp v0.0.0-20240201092245-8e4835c0e974 h1:oEjH9SSKBlzwDyYjzZaqRpxo7GlfUJCyRoOk7QHKSDs= -github.com/hitenjain14/fasthttp v0.0.0-20240201092245-8e4835c0e974/go.mod h1:RZMcXy7u4S+E97IXYTe7WHZ3+mCYOh4vys8PkIGZeXk= -github.com/hitenjain14/fasthttp v0.0.0-20240223160417-7458814cf3d0 h1:XCrNCir8+zDo+Ku7B+AXhzFnsJvSelx1YbQQTNpxtqI= -github.com/hitenjain14/fasthttp v0.0.0-20240223160417-7458814cf3d0/go.mod h1:RZMcXy7u4S+E97IXYTe7WHZ3+mCYOh4vys8PkIGZeXk= -github.com/hitenjain14/fasthttp v0.0.0-20240228200624-800f4e3a9663 h1:OS1ehSzPOz32fw+mgLL6f2dHXweh4sEB62jyLVwT7NI= -github.com/hitenjain14/fasthttp v0.0.0-20240228200624-800f4e3a9663/go.mod h1:RZMcXy7u4S+E97IXYTe7WHZ3+mCYOh4vys8PkIGZeXk= github.com/hitenjain14/fasthttp v0.0.0-20240229173600-722723e15e17 h1:FbyIK0BfvXVZTOxKOe2dlxJqSPSF2ZXOv2Mc7dvS7sc= github.com/hitenjain14/fasthttp v0.0.0-20240229173600-722723e15e17/go.mod h1:RZMcXy7u4S+E97IXYTe7WHZ3+mCYOh4vys8PkIGZeXk= github.com/holiman/bloomfilter/v2 v2.0.3 h1:73e0e/V0tCydx14a0SCYS/EWCxgwLZ18CZcZKVu0fao= From da60ec4aa9380796858f2777db1c352415d9de07 Mon Sep 17 00:00:00 2001 From: Hitenjain14 Date: Fri, 22 Mar 2024 18:54:23 +0530 Subject: [PATCH 09/32] rmv toolchain --- go.mod | 2 -- 1 file changed, 2 deletions(-) diff --git a/go.mod b/go.mod index 6c8595fb0..c1280b8db 100644 --- a/go.mod +++ b/go.mod @@ -2,8 +2,6 @@ module github.com/0chain/gosdk go 1.21 -toolchain go1.21.5 - require ( github.com/0chain/common v0.0.6-0.20230127095721-8df4d1d72565 github.com/0chain/errors v1.0.3 From 22d64399ab12c2010692a0fb49b179c56a36a1f5 Mon Sep 17 00:00:00 2001 From: Hitenjain14 Date: Sun, 24 Mar 2024 15:01:43 +0530 Subject: [PATCH 10/32] increase timeout to 120s --- zboxcore/sdk/chunked_upload.go | 2 +- zboxcore/zboxutil/http.go | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/zboxcore/sdk/chunked_upload.go b/zboxcore/sdk/chunked_upload.go index c9ef5f0ac..f9fb00619 100644 --- a/zboxcore/sdk/chunked_upload.go +++ b/zboxcore/sdk/chunked_upload.go @@ -33,7 +33,7 @@ import ( ) const ( - DefaultUploadTimeOut = 90 * time.Second + DefaultUploadTimeOut = 120 * time.Second ) var ( diff --git a/zboxcore/zboxutil/http.go b/zboxcore/zboxutil/http.go index 107dc0b10..771c60d90 100644 --- a/zboxcore/zboxutil/http.go +++ b/zboxcore/zboxutil/http.go @@ -189,8 +189,8 @@ func init() { Concurrency: 4096, DNSCacheDuration: time.Hour, }).Dial, - ReadTimeout: 90 * time.Second, - WriteTimeout: 90 * time.Second, + ReadTimeout: 120 * time.Second, + WriteTimeout: 120 * time.Second, } envProxy.initialize() log.Init(logger.DEBUG, "0box-sdk") From faf22f7a44c0112bcd85f85abc01b1d3c676c258 Mon Sep 17 00:00:00 2001 From: Hitenjain14 Date: Sun, 24 Mar 2024 20:51:53 +0530 Subject: [PATCH 11/32] clear buffer and map --- zboxcore/sdk/downloadworker.go | 7 +++++++ zboxcore/zboxutil/download_buffer.go | 11 +++++++++++ 2 files changed, 18 insertions(+) diff --git a/zboxcore/sdk/downloadworker.go b/zboxcore/sdk/downloadworker.go index f7eb1f108..b9e51f64c 100644 --- a/zboxcore/sdk/downloadworker.go +++ b/zboxcore/sdk/downloadworker.go @@ -534,6 +534,13 @@ func (req *DownloadRequest) processDownload() { if !req.shouldVerify { var pos uint64 req.bufferMap = make(map[int]zboxutil.DownloadBuffer) + defer func() { + l.Logger.Info("Clearing download buffers: ", len(req.bufferMap)) + for _, rb := range req.bufferMap { + rb.ClearBuffer() + } + clear(req.bufferMap) + }() sz := downloadWorkerCount + EXTRA_COUNT if sz > n { sz = n diff --git a/zboxcore/zboxutil/download_buffer.go b/zboxcore/zboxutil/download_buffer.go index 70694c694..500297395 100644 --- a/zboxcore/zboxutil/download_buffer.go +++ b/zboxcore/zboxutil/download_buffer.go @@ -9,6 +9,7 @@ import ( type DownloadBuffer interface { RequestChunk(ctx context.Context, num int) []byte ReleaseChunk(num int) + ClearBuffer() } type DownloadBufferWithChan struct { @@ -59,6 +60,12 @@ func (r *DownloadBufferWithChan) RequestChunk(ctx context.Context, num int) []by } } +func (r *DownloadBufferWithChan) ClearBuffer() { + r.buf = nil + close(r.ch) + clear(r.mp) +} + type DownloadBufferWithMask struct { buf []byte length int @@ -107,3 +114,7 @@ func (r *DownloadBufferWithMask) ReleaseChunk(num int) { defer r.mu.Unlock() r.mask |= 1 << num } + +func (r *DownloadBufferWithMask) ClearBuffer() { + r.buf = nil +} From 1ca1e3be84998b6cbb219cf3e3792d01d195dacc Mon Sep 17 00:00:00 2001 From: Hitenjain14 Date: Sun, 24 Mar 2024 21:00:14 +0530 Subject: [PATCH 12/32] delete key --- zboxcore/sdk/downloadworker.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/zboxcore/sdk/downloadworker.go b/zboxcore/sdk/downloadworker.go index b9e51f64c..fdd0859fa 100644 --- a/zboxcore/sdk/downloadworker.go +++ b/zboxcore/sdk/downloadworker.go @@ -536,10 +536,11 @@ func (req *DownloadRequest) processDownload() { req.bufferMap = make(map[int]zboxutil.DownloadBuffer) defer func() { l.Logger.Info("Clearing download buffers: ", len(req.bufferMap)) - for _, rb := range req.bufferMap { + for ind, rb := range req.bufferMap { rb.ClearBuffer() + delete(req.bufferMap, ind) } - clear(req.bufferMap) + req.bufferMap = nil }() sz := downloadWorkerCount + EXTRA_COUNT if sz > n { From 4f29732bf82b3e46cee0d4a9d728000cdea3246b Mon Sep 17 00:00:00 2001 From: Hitenjain14 Date: Sun, 24 Mar 2024 21:09:57 +0530 Subject: [PATCH 13/32] use delete instead of clear --- zboxcore/zboxutil/download_buffer.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/zboxcore/zboxutil/download_buffer.go b/zboxcore/zboxutil/download_buffer.go index 500297395..da7ad1913 100644 --- a/zboxcore/zboxutil/download_buffer.go +++ b/zboxcore/zboxutil/download_buffer.go @@ -63,7 +63,10 @@ func (r *DownloadBufferWithChan) RequestChunk(ctx context.Context, num int) []by func (r *DownloadBufferWithChan) ClearBuffer() { r.buf = nil close(r.ch) - clear(r.mp) + for k := range r.mp { + delete(r.mp, k) + } + r.mp = nil } type DownloadBufferWithMask struct { From c1e784ea03e41eabbceda514a3797ef71acf8dc6 Mon Sep 17 00:00:00 2001 From: Hitenjain14 Date: Sun, 24 Mar 2024 23:44:07 +0530 Subject: [PATCH 14/32] lock and set --- zboxcore/zboxutil/download_buffer.go | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/zboxcore/zboxutil/download_buffer.go b/zboxcore/zboxutil/download_buffer.go index da7ad1913..ba4b8c692 100644 --- a/zboxcore/zboxutil/download_buffer.go +++ b/zboxcore/zboxutil/download_buffer.go @@ -74,7 +74,7 @@ type DownloadBufferWithMask struct { length int reqSize int mask uint32 - mu sync.RWMutex + mu sync.Mutex } func NewDownloadBufferWithMask(size, numBlocks, effectiveBlockSize int) *DownloadBufferWithMask { @@ -95,16 +95,15 @@ func (r *DownloadBufferWithMask) RequestChunk(ctx context.Context, num int) []by return nil default: } - r.mu.RLock() + r.mu.Lock() isSet := r.mask & (1 << num) - r.mu.RUnlock() // already assigned if isSet == 0 { + r.mu.Unlock() time.Sleep(500 * time.Millisecond) continue } // assign the chunk by clearing the bit - r.mu.Lock() r.mask &= ^(1 << num) r.mu.Unlock() return r.buf[num*r.reqSize : (num+1)*r.reqSize] From 4c9e73a8d034080faeec54ff2097aa66c8f339ce Mon Sep 17 00:00:00 2001 From: Hitenjain14 Date: Mon, 25 Mar 2024 20:59:40 +0530 Subject: [PATCH 15/32] set workers to 1 in wasm --- zboxcore/sdk/chunked_upload.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/zboxcore/sdk/chunked_upload.go b/zboxcore/sdk/chunked_upload.go index f9fb00619..d7ad05229 100644 --- a/zboxcore/sdk/chunked_upload.go +++ b/zboxcore/sdk/chunked_upload.go @@ -297,6 +297,11 @@ func CreateChunkedUpload( } func calculateWorkersAndRequests(dataShards, totalShards, chunknumber int) (uploadWorkers int, uploadRequests int) { + if IsWasm { + uploadWorkers = 1 + uploadRequests = 2 + return + } if totalShards < 4 { uploadWorkers = 4 } else { From af119d5a405ddd71466bae9bb650d8f365fef651 Mon Sep 17 00:00:00 2001 From: hitenjain14 Date: Tue, 26 Mar 2024 13:27:42 +0530 Subject: [PATCH 16/32] fix release chunk --- zboxcore/sdk/blockdownloadworker.go | 2 +- zboxcore/sdk/download_progress_storer.go | 6 ++++-- zboxcore/sdk/downloadworker.go | 14 +++++++------- 3 files changed, 12 insertions(+), 10 deletions(-) diff --git a/zboxcore/sdk/blockdownloadworker.go b/zboxcore/sdk/blockdownloadworker.go index 1ac298641..d8eeb1672 100644 --- a/zboxcore/sdk/blockdownloadworker.go +++ b/zboxcore/sdk/blockdownloadworker.go @@ -263,7 +263,7 @@ func AddBlockDownloadReq(ctx context.Context, req *BlockDownloadRequest, rb zbox if rb != nil { reqCtx, cncl := context.WithTimeout(ctx, (time.Second * 10)) defer cncl() - req.respBuf = rb.RequestChunk(reqCtx, int(req.blockNum/req.numBlocks)) + req.respBuf = rb.RequestChunk(reqCtx, int(req.blockNum)) if len(req.respBuf) == 0 { req.respBuf = make([]byte, int(req.numBlocks)*effectiveBlockSize) } diff --git a/zboxcore/sdk/download_progress_storer.go b/zboxcore/sdk/download_progress_storer.go index 0ac364496..67b6f1743 100644 --- a/zboxcore/sdk/download_progress_storer.go +++ b/zboxcore/sdk/download_progress_storer.go @@ -14,7 +14,7 @@ import ( type DownloadProgressStorer interface { // Load load download progress by id - Load(id string) *DownloadProgress + Load(id string, numBlocks int) *DownloadProgress // Update download progress Update(writtenBlock int) // Remove remove download progress by id @@ -72,7 +72,7 @@ func (ds *FsDownloadProgressStorer) Start(ctx context.Context) { }() } -func (ds *FsDownloadProgressStorer) Load(progressID string) *DownloadProgress { +func (ds *FsDownloadProgressStorer) Load(progressID string, numBlocks int) *DownloadProgress { dp := &DownloadProgress{} buf, err := sys.Files.ReadFile(progressID) if err != nil { @@ -82,6 +82,8 @@ func (ds *FsDownloadProgressStorer) Load(progressID string) *DownloadProgress { return nil } ds.dp = dp + dp.numBlocks = numBlocks + ds.next = dp.LastWrittenBlock return ds.dp } diff --git a/zboxcore/sdk/downloadworker.go b/zboxcore/sdk/downloadworker.go index f7eb1f108..a924e943e 100644 --- a/zboxcore/sdk/downloadworker.go +++ b/zboxcore/sdk/downloadworker.go @@ -309,7 +309,7 @@ func (req *DownloadRequest) downloadBlock( err.Error(), req.blobbers[result.idx].Baseurl) logger.Logger.Error(err) if req.bufferMap != nil { - req.bufferMap[result.idx].ReleaseChunk(int(req.startBlock / req.numBlocks)) + req.bufferMap[result.idx].ReleaseChunk(int(req.startBlock)) } } else if timeRequest { req.downloadQueue[result.idx].timeTaken = result.timeTaken @@ -603,7 +603,7 @@ func (req *DownloadRequest) processDownload() { hashWg.Wait() } for _, rb := range req.bufferMap { - rb.ReleaseChunk(i) + rb.ReleaseChunk(int(startBlock + int64(i)*numBlocks)) } downloaded = downloaded + totalWritten remainingSize -= int64(totalWritten) @@ -651,7 +651,7 @@ func (req *DownloadRequest) processDownload() { hashWg.Wait() } for _, rb := range req.bufferMap { - rb.ReleaseChunk(i) + rb.ReleaseChunk(int(startBlock + int64(i)*numBlocks)) } downloaded = downloaded + totalWritten @@ -729,7 +729,7 @@ func (req *DownloadRequest) processDownload() { return errors.Wrap(err, fmt.Sprintf("WriteAt failed for block %d. ", startBlock+int64(j)*numBlocks)) } for _, rb := range req.bufferMap { - rb.ReleaseChunk(j) + rb.ReleaseChunk(int(startBlock + int64(j)*numBlocks)) } if req.downloadStorer != nil { go req.downloadStorer.Update(int(startBlock + int64(j)*numBlocks + blocksToDownload)) @@ -1035,17 +1035,17 @@ func (req *DownloadRequest) calculateShardsParams( progressID := req.progressID() var dp *DownloadProgress if info.Size() > 0 { - dp = req.downloadStorer.Load(progressID) + dp = req.downloadStorer.Load(progressID, int(req.numBlocks)) } if dp != nil { req.startBlock = int64(dp.LastWrittenBlock) } else { dp = &DownloadProgress{ - ID: progressID, + ID: progressID, + numBlocks: int(req.numBlocks), } req.downloadStorer.Save(dp) } - dp.numBlocks = int(req.numBlocks) } } From f949a026f2841de034461e7ce59125121ab5a16f Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Wed, 27 Mar 2024 21:36:09 +0530 Subject: [PATCH 17/32] Log multi operation errors --- zboxcore/sdk/allocation.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/zboxcore/sdk/allocation.go b/zboxcore/sdk/allocation.go index cd0f01f6e..de6032cd5 100644 --- a/zboxcore/sdk/allocation.go +++ b/zboxcore/sdk/allocation.go @@ -844,6 +844,11 @@ func (a *Allocation) DoMultiOperation(operations []OperationRequest, opts ...Mul wg.Wait() // Check consensus if mo.operationMask.CountOnes() < mo.consensusThresh { + l.Logger.Error("Multioperation: create connection failed. Required consensus not met", + zap.Int("consensusThresh", mo.consensusThresh), + zap.Int("operationMask", mo.operationMask.CountOnes()), + zap.Any("connectionErrors", connectionErrors)) + majorErr := zboxutil.MajorError(connectionErrors) if majorErr != nil { return errors.New("consensus_not_met", From d62a6599093d303157b5c3912f1216c70e3c7f37 Mon Sep 17 00:00:00 2001 From: Yaroslav Svitlytskyi Date: Fri, 15 Mar 2024 10:50:47 +0100 Subject: [PATCH 18/32] feature: added gas price estimation with the usage of Alchemy provider --- go.mod | 2 ++ wasmsdk/bridge.go | 2 +- zcnbridge/bridge.go | 18 +++++++----------- zcnbridge/bridge_helper.go | 2 +- 4 files changed, 11 insertions(+), 13 deletions(-) diff --git a/go.mod b/go.mod index c1280b8db..8d3c29363 100644 --- a/go.mod +++ b/go.mod @@ -2,6 +2,8 @@ module github.com/0chain/gosdk go 1.21 +toolchain go1.21.0 + require ( github.com/0chain/common v0.0.6-0.20230127095721-8df4d1d72565 github.com/0chain/errors v1.0.3 diff --git a/wasmsdk/bridge.go b/wasmsdk/bridge.go index be70669f5..f70eaf6af 100644 --- a/wasmsdk/bridge.go +++ b/wasmsdk/bridge.go @@ -175,7 +175,7 @@ func getNotProcessedZCNBurnTickets() string { // estimateGasPrice performs gas estimation for the given transaction using Alchemy enhanced API returning // approximate final gas fee. -func estimateGasPrice(from, to string, value int64) string { // nolint:golint,unused +func estimateGasPrice(from, to string, value uint64) string { estimateGasPriceResponse, err := bridge.EstimateGasPrice(context.Background(), from, to, value) if err != nil { return errors.Wrap("estimateGasPrice", "failed to estimate gas price", err).Error() diff --git a/zcnbridge/bridge.go b/zcnbridge/bridge.go index eec3623bc..98fadd0e8 100644 --- a/zcnbridge/bridge.go +++ b/zcnbridge/bridge.go @@ -832,7 +832,7 @@ func (b *BridgeClient) prepareBancor(ctx context.Context, value *big.Int, method } gasLimitUnits = addPercents(gasLimitUnits, 10).Uint64() - + b transactOpts := b.CreateSignedTransactionFromKeyStore(b.ethereumClient, gasLimitUnits) if value.Int64() != 0 { transactOpts.Value = value @@ -1000,12 +1000,12 @@ func (b *BridgeClient) prepareBridge(ctx context.Context, ethereumAddress, metho // isEstimateGasPriceAvailable checks if currently selected ethereum node url can be used for gas estimation. func (b *BridgeClient) isEstimateGasPriceAvailable() bool { - return strings.Contains(b.EthereumNodeURL, "eth-mainnet.g.alchemy.com") + return strings.Contains(b.EthereumNodeURL, "eth-mainnet.g.alchemy.com/v2/") } // EstimateGasPrice performs gas estimation for the given transaction using Alchemy enhanced API returning // approximate final gas fee. -func (b *BridgeClient) EstimateGasPrice(ctx context.Context, from, to string, value int64) (*GasPriceEstimationResult, error) { +func (b *BridgeClient) EstimateGasPrice(ctx context.Context, from, to string, value uint64) (*GasPriceEstimationResult, error) { if !b.isEstimateGasPriceAvailable() { return nil, errors.New("used json-rpc does not allow to estimate gas price") } @@ -1029,13 +1029,11 @@ func (b *BridgeClient) EstimateGasPrice(ctx context.Context, from, to string, va return nil, errors.New("failed to parse gas amount") } - gasAmountInt := new(big.Float) - gasAmountInt.SetString(gasAmountRaw) + gasAmountInt := new(big.Int) + gasAmountInt.SetString(gasAmountRaw, 16) gasAmountFloat, _ := gasAmountInt.Float64() - fmt.Println(gasAmountFloat) - resp, err = client.Call(ctx, "eth_gasPrice") if err != nil { return nil, errors.Wrap(err, "gas price estimation failed") @@ -1051,13 +1049,11 @@ func (b *BridgeClient) EstimateGasPrice(ctx context.Context, from, to string, va return nil, errors.New("failed to parse gas price") } - gasPriceInt := new(big.Float) - gasPriceInt.SetString(gasPriceRaw) + gasPriceInt := new(big.Int) + gasPriceInt.SetString(gasPriceRaw, 16) gasPriceFloat, _ := gasPriceInt.Float64() - fmt.Println(gasPriceFloat) - return &GasPriceEstimationResult{ Value: gasPriceFloat * gasAmountFloat}, nil } diff --git a/zcnbridge/bridge_helper.go b/zcnbridge/bridge_helper.go index 08014f445..dcfb6071d 100644 --- a/zcnbridge/bridge_helper.go +++ b/zcnbridge/bridge_helper.go @@ -88,6 +88,6 @@ func addPercents(gasLimitUnits uint64, percents int) *big.Int { } // ConvertIntToHex converts given int value to hex string. -func ConvertIntToHex(value int64) string { +func ConvertIntToHex(value uint64) string { return fmt.Sprintf("%#x", value) } From 01a27ac82fea0eed1a37d00340d7b79429456a54 Mon Sep 17 00:00:00 2001 From: Yaroslav Svitlytskyi Date: Wed, 27 Mar 2024 14:55:23 +0100 Subject: [PATCH 19/32] feature: implemented gas amount estimation --- wasmsdk/bridge.go | 16 ++++++++ wasmsdk/proxy.go | 1 + zcnbridge/bridge.go | 83 ++++++++++++++++++++++++-------------- zcnbridge/bridge_helper.go | 2 +- zcnbridge/bridge_test.go | 16 ++++++-- zcnbridge/config.go | 6 +++ 6 files changed, 88 insertions(+), 36 deletions(-) diff --git a/wasmsdk/bridge.go b/wasmsdk/bridge.go index f70eaf6af..1d39208a1 100644 --- a/wasmsdk/bridge.go +++ b/wasmsdk/bridge.go @@ -173,6 +173,22 @@ func getNotProcessedZCNBurnTickets() string { return string(result) } +// estimateGasAmount performs gas amount estimation for the given transaction. +func estimateGasAmount(from, to string, value int64) string { // nolint:golint,unused + estimateGasAmountResponse, err := bridge.EstimateGasAmount(context.Background(), from, to, value) + if err != nil { + return errors.Wrap("estimateGasPrice", "failed to estimate gas price", err).Error() + } + + var result []byte + result, err = json.Marshal(estimateGasAmountResponse) + if err != nil { + return errors.Wrap("estimateGasAmount", "failed to marshal gas amount estimation result", err).Error() + } + + return string(result) +} + // estimateGasPrice performs gas estimation for the given transaction using Alchemy enhanced API returning // approximate final gas fee. func estimateGasPrice(from, to string, value uint64) string { diff --git a/wasmsdk/proxy.go b/wasmsdk/proxy.go index 66d88f2af..bfbabaf50 100644 --- a/wasmsdk/proxy.go +++ b/wasmsdk/proxy.go @@ -237,6 +237,7 @@ func main() { "getMintWZCNPayload": getMintWZCNPayload, "getNotProcessedWZCNBurnEvents": getNotProcessedWZCNBurnEvents, "getNotProcessedZCNBurnTickets": getNotProcessedZCNBurnTickets, + "estimateGasAmount": estimateGasAmount, "estimateGasPrice": estimateGasPrice, //zcn diff --git a/zcnbridge/bridge.go b/zcnbridge/bridge.go index 98fadd0e8..e27c8c810 100644 --- a/zcnbridge/bridge.go +++ b/zcnbridge/bridge.go @@ -832,7 +832,7 @@ func (b *BridgeClient) prepareBancor(ctx context.Context, value *big.Int, method } gasLimitUnits = addPercents(gasLimitUnits, 10).Uint64() - b + transactOpts := b.CreateSignedTransactionFromKeyStore(b.ethereumClient, gasLimitUnits) if value.Int64() != 0 { transactOpts.Value = value @@ -998,18 +998,19 @@ func (b *BridgeClient) prepareBridge(ctx context.Context, ethereumAddress, metho return bridgeInstance, transactOpts, nil } -// isEstimateGasPriceAvailable checks if currently selected ethereum node url can be used for gas estimation. -func (b *BridgeClient) isEstimateGasPriceAvailable() bool { - return strings.Contains(b.EthereumNodeURL, "eth-mainnet.g.alchemy.com/v2/") -} - -// EstimateGasPrice performs gas estimation for the given transaction using Alchemy enhanced API returning -// approximate final gas fee. -func (b *BridgeClient) EstimateGasPrice(ctx context.Context, from, to string, value uint64) (*GasPriceEstimationResult, error) { - if !b.isEstimateGasPriceAvailable() { - return nil, errors.New("used json-rpc does not allow to estimate gas price") +// getProviderType validates the provider url and exposes pre-defined type definition. +func (b *BridgeClient) getProviderType() int { + if strings.Contains(b.EthereumNodeURL, "g.alchemy.com") { + return AlchemyProvider + } else if strings.Contains(b.EthereumNodeURL, "rpc.tenderly.co") { + return TenderlyProvider + } else { + return UnknownProvider } +} +// EstimateGasAmount performs gas amount estimation for the given transaction. +func (b *BridgeClient) EstimateGasAmount(ctx context.Context, from, to string, value int64) (float64, error) { client := jsonrpc.NewClient(b.EthereumNodeURL) valueHex := ConvertIntToHex(value) @@ -1017,16 +1018,16 @@ func (b *BridgeClient) EstimateGasPrice(ctx context.Context, from, to string, va resp, err := client.Call(ctx, "eth_estimateGas", &GasEstimationRequest{ From: from, To: to, Value: valueHex}) if err != nil { - return nil, errors.Wrap(err, "gas price estimation failed") + return 0, errors.Wrap(err, "gas price estimation failed") } if resp.Error != nil { - return nil, errors.Wrap(errors.New(resp.Error.Error()), "gas price estimation failed") + return 0, errors.Wrap(errors.New(resp.Error.Error()), "gas price estimation failed") } gasAmountRaw, ok := resp.Result.(string) if !ok { - return nil, errors.New("failed to parse gas amount") + return 0, errors.New("failed to parse gas amount") } gasAmountInt := new(big.Int) @@ -1034,26 +1035,46 @@ func (b *BridgeClient) EstimateGasPrice(ctx context.Context, from, to string, va gasAmountFloat, _ := gasAmountInt.Float64() - resp, err = client.Call(ctx, "eth_gasPrice") - if err != nil { - return nil, errors.Wrap(err, "gas price estimation failed") - } + return gasAmountFloat, nil +} - if resp.Error != nil { - return nil, errors.Wrap(errors.New(resp.Error.Error()), "gas price estimation failed") - } +// EstimateGasPrice performs gas estimation for the given transaction using Alchemy enhanced API returning +// approximate final gas fee. +func (b *BridgeClient) EstimateGasPrice(ctx context.Context, from, to string, value int64) (*GasPriceEstimationResult, error) { + switch b.getProviderType() { + case AlchemyProvider: + gasAmount, err := b.EstimateGasAmount(ctx, from, to, value) + if err != nil { + return nil, err + } - var gasPriceRaw string - gasPriceRaw, ok = resp.Result.(string) - if !ok { - return nil, errors.New("failed to parse gas price") - } + client := jsonrpc.NewClient(b.EthereumNodeURL) - gasPriceInt := new(big.Int) - gasPriceInt.SetString(gasPriceRaw, 16) + var resp *jsonrpc.RPCResponse + resp, err = client.Call(ctx, "eth_gasPrice") + if err != nil { + return nil, errors.Wrap(err, "gas price estimation failed") + } - gasPriceFloat, _ := gasPriceInt.Float64() + if resp.Error != nil { + return nil, errors.Wrap(errors.New(resp.Error.Error()), "gas price estimation failed") + } + + gasPriceRaw, ok := resp.Result.(string) + if !ok { + return nil, errors.New("failed to parse gas price") + } + + gasPriceInt := new(big.Float) + gasPriceInt.SetString(gasPriceRaw) + + gasPriceFloat, _ := gasPriceInt.Float64() + + return &GasPriceEstimationResult{ + Value: gasAmount * gasPriceFloat}, nil + case TenderlyProvider: + return &GasPriceEstimationResult{0}, nil + } - return &GasPriceEstimationResult{ - Value: gasPriceFloat * gasAmountFloat}, nil + return nil, errors.New("used json-rpc does not allow to estimate gas price") } diff --git a/zcnbridge/bridge_helper.go b/zcnbridge/bridge_helper.go index dcfb6071d..08014f445 100644 --- a/zcnbridge/bridge_helper.go +++ b/zcnbridge/bridge_helper.go @@ -88,6 +88,6 @@ func addPercents(gasLimitUnits uint64, percents int) *big.Int { } // ConvertIntToHex converts given int value to hex string. -func ConvertIntToHex(value uint64) string { +func ConvertIntToHex(value int64) string { return fmt.Sprintf("%#x", value) } diff --git a/zcnbridge/bridge_test.go b/zcnbridge/bridge_test.go index 51a750dec..75e06cf7d 100644 --- a/zcnbridge/bridge_test.go +++ b/zcnbridge/bridge_test.go @@ -41,9 +41,10 @@ import ( const ( ethereumAddress = "0xD8c9156e782C68EE671C09b6b92de76C97948432" - alchemyEthereumNodeURL = "https://eth-mainnet.g.alchemy.com/v2/9VanLUbRE0pLmDHwCHGJlhs9GHosrfD9" - infuraEthereumNodeURL = "https://mainnet.infura.io/v3/7238211010344719ad14a89db874158c" - value = 1e+10 + alchemyEthereumNodeURL = "https://eth-mainnet.g.alchemy.com/v2/9VanLUbRE0pLmDHwCHGJlhs9GHosrfD9" + tenderlyEthereumNodeURL = "https://rpc.tenderly.co/fork/835ecb4e-1f60-4129-adc2-b0c741193839" + infuraEthereumNodeURL = "https://mainnet.infura.io/v3/7238211010344719ad14a89db874158c" + value = 1e+10 password = "02289b9" @@ -628,13 +629,20 @@ func Test_ZCNBridge(t *testing.T) { )) }) - t.Run("should check if gas price estimation works with correct ethereum node url", func(t *testing.T) { + t.Run("should check if gas price estimation works with correct alchemy ethereum node url", func(t *testing.T) { bridgeClient = getBridgeClient(bancorMockServerURL, alchemyEthereumNodeURL, ethereumClient, transactionProvider, keyStore) _, err := bridgeClient.EstimateGasPrice(context.Background(), tokenAddress, bridgeAddress, value) require.Contains(t, err.Error(), "Must be authenticated!") }) + t.Run("should check if gas price estimation works with correct tenderly ethereum node url", func(t *testing.T) { + bridgeClient = getBridgeClient(bancorMockServerURL, tenderlyEthereumNodeURL, ethereumClient, transactionProvider, keyStore) + + _, err := bridgeClient.EstimateGasPrice(context.Background(), tokenAddress, bridgeAddress, value) + require.NoError(t, err) + }) + t.Run("should check if gas price estimation works with incorrect ethereum node url", func(t *testing.T) { bridgeClient = getBridgeClient(bancorMockServerURL, infuraEthereumNodeURL, ethereumClient, transactionProvider, keyStore) diff --git a/zcnbridge/config.go b/zcnbridge/config.go index 03ba097ba..f01e3e0aa 100644 --- a/zcnbridge/config.go +++ b/zcnbridge/config.go @@ -15,6 +15,12 @@ import ( "github.com/spf13/viper" ) +const ( + TenderlyProvider = iota + AlchemyProvider + UnknownProvider +) + const ( ZChainsClientConfigName = "config.yaml" ZChainWalletConfigName = "wallet.json" From 979a43b5848c861ff30d55a3ea6d2a78c6b71e60 Mon Sep 17 00:00:00 2001 From: Yaroslav Svitlytskyi Date: Sat, 30 Mar 2024 03:33:32 +0100 Subject: [PATCH 20/32] fix: removed dead method arguments, which were breaking the flow --- go.mod | 2 - wasmsdk/bridge.go | 34 +++++++--- wasmsdk/proxy.go | 3 +- zcnbridge/bridge.go | 136 +++++++++++++++++++++++++++++-------- zcnbridge/bridge_helper.go | 8 +++ zcnbridge/bridge_test.go | 6 +- 6 files changed, 148 insertions(+), 41 deletions(-) diff --git a/go.mod b/go.mod index 8d3c29363..c1280b8db 100644 --- a/go.mod +++ b/go.mod @@ -2,8 +2,6 @@ module github.com/0chain/gosdk go 1.21 -toolchain go1.21.0 - require ( github.com/0chain/common v0.0.6-0.20230127095721-8df4d1d72565 github.com/0chain/errors v1.0.3 diff --git a/wasmsdk/bridge.go b/wasmsdk/bridge.go index 1d39208a1..1a722b961 100644 --- a/wasmsdk/bridge.go +++ b/wasmsdk/bridge.go @@ -173,17 +173,35 @@ func getNotProcessedZCNBurnTickets() string { return string(result) } -// estimateGasAmount performs gas amount estimation for the given transaction. -func estimateGasAmount(from, to string, value int64) string { // nolint:golint,unused - estimateGasAmountResponse, err := bridge.EstimateGasAmount(context.Background(), from, to, value) +// estimateBurnWZCNGasAmount performs gas amount estimation for the given burn wzcn transaction. +func estimateBurnWZCNGasAmount(from, to string, amountTokens int) string { // nolint:golint,unused + estimateBurnWZCNGasAmountResponse, err := bridge.EstimateBurnWZCNGasAmount( + context.Background(), from, to, amountTokens) if err != nil { - return errors.Wrap("estimateGasPrice", "failed to estimate gas price", err).Error() + return errors.Wrap("estimateBurnWZCNGasAmount", "failed to estimate gas amount", err).Error() + } + + var result []byte + result, err = json.Marshal(estimateBurnWZCNGasAmountResponse) + if err != nil { + return errors.Wrap("estimateBurnWZCNGasAmount", "failed to marshal gas amount estimation result", err).Error() + } + + return string(result) +} + +// estimateMintWZCNGasAmount performs gas amount estimation for the given mint wzcn transaction. +func estimateMintWZCNGasAmount(from, to, zcnTransaction string, amountToken, nonce int64, signatures []string) string { // nolint:golint,unused + estimateMintWZCNGasAmountResponse, err := bridge.EstimateMintWZCNGasAmount( + context.Background(), from, to, zcnTransaction, amountToken, nonce, signatures) + if err != nil { + return errors.Wrap("estimateMintWZCNGasAmount", "failed to estimate gas amount", err).Error() } var result []byte - result, err = json.Marshal(estimateGasAmountResponse) + result, err = json.Marshal(estimateMintWZCNGasAmountResponse) if err != nil { - return errors.Wrap("estimateGasAmount", "failed to marshal gas amount estimation result", err).Error() + return errors.Wrap("estimateMintWZCNGasAmount", "failed to marshal gas amount estimation result", err).Error() } return string(result) @@ -191,8 +209,8 @@ func estimateGasAmount(from, to string, value int64) string { // nolint:golint,u // estimateGasPrice performs gas estimation for the given transaction using Alchemy enhanced API returning // approximate final gas fee. -func estimateGasPrice(from, to string, value uint64) string { - estimateGasPriceResponse, err := bridge.EstimateGasPrice(context.Background(), from, to, value) +func estimateGasPrice() string { // nolint:golint,unused + estimateGasPriceResponse, err := bridge.EstimateGasPrice(context.Background()) if err != nil { return errors.Wrap("estimateGasPrice", "failed to estimate gas price", err).Error() } diff --git a/wasmsdk/proxy.go b/wasmsdk/proxy.go index bfbabaf50..301183784 100644 --- a/wasmsdk/proxy.go +++ b/wasmsdk/proxy.go @@ -237,7 +237,8 @@ func main() { "getMintWZCNPayload": getMintWZCNPayload, "getNotProcessedWZCNBurnEvents": getNotProcessedWZCNBurnEvents, "getNotProcessedZCNBurnTickets": getNotProcessedZCNBurnTickets, - "estimateGasAmount": estimateGasAmount, + "estimateBurnWZCNGasAmount": estimateBurnWZCNGasAmount, + "estimateMintWZCNGasAmount": estimateMintWZCNGasAmount, "estimateGasPrice": estimateGasPrice, //zcn diff --git a/zcnbridge/bridge.go b/zcnbridge/bridge.go index e27c8c810..36b5343c8 100644 --- a/zcnbridge/bridge.go +++ b/zcnbridge/bridge.go @@ -1009,14 +1009,22 @@ func (b *BridgeClient) getProviderType() int { } } -// EstimateGasAmount performs gas amount estimation for the given transaction. -func (b *BridgeClient) EstimateGasAmount(ctx context.Context, from, to string, value int64) (float64, error) { +// estimateTenderlyGasAmount performs gas amount estimation for the given transaction using Tenderly provider. +func (b *BridgeClient) estimateTenderlyGasAmount(ctx context.Context, from, to string, value int64) (float64, error) { + return 8000000, nil +} + +// estimateAlchemyGasAmount performs gas amount estimation for the given transaction using Alchemy provider +func (b *BridgeClient) estimateAlchemyGasAmount(ctx context.Context, from, to, data string, value int64) (float64, error) { client := jsonrpc.NewClient(b.EthereumNodeURL) valueHex := ConvertIntToHex(value) - resp, err := client.Call(ctx, "eth_estimateGas", &GasEstimationRequest{ - From: from, To: to, Value: valueHex}) + resp, err := client.Call(ctx, "eth_estimateGas", &AlchemyGasEstimationRequest{ + From: from, + To: to, + Value: valueHex, + Data: data}) if err != nil { return 0, errors.Wrap(err, "gas price estimation failed") } @@ -1030,51 +1038,125 @@ func (b *BridgeClient) EstimateGasAmount(ctx context.Context, from, to string, v return 0, errors.New("failed to parse gas amount") } - gasAmountInt := new(big.Int) - gasAmountInt.SetString(gasAmountRaw, 16) + gasAmountInt := new(big.Float) + gasAmountInt.SetString(gasAmountRaw) gasAmountFloat, _ := gasAmountInt.Float64() return gasAmountFloat, nil } -// EstimateGasPrice performs gas estimation for the given transaction using Alchemy enhanced API returning -// approximate final gas fee. -func (b *BridgeClient) EstimateGasPrice(ctx context.Context, from, to string, value int64) (*GasPriceEstimationResult, error) { +// EstimateBurnWZCNGasAmount performs gas amount estimation for the given wzcn burn transaction. +func (b *BridgeClient) EstimateBurnWZCNGasAmount(ctx context.Context, from, to string, amountTokens int) (float64, error) { switch b.getProviderType() { case AlchemyProvider: - gasAmount, err := b.EstimateGasAmount(ctx, from, to, value) + abi, err := bridge.BridgeMetaData.GetAbi() if err != nil { - return nil, err + return 0, errors.Wrap(err, "failed to get ABI") } - client := jsonrpc.NewClient(b.EthereumNodeURL) + clientID := DefaultClientIDEncoder(zcncore.GetClientWalletID()) - var resp *jsonrpc.RPCResponse - resp, err = client.Call(ctx, "eth_gasPrice") + amount := new(big.Int) + amount.SetInt64(int64(amountTokens)) + + var packRaw []byte + packRaw, err = abi.Pack("burn", amount, clientID) if err != nil { - return nil, errors.Wrap(err, "gas price estimation failed") + return 0, errors.Wrap(err, "failed to pack arguments") } - if resp.Error != nil { - return nil, errors.Wrap(errors.New(resp.Error.Error()), "gas price estimation failed") + pack := "0x" + hex.EncodeToString(packRaw) + + return b.estimateAlchemyGasAmount(ctx, from, to, pack, 0) + case TenderlyProvider: + return b.estimateTenderlyGasAmount(ctx, from, to, 0) + } + + return 0, errors.New("used json-rpc does not allow to estimate gas amount") +} + +// EstimateMintWZCNGasAmount performs gas amount estimation for the given wzcn mint transaction. +func (b *BridgeClient) EstimateMintWZCNGasAmount( + ctx context.Context, from, to, zcnTransactionRaw string, amountToken, nonceRaw int64, signaturesRaw []string) (float64, error) { + switch b.getProviderType() { + case AlchemyProvider: + amount := new(big.Int) + amount.SetInt64(amountToken) + + zcnTransaction := DefaultClientIDEncoder(zcnTransactionRaw) + + nonce := new(big.Int) + nonce.SetInt64(nonceRaw) + + var signatures [][]byte + for _, signature := range signaturesRaw { + signatures = append(signatures, []byte(signature)) } - gasPriceRaw, ok := resp.Result.(string) - if !ok { - return nil, errors.New("failed to parse gas price") + fromRaw := common.HexToAddress(from) + + abi, err := bridge.BridgeMetaData.GetAbi() + if err != nil { + return 0, errors.Wrap(err, "failed to get ABI") } - gasPriceInt := new(big.Float) - gasPriceInt.SetString(gasPriceRaw) + var packRaw []byte + packRaw, err = abi.Pack("mint", fromRaw, amount, zcnTransaction, nonce, signatures) + if err != nil { + return 0, errors.Wrap(err, "failed to pack arguments") + } + + pack := "0x" + hex.EncodeToString(packRaw) + + return b.estimateAlchemyGasAmount(ctx, from, to, pack, 0) + case TenderlyProvider: + return b.estimateTenderlyGasAmount(ctx, from, to, 0) + } + + return 0, errors.New("used json-rpc does not allow to estimate gas amount") +} + +// estimateTenderlyGasPrice performs gas estimation for the given transaction using Tenderly API. +func (b *BridgeClient) estimateTenderlyGasPrice(ctx context.Context) (float64, error) { + return 1, nil +} + +// estimateAlchemyGasPrice performs gas estimation for the given transaction using Alchemy enhanced API returning +// approximate final gas fee. +func (b *BridgeClient) estimateAlchemyGasPrice(ctx context.Context) (float64, error) { + client := jsonrpc.NewClient(b.EthereumNodeURL) - gasPriceFloat, _ := gasPriceInt.Float64() + resp, err := client.Call(ctx, "eth_gasPrice") + if err != nil { + return 0, errors.Wrap(err, "gas price estimation failed") + } - return &GasPriceEstimationResult{ - Value: gasAmount * gasPriceFloat}, nil + if resp.Error != nil { + return 0, errors.Wrap(errors.New(resp.Error.Error()), "gas price estimation failed") + } + + gasPriceRaw, ok := resp.Result.(string) + if !ok { + return 0, errors.New("failed to parse gas price") + } + + gasPriceInt := new(big.Float) + gasPriceInt.SetString(gasPriceRaw) + + gasPriceFloat, _ := gasPriceInt.Float64() + + return gasPriceFloat, nil +} + +// EstimateGasPrice performs gas estimation for the given transaction. +func (b *BridgeClient) EstimateGasPrice(ctx context.Context) (float64, error) { + switch b.getProviderType() { + case AlchemyProvider: + return b.estimateAlchemyGasPrice(ctx) case TenderlyProvider: - return &GasPriceEstimationResult{0}, nil + return b.estimateTenderlyGasPrice(ctx) } - return nil, errors.New("used json-rpc does not allow to estimate gas price") + return 0, errors.New("used json-rpc does not allow to estimate gas price") } diff --git a/zcnbridge/bridge_helper.go b/zcnbridge/bridge_helper.go index 08014f445..8ffb69dcc 100644 --- a/zcnbridge/bridge_helper.go +++ b/zcnbridge/bridge_helper.go @@ -10,6 +10,14 @@ import ( "github.com/pkg/errors" ) +// AlchemyGasEstimationRequest describes request used for Alchemy enhanced JSON-RPC API. +type AlchemyGasEstimationRequest struct { + From string `json:"from"` + To string `json:"to"` + Value string `json:"value"` + Data string `json:"data"` +} + // GasEstimationRequest describes request used for Alchemy enhanced JSON-RPC API. type GasEstimationRequest struct { From string `json:"from"` diff --git a/zcnbridge/bridge_test.go b/zcnbridge/bridge_test.go index 75e06cf7d..93cea8c0d 100644 --- a/zcnbridge/bridge_test.go +++ b/zcnbridge/bridge_test.go @@ -632,21 +632,21 @@ func Test_ZCNBridge(t *testing.T) { t.Run("should check if gas price estimation works with correct alchemy ethereum node url", func(t *testing.T) { bridgeClient = getBridgeClient(bancorMockServerURL, alchemyEthereumNodeURL, ethereumClient, transactionProvider, keyStore) - _, err := bridgeClient.EstimateGasPrice(context.Background(), tokenAddress, bridgeAddress, value) + _, err := bridgeClient.EstimateGasPrice(context.Background()) require.Contains(t, err.Error(), "Must be authenticated!") }) t.Run("should check if gas price estimation works with correct tenderly ethereum node url", func(t *testing.T) { bridgeClient = getBridgeClient(bancorMockServerURL, tenderlyEthereumNodeURL, ethereumClient, transactionProvider, keyStore) - _, err := bridgeClient.EstimateGasPrice(context.Background(), tokenAddress, bridgeAddress, value) + _, err := bridgeClient.EstimateGasPrice(context.Background()) require.NoError(t, err) }) t.Run("should check if gas price estimation works with incorrect ethereum node url", func(t *testing.T) { bridgeClient = getBridgeClient(bancorMockServerURL, infuraEthereumNodeURL, ethereumClient, transactionProvider, keyStore) - _, err := bridgeClient.EstimateGasPrice(context.Background(), tokenAddress, bridgeAddress, value) + _, err := bridgeClient.EstimateGasPrice(context.Background()) require.Error(t, err) }) } From ddb5823e5c55c97ffb0a302fe0741cecd0324d85 Mon Sep 17 00:00:00 2001 From: shahnawaz-creator <117025384+shahnawaz-creator@users.noreply.github.com> Date: Sat, 23 Mar 2024 23:38:46 +0530 Subject: [PATCH 21/32] Update go version --- .github/workflows/build-sdks.yml | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/.github/workflows/build-sdks.yml b/.github/workflows/build-sdks.yml index 2069e90b1..91d194090 100644 --- a/.github/workflows/build-sdks.yml +++ b/.github/workflows/build-sdks.yml @@ -27,7 +27,7 @@ jobs: - name: Set up Go 1.x uses: actions/setup-go@v3 with: - go-version: 1.20.4 + go-version: 1.21.5 - name: Clean build run: make clean-mobilesdk @@ -99,7 +99,7 @@ jobs: - name: Set up Go 1.x uses: actions/setup-go@v3 with: - go-version: 1.20.4 + go-version: 1.21.5 - name: Install deps run: | @@ -202,7 +202,7 @@ jobs: - name: Set up Go 1.x uses: actions/setup-go@v3 with: - go-version: 1.20.4 + go-version: 1.21.5 - name: Clean build run: make clean-mobilesdk @@ -274,7 +274,7 @@ jobs: - name: Set up Go 1.x uses: actions/setup-go@v3 with: - go-version: 1.20.4 + go-version: 1.21.5 - name: Install deps run: | @@ -335,10 +335,10 @@ jobs: name: Build-wasm runs-on: [self-hosted, arc-runner] steps: - - name: Set up Go 1.20 - uses: actions/setup-go@v2 + - name: Set up Go 1.x + uses: actions/setup-go@v3 with: - go-version: ^1.20 + go-version: 1.21.5 - name: Checkout uses: actions/checkout@v2 @@ -349,7 +349,7 @@ jobs: sudo apt-get -y install build-essential nghttp2 libnghttp2-dev libssl-dev wget - name: Build - run: docker run --rm -v $PWD:/gosdk -w /gosdk golang:1.20 make wasm-build + run: docker run --rm -v $PWD:/gosdk -w /gosdk golang:1.21.5 make wasm-build - name: 'Upload Artifact' uses: actions/upload-artifact@v3 From f76f85431444a0fba10f10dfcb7598812424de2f Mon Sep 17 00:00:00 2001 From: shahnawaz-creator <117025384+shahnawaz-creator@users.noreply.github.com> Date: Sat, 23 Mar 2024 23:45:31 +0530 Subject: [PATCH 22/32] Update tests.yml --- .github/workflows/tests.yml | 38 +++++++++++-------------------------- 1 file changed, 11 insertions(+), 27 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 2c25a29f4..f7da96041 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -23,18 +23,10 @@ jobs: sudo apt update -y sudo apt -y install build-essential nghttp2 libnghttp2-dev libssl-dev wget - - name: "Setup Go" - shell: 'script --return --quiet --command "bash {0}"' - run: | - [ -f ./https://go.dev/dl/go1.20.3.linux-amd64.tar.gz ] || wget https://go.dev/dl/go1.20.3.linux-amd64.tar.gz - [ -d /usr/local/go ] && sudo rm -rf /usr/local/go - [ -f /usr/local/bin/go ] && sudo rm -rf /usr/local/bin/go - sudo tar -C /usr/local -xzf ./go1.20.3.linux-amd64.tar.gz - - echo "PATH=$PATH:/usr/local/go/bin" >> $GITHUB_ENV - export PATH=$PATH:/usr/local/go/bin - which go - go env + - name: Set up Go 1.x + uses: actions/setup-go@v3 + with: + go-version: 1.21.5 - uses: actions/checkout@v3 @@ -56,18 +48,10 @@ jobs: sudo apt update -y sudo apt -y install build-essential nghttp2 libnghttp2-dev libssl-dev wget - - name: "Setup Go" - shell: 'script --return --quiet --command "bash {0}"' - run: | - [ -f ./https://go.dev/dl/go1.20.3.linux-amd64.tar.gz ] || wget https://go.dev/dl/go1.20.3.linux-amd64.tar.gz - [ -d /usr/local/go ] && sudo rm -rf /usr/local/go - [ -f /usr/local/bin/go ] && sudo rm -rf /usr/local/bin/go - sudo tar -C /usr/local -xzf ./go1.20.3.linux-amd64.tar.gz - - echo "PATH=$PATH:/usr/local/go/bin" >> $GITHUB_ENV - export PATH=$PATH:/usr/local/go/bin - which go - go env + - name: Set up Go 1.x + uses: actions/setup-go@v3 + with: + go-version: 1.21.5 - name: Install deps run: | @@ -248,10 +232,10 @@ jobs: steps: - uses: actions/checkout@v2 - - name: Setup go 1.20 - uses: actions/setup-go@v2 + - name: Set up Go 1.x + uses: actions/setup-go@v3 with: - go-version: '1.20' # The Go version to download (if necessary) and use. + go-version: 1.21.5 - uses: actions/setup-node@v2 with: From 4e12f45e209e3b98e1aeb917f7072541b7d57781 Mon Sep 17 00:00:00 2001 From: shahnawaz-creator <117025384+shahnawaz-creator@users.noreply.github.com> Date: Sun, 24 Mar 2024 15:40:35 +0530 Subject: [PATCH 23/32] updated golint version latest tests.yml --- .github/workflows/tests.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index f7da96041..2d067666e 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -33,7 +33,7 @@ jobs: - name: golangci-lint uses: golangci/golangci-lint-action@v3 with: - version: v1.52.2 + version: v1.57.1 skip-build-cache: true skip-pkg-cache: true only-new-issues: true From 404424b454270f6b0600e268fbb17a9ba1019539 Mon Sep 17 00:00:00 2001 From: shahnawaz-creator <117025384+shahnawaz-creator@users.noreply.github.com> Date: Sun, 24 Mar 2024 15:52:31 +0530 Subject: [PATCH 24/32] updated node version --- .github/workflows/tests.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 2d067666e..233217c20 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -239,7 +239,7 @@ jobs: - uses: actions/setup-node@v2 with: - node-version: '14' + node-version: 20.11.1 - name: Setup PATH for wasm run: echo "${{env.GOROOT}}/misc/wasm" >> $GITHUB_PATH From c6d6186063a3792b31137173f91a146e39e4c3fc Mon Sep 17 00:00:00 2001 From: Yaroslav Svitlytskyi Date: Tue, 2 Apr 2024 20:54:03 +0200 Subject: [PATCH 25/32] hotfix: fixed mint signature decoding --- zcnbridge/bridge.go | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/zcnbridge/bridge.go b/zcnbridge/bridge.go index 36b5343c8..a70520e62 100644 --- a/zcnbridge/bridge.go +++ b/zcnbridge/bridge.go @@ -1089,9 +1089,23 @@ func (b *BridgeClient) EstimateMintWZCNGasAmount( nonce := new(big.Int) nonce.SetInt64(nonceRaw) + var ( + signatureRaw string + + signatureHex []byte + err error + ) + var signatures [][]byte for _, signature := range signaturesRaw { - signatures = append(signatures, []byte(signature)) + signatureRaw, _ = strings.CutPrefix(signature, "0x") + + signatureHex, err = hex.DecodeString(signatureRaw) + if err != nil { + return 0, err + } + + signatures = append(signatures, signatureHex) } fromRaw := common.HexToAddress(from) From 4ecae004973676c3663979bc87c647697929b658 Mon Sep 17 00:00:00 2001 From: Hitenjain14 Date: Wed, 3 Apr 2024 12:54:25 +0530 Subject: [PATCH 26/32] fix download and mask index --- zboxcore/sdk/downloadworker.go | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/zboxcore/sdk/downloadworker.go b/zboxcore/sdk/downloadworker.go index 2d0d57d8b..1e9907184 100644 --- a/zboxcore/sdk/downloadworker.go +++ b/zboxcore/sdk/downloadworker.go @@ -303,16 +303,21 @@ func (req *DownloadRequest) downloadBlock( var err error defer func() { if err != nil { - atomic.AddInt32(&failed, 1) - req.removeFromMask(uint64(result.maskIdx)) + totalFail := atomic.AddInt32(&failed, 1) + // if first request remove from end as we will convert the slice into heap + if timeRequest { + req.removeFromMask(uint64(activeBlobbers - int(totalFail))) + } else { + req.removeFromMask(uint64(result.maskIdx)) + } downloadErrors[i] = fmt.Sprintf("Error %s from %s", err.Error(), req.blobbers[result.idx].Baseurl) logger.Logger.Error(err) - if req.bufferMap != nil { + if req.bufferMap != nil && req.bufferMap[result.idx] != nil { req.bufferMap[result.idx].ReleaseChunk(int(req.startBlock)) } } else if timeRequest { - req.downloadQueue[result.idx].timeTaken = result.timeTaken + req.downloadQueue[result.maskIdx].timeTaken = result.timeTaken } wg.Done() }() @@ -548,7 +553,7 @@ func (req *DownloadRequest) processDownload() { } for i := req.downloadMask; !i.Equals64(0); i = i.And(zboxutil.NewUint128(1).Lsh(pos).Not()) { pos = uint64(i.TrailingZeros()) - blobberIdx := req.downloadQueue[pos].blobberIdx + blobberIdx := int(pos) if writerAt { req.bufferMap[blobberIdx] = zboxutil.NewDownloadBufferWithChan(sz, int(numBlocks), req.effectiveBlockSize) } else { @@ -556,6 +561,9 @@ func (req *DownloadRequest) processDownload() { } } } + // reset mask to number of active blobbers, not it denotes index of download queue and not blobber index + activeBlobbers := req.downloadMask.CountOnes() + req.downloadMask = zboxutil.NewUint128(1).Lsh(uint64(activeBlobbers)).Sub64(1) logger.Logger.Info( fmt.Sprintf("Downloading file with size: %d from start block: %d and end block: %d. "+ From da2bd0e7cf5a3b5fba816874b64736dc996478fc Mon Sep 17 00:00:00 2001 From: Yaroslav Svitlytskyi Date: Thu, 4 Apr 2024 03:10:08 +0200 Subject: [PATCH 27/32] hotfix: fixed mint wzcn gas estimation decoding --- wasmsdk/bridge.go | 21 +++++++++++++++++++-- zcnbridge/bridge.go | 23 ++--------------------- 2 files changed, 21 insertions(+), 23 deletions(-) diff --git a/wasmsdk/bridge.go b/wasmsdk/bridge.go index 1a722b961..b50652216 100644 --- a/wasmsdk/bridge.go +++ b/wasmsdk/bridge.go @@ -2,6 +2,7 @@ package main import ( "context" + "encoding/base64" "encoding/json" "path" "strconv" @@ -191,9 +192,25 @@ func estimateBurnWZCNGasAmount(from, to string, amountTokens int) string { // no } // estimateMintWZCNGasAmount performs gas amount estimation for the given mint wzcn transaction. -func estimateMintWZCNGasAmount(from, to, zcnTransaction string, amountToken, nonce int64, signatures []string) string { // nolint:golint,unused +func estimateMintWZCNGasAmount(from, to, zcnTransaction string, amountToken, nonce int64, signaturesRaw []string) string { // nolint:golint,unused + var signaturesBytes [][]byte + + var ( + signatureBytes []byte + err error + ) + + for _, signature := range signaturesRaw { + signatureBytes, err = base64.StdEncoding.DecodeString(signature) + if err != nil { + return errors.Wrap("estimateMintWZCNGasAmount", "failed to convert raw signature into bytes", err).Error() + } + + signaturesBytes = append(signaturesBytes, signatureBytes) + } + estimateMintWZCNGasAmountResponse, err := bridge.EstimateMintWZCNGasAmount( - context.Background(), from, to, zcnTransaction, amountToken, nonce, signatures) + context.Background(), from, to, zcnTransaction, amountToken, nonce, signaturesBytes) if err != nil { return errors.Wrap("estimateMintWZCNGasAmount", "failed to estimate gas amount", err).Error() } diff --git a/zcnbridge/bridge.go b/zcnbridge/bridge.go index a70520e62..acb78e214 100644 --- a/zcnbridge/bridge.go +++ b/zcnbridge/bridge.go @@ -1078,7 +1078,7 @@ func (b *BridgeClient) EstimateBurnWZCNGasAmount(ctx context.Context, from, to s // EstimateMintWZCNGasAmount performs gas amount estimation for the given wzcn mint transaction. func (b *BridgeClient) EstimateMintWZCNGasAmount( - ctx context.Context, from, to, zcnTransactionRaw string, amountToken, nonceRaw int64, signaturesRaw []string) (float64, error) { + ctx context.Context, from, to, zcnTransactionRaw string, amountToken, nonceRaw int64, signaturesRaw [][]byte) (float64, error) { switch b.getProviderType() { case AlchemyProvider: amount := new(big.Int) @@ -1089,25 +1089,6 @@ func (b *BridgeClient) EstimateMintWZCNGasAmount( nonce := new(big.Int) nonce.SetInt64(nonceRaw) - var ( - signatureRaw string - - signatureHex []byte - err error - ) - - var signatures [][]byte - for _, signature := range signaturesRaw { - signatureRaw, _ = strings.CutPrefix(signature, "0x") - - signatureHex, err = hex.DecodeString(signatureRaw) - if err != nil { - return 0, err - } - - signatures = append(signatures, signatureHex) - } - fromRaw := common.HexToAddress(from) abi, err := bridge.BridgeMetaData.GetAbi() @@ -1116,7 +1097,7 @@ func (b *BridgeClient) EstimateMintWZCNGasAmount( } var packRaw []byte - packRaw, err = abi.Pack("mint", fromRaw, amount, zcnTransaction, nonce, signatures) + packRaw, err = abi.Pack("mint", fromRaw, amount, zcnTransaction, nonce, signaturesRaw) if err != nil { return 0, errors.Wrap(err, "failed to pack arguments") } From 3dfcba754529c0ce44ad5f07b3d11700a5051c5e Mon Sep 17 00:00:00 2001 From: Hitenjain14 Date: Fri, 5 Apr 2024 12:24:30 +0530 Subject: [PATCH 28/32] init queue after meta result --- zboxcore/sdk/blockdownloadworker.go | 2 ++ zboxcore/sdk/downloadworker.go | 1 + 2 files changed, 3 insertions(+) diff --git a/zboxcore/sdk/blockdownloadworker.go b/zboxcore/sdk/blockdownloadworker.go index d8eeb1672..e85d02ec2 100644 --- a/zboxcore/sdk/blockdownloadworker.go +++ b/zboxcore/sdk/blockdownloadworker.go @@ -267,6 +267,8 @@ func AddBlockDownloadReq(ctx context.Context, req *BlockDownloadRequest, rb zbox if len(req.respBuf) == 0 { req.respBuf = make([]byte, int(req.numBlocks)*effectiveBlockSize) } + } else { + req.respBuf = make([]byte, int(req.numBlocks)*effectiveBlockSize) } downloadBlockChan[req.blobber.ID] <- req } diff --git a/zboxcore/sdk/downloadworker.go b/zboxcore/sdk/downloadworker.go index 1e9907184..4675ce4c8 100644 --- a/zboxcore/sdk/downloadworker.go +++ b/zboxcore/sdk/downloadworker.go @@ -1244,6 +1244,7 @@ func (req *DownloadRequest) getFileMetaConsensus(fMetaResp []*fileMetaResponse) return nil, fmt.Errorf("consensus_not_met") } req.downloadMask = foundMask + heap.Init(&req.downloadQueue) return selected.fileref, nil } From edac98e557fb2de57f84bd34cf1fb7aeeb191e43 Mon Sep 17 00:00:00 2001 From: hitenjain14 Date: Sat, 6 Apr 2024 19:20:01 +0530 Subject: [PATCH 29/32] changes for benchmark --- zboxcore/sdk/allocation.go | 4 +++- zboxcore/sdk/blockdownloadworker.go | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/zboxcore/sdk/allocation.go b/zboxcore/sdk/allocation.go index cd0f01f6e..bbd751d9a 100644 --- a/zboxcore/sdk/allocation.go +++ b/zboxcore/sdk/allocation.go @@ -1048,7 +1048,9 @@ func (a *Allocation) DownloadThumbnail(localPath string, remotePath string, veri } err = a.addAndGenerateDownloadRequest(f, remotePath, DOWNLOAD_CONTENT_THUMB, 1, 0, - numBlockDownloads, verifyDownload, status, isFinal, localFilePath) + numBlockDownloads, verifyDownload, status, isFinal, localFilePath, WithFileCallback(func() { + f.Close() //nolint: errcheck + })) if err != nil { if !toKeep { os.Remove(localFilePath) //nolint: errcheck diff --git a/zboxcore/sdk/blockdownloadworker.go b/zboxcore/sdk/blockdownloadworker.go index 1ac298641..dacf35109 100644 --- a/zboxcore/sdk/blockdownloadworker.go +++ b/zboxcore/sdk/blockdownloadworker.go @@ -157,7 +157,7 @@ func (req *BlockDownloadRequest) downloadBlobberBlock(hostClient *fasthttp.HostC err = func() error { now := time.Now() - statuscode, respBuf, err := hostClient.GetWithRequestTimeout(httpreq, req.respBuf, 45*time.Second) + statuscode, respBuf, err := hostClient.GetWithRequest(httpreq, req.respBuf) fasthttp.ReleaseRequest(httpreq) timeTaken := time.Since(now).Milliseconds() if err != nil { From 90a0e78efa7011d0aedcc2b511ee1c8f06b9db5c Mon Sep 17 00:00:00 2001 From: hitenjain14 Date: Sat, 6 Apr 2024 19:35:14 +0530 Subject: [PATCH 30/32] initialize buf for form --- zboxcore/sdk/chunked_upload_form_builder.go | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/zboxcore/sdk/chunked_upload_form_builder.go b/zboxcore/sdk/chunked_upload_form_builder.go index f3ef02265..95a948fd8 100644 --- a/zboxcore/sdk/chunked_upload_form_builder.go +++ b/zboxcore/sdk/chunked_upload_form_builder.go @@ -92,7 +92,15 @@ func (b *chunkedUploadFormBuilder) Build( for i := 0; i < numBodies; i++ { - body := new(bytes.Buffer) + startRange := i * MAX_BLOCKS + endRange := startRange + MAX_BLOCKS + if endRange > len(fileChunksData) { + endRange = len(fileChunksData) + } + + bodyBuf := make([]byte, 0, endRange-startRange+1024) + + body := bytes.NewBuffer(bodyBuf) formWriter := multipart.NewWriter(body) defer formWriter.Close() @@ -101,11 +109,6 @@ func (b *chunkedUploadFormBuilder) Build( return res, err } - startRange := i * MAX_BLOCKS - endRange := startRange + MAX_BLOCKS - if endRange > len(fileChunksData) { - endRange = len(fileChunksData) - } for _, chunkBytes := range fileChunksData[startRange:endRange] { _, err = uploadFile.Write(chunkBytes) if err != nil { From b5deca4077ad02956fc354227c53c4883723108d Mon Sep 17 00:00:00 2001 From: hitenjain14 Date: Sat, 6 Apr 2024 19:42:44 +0530 Subject: [PATCH 31/32] fix buf cap --- zboxcore/sdk/chunked_upload_form_builder.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/zboxcore/sdk/chunked_upload_form_builder.go b/zboxcore/sdk/chunked_upload_form_builder.go index 95a948fd8..94cb956f5 100644 --- a/zboxcore/sdk/chunked_upload_form_builder.go +++ b/zboxcore/sdk/chunked_upload_form_builder.go @@ -98,7 +98,7 @@ func (b *chunkedUploadFormBuilder) Build( endRange = len(fileChunksData) } - bodyBuf := make([]byte, 0, endRange-startRange+1024) + bodyBuf := make([]byte, 0, (CHUNK_SIZE*(endRange-startRange))+1024) body := bytes.NewBuffer(bodyBuf) formWriter := multipart.NewWriter(body) From 6e597b309a10caa0f8afbceff790029e02ec45ab Mon Sep 17 00:00:00 2001 From: Hitenjain14 Date: Thu, 11 Apr 2024 01:20:12 +0530 Subject: [PATCH 32/32] set object mimetype --- wasmsdk/statusbar.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wasmsdk/statusbar.go b/wasmsdk/statusbar.go index 47455a456..d07ad5e19 100644 --- a/wasmsdk/statusbar.go +++ b/wasmsdk/statusbar.go @@ -68,7 +68,7 @@ func (s *StatusBar) Completed(allocationID, filePath string, filename string, mi if s.localPath != "" { fs, _ := sys.Files.Open(s.localPath) mf, _ := fs.(*sys.MemFile) - s.objURL = CreateObjectURL(mf.Buffer, "application/octet-stream") + s.objURL = CreateObjectURL(mf.Buffer, mimetype) } if s.callback != nil { jsCallbackMutex.Lock()