Skip to content

Commit

Permalink
fix: updated go-cnc to match celetia light node v0.11 (dymensionxyz#400)
Browse files Browse the repository at this point in the history
  • Loading branch information
omritoptix authored Jul 20, 2023
1 parent d690b09 commit a67e445
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 46 deletions.
8 changes: 4 additions & 4 deletions da/celestia/celestia.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ import (
)

type CNCClientI interface {
SubmitPFD(ctx context.Context, namespaceID [8]byte, blob []byte, fee int64, gasLimit uint64) (*cnc.TxResponse, error)
NamespacedShares(ctx context.Context, namespaceID [8]byte, height uint64) ([][]byte, error)
NamespacedData(ctx context.Context, namespaceID [8]byte, height uint64) ([][]byte, error)
SubmitPFB(ctx context.Context, namespaceID cnc.Namespace, blob []byte, fee int64, gasLimit uint64) (*cnc.TxResponse, error)
NamespacedShares(ctx context.Context, namespaceID cnc.Namespace, height uint64) ([][]byte, error)
NamespacedData(ctx context.Context, namespaceID cnc.Namespace, height uint64) ([][]byte, error)
}

// DataAvailabilityLayerClient use celestia-node public API.
Expand Down Expand Up @@ -159,7 +159,7 @@ func (c *DataAvailabilityLayerClient) SubmitBatch(batch *types.Batch) da.ResultS
c.logger.Debug("Context cancelled")
return da.ResultSubmitBatch{}
default:
txResponse, err := c.client.SubmitPFD(c.ctx, c.config.NamespaceID, blob, c.config.Fee, c.config.GasLimit)
txResponse, err := c.client.SubmitPFB(c.ctx, c.config.NamespaceID, blob, c.config.Fee, c.config.GasLimit)
if txResponse != nil {
if txResponse.Code != 0 {
c.logger.Debug("Failed to submit DA batch. Emitting health event and trying again", "txResponse", txResponse, "error", err)
Expand Down
40 changes: 20 additions & 20 deletions da/celestia/celestia_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
)

const (
submitPFDFuncName = "SubmitPFD"
submitPFBFuncName = "SubmitPFB"
TxFuncName = "Tx"
)

Expand All @@ -39,59 +39,59 @@ func TestSubmitBatch(t *testing.T) {
}
cases := []struct {
name string
submitPFDReturn []interface{}
submitPFBReturn []interface{}
sumbitPFDRun func(args mock.Arguments)
TxFnReturn []interface{}
TxFnRun func(args mock.Arguments)
isSubmitBatchAsync bool
expectedSubmitPFDMinCalls int
expectedSubmitPFBMinCalls int
expectedInclusionHeight int
expectedHealthEvent *da.EventDataDAHealthStatus
}{
{
name: "TestSubmitPFDResponseNil",
submitPFDReturn: []interface{}{nil, nil},
name: "TestSubmitPFBResponseNil",
submitPFBReturn: []interface{}{nil, nil},
sumbitPFDRun: func(args mock.Arguments) { time.Sleep(10 * time.Millisecond) },
isSubmitBatchAsync: true,
expectedSubmitPFDMinCalls: 2,
expectedSubmitPFBMinCalls: 2,
expectedHealthEvent: &da.EventDataDAHealthStatus{Healthy: false},
},
{
name: "TestSubmitPFDResponseCodeSuccess",
submitPFDReturn: []interface{}{&cnc.TxResponse{Code: 0, Height: int64(143)}, nil},
name: "TestSubmitPFBResponseCodeSuccess",
submitPFBReturn: []interface{}{&cnc.TxResponse{Code: 0, Height: int64(143)}, nil},
sumbitPFDRun: func(args mock.Arguments) { time.Sleep(10 * time.Millisecond) },
isSubmitBatchAsync: false,
expectedSubmitPFDMinCalls: 1,
expectedSubmitPFBMinCalls: 1,
expectedInclusionHeight: 143,
expectedHealthEvent: &da.EventDataDAHealthStatus{Healthy: true},
},
{
name: "TestSubmitPFDResponseCodeFailure",
submitPFDReturn: []interface{}{&cnc.TxResponse{Code: 1}, nil},
name: "TestSubmitPFBResponseCodeFailure",
submitPFBReturn: []interface{}{&cnc.TxResponse{Code: 1}, nil},
sumbitPFDRun: func(args mock.Arguments) { time.Sleep(10 * time.Millisecond) },
isSubmitBatchAsync: true,
expectedSubmitPFDMinCalls: 2,
expectedSubmitPFBMinCalls: 2,
expectedHealthEvent: &da.EventDataDAHealthStatus{Healthy: false},
},
{
name: "TestSubmitPFDDelayedInclusion",
submitPFDReturn: []interface{}{&cnc.TxResponse{TxHash: "1234"}, errors.New("timeout")},
name: "TestSubmitPFBDelayedInclusion",
submitPFBReturn: []interface{}{&cnc.TxResponse{TxHash: "1234"}, errors.New("timeout")},
sumbitPFDRun: func(args mock.Arguments) { time.Sleep(10 * time.Millisecond) },
TxFnReturn: []interface{}{&coretypes.ResultTx{Hash: bytes.HexBytes("1234"), Height: int64(145)}, nil},
TxFnRun: func(args mock.Arguments) { time.Sleep(10 * time.Millisecond) },
isSubmitBatchAsync: false,
expectedSubmitPFDMinCalls: 1,
expectedSubmitPFBMinCalls: 1,
expectedInclusionHeight: 145,
expectedHealthEvent: &da.EventDataDAHealthStatus{Healthy: true},
},
{
name: "TestSubmitPFDDelayedInclusionTxNotFound",
submitPFDReturn: []interface{}{&cnc.TxResponse{TxHash: "1234"}, errors.New("timeout")},
name: "TestSubmitPFBDelayedInclusionTxNotFound",
submitPFBReturn: []interface{}{&cnc.TxResponse{TxHash: "1234"}, errors.New("timeout")},
sumbitPFDRun: func(args mock.Arguments) { time.Sleep(10 * time.Millisecond) },
TxFnReturn: []interface{}{nil, errors.New("notFound")},
TxFnRun: func(args mock.Arguments) { time.Sleep(10 * time.Millisecond) },
isSubmitBatchAsync: true,
expectedSubmitPFDMinCalls: 2,
expectedSubmitPFBMinCalls: 2,
expectedHealthEvent: &da.EventDataDAHealthStatus{Healthy: false},
},
}
Expand Down Expand Up @@ -119,7 +119,7 @@ func TestSubmitBatch(t *testing.T) {
err = dalc.Start()
require.NoError(err)
// Set the mock functions
mockCNCClient.On(submitPFDFuncName, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(tc.submitPFDReturn...).Run(tc.sumbitPFDRun)
mockCNCClient.On(submitPFBFuncName, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(tc.submitPFBReturn...).Run(tc.sumbitPFDRun)
rpcmockClient.On(TxFuncName, mock.Anything, mock.Anything, mock.Anything).Return(tc.TxFnReturn...).Run(tc.TxFnRun)
if tc.isSubmitBatchAsync {
go dalc.SubmitBatch(batch)
Expand Down Expand Up @@ -148,6 +148,6 @@ func TestSubmitBatch(t *testing.T) {
// Wait for the goroutines to finish before accessing the mock calls
time.Sleep(3 * time.Second)
t.Log("Verifying mock calls")
assert.GreaterOrEqual(testutil.CountMockCalls(mockCNCClient.Calls, submitPFDFuncName), tc.expectedSubmitPFDMinCalls)
assert.GreaterOrEqual(testutil.CountMockCalls(mockCNCClient.Calls, submitPFBFuncName), tc.expectedSubmitPFBMinCalls)
}
}
17 changes: 14 additions & 3 deletions da/celestia/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@ package celestia
import (
"encoding/hex"
"time"

"github.com/celestiaorg/go-cnc"
)

const (
defaultTxPollingRetryDelay = 20 * time.Second
defaultSubmitRetryDelay = 10 * time.Second
defaultTxPollingAttempts = 5
namespaceVersion = 0
)

// Config stores Celestia DALC configuration parameters.
Expand All @@ -19,7 +22,7 @@ type Config struct {
Fee int64 `json:"fee"`
GasLimit uint64 `json:"gas_limit"`
NamespaceIDStr string `json:"namespace_id"`
NamespaceID [8]byte `json:"-"`
NamespaceID cnc.Namespace `json:"-"`
}

var CelestiaDefaultConfig = Config{
Expand All @@ -29,7 +32,7 @@ var CelestiaDefaultConfig = Config{
Fee: 20000,
GasLimit: 20000000,
NamespaceIDStr: "000000000000ffff",
NamespaceID: [8]byte{0, 0, 0, 0, 0, 0, 255, 255},
NamespaceID: cnc.Namespace{Version: namespaceVersion, ID: []byte{0, 0, 0, 0, 0, 0, 255, 255}},
}

func (c *Config) InitNamespaceID() error {
Expand All @@ -38,6 +41,14 @@ func (c *Config) InitNamespaceID() error {
if err != nil {
return err
}
copy(c.NamespaceID[:], namespaceBytes)
// TODO(omritoptix): a hack. need to enforce in the config
if len(namespaceBytes) != cnc.NamespaceIDSize {
// pad namespaceBytes with 0s
namespaceBytes = append(make([]byte, cnc.NamespaceIDSize-len(namespaceBytes)), namespaceBytes...)
}
c.NamespaceID, err = cnc.New(namespaceVersion, namespaceBytes)
if err != nil {
return err
}
return nil
}
4 changes: 2 additions & 2 deletions da/celestia/mock/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,15 @@ func (s *Server) Stop() {

func (s *Server) getHandler() http.Handler {
mux := mux2.NewRouter()
mux.HandleFunc("/submit_pfd", s.submit).Methods(http.MethodPost)
mux.HandleFunc("/submit_pfb", s.submit).Methods(http.MethodPost)
mux.HandleFunc("/namespaced_shares/{namespace}/height/{height}", s.shares).Methods(http.MethodGet)
mux.HandleFunc("/namespaced_data/{namespace}/height/{height}", s.data).Methods(http.MethodGet)

return mux
}

func (s *Server) submit(w http.ResponseWriter, r *http.Request) {
req := cnc.SubmitPFDRequest{}
req := cnc.SubmitPFBRequest{}
err := json.NewDecoder(r.Body).Decode(&req)
if err != nil {
s.writeError(w, err)
Expand Down
5 changes: 3 additions & 2 deletions da/da_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"testing"
"time"

"github.com/celestiaorg/go-cnc"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/tendermint/tendermint/libs/pubsub"
Expand Down Expand Up @@ -98,7 +99,7 @@ func doTestDALC(t *testing.T, dalc da.DataAvailabilityLayerClient) {
BaseURL: "http://localhost:26658",
Timeout: 30 * time.Second,
GasLimit: 3000000,
NamespaceID: [8]byte{0, 1, 2, 3, 4, 5, 6, 7},
NamespaceID: cnc.Namespace{Version: 0, ID: []byte{0, 0, 0, 0, 0, 0, 255, 255}},
}
conf, _ = json.Marshal(config)
}
Expand Down Expand Up @@ -218,7 +219,7 @@ func doTestRetrieve(t *testing.T, dalc da.DataAvailabilityLayerClient) {
BaseURL: "http://localhost:26658",
Timeout: 30 * time.Second,
GasLimit: 3000000,
NamespaceID: [8]byte{0, 1, 2, 3, 4, 5, 6, 7},
NamespaceID: cnc.Namespace{Version: 0, ID: []byte{0, 0, 0, 0, 0, 0, 255, 255}},
}
conf, _ = json.Marshal(config)
}
Expand Down
5 changes: 2 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ go 1.19

require (
code.cloudfoundry.org/go-diodes v0.0.0-20220725190411-383eb6634c40
cosmossdk.io/errors v1.0.0-beta.7
github.com/avast/retry-go v3.0.0+incompatible
github.com/celestiaorg/go-cnc v0.2.0
github.com/celestiaorg/go-cnc v0.4.2
github.com/centrifuge/go-substrate-rpc-client/v4 v4.0.12
github.com/dgraph-io/badger/v3 v3.2103.3
github.com/dymensionxyz/cosmosclient v0.3.0-beta.0.20230621132116-77eb2ae5ab92
Expand Down Expand Up @@ -38,7 +39,6 @@ require (
)

require (
cosmossdk.io/errors v1.0.0-beta.7 // indirect
cosmossdk.io/math v1.0.0-rc.0 // indirect
github.com/99designs/go-keychain v0.0.0-20191008050251-8e49817e8af4 // indirect
github.com/StackExchange/wmi v1.2.1 // indirect
Expand Down Expand Up @@ -242,7 +242,6 @@ require (
)

replace (
github.com/celestiaorg/go-cnc => github.com/dymensionxyz/go-cnc v0.2.2
github.com/centrifuge/go-substrate-rpc-client/v4 => github.com/availproject/go-substrate-rpc-client/v4 v4.0.12-avail-1.4.0-rc1-5e286e3
github.com/gogo/protobuf => github.com/regen-network/protobuf v1.3.2-alpha.regen.4
github.com/gorilla/rpc => github.com/dymensionxyz/rpc v1.3.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ github.com/btcsuite/btcd/btcutil v1.1.2 h1:XLMbX8JQEiwMcYft2EGi8zPUkoa0abKIU6/BJ
github.com/btcsuite/btcd/chaincfg/chainhash v1.0.1 h1:q0rUy8C/TYNBQS1+CGKw68tLOFYSNEs0TFnxxnS9+4U=
github.com/btcsuite/btcutil v1.0.3-0.20201208143702-a53e38424cce h1:YtWJF7RHm2pYCvA5t0RPmAaLUhREsKuKd+SLhxFbFeQ=
github.com/buger/jsonparser v0.0.0-20181115193947-bf1c66bbce23/go.mod h1:bbYlZJ7hK1yFx9hf58LP0zeX7UjIGs20ufpu3evjr+s=
github.com/celestiaorg/go-cnc v0.4.2 h1:7ixf3tevMB7Lvz2mbyRG0ZOK+8qoPm7wNhdgpi8VreU=
github.com/celestiaorg/go-cnc v0.4.2/go.mod h1:zYzvHudSd1iNPuHBMyvZ1YvWou5aT9JXgtch9Tkaf70=
github.com/cenkalti/backoff v2.2.1+incompatible h1:tNowT99t7UNflLxfYYSlKYsBpXdEet03Pg2g16Swow4=
github.com/cenkalti/backoff v2.2.1+incompatible/go.mod h1:90ReRw6GdpyfrHakVjL/QHaoyV4aDUVVkXQJJJ3NXXM=
github.com/cenkalti/backoff/v4 v4.1.3 h1:cFAlzYUlVYDysBEH2T5hyJZMh3+5+WCBvSnK6Q8UtC4=
Expand Down Expand Up @@ -210,8 +212,6 @@ github.com/dymensionxyz/cosmosclient v0.3.0-beta.0.20230621132116-77eb2ae5ab92 h
github.com/dymensionxyz/cosmosclient v0.3.0-beta.0.20230621132116-77eb2ae5ab92/go.mod h1:3y64ecWDzhnd0sSYZfaL4QpwgK0b0j6LFLVRGdygg+o=
github.com/dymensionxyz/dymension v0.2.0-beta.0.20230607115558-745644a96ea6 h1:dnriGXmMdYEiF/8lMrj+PDlN1vyQc6zgs/ZHL67eoyI=
github.com/dymensionxyz/dymension v0.2.0-beta.0.20230607115558-745644a96ea6/go.mod h1:rDkVuF+DxBDi5tTgVHFk1D2xpqf8bOccs6aB1wTOvP0=
github.com/dymensionxyz/go-cnc v0.2.2 h1:C7WUFJ+PkkB62HPegBJJL+YlQExqvYudTTRdNiCNIDk=
github.com/dymensionxyz/go-cnc v0.2.2/go.mod h1:CZBVUhQnJnAVcfQnnEAqREF+PNWr97m/BhJ5fp1K44Q=
github.com/dymensionxyz/rpc v1.3.1 h1:7EXWIobaBes5zldRvTIg7TmNsEKjicrWA/OjCc0NaGs=
github.com/dymensionxyz/rpc v1.3.1/go.mod h1:f+WpX8ysy8wt95iGc6auYlHcnHj2bUkhiRVkkKNys8c=
github.com/elastic/gosigar v0.12.0/go.mod h1:iXRIGg2tLnu7LBdpqzyQfGDEidKCfWcCMS0WKyPWoMs=
Expand Down
20 changes: 10 additions & 10 deletions mocks/da/celestia/cnc_client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit a67e445

Please sign in to comment.