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

Commit

Permalink
use heightMapper functionality to retrieve and sample
Browse files Browse the repository at this point in the history
  • Loading branch information
evan-forbes committed Jan 11, 2022
1 parent b89c756 commit f8f6218
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 37 deletions.
2 changes: 2 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,13 @@ func DefaultServerConfig() ServerConfig {
// BaseConfig contains the basic configurations required for the grpc server
type BaseConfig struct {
ListenAddr string `toml:"laddr"`
Namespace string `toml:"namespace"`
}

func DefaultBaseConfig() BaseConfig {
return BaseConfig{
ListenAddr: "0.0.0.0:4200",
Namespace: "0102030405060708",
}
}

Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ replace (
github.com/celestiaorg/celestia-node => github.com/celestiaorg/celestia-node v0.1.1
github.com/cosmos/cosmos-sdk => github.com/celestiaorg/cosmos-sdk v0.44.1-celestia
github.com/gogo/protobuf => github.com/regen-network/protobuf v1.3.3-alpha.regen.1
github.com/ipfs/go-verifcid => github.com/celestiaorg/go-verifcid v0.0.1-lazypatch
github.com/tendermint/tendermint v0.34.14 => github.com/celestiaorg/celestia-core v0.34.14-celestia.0.20220110174237-266919cced26
google.golang.org/grpc => google.golang.org/grpc v1.33.2
)
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,8 @@ github.com/celestiaorg/go-leopard v0.1.0 h1:28z2EkvKJIez5J9CEaiiUEC+OxalRLtTGJJ1
github.com/celestiaorg/go-leopard v0.1.0/go.mod h1:NtO/rjlB8dw2aq7jr06vZFKGvryQcTDXaNHelmPNOAM=
github.com/celestiaorg/go-libp2p-messenger v0.1.0 h1:rFldTa3ZWcRRn8E2bRWS94Qp1GFYXO2a0uvqpIey1B8=
github.com/celestiaorg/go-libp2p-messenger v0.1.0/go.mod h1:XzNksXrH0VxuNRGOnjPL9Ck4UyQlbmMpCYg9YwSBerI=
github.com/celestiaorg/go-verifcid v0.0.1-lazypatch h1:9TSe3w1cmJmbWlweCwCTIZkan7jV8M+KwglXpdD+UG8=
github.com/celestiaorg/go-verifcid v0.0.1-lazypatch/go.mod h1:kXPYu0XqTNUKWA1h3M95UHjUqBzDwXVVt/RXZDjKJmQ=
github.com/celestiaorg/merkletree v0.0.0-20210714075610-a84dc3ddbbe4 h1:CJdIpo8n5MFP2MwK0gSRcOVlDlFdQJO1p+FqdxYzmvc=
github.com/celestiaorg/merkletree v0.0.0-20210714075610-a84dc3ddbbe4/go.mod h1:fzuHnhzj1pUygGz+1ZkB3uQbEUL4htqCGJ4Qs2LwMZA=
github.com/celestiaorg/nmt v0.7.0/go.mod h1:3bqzTj8xKj0DgQUpOgZzoxvtNkC3MS/hTbQ6dn8SIa0=
Expand Down Expand Up @@ -723,8 +725,6 @@ github.com/ipfs/go-peertaskqueue v0.1.0/go.mod h1:Jmk3IyCcfl1W3jTW3YpghSwSEC6IJ3
github.com/ipfs/go-peertaskqueue v0.2.0/go.mod h1:5/eNrBEbtSKWCG+kQK8K8fGNixoYUnr+P7jivavs9lY=
github.com/ipfs/go-peertaskqueue v0.4.0 h1:x1hFgA4JOUJ3ntPfqLRu6v4k6kKL0p07r3RSg9JNyHI=
github.com/ipfs/go-peertaskqueue v0.4.0/go.mod h1:KL9F49hXJMoXCad8e5anivjN+kWdr+CyGcyh4K6doLc=
github.com/ipfs/go-verifcid v0.0.1 h1:m2HI7zIuR5TFyQ1b79Da5N9dnnCP1vcu2QqawmWlK2E=
github.com/ipfs/go-verifcid v0.0.1/go.mod h1:5Hrva5KBeIog4A+UpqlaIU+DEstipcJYQQZc0g37pY0=
github.com/ipld/go-ipld-prime v0.9.0/go.mod h1:KvBLMr4PX1gWptgkzRjVZCrLmSGcZCb/jioOQwCqZN8=
github.com/ipld/go-ipld-prime v0.11.0 h1:jD/b/22R7CSL+F9xNffcexs+wO0Ji/TfwXO/TWck+70=
github.com/ipld/go-ipld-prime v0.11.0/go.mod h1:+WIAkokurHmZ/KwzDOMUuoeJgaRQktHtEaLglS3ZeV8=
Expand Down
26 changes: 26 additions & 0 deletions server/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package server

import "fmt"

type PreExistingBlockMappingError struct {
OptimintBlockHeight, CelestiaBlockHeight int64
}

func (pe PreExistingBlockMappingError) Error() string {
return fmt.Sprintf(
"optimint block at height %d is already associated with a celestia block height %d",
pe.OptimintBlockHeight,
pe.CelestiaBlockHeight,
)
}

type NoAssociatedBlockError struct {
OptimintBlockHeight int64
}

func (ne NoAssociatedBlockError) Error() string {
return fmt.Sprintf(
"No assocated celestia block for optimint block of height %d",
ne.OptimintBlockHeight,
)
}
9 changes: 4 additions & 5 deletions server/height_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,10 @@ func (hm *HeightMapper) Save(optimintHeight, celestiaHeight int64) error {
strOptiHeight := fmt.Sprintf("%d", optimintHeight)

if cHeight, has := hm.Heights[strOptiHeight]; has {
return fmt.Errorf(
"optimint block at height %s already saved on celestia at celestia height %d",
strOptiHeight,
cHeight,
)
return PreExistingBlockMappingError{
CelestiaBlockHeight: cHeight,
OptimintBlockHeight: optimintHeight,
}
}
hm.Heights[strOptiHeight] = celestiaHeight
return nil
Expand Down
57 changes: 44 additions & 13 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package server

import (
"context"
"encoding/hex"
"fmt"
"math"
"os"
Expand All @@ -26,7 +27,7 @@ func New(cfg config.ServerConfig, configPath, nodePath string) (*grpc.Server, er
logger := tmlog.NewTMLogger(os.Stdout)

// load the height map
hm, err := HeightMapperFromFile(configPath + "/height_map.json")
hm, err := HeightMapperFromFile("/home/evan/.dalc" + "/height_map.json")
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -67,10 +68,15 @@ func New(cfg config.ServerConfig, configPath, nodePath string) (*grpc.Server, er

node.CoreClient = coreClient

lc := &DataAvailabilityLightClient{
logger: logger,
heightMapper: hm,
namespace, err := hex.DecodeString(cfg.Namespace)
if err != nil {
return nil, err
}

lc := &DataAvailabilityLightClient{
logger: logger,
heightMapper: hm,
namespace: namespace,
blockSubmitter: bs,
node: node,
}
Expand All @@ -84,13 +90,23 @@ func New(cfg config.ServerConfig, configPath, nodePath string) (*grpc.Server, er
type DataAvailabilityLightClient struct {
logger tmlog.Logger

namespace []byte
heightMapper HeightMapper
blockSubmitter blockSubmitter
node *cnode.Node
}

// SubmitBlock posts an optimint block to celestia
func (d *DataAvailabilityLightClient) SubmitBlock(ctx context.Context, blockReq *dalc.SubmitBlockRequest) (*dalc.SubmitBlockResponse, error) {
// check if the block we're trying to submit already has a celestia height associated with it
optimintHeight := int64(blockReq.Block.Header.Height)
if cHeight, has := d.heightMapper.Search(optimintHeight); has {
return nil, PreExistingBlockMappingError{
CelestiaBlockHeight: cHeight,
OptimintBlockHeight: optimintHeight,
}
}

// submit the block
broadcastResp, err := d.blockSubmitter.SubmitBlock(ctx, blockReq.Block)
if err != nil {
Expand All @@ -110,15 +126,29 @@ func (d *DataAvailabilityLightClient) SubmitBlock(ctx context.Context, blockReq
}, err
}

err = d.heightMapper.Save(int64(blockReq.Block.Header.Height), resp.Height)
if err != nil {
// TODO: we proabably need to do something more drastic than warn if a
// block we just submitted already exists in the height mapper
d.logger.Error(err.Error())
return nil, err
}

d.logger.Info("Submitted block to celstia", "height", resp.Height, "gas used", resp.GasUsed, "hash", resp.TxHash)
return &dalc.SubmitBlockResponse{Result: &dalc.DAResponse{Code: dalc.StatusCode_STATUS_CODE_SUCCESS}}, nil
}

// CheckBlockAvailability samples shares from the underlying data availability layer
func (d *DataAvailabilityLightClient) CheckBlockAvailability(ctx context.Context, req *dalc.CheckBlockAvailabilityRequest) (*dalc.CheckBlockAvailabilityResponse, error) {
// check if the block we're trying to submit already has a celestia height associated with it
optimintHeight := int64(req.Header.Height)
celestiaHeight, has := d.heightMapper.Search(optimintHeight)
if !has {
return nil, NoAssociatedBlockError{optimintHeight}
}

// get the dah for the block
// todo(evan): change the optimint header to include some height for celestia instead of using incorrect optimint height
dah, err := getDAH(ctx, d.node.CoreClient, int64(req.Header.Height))
dah, err := getDAH(ctx, d.node.CoreClient, celestiaHeight)
if err != nil {
return nil, err
}
Expand All @@ -144,22 +174,23 @@ func (d *DataAvailabilityLightClient) CheckBlockAvailability(ctx context.Context
}

func (d *DataAvailabilityLightClient) RetrieveBlock(ctx context.Context, req *dalc.RetrieveBlockRequest) (*dalc.RetrieveBlockResponse, error) {
// todo(evan) don't use optimint heigt use correct celestia height
// check if the block we're trying to submit already has a celestia height associated with it
optimintHeight := int64(req.Height)
if _, has := d.heightMapper.Search(optimintHeight); !has {
return nil, NoAssociatedBlockError{optimintHeight}
}

dah, err := getDAH(ctx, d.node.CoreClient, int64(req.Height))
if err != nil {
return nil, err
}

fmt.Println("got dah", dah)

// todo include namespace inside the request, not preconfigured
shares, err := d.node.ShareServ.GetSharesByNamespace(ctx, dah, []byte{1, 1, 1, 1, 1, 1, 1, 1})
shares, err := d.node.ShareServ.GetSharesByNamespace(ctx, dah, d.namespace)
if err != nil {
return nil, err
}

fmt.Println("got shares", shares)

rawShares := make([][]byte, len(shares))
for i, share := range shares {
rawShares[i] = share.Data()
Expand Down Expand Up @@ -201,7 +232,7 @@ func getDAH(ctx context.Context, client core.Client, hate int64) (*da.DataAvaila

eds, err := da.ExtendShares(squareSize, rawShares)
if err != nil {
panic(err)
return nil, err
}

dah := da.NewDataAvailabilityHeader(eds)
Expand Down
44 changes: 27 additions & 17 deletions server/test/server_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package test
import (
"bytes"
"context"
"fmt"
"log"
"net"
"testing"
Expand All @@ -16,6 +15,7 @@ import (
"github.com/celestiaorg/dalc/proto/dalc"
"github.com/celestiaorg/dalc/proto/optimint"
"github.com/celestiaorg/dalc/server"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/tendermint/spm/cosmoscmd"
"google.golang.org/grpc"
Expand Down Expand Up @@ -66,27 +66,30 @@ func TestIntegration(t *testing.T) {
// set the global client
dalcClient = dalc.NewDALCServiceClient(conn)

_ = testSubmitBlock(t)
testBlockAvailability(t, 1)
optimintBlock := testSubmitBlock(t)

// req := dalc.RetrieveBlockRequest{
// Height: 8,
// }
// resp, err := dalcClient.RetrieveBlock(context.TODO(), &req)
// require.NoError(t, err)
testBlockAvailability(t, optimintBlock.Header)

// assert.Equal(t, dalc.StatusCode_STATUS_CODE_SUCCESS, resp.Result.Code)

// assert.Equal(t, block, resp.Block)
req := dalc.RetrieveBlockRequest{
Height: optimintBlock.Header.Height,
}
resp, err := dalcClient.RetrieveBlock(context.TODO(), &req)
require.NoError(t, err)
assert.Equal(t, dalc.StatusCode_STATUS_CODE_SUCCESS, resp.Result.Code)
assert.Equal(t, optimintBlock, resp.Block)
srv.Stop()
}

func testBlockAvailability(t *testing.T, height uint64) {
resp, err := dalcClient.CheckBlockAvailability(context.TODO(), &dalc.CheckBlockAvailabilityRequest{})
fmt.Println("this error", err)
fmt.Println(resp)
// hash := resp.Block.Header.DataHash
// assert.Greater(t, len(hash), 0)
func testBlockAvailability(t *testing.T, header *optimint.Header) {
resp, err := dalcClient.CheckBlockAvailability(
context.TODO(),
&dalc.CheckBlockAvailabilityRequest{
Header: header,
},
)
require.NoError(t, err)
assert.True(t, resp.DataAvailable)
assert.Equal(t, dalc.StatusCode_STATUS_CODE_SUCCESS, resp.Result.Code)
}

func testSubmitBlock(t *testing.T) *optimint.Block {
Expand Down Expand Up @@ -114,5 +117,12 @@ func testSubmitBlock(t *testing.T) *optimint.Block {
}

func testRetrieveBlock(t *testing.T, block *optimint.Block) {
req := &dalc.RetrieveBlockRequest{
Height: block.Header.Height,
}

resp, err := dalcClient.RetrieveBlock(context.TODO(), req)
require.NoError(t, err)
require.Equal(t, dalc.StatusCode_STATUS_CODE_SUCCESS, resp.Result.Code)

}

0 comments on commit f8f6218

Please sign in to comment.