Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(pkg)!: move wrapper package from core -> app #707

Merged
merged 2 commits into from
Sep 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/inclusion/nmt_caching.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import (
"fmt"

"github.com/celestiaorg/celestia-app/pkg/da"
"github.com/celestiaorg/celestia-app/pkg/wrapper"
"github.com/celestiaorg/nmt"
"github.com/celestiaorg/rsmt2d"
"github.com/tendermint/tendermint/pkg/wrapper"
)

// WalkInstruction wraps the bool type to indicate the direction that should be
Expand Down
2 changes: 1 addition & 1 deletion pkg/inclusion/nmt_caching_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import (
"testing"

"github.com/celestiaorg/celestia-app/pkg/da"
"github.com/celestiaorg/celestia-app/pkg/wrapper"
"github.com/celestiaorg/celestia-app/testutil/coretestutil"
"github.com/celestiaorg/nmt"
"github.com/celestiaorg/rsmt2d"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/tendermint/tendermint/pkg/consts"
"github.com/tendermint/tendermint/pkg/wrapper"
)

func TestWalkCachedSubTreeRoot(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/prove/proof.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import (
"errors"
"fmt"

"github.com/celestiaorg/celestia-app/pkg/wrapper"
"github.com/celestiaorg/rsmt2d"
tmbytes "github.com/tendermint/tendermint/libs/bytes"
"github.com/tendermint/tendermint/pkg/consts"
"github.com/tendermint/tendermint/pkg/wrapper"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
"github.com/tendermint/tendermint/types"
)
Expand Down
83 changes: 83 additions & 0 deletions pkg/wrapper/nmt_wrapper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package wrapper

import (
"fmt"

"github.com/celestiaorg/nmt"
"github.com/celestiaorg/rsmt2d"

"github.com/tendermint/tendermint/pkg/consts"
)

// Fulfills the rsmt2d.Tree interface and rsmt2d.TreeConstructorFn function
var _ rsmt2d.TreeConstructorFn = ErasuredNamespacedMerkleTree{}.Constructor
var _ rsmt2d.Tree = &ErasuredNamespacedMerkleTree{}

// ErasuredNamespacedMerkleTree wraps NamespaceMerkleTree to conform to the
// rsmt2d.Tree interface while also providing the correct namespaces to the
// underlying NamespaceMerkleTree. It does this by adding the already included
// namespace to the first half of the tree, and then uses the parity namespace
// ID for each share pushed to the second half of the tree. This allows for the
// namespaces to be included in the erasure data, while also keeping the nmt
// library sufficiently general
type ErasuredNamespacedMerkleTree struct {
squareSize uint64 // note: this refers to the width of the original square before erasure-coded
options []nmt.Option
tree *nmt.NamespacedMerkleTree
}

// NewErasuredNamespacedMerkleTree issues a new ErasuredNamespacedMerkleTree. squareSize must be greater than zero
func NewErasuredNamespacedMerkleTree(origSquareSize uint64, setters ...nmt.Option) ErasuredNamespacedMerkleTree {
if origSquareSize == 0 {
panic("cannot create a ErasuredNamespacedMerkleTree of squareSize == 0")
}
tree := nmt.New(consts.NewBaseHashFunc(), setters...)
return ErasuredNamespacedMerkleTree{squareSize: origSquareSize, options: setters, tree: tree}
}

// Constructor acts as the rsmt2d.TreeConstructorFn for
// ErasuredNamespacedMerkleTree
func (w ErasuredNamespacedMerkleTree) Constructor() rsmt2d.Tree {
newTree := NewErasuredNamespacedMerkleTree(w.squareSize, w.options...)
return &newTree
}

// Push adds the provided data to the underlying NamespaceMerkleTree, and
// automatically uses the first DefaultNamespaceIDLen number of bytes as the
// namespace unless the data pushed to the second half of the tree. Fulfills the
// rsmt.Tree interface. NOTE: panics if an error is encountered while pushing or
// if the tree size is exceeded.
func (w *ErasuredNamespacedMerkleTree) Push(data []byte, idx rsmt2d.SquareIndex) {
if idx.Axis+1 > 2*uint(w.squareSize) || idx.Cell+1 > 2*uint(w.squareSize) {
panic(fmt.Sprintf("pushed past predetermined square size: boundary at %d index at %+v", 2*w.squareSize, idx))
}
nidAndData := make([]byte, consts.NamespaceSize+len(data))
copy(nidAndData[consts.NamespaceSize:], data)
// use the parity namespace if the cell is not in Q0 of the extended data square
if idx.Axis+1 > uint(w.squareSize) || idx.Cell+1 > uint(w.squareSize) {
copy(nidAndData[:consts.NamespaceSize], consts.ParitySharesNamespaceID)
} else {
copy(nidAndData[:consts.NamespaceSize], data[:consts.NamespaceSize])
}
// push to the underlying tree
err := w.tree.Push(nidAndData)
// panic on error
if err != nil {
panic(err)
}
}

// Root fulfills the rsmt.Tree interface by generating and returning the
// underlying NamespaceMerkleTree Root.
func (w *ErasuredNamespacedMerkleTree) Root() []byte {
return w.tree.Root()
}

func (w *ErasuredNamespacedMerkleTree) Prove(ind int) (nmt.Proof, error) {
return w.tree.Prove(ind)
}

// Tree returns the underlying NamespacedMerkleTree
func (w *ErasuredNamespacedMerkleTree) Tree() *nmt.NamespacedMerkleTree {
return w.tree
}
168 changes: 168 additions & 0 deletions pkg/wrapper/nmt_wrapper_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
package wrapper

import (
"bytes"
"crypto/rand"
"crypto/sha256"
"sort"
"testing"

"github.com/celestiaorg/nmt"
"github.com/celestiaorg/rsmt2d"
"github.com/stretchr/testify/assert"
"github.com/tendermint/tendermint/pkg/consts"
)

func TestPushErasuredNamespacedMerkleTree(t *testing.T) {
testCases := []struct {
name string
squareSize int
}{
{"extendedSquareSize = 16", 8},
{"extendedSquareSize = 256", 128},
}
for _, tc := range testCases {
tc := tc
n := NewErasuredNamespacedMerkleTree(uint64(tc.squareSize))
tree := n.Constructor()

// push test data to the tree
for i, d := range generateErasuredData(t, tc.squareSize, consts.DefaultCodec()) {
// push will panic if there's an error
tree.Push(d, rsmt2d.SquareIndex{Axis: uint(0), Cell: uint(i)})
}
}
}

func TestRootErasuredNamespacedMerkleTree(t *testing.T) {
// check that the root is different from a standard nmt tree this should be
// the case, because the ErasuredNamespacedMerkleTree should add namespaces
// to the second half of the tree
size := 8
data := generateRandNamespacedRawData(size, consts.NamespaceSize, consts.MsgShareSize)
n := NewErasuredNamespacedMerkleTree(uint64(size))
tree := n.Constructor()
nmtTree := nmt.New(sha256.New())

for i, d := range data {
tree.Push(d, rsmt2d.SquareIndex{Axis: uint(0), Cell: uint(i)})
err := nmtTree.Push(d)
if err != nil {
t.Error(err)
}
}

assert.NotEqual(t, nmtTree.Root(), tree.Root())
}

func TestErasureNamespacedMerkleTreePanics(t *testing.T) {
testCases := []struct {
name string
pFunc assert.PanicTestFunc
}{
{
"push over square size",
assert.PanicTestFunc(
func() {
data := generateErasuredData(t, 16, consts.DefaultCodec())
n := NewErasuredNamespacedMerkleTree(uint64(15))
tree := n.Constructor()
for i, d := range data {
tree.Push(d, rsmt2d.SquareIndex{Axis: uint(0), Cell: uint(i)})
}
}),
},
{
"push in incorrect lexigraphic order",
assert.PanicTestFunc(
func() {
data := generateErasuredData(t, 16, consts.DefaultCodec())
n := NewErasuredNamespacedMerkleTree(uint64(16))
tree := n.Constructor()
for i := len(data) - 1; i > 0; i-- {
tree.Push(data[i], rsmt2d.SquareIndex{Axis: uint(0), Cell: uint(i)})
}
},
),
},
}
for _, tc := range testCases {
tc := tc
assert.Panics(t, tc.pFunc, tc.name)

}
}

func TestExtendedDataSquare(t *testing.T) {
squareSize := 4
// data for a 4X4 square
raw := generateRandNamespacedRawData(
squareSize*squareSize,
consts.NamespaceSize,
consts.MsgShareSize,
)

tree := NewErasuredNamespacedMerkleTree(uint64(squareSize))

_, err := rsmt2d.ComputeExtendedDataSquare(raw, consts.DefaultCodec(), tree.Constructor)
assert.NoError(t, err)
}

func TestErasuredNamespacedMerkleTree(t *testing.T) {
// check that the Tree() returns exact underlying nmt tree
size := 8
data := generateRandNamespacedRawData(size, consts.NamespaceSize, consts.MsgShareSize)
n := NewErasuredNamespacedMerkleTree(uint64(size))
tree := n.Constructor()

for i, d := range data {
tree.Push(d, rsmt2d.SquareIndex{Axis: uint(0), Cell: uint(i)})
}

assert.Equal(t, n.Tree(), n.tree)
assert.Equal(t, n.Tree().Root(), n.tree.Root())
}

// generateErasuredData produces a slice that is twice as long as it erasures
// the data
func generateErasuredData(t *testing.T, numLeaves int, codec rsmt2d.Codec) [][]byte {
raw := generateRandNamespacedRawData(
numLeaves,
consts.NamespaceSize,
consts.MsgShareSize,
)
erasuredData, err := codec.Encode(raw)
if err != nil {
t.Error(err)
}
return append(raw, erasuredData...)
}

// this code is copy pasted from the plugin, and should likely be exported in the plugin instead
func generateRandNamespacedRawData(total int, nidSize int, leafSize int) [][]byte {
data := make([][]byte, total)
for i := 0; i < total; i++ {
nid := make([]byte, nidSize)
_, err := rand.Read(nid)
if err != nil {
panic(err)
}
data[i] = nid
}

sortByteArrays(data)
for i := 0; i < total; i++ {
d := make([]byte, leafSize)
_, err := rand.Read(d)
if err != nil {
panic(err)
}
data[i] = append(data[i], d...)
}

return data
}

func sortByteArrays(src [][]byte) {
sort.Slice(src, func(i, j int) bool { return bytes.Compare(src[i], src[j]) < 0 })
}
2 changes: 1 addition & 1 deletion x/payment/types/payfordata.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import (
"fmt"
"math/bits"

"github.com/celestiaorg/celestia-app/pkg/wrapper"
"github.com/celestiaorg/rsmt2d"
sdkclient "github.com/cosmos/cosmos-sdk/client"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/tx/signing"
authsigning "github.com/cosmos/cosmos-sdk/x/auth/signing"
"github.com/tendermint/tendermint/crypto/merkle"
"github.com/tendermint/tendermint/pkg/consts"
"github.com/tendermint/tendermint/pkg/wrapper"
coretypes "github.com/tendermint/tendermint/types"

shares "github.com/celestiaorg/celestia-app/pkg/shares"
Expand Down