Skip to content

Commit

Permalink
Add rpc aggregator test
Browse files Browse the repository at this point in the history
  • Loading branch information
hkalodner committed May 13, 2022
1 parent 026db98 commit f32e9a1
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
11 changes: 11 additions & 0 deletions das/dasrpc/rpc_aggregator.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ package dasrpc

import (
"encoding/json"

"github.com/offchainlabs/nitro/solgen/go/bridgegen"

"github.com/ethereum/go-ethereum/common"
"github.com/offchainlabs/nitro/arbutil"

Expand Down Expand Up @@ -33,6 +36,14 @@ func NewRPCAggregatorWithL1Info(config das.AggregatorConfig, l1client arbutil.L1
return das.NewAggregatorWithL1Info(config, services, l1client, seqInboxAddress)
}

func NewRPCAggregatorWithSeqInboxCaller(config das.AggregatorConfig, seqInboxCaller *bridgegen.SequencerInboxCaller) (*das.Aggregator, error) {
services, err := setUpServices(config)
if err != nil {
return nil, err
}
return das.NewAggregatorWithSeqInboxCaller(config, services, seqInboxCaller)
}

func setUpServices(config das.AggregatorConfig) ([]das.ServiceDetails, error) {
var cs []BackendConfig
err := json.Unmarshal([]byte(config.Backends), &cs)
Expand Down
65 changes: 65 additions & 0 deletions das/dasrpc/rpc_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package dasrpc

import (
"bytes"
"context"
"encoding/base64"
"encoding/json"
"net"
"testing"

"github.com/offchainlabs/nitro/blsSignatures"
"github.com/offchainlabs/nitro/das"
"github.com/offchainlabs/nitro/util/testhelpers"
)

func blsPubToBase64(pubkey *blsSignatures.PublicKey) string {
pubkeyBytes := blsSignatures.PublicKeyToBytes(*pubkey)
encodedPubkey := make([]byte, base64.StdEncoding.EncodedLen(len(pubkeyBytes)))
base64.StdEncoding.Encode(encodedPubkey, pubkeyBytes)
return string(encodedPubkey)
}

func TestRPC(t *testing.T) {
ctx := context.Background()
lis, err := net.Listen("tcp", "localhost:0")
testhelpers.RequireImpl(t, err)
keyDir := t.TempDir()
dataDir := t.TempDir()
pubkey, _, err := das.GenerateAndStoreKeys(keyDir)
testhelpers.RequireImpl(t, err)
dasConfig := das.LocalDiskDASConfig{
KeyDir: keyDir,
DataDir: dataDir,
}
localDas, err := das.NewLocalDiskDASWithSeqInboxCaller(dasConfig, nil)
testhelpers.RequireImpl(t, err)
dasServer, err := StartDASRPCServerOnListener(ctx, lis, localDas)
defer dasServer.Shutdown(ctx)
testhelpers.RequireImpl(t, err)
config := BackendConfig{
URL: "http://" + lis.Addr().String(),
PubKeyBase64Encoded: blsPubToBase64(pubkey),
SignerMask: 1,
}

backendsJsonByte, err := json.Marshal([]BackendConfig{config})
testhelpers.RequireImpl(t, err)
aggConf := das.AggregatorConfig{
AssumedHonest: 1,
Backends: string(backendsJsonByte),
}
rpcAgg, err := NewRPCAggregatorWithSeqInboxCaller(aggConf, nil)
testhelpers.RequireImpl(t, err)

msg := testhelpers.RandomizeSlice(make([]byte, 100))
cert, err := rpcAgg.Store(ctx, msg, 0, nil)
testhelpers.RequireImpl(t, err)

retrievedMessage, err := rpcAgg.Retrieve(ctx, cert)
testhelpers.RequireImpl(t, err)

if !bytes.Equal(msg, retrievedMessage) {
testhelpers.FailImpl(t, "failed to retrieve correct message")
}
}

0 comments on commit f32e9a1

Please sign in to comment.