Skip to content
This repository has been archived by the owner on May 11, 2024. It is now read-only.

Commit

Permalink
fix(rpc): fix a bug / update logic (#501)
Browse files Browse the repository at this point in the history
Co-authored-by: David <[email protected]>
  • Loading branch information
mask-pp and davidtaikocha committed Jan 14, 2024
1 parent 75f19a2 commit c474956
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 20 deletions.
8 changes: 8 additions & 0 deletions common/utils/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package utils

import "github.com/modern-go/reflect2"

// IsNil checks if the interface is empty.
func IsNil(i interface{}) bool {
return i == nil || reflect2.IsNil(i)
}
1 change: 1 addition & 0 deletions driver/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ func (d *Driver) Start() error {

// Close closes the driver instance.
func (d *Driver) Close(ctx context.Context) {
d.l1HeadSub.Unsubscribe()
d.state.Close()
d.wg.Wait()
}
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require (
github.com/ethereum/go-ethereum v1.13.8
github.com/go-resty/resty/v2 v2.7.0
github.com/labstack/echo/v4 v4.11.1
github.com/modern-go/reflect2 v1.0.2
github.com/phayes/freeport v0.0.0-20220201140144-74d24b5ae9f5
github.com/prysmaticlabs/prysm/v4 v4.0.1
github.com/stretchr/testify v1.8.4
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,8 @@ github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJ
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 h1:RWengNIwukTxcDr9M+97sNutRR1RKhG96O6jWumTTnw=
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826/go.mod h1:TaXosZuwdSHYgviHp1DAtfrULt5eUgsSMsZf+YrPgl8=
github.com/moul/http2curl v1.0.0/go.mod h1:8UbvGypXm98wA/IqH45anm5Y2Z6ep6O31QGOAZ3H0fQ=
Expand Down
24 changes: 10 additions & 14 deletions pkg/rpc/methods.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,21 +345,17 @@ func (c *Client) GetProtocolStateVariables(opts *bind.CallOpts) (*struct {
A bindings.TaikoDataSlotA
B bindings.TaikoDataSlotB
}, error) {
var (
ctxWithTimeout context.Context
cancel context.CancelFunc
)
if opts != nil && opts.Context != nil {
if _, ok := opts.Context.Deadline(); !ok {
ctxWithTimeout, cancel = context.WithTimeout(opts.Context, defaultWaitReceiptTimeout)
defer cancel()
opts.Context = ctxWithTimeout
}
} else {
ctxWithTimeout, cancel = context.WithTimeout(context.Background(), defaultWaitReceiptTimeout)
defer cancel()
opts = &bind.CallOpts{Context: ctxWithTimeout}
if opts == nil {
opts = &bind.CallOpts{}
}

var ctx = context.Background()
if opts.Context != nil {
ctx = opts.Context
}
ctxWithTimeout, cancel := context.WithTimeout(ctx, defaultWaitReceiptTimeout)
defer cancel()
opts.Context = ctxWithTimeout

return GetProtocolStateVariables(c.TaikoL1, opts)
}
Expand Down
13 changes: 7 additions & 6 deletions pkg/rpc/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ import (
"github.com/ethereum/go-ethereum/ethclient/gethclient"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/rpc"

"github.com/taikoxyz/taiko-client/bindings"
"github.com/taikoxyz/taiko-client/bindings/encoding"
"github.com/taikoxyz/taiko-client/common/utils"
)

var (
Expand Down Expand Up @@ -377,13 +379,12 @@ func IsArchiveNode(ctx context.Context, client *EthClient, l2GenesisHeight uint6
// and otherwise returns the context as passed in. cancel func is always set to an empty function
// so is safe to defer the cancel.
func ctxWithTimeoutOrDefault(ctx context.Context, defaultTimeout time.Duration) (context.Context, context.CancelFunc) {
var (
ctxWithTimeout = ctx
cancel context.CancelFunc = func() {}
)
if utils.IsNil(ctx) {
return context.WithTimeout(context.Background(), defaultTimeout)
}
if _, ok := ctx.Deadline(); !ok {
ctxWithTimeout, cancel = context.WithTimeout(ctx, defaultTimeout)
return context.WithTimeout(ctx, defaultTimeout)
}

return ctxWithTimeout, cancel
return ctx, func() {}
}

0 comments on commit c474956

Please sign in to comment.