From 55a4e8130aa15505b44c3684c62438cedcd8b6bf Mon Sep 17 00:00:00 2001 From: Omri Date: Wed, 11 Jan 2023 17:09:25 +0100 Subject: [PATCH] Added the ability to pass the proposer pub key to the mock settlement config. (#182) --- block/manager_test.go | 3 ++- rpc/client/client_test.go | 5 +++-- rpc/json/service_test.go | 3 ++- settlement/mock/mock.go | 20 ++++++++++++++++++-- 4 files changed, 25 insertions(+), 6 deletions(-) diff --git a/block/manager_test.go b/block/manager_test.go index a42c0a54b..8c4988234 100644 --- a/block/manager_test.go +++ b/block/manager_test.go @@ -3,6 +3,7 @@ package block import ( "context" "crypto/rand" + "encoding/hex" "encoding/json" "sync/atomic" "testing" @@ -450,7 +451,7 @@ func initSettlementLayerMock(settlementlc settlement.LayerClient, batchSize uint Config: &settlement.Config{ BatchSize: batchSize, }, - ProposerPubKey: proposerPubKey, + ProposerPubKey: hex.EncodeToString(proposerPubKey), } byteconf, _ := json.Marshal(conf) err := settlementlc.Init(byteconf, pubsubServer, logger) diff --git a/rpc/client/client_test.go b/rpc/client/client_test.go index a0a55fe6a..dbebe76b3 100644 --- a/rpc/client/client_test.go +++ b/rpc/client/client_test.go @@ -3,6 +3,7 @@ package client import ( "context" crand "crypto/rand" + "encoding/hex" "encoding/json" "fmt" "math/rand" @@ -410,7 +411,7 @@ func TestTx(t *testing.T) { require.NoError(err) proposerPubKeyBytes, err := proposerPubKey.Raw() - settlementLayerConfig, err := json.Marshal(slmock.Config{ProposerPubKey: proposerPubKeyBytes}) + settlementLayerConfig, err := json.Marshal(slmock.Config{ProposerPubKey: hex.EncodeToString(proposerPubKeyBytes)}) require.NoError(err) node, err := node.NewNode(context.Background(), config.NodeConfig{ @@ -658,7 +659,7 @@ func TestValidatorSetHandling(t *testing.T) { require.NoError(err) proposerPubKeyBytes, err := proposerPubKey.Raw() - settlementLayerConfig, err := json.Marshal(slmock.Config{ProposerPubKey: proposerPubKeyBytes}) + settlementLayerConfig, err := json.Marshal(slmock.Config{ProposerPubKey: hex.EncodeToString(proposerPubKeyBytes)}) require.NoError(err) vKeys := make([]tmcrypto.PrivKey, 4) diff --git a/rpc/json/service_test.go b/rpc/json/service_test.go index a4a69cfb2..545b8367d 100644 --- a/rpc/json/service_test.go +++ b/rpc/json/service_test.go @@ -4,6 +4,7 @@ import ( "bytes" "context" "crypto/rand" + "encoding/hex" "encoding/json" "fmt" "net/http" @@ -292,7 +293,7 @@ func getRPC(t *testing.T) (*mocks.Application, *client.Client) { key, _, _ := crypto.GenerateEd25519Key(rand.Reader) signingKey, proposerPubKey, _ := crypto.GenerateEd25519Key(rand.Reader) proposerPubKeyBytes, err := proposerPubKey.Raw() - settlementLayerConfig, err := json.Marshal(slmock.Config{ProposerPubKey: proposerPubKeyBytes}) + settlementLayerConfig, err := json.Marshal(slmock.Config{ProposerPubKey: hex.EncodeToString(proposerPubKeyBytes)}) require.NoError(err) config := config.NodeConfig{Aggregator: true, DALayer: "mock", SettlementLayer: "mock", BlockManagerConfig: config.BlockManagerConfig{BlockTime: 1 * time.Second, BatchSyncInterval: time.Second}, SettlementConfig: string(settlementLayerConfig)} node, err := node.NewNode(context.Background(), config, key, signingKey, proxy.NewLocalClientCreator(app), &types.GenesisDoc{ChainID: "test"}, log.TestingLogger()) diff --git a/settlement/mock/mock.go b/settlement/mock/mock.go index 4bf6bc5a1..e26e554d6 100644 --- a/settlement/mock/mock.go +++ b/settlement/mock/mock.go @@ -3,10 +3,14 @@ package mock import ( "context" "encoding/binary" + "encoding/hex" "encoding/json" "errors" + "strings" "sync/atomic" + tmp2p "github.com/tendermint/tendermint/p2p" + "github.com/cosmos/cosmos-sdk/crypto/keys/ed25519" rollapptypes "github.com/dymensionxyz/dymension/x/rollapp/types" "github.com/dymensionxyz/dymint/da" @@ -58,7 +62,7 @@ type Config struct { *settlement.Config DBPath string `json:"db_path"` RootDir string `json:"root_dir"` - ProposerPubKey []byte `json:"proposer_pub_key"` + ProposerPubKey string `json:"proposer_pub_key"` store store.KVStore } @@ -143,6 +147,14 @@ func getConfig(config []byte) (*Config, error) { } else { conf.store = store.NewDefaultInMemoryKVStore() } + // If the proposer pub key is a path, load the key from the file. otherwise, use the key as is. + if strings.HasSuffix(string(conf.ProposerPubKey), ".json") { + key, err := tmp2p.LoadOrGenNodeKey(string(conf.ProposerPubKey)) + if err != nil { + return nil, err + } + conf.ProposerPubKey = hex.EncodeToString(key.PubKey().Bytes()) + } } else { conf = &Config{ store: store.NewDefaultInMemoryKVStore(), @@ -203,7 +215,11 @@ func (c *HubClient) GetBatchAtIndex(rollappID string, index uint64) (*settlement // GetSequencers returns a list of sequencers. Currently only returns a single sequencer func (c *HubClient) GetSequencers(rollappID string) ([]*types.Sequencer, error) { - pubKey := &ed25519.PubKey{Key: c.config.ProposerPubKey} + pubKeyBytes, err := hex.DecodeString(c.config.ProposerPubKey) + if err != nil { + return nil, err + } + pubKey := &ed25519.PubKey{Key: pubKeyBytes} return []*types.Sequencer{ { PublicKey: pubKey,