Skip to content

Commit

Permalink
warp get message test
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivan Sukach committed Dec 12, 2024
1 parent 189ed2c commit c4ea780
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 15 deletions.
19 changes: 18 additions & 1 deletion vm/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func (rpc *RPC) Routes() map[string]*jsonrpc.RPCFunc {
"abci_info": jsonrpc.NewRPCFunc(rpc.ABCIInfo, "", jsonrpc.Cacheable()),

// warp
"warp_get_message": jsonrpc.NewRPCFunc(rpc.vm.warpService.GetMessage, "messageID"),
"warp_get_message": jsonrpc.NewRPCFunc(rpc.GetMessage, "messageID"),
"warp_get_message_signature": jsonrpc.NewRPCFunc(rpc.vm.warpService.GetMessageSignature, "messageID"),
"warp_get_message_aggregate_signature": jsonrpc.NewRPCFunc(rpc.vm.warpService.GetMessageAggregateSignature, "messageID,quorumNum,subnetID"),
"warp_get_block_aggregate_signature": jsonrpc.NewRPCFunc(rpc.vm.warpService.GetBlockAggregateSignature, "blockID,quorumNum,subnetID"),
Expand Down Expand Up @@ -805,3 +805,20 @@ func (rpc *RPC) Status(_ *rpctypes.Context) (*ctypes.ResultStatus, error) {

return result, nil
}

// Message content
type ResultGetMessage struct {
Message []byte `json:"message"`
}

func (rpc *RPC) GetMessage(_ *rpctypes.Context, messageID string) (*ResultGetMessage, error) {
msgID, err := ids.FromString(messageID)
if err != nil {
return nil, err
}
msgContent, err := rpc.vm.warpService.GetMessage(context.Background(), msgID)
if err != nil {
return nil, err
}
return &ResultGetMessage{Message: msgContent.Bytes()}, nil
}
19 changes: 19 additions & 0 deletions vm/rpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package vm

import (
"context"
"github.com/cometbft/cometbft/libs/rand"
"github.com/landslidenetwork/slide-sdk/utils/ids"
warputils "github.com/landslidenetwork/slide-sdk/utils/warp"
"net/http"
"testing"
"time"
Expand Down Expand Up @@ -59,6 +62,22 @@ func TestStatus(t *testing.T) {
t.Logf("Status result %+v", result)
}

func TestWarpGetMessage(t *testing.T) {
server, vm, rpcClient := setupRPC(t)
defer server.Close()

chainID, err := ids.ToID(vm.appOpts.ChainID)
require.NoError(t, err)
testUnsignedMessage, err := warputils.NewUnsignedMessage(vm.appOpts.NetworkID, chainID, []byte(rand.Str(30)))

Check failure on line 71 in vm/rpc_test.go

View workflow job for this annotation

GitHub Actions / build

this value of err is never used (SA4006)
vm.warpBackend.AddMessage(testUnsignedMessage)
result := new(ResultGetMessage)
_, err = rpcClient.Call(context.Background(), "warp_get_message", map[string]interface{}{"messageID": testUnsignedMessage.ID().String()}, result)
require.NoError(t, err)
t.Log(result.Message)
t.Log(testUnsignedMessage.Bytes())
require.Equal(t, result.Message, testUnsignedMessage.Bytes())
}

// TestRPC is a test RPC server for the LandslideVM.
type TestRPC struct {
vm *LandslideVM
Expand Down
19 changes: 10 additions & 9 deletions vm/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ type (
// Avalanche Warp Messaging backend
// Used to serve BLS signatures of warp messages over RPC
warpBackend warp.Backend
warpService *warp.API
warpService *API

clientConn grpc.ClientConnInterface
optClientConn *grpc.ClientConn
Expand Down Expand Up @@ -452,16 +452,17 @@ func (vm *LandslideVM) Initialize(_ context.Context, req *vmpb.InitializeRequest
parentHash := block.ParentHash(blk)

warpDB := dbm.NewPrefixDB(vm.database, dbPrefixWarp)
if vm.config.BLSSecretKey == nil {
if err != nil {
return nil, err
}
}
// TODO: implement bls secret key check
//if vm.config.BLSSecretKey == nil {
// if err != nil {
// return nil, err
// }
//}
chainID, err := ids.ToID(req.ChainId)
if err != nil {
return nil, err
}
warpSigner := warputils.NewSigner(vm.config.BLSSecretKey, req.NetworkId, chainID)
warpSigner := warputils.NewSigner(&vm.config.BLSSecretKey, req.NetworkId, chainID)
vm.warpBackend = warp.NewBackend(
req.NetworkId,
chainID,
Expand All @@ -470,11 +471,11 @@ func (vm *LandslideVM) Initialize(_ context.Context, req *vmpb.InitializeRequest
warpDB,
)

subnetID, err := ids.ToID(req.ChainId)
subnetID, err := ids.ToID(req.SubnetId)
if err != nil {
return nil, err
}
vm.warpService = warp.NewAPI(req.NetworkId, subnetID, chainID, vm.warpBackend)
vm.warpService = NewAPI(vm, req.NetworkId, subnetID, chainID, vm.warpBackend)

return &vmpb.InitializeResponse{
LastAcceptedId: blk.Hash(),
Expand Down
3 changes: 3 additions & 0 deletions vm/vm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package vm
import (
"context"
_ "embed"
"github.com/cometbft/cometbft/libs/rand"
"net"
"testing"

Expand Down Expand Up @@ -58,6 +59,8 @@ func newKvApp(t *testing.T, vmdb, appdb dbm.DB) vmpb.VMServer {
initRes, err := vm.Initialize(context.TODO(), &vmpb.InitializeRequest{
DbServerAddr: "inmemory",
GenesisBytes: kvstorevmGenesis,
ChainId: []byte(rand.Str(32)),
SubnetId: []byte(rand.Str(32)),
})
require.NoError(t, err)
require.NotNil(t, initRes)
Expand Down
11 changes: 6 additions & 5 deletions warp/service.go → vm/warp_service.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// (c) 2023, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

package warp
package vm

import (
"context"
Expand All @@ -11,20 +11,21 @@ import (
"github.com/landslidenetwork/slide-sdk/utils/ids"
warputils "github.com/landslidenetwork/slide-sdk/utils/warp"
"github.com/landslidenetwork/slide-sdk/utils/warp/payload"
"github.com/landslidenetwork/slide-sdk/vm"
"github.com/landslidenetwork/slide-sdk/warp"
)

// API introduces snowman specific functionality to the evm
type API struct {
vm vm.LandslideVM
vm *LandslideVM
logger log.Logger

Check failure on line 20 in vm/warp_service.go

View workflow job for this annotation

GitHub Actions / build

field logger is unused (U1000)
networkID uint32
sourceSubnetID, sourceChainID ids.ID
backend Backend
backend warp.Backend
}

func NewAPI(networkID uint32, sourceSubnetID ids.ID, sourceChainID ids.ID, backend Backend) *API {
func NewAPI(vm *LandslideVM, networkID uint32, sourceSubnetID ids.ID, sourceChainID ids.ID, backend warp.Backend) *API {
return &API{
vm: vm,
networkID: networkID,
sourceSubnetID: sourceSubnetID,
sourceChainID: sourceChainID,
Expand Down

0 comments on commit c4ea780

Please sign in to comment.