Skip to content

Commit

Permalink
review feedback: follow the spec and pad shares when generating the s…
Browse files Browse the repository at this point in the history
…hare commitment instead of not allowing chains

update failing test to new expected result now that the square size is 128 instead of 64
  • Loading branch information
evan-forbes committed Feb 21, 2021
1 parent 04cbae1 commit a41e189
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 5 deletions.
25 changes: 22 additions & 3 deletions x/lazyledgerapp/types/payformessage.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,10 @@ func (msg *SignedTransactionDataPayForMessage) GetSigners() []sdk.AccAddress {
// https://github.com/lazyledger/lazyledger-specs/blob/master/rationale/message_block_layout.md#non-interactive-default-rules
func CreateCommit(k uint64, namespace, message []byte) ([]byte, error) {
// break message into shares
shares := ChunkMessage(message)
shares := chunkMessage(message)

// add padding if necessary
shares = addSharePadding(shares)

// organize shares for merkle mountain range
heights := PowerOf2MountainRange(uint64(len(shares)), k)
Expand Down Expand Up @@ -234,8 +237,8 @@ func CreateCommit(k uint64, namespace, message []byte) ([]byte, error) {
return merkle.HashFromByteSlices(subTreeRoots), nil
}

// ChunkMessage breaks the message into 256 byte pieces
func ChunkMessage(message []byte) [][]byte {
// chunkMessage breaks the message into 256 byte pieces
func chunkMessage(message []byte) [][]byte {
var shares [][]byte
for i := 0; i < len(message); i += ShareSize {
end := i + ShareSize
Expand All @@ -247,6 +250,22 @@ func ChunkMessage(message []byte) [][]byte {
return shares
}

// addSharePadding will add padding to the last share if necessary
func addSharePadding(shares [][]byte) [][]byte {
if len(shares) == 0 {
return shares
}

// add padding to the last share if necessary
if len(shares[len(shares)-1]) != ShareSize {
padded := make([]byte, ShareSize)
copy(padded, shares[len(shares)-1])
shares[len(shares)-1] = padded
}

return shares
}

// PowerOf2MountainRange returns the heights of the subtrees for binary merkle
// mountian range
func PowerOf2MountainRange(l, k uint64) []uint64 {
Expand Down
31 changes: 29 additions & 2 deletions x/lazyledgerapp/types/payformessage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,39 @@ func TestGetCommitmentSignBytes(t *testing.T) {
TipRateMax: 1000,
},
},
expected: []byte(`{"fee":{"base_rate_max":"10000","tip_rate_max":"1000"},"message_namespace_id":"AQIDBAECAwQ=","message_share_commitment":"kLkMnfL0wruFOdgRJ4KnyjJBLJWlKxbEyks8SI0cfZs=","message_size":"4","nonce":"1"}`),
expected: []byte(`{"fee":{"base_rate_max":"10000","tip_rate_max":"1000"},"message_namespace_id":"AQIDBAECAwQ=","message_share_commitment":"byozRVIrw5NF/rU1PPyq6BAo3g2ny3uLTiOFedtgSwo=","message_size":"4","nonce":"1"}`),
},
}
for _, tt := range tests {
res, err := tt.msg.GetCommitmentSignBytes(64)
res, err := tt.msg.GetCommitmentSignBytes(SquareSize)
assert.NoError(t, err)
assert.Equal(t, tt.expected, res)
}
}

func TestShareChunkingAndPadding(t *testing.T) {
type test struct {
input []byte
expect [][]byte
}
tests := []test{
{
input: []byte{1},
expect: [][]byte{append([]byte{1}, bytes.Repeat([]byte{0}, ShareSize-1)...)},
},
{
input: bytes.Repeat([]byte{1}, ShareSize),
expect: [][]byte{bytes.Repeat([]byte{1}, ShareSize)},
},
}
for _, tt := range tests {
shares := chunkMessage(tt.input)
shares = addSharePadding(shares)
for _, share := range shares {
if len(share) != ShareSize {
t.Errorf("invalid share length: got %d wanted core.ShareSize (%d)", len(share), ShareSize)
}
}
assert.Equal(t, tt.expect, shares)
}
}

0 comments on commit a41e189

Please sign in to comment.