diff --git a/das/dasrpc/rpc_aggregator.go b/das/dasrpc/rpc_aggregator.go index 3e6d22a9eb..7800a00652 100644 --- a/das/dasrpc/rpc_aggregator.go +++ b/das/dasrpc/rpc_aggregator.go @@ -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" @@ -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) diff --git a/das/dasrpc/rpc_test.go b/das/dasrpc/rpc_test.go new file mode 100644 index 0000000000..146a475424 --- /dev/null +++ b/das/dasrpc/rpc_test.go @@ -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") + } +}