Skip to content

Commit

Permalink
Add E2E test for banana
Browse files Browse the repository at this point in the history
  • Loading branch information
arnaubennassar committed Jun 20, 2024
1 parent bbfd2bf commit 2853144
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 3 deletions.
21 changes: 21 additions & 0 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type Client interface {
GetOffChainData(ctx context.Context, hash common.Hash) ([]byte, error)
ListOffChainData(ctx context.Context, hashes []common.Hash) (map[common.Hash][]byte, error)
SignSequence(ctx context.Context, signedSequence types.SignedSequence) ([]byte, error)
SignSequenceBanana(ctx context.Context, signedSequence types.SignedSequenceBanana) ([]byte, error)
}

// factory is the implementation of the data committee client factory
Expand Down Expand Up @@ -87,6 +88,26 @@ func (c *client) SignSequence(ctx context.Context, signedSequence types.SignedSe
return result, nil
}

// SignSequenceBanana sends a request to sign the given sequence by the data committee member
// if successful returns the signature. The signature should be validated after using this method!
func (c *client) SignSequenceBanana(ctx context.Context, signedSequence types.SignedSequenceBanana) ([]byte, error) {
response, err := rpc.JSONRPCCallWithContext(ctx, c.url, "datacom_signSequenceBanana", signedSequence)
if err != nil {
return nil, err
}

if response.Error != nil {
return nil, fmt.Errorf("%v %v", response.Error.Code, response.Error.Message)
}

var result types.ArgBytes
if err = json.Unmarshal(response.Result, &result); err != nil {
return nil, err
}

return result, nil
}

// GetOffChainData returns data based on it's hash
func (c *client) GetOffChainData(ctx context.Context, hash common.Hash) ([]byte, error) {
response, err := rpc.JSONRPCCallWithContext(ctx, c.url, "sync_getOffChainData", hash)
Expand Down
3 changes: 0 additions & 3 deletions services/datacom/datacom.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@ import (
"fmt"

"github.com/0xPolygon/cdk-data-availability/db"
"github.com/0xPolygon/cdk-data-availability/log"
"github.com/0xPolygon/cdk-data-availability/rpc"
"github.com/0xPolygon/cdk-data-availability/sequencer"
"github.com/0xPolygon/cdk-data-availability/types"
"github.com/ethereum/go-ethereum/common"
)

// APIDATACOM is the namespace of the datacom service
Expand Down Expand Up @@ -74,6 +72,5 @@ func (d *Endpoints) signSequence(signedSequence types.SignedSequenceInterface) (
return "0x0", rpc.NewRPCError(rpc.DefaultErrorCode, fmt.Errorf("failed to sign. Error: %w", err).Error())
}
// Return signature
log.Infof("this is the signature: %s", common.Bytes2Hex(signature))
return signature, nil
}
Binary file removed test/e2e/__debug_bin2997315904
Binary file not shown.
132 changes: 132 additions & 0 deletions test/e2e/e2ebanana_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
package e2e

import (
"context"
"errors"
"testing"

"github.com/0xPolygon/cdk-data-availability/types"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestSignSequenceBanana(t *testing.T) {
if testing.Short() {
t.Skip()
}

tc, pk := initTest(t)
type testSequences struct {
name string
sequence types.SignedSequenceBanana
expectedErr error
}
expectedSequence := types.SequenceBanana{
Batches: []types.Batch{
{
L2Data: common.FromHex("723473475757adadaddada"),
ForcedGER: common.Hash{},
ForcedTimestamp: 0,
Coinbase: common.HexToAddress("aabbccddee"),
ForcedBlockHashL1: common.Hash{},
},
{
L2Data: common.FromHex("723473475757adadaddada723473475757adadaddada"),
ForcedGER: common.Hash{},
ForcedTimestamp: 0,
Coinbase: common.HexToAddress("aabbccddee"),
ForcedBlockHashL1: common.Hash{},
},
},
OldAccInputHash: common.HexToHash("abcdef0987654321"),
L1InfoRoot: common.HexToHash("ffddeeaabb09876"),
MaxSequenceTimestamp: 78945,
}

unexpectedSenderPrivKey, err := crypto.GenerateKey()
require.NoError(t, err)
unexpectedSenderSignature, err := expectedSequence.Sign(unexpectedSenderPrivKey)
require.NoError(t, err)
tSequences := []testSequences{
{
name: "invalid_signature",
sequence: types.SignedSequenceBanana{
Sequence: types.SequenceBanana{},
Signature: common.Hex2Bytes("f00"),
},
expectedErr: errors.New("-32000 failed to verify sender"),
},
{
name: "signature_not_from_sender",
sequence: types.SignedSequenceBanana{
Sequence: expectedSequence,
Signature: unexpectedSenderSignature,
},
expectedErr: errors.New("-32000 unauthorized"),
},
{
name: "empty_batch",
sequence: types.SignedSequenceBanana{},
expectedErr: nil,
},
{
name: "success",
sequence: types.SignedSequenceBanana{
Sequence: expectedSequence,
Signature: nil,
},
expectedErr: nil,
},
}
for _, ts := range tSequences {
t.Run(ts.name, func(t *testing.T) {
if ts.sequence.Signature == nil {
signature, err := ts.sequence.Sequence.Sign(pk)
require.NoError(t, err)
ts.sequence = types.SignedSequenceBanana{
Sequence: ts.sequence.Sequence,
Signature: signature,
}
}
tc.signSequenceBanana(t, &ts.sequence, ts.expectedErr)
})
}
}

func (tc *testClient) signSequenceBanana(t *testing.T, expected *types.SignedSequenceBanana, expectedErr error) {
if signature, err := tc.client.SignSequenceBanana(context.Background(), *expected); err != nil {
assert.Equal(t, expectedErr.Error(), err.Error())
} else {
// Verify signature
require.NoError(t, expectedErr)
expected.Signature = signature
actualAddr, err := expected.Signer()
require.NoError(t, err)
assert.Equal(t, tc.dacMemberAddr, actualAddr)

// Check that offchain data has been stored
expectedOffchainData := expected.Sequence.OffChainData()
for _, od := range expectedOffchainData {
actualData, err := tc.client.GetOffChainData(
context.Background(),
od.Key,
)
require.NoError(t, err)
assert.Equal(t, od.Value, actualData)
}

hashes := make([]common.Hash, len(expectedOffchainData))
for i, od := range expectedOffchainData {
hashes[i] = od.Key
}

actualData, err := tc.client.ListOffChainData(context.Background(), hashes)
require.NoError(t, err)

for _, od := range expectedOffchainData {
assert.Equal(t, od.Value, actualData[od.Key])
}
}
}

0 comments on commit 2853144

Please sign in to comment.