-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #30 from datachainlab/fix-message-aggregation
Fix message aggregation Signed-off-by: Jun Kimura <[email protected]>
- Loading branch information
Showing
2 changed files
with
246 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,216 @@ | ||
package relay | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/datachainlab/lcp-go/relay/elc" | ||
"github.com/hyperledger-labs/yui-relayer/log" | ||
"github.com/stretchr/testify/require" | ||
"google.golang.org/grpc" | ||
) | ||
|
||
func TestSplitIntoMultiBatch(t *testing.T) { | ||
var M = func(n uint8) []byte { | ||
return []byte(fmt.Sprintf("message-%03d", n)) | ||
} | ||
var S = func(n uint8) []byte { | ||
return []byte(fmt.Sprintf("signature-%03d", n)) | ||
} | ||
var Signer = func(n uint8) []byte { | ||
return []byte(fmt.Sprintf("signer-%03d", n)) | ||
} | ||
var cases = []struct { | ||
Messages [][]byte | ||
Signatures [][]byte | ||
BatchSizes []int | ||
Signer []byte | ||
MessageBatchSize uint64 | ||
Error bool | ||
}{ | ||
// Messages.len = 1 is invalid | ||
{ | ||
Messages: [][]byte{M(0)}, | ||
Signatures: [][]byte{S(0)}, | ||
BatchSizes: []int{1}, | ||
Signer: Signer(0), | ||
MessageBatchSize: 1, | ||
Error: true, | ||
}, | ||
{ | ||
Messages: [][]byte{M(0), M(1)}, | ||
Signatures: [][]byte{S(0), S(1)}, | ||
BatchSizes: []int{2}, | ||
Signer: Signer(0), | ||
MessageBatchSize: 2, | ||
Error: false, | ||
}, | ||
{ | ||
Messages: [][]byte{M(0), M(1), M(2)}, | ||
Signatures: [][]byte{S(0), S(1), S(2)}, | ||
BatchSizes: []int{3}, | ||
Signer: Signer(0), | ||
MessageBatchSize: 3, | ||
Error: false, | ||
}, | ||
{ | ||
Messages: [][]byte{M(0), M(1), M(2)}, | ||
Signatures: [][]byte{S(0), S(1), S(2)}, | ||
BatchSizes: []int{2, 1}, | ||
Signer: Signer(0), | ||
MessageBatchSize: 2, | ||
Error: false, | ||
}, | ||
{ | ||
Messages: [][]byte{M(0), M(1), M(2), M(3)}, | ||
Signatures: [][]byte{S(0), S(1), S(2), S(3)}, | ||
BatchSizes: []int{3, 1}, | ||
Signer: Signer(0), | ||
MessageBatchSize: 3, | ||
Error: false, | ||
}, | ||
{ | ||
Messages: [][]byte{M(0), M(1), M(2), M(3), M(4)}, | ||
Signatures: [][]byte{S(0), S(1), S(2), S(3), S(4)}, | ||
BatchSizes: []int{3, 2}, | ||
Signer: Signer(0), | ||
MessageBatchSize: 3, | ||
Error: false, | ||
}, | ||
} | ||
for i, c := range cases { | ||
t.Run(fmt.Sprintf("case-%d", i), func(t *testing.T) { | ||
require := require.New(t) | ||
batches, err := splitIntoMultiBatch(c.Messages, c.Signatures, c.Signer, c.MessageBatchSize) | ||
if c.Error { | ||
require.Error(err) | ||
return | ||
} else { | ||
require.NoError(err) | ||
} | ||
require.Len(batches, len(c.BatchSizes)) | ||
for i, size := range c.BatchSizes { | ||
require.Equal(batches[i].Signer, c.Signer) | ||
require.Len(batches[i].Messages, size) | ||
require.Len(batches[i].Signatures, size) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestAggregateMessages(t *testing.T) { | ||
var M = func(n uint8) []byte { | ||
return []byte(fmt.Sprintf("message-%03d", n)) | ||
} | ||
var S = func(n uint8) []byte { | ||
return []byte(fmt.Sprintf("signature-%03d", n)) | ||
} | ||
var Signer = func(n uint8) []byte { | ||
return []byte(fmt.Sprintf("signer-%03d", n)) | ||
} | ||
|
||
err := log.InitLogger("DEBUG", "text", "stdout") | ||
require.NoError(t, err) | ||
logger := log.GetLogger() | ||
|
||
var cases = []struct { | ||
Messages [][]byte | ||
Signatures [][]byte | ||
Signer []byte | ||
BatchSize uint64 | ||
Error bool | ||
}{ | ||
// Messages.len = 0 is invalid | ||
{ | ||
Messages: [][]byte{}, | ||
Signatures: [][]byte{}, | ||
Signer: Signer(0), | ||
BatchSize: 2, | ||
Error: true, | ||
}, | ||
{ | ||
Messages: [][]byte{M(0)}, | ||
Signatures: [][]byte{S(0)}, | ||
Signer: Signer(0), | ||
BatchSize: 2, | ||
Error: false, | ||
}, | ||
{ | ||
Messages: [][]byte{M(0), M(1)}, | ||
Signatures: [][]byte{S(0), S(1)}, | ||
Signer: Signer(0), | ||
BatchSize: 2, | ||
Error: false, | ||
}, | ||
// BatchSize = 1 is invalid | ||
{ | ||
Messages: [][]byte{M(0), M(1)}, | ||
Signatures: [][]byte{S(0), S(1)}, | ||
Signer: Signer(0), | ||
BatchSize: 1, | ||
Error: true, | ||
}, | ||
{ | ||
Messages: [][]byte{M(0), M(1), M(2)}, | ||
Signatures: [][]byte{S(0), S(1), S(2)}, | ||
Signer: Signer(0), | ||
BatchSize: 2, | ||
Error: false, | ||
}, | ||
{ | ||
Messages: [][]byte{M(0), M(1), M(2), M(3)}, | ||
Signatures: [][]byte{S(0), S(1), S(2), S(3)}, | ||
Signer: Signer(0), | ||
BatchSize: 2, | ||
Error: false, | ||
}, | ||
{ | ||
Messages: [][]byte{M(0), M(1), M(2), M(3), M(4)}, | ||
Signatures: [][]byte{S(0), S(1), S(2), S(3), S(4)}, | ||
Signer: Signer(0), | ||
BatchSize: 2, | ||
Error: false, | ||
}, | ||
} | ||
|
||
for i, c := range cases { | ||
t.Run(fmt.Sprintf("case-%d", i), func(t *testing.T) { | ||
require := require.New(t) | ||
res, err := aggregateMessages(logger, c.BatchSize, mockMessageAggregator, c.Messages, c.Signatures, c.Signer) | ||
if c.Error { | ||
require.Error(err) | ||
return | ||
} else { | ||
require.NoError(err) | ||
} | ||
require.Equal(res.ProxyMessage, concatBytes(c.Messages)) | ||
require.Equal(res.Signatures[0], concatBytes(c.Signatures)) | ||
}) | ||
} | ||
} | ||
|
||
func concatBytes(bzs [][]byte) []byte { | ||
var res []byte | ||
for _, b := range bzs { | ||
res = append(res, b...) | ||
} | ||
return res | ||
} | ||
|
||
func mockMessageAggregator(_ context.Context, in *elc.MsgAggregateMessages, _ ...grpc.CallOption) (*elc.MsgAggregateMessagesResponse, error) { | ||
var res elc.MsgAggregateMessagesResponse | ||
if len(in.Messages) != len(in.Signatures) { | ||
return nil, fmt.Errorf("messages and signatures must have the same length") | ||
} | ||
if len(in.Messages) == 0 { | ||
return nil, fmt.Errorf("messages.len = 0 is invalid") | ||
} else if len(in.Messages) == 1 { | ||
return nil, fmt.Errorf("messages.len = 1 is invalid") | ||
} | ||
for i := 0; i < len(in.Messages); i++ { | ||
res.Message = append(res.Message, in.Messages[i]...) | ||
res.Signature = append(res.Signature, in.Signatures[i]...) | ||
} | ||
return &res, nil | ||
} |