Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(debug) nonce #369

Merged
merged 15 commits into from
Mar 8, 2024
30 changes: 19 additions & 11 deletions integration-tests/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,14 @@ type TestEnvDetails struct {
}

type RPCDetails struct {
RPCL1Internal string
RPCL2Internal string
RPCL1External string
RPCL2External string
MockServerUrl string
MockServerEndpoint string
P2PPort string
RPCL1Internal string
RPCL2Internal string
RPCL2InternalApiKey string
RPCL1External string
RPCL2External string
MockServerUrl string
MockServerEndpoint string
P2PPort string
}

func New(testConfig *testconfig.TestConfig) *Common {
Expand All @@ -72,6 +73,11 @@ func New(testConfig *testconfig.TestConfig) *Common {
if *testConfig.Common.Network == "testnet" {
chainDetails = chainconfig.SepoliaConfig()
chainDetails.L2RPCInternal = *testConfig.Common.L2RPCUrl
if testConfig.Common.L2RPCApiKey == nil {
chainDetails.L2RPCInternalApiKey = ""
} else {
chainDetails.L2RPCInternalApiKey = *testConfig.Common.L2RPCApiKey
}
} else {
// set up mocked local feedernet server because starknet-devnet does not provide one
localDevnetFeederSrv := starknet.NewTestServer()
Expand All @@ -85,8 +91,9 @@ func New(testConfig *testconfig.TestConfig) *Common {
TestDuration: duration,
},
RPCDetails: &RPCDetails{
P2PPort: "6690",
RPCL2Internal: chainDetails.L2RPCInternal,
P2PPort: "6690",
RPCL2Internal: chainDetails.L2RPCInternal,
RPCL2InternalApiKey: chainDetails.L2RPCInternalApiKey,
},
}

Expand Down Expand Up @@ -147,8 +154,9 @@ func (c *Common) DefaultNodeConfig() *cl.Config {
FeederURL: common_cfg.MustParseURL(c.ChainDetails.FeederURL),
Nodes: []*config.Node{
{
Name: ptr.Ptr("primary"),
URL: common_cfg.MustParseURL(c.RPCDetails.RPCL2Internal),
Name: ptr.Ptr("primary"),
URL: common_cfg.MustParseURL(c.RPCDetails.RPCL2Internal),
APIKey: ptr.Ptr(c.RPCDetails.RPCL2InternalApiKey),
},
},
}
Expand Down
5 changes: 3 additions & 2 deletions integration-tests/common/gauntlet_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/smartcontractkit/chainlink-starknet/integration-tests/utils"
"os"

"github.com/smartcontractkit/chainlink-starknet/integration-tests/utils"
)

func (m *OCRv2TestState) fundNodes() ([]string, error) {
Expand All @@ -26,7 +27,7 @@ func (m *OCRv2TestState) fundNodes() ([]string, error) {
for _, key := range nAccounts {
// We are not deploying in parallel here due to testnet limitations (429 too many requests)
l.Debug().Msg(fmt.Sprintf("Funding node with address: %s", key))
_, err := m.Clients.GauntletClient.TransferToken(m.Common.ChainDetails.StarkTokenAddress, key, "100000000000000000") // Transferring 0.1 STRK to each node
_, err := m.Clients.GauntletClient.TransferToken(m.Common.ChainDetails.StarkTokenAddress, key, "1000000000000000000") // Transferring 0.1 STRK to each node
if err != nil {
return nil, err
}
Expand Down
12 changes: 7 additions & 5 deletions integration-tests/common/test_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,23 @@ package common
import (
"context"
"fmt"
"net/http"

starknetdevnet "github.com/NethermindEth/starknet.go/devnet"
"github.com/go-resty/resty/v2"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/smartcontractkit/chainlink-common/pkg/logger"
test_env_ctf "github.com/smartcontractkit/chainlink-testing-framework/docker/test_env"
"net/http"

"math/big"
"testing"
"time"

test_env_starknet "github.com/smartcontractkit/chainlink-starknet/integration-tests/docker/test_env"
"github.com/smartcontractkit/chainlink-starknet/integration-tests/testconfig"
"github.com/smartcontractkit/chainlink-testing-framework/logging"
"github.com/smartcontractkit/chainlink/integration-tests/docker/test_env"
"math/big"
"testing"
"time"

"github.com/NethermindEth/juno/core/felt"
starknetutils "github.com/NethermindEth/starknet.go/utils"
Expand Down Expand Up @@ -200,7 +202,7 @@ func (m *OCRv2TestState) DeployCluster() {
require.NoError(m.TestConfig.T, m.TestConfig.err)
}
lggr := logger.Nop()
m.Clients.StarknetClient, m.TestConfig.err = starknet.NewClient(m.Common.ChainDetails.ChainID, m.Common.RPCDetails.RPCL2External, lggr, &rpcRequestTimeout)
m.Clients.StarknetClient, m.TestConfig.err = starknet.NewClient(m.Common.ChainDetails.ChainID, m.Common.RPCDetails.RPCL2External, m.Common.RPCDetails.RPCL2InternalApiKey, lggr, &rpcRequestTimeout)
require.NoError(m.TestConfig.T, m.TestConfig.err, "Creating starknet client should not fail")
m.Clients.OCR2Client, m.TestConfig.err = ocr2.NewClient(m.Clients.StarknetClient, lggr)
require.NoError(m.TestConfig.T, m.TestConfig.err, "Creating ocr2 client should not fail")
Expand Down
13 changes: 7 additions & 6 deletions integration-tests/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ var (
)

type Config struct {
ChainName string
ChainID string
StarkTokenAddress string
L2RPCInternal string
TokenName string
FeederURL string
ChainName string
ChainID string
StarkTokenAddress string
L2RPCInternal string
L2RPCInternalApiKey string
TokenName string
FeederURL string
}

func SepoliaConfig() *Config {
Expand Down
11 changes: 7 additions & 4 deletions integration-tests/testconfig/testconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import (
"embed"
"encoding/base64"
"fmt"
"github.com/smartcontractkit/chainlink-testing-framework/docker/test_env"
"os"
"strings"

"github.com/smartcontractkit/chainlink-testing-framework/docker/test_env"

"github.com/barkimedes/go-deepcopy"
"github.com/google/uuid"
"github.com/pelletier/go-toml/v2"
Expand Down Expand Up @@ -109,9 +110,11 @@ func (c *TestConfig) AsBase64() (string, error) {
}

type Common struct {
Network *string `toml:"network"`
InsideK8s *bool `toml:"inside_k8"`
User *string `toml:"user"`
Network *string `toml:"network"`
InsideK8s *bool `toml:"inside_k8"`
User *string `toml:"user"`
// if rpc requires api key to be passed as an HTTP header
L2RPCApiKey *string `toml:"l2_rpc_url_api_key"`
L2RPCUrl *string `toml:"l2_rpc_url"`
PrivateKey *string `toml:"private_key"`
Account *string `toml:"account"`
Expand Down
1 change: 1 addition & 0 deletions monitoring/cmd/monitoring/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func main() {
starknetClient, err := starknet.NewClient(
starknetConfig.GetChainID(),
starknetConfig.GetRPCEndpoint(),
starknetConfig.GetRPCApiKey(),
logger.With(log, "component", "starknet-client"),
&readTimeout,
)
Expand Down
5 changes: 5 additions & 0 deletions monitoring/pkg/monitoring/config_chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

type StarknetConfig struct {
rpcEndpoint string
rpcApiKey string
networkName string
networkID string
chainID string
Expand All @@ -22,6 +23,7 @@ type StarknetConfig struct {
var _ relayMonitoring.ChainConfig = StarknetConfig{}

func (s StarknetConfig) GetRPCEndpoint() string { return s.rpcEndpoint }
func (s StarknetConfig) GetRPCApiKey() string { return s.rpcApiKey }
func (s StarknetConfig) GetNetworkName() string { return s.networkName }
func (s StarknetConfig) GetNetworkID() string { return s.networkID }
func (s StarknetConfig) GetChainID() string { return s.chainID }
Expand Down Expand Up @@ -54,6 +56,9 @@ func parseEnvVars(cfg *StarknetConfig) error {
if value, isPresent := os.LookupEnv("STARKNET_RPC_ENDPOINT"); isPresent {
cfg.rpcEndpoint = value
}
if value, isPresent := os.LookupEnv("STARKNET_RPC_API_KEY"); isPresent {
cfg.rpcApiKey = value
}
if value, isPresent := os.LookupEnv("STARKNET_NETWORK_NAME"); isPresent {
cfg.networkName = value
}
Expand Down
2 changes: 1 addition & 1 deletion relayer/pkg/chainlink/chain/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ func (c *chain) getClient() (*starknet.Client, error) {
for _, i := range index {
node = nodes[i]
// create client and check
client, err = starknet.NewClient(node.ChainID, node.URL, c.lggr, &timeout)
client, err = starknet.NewClient(node.ChainID, node.URL, node.APIKey, c.lggr, &timeout)
// if error, try another node
if err != nil {
c.lggr.Warnw("failed to create node", "name", node.Name, "starknet-url", node.URL, "err", err.Error())
Expand Down
3 changes: 3 additions & 0 deletions relayer/pkg/chainlink/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ func (c *Chain) SetDefaults() {
type Node struct {
Name *string
URL *config.URL
// only if rpc url needs api key passed in header
APIKey *string
}

type TOMLConfigs []*TOMLConfig
Expand Down Expand Up @@ -227,6 +229,7 @@ func legacyNode(n *Node, id string) db.Node {
Name: *n.Name,
ChainID: id,
URL: (*url.URL)(n.URL).String(),
APIKey: *n.APIKey,
}
}

Expand Down
1 change: 1 addition & 0 deletions relayer/pkg/chainlink/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ type Node struct {
Name string
ChainID string `db:"starknet_chain_id"`
URL string
APIKey string
CreatedAt time.Time
UpdatedAt time.Time
}
2 changes: 1 addition & 1 deletion relayer/pkg/chainlink/ocr2/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func TestOCR2Client(t *testing.T) {

url := mockServer.URL
duration := 10 * time.Second
reader, err := starknet.NewClient(chainID, url, lggr, &duration)
reader, err := starknet.NewClient(chainID, url, "", lggr, &duration)
require.NoError(t, err)
client, err := NewClient(reader, lggr)
assert.NoError(t, err)
Expand Down
32 changes: 16 additions & 16 deletions relayer/pkg/chainlink/txm/nonce.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type NonceManager interface {
NextSequence(address *felt.Felt, chainID string) (*felt.Felt, error)
IncrementNextSequence(address *felt.Felt, chainID string, currentNonce *felt.Felt) error
// Resets local account nonce to on-chain account nonce
Sync(ctx context.Context, address *felt.Felt, chainId string, client NonceManagerClient) error
Sync(ctx context.Context, address *felt.Felt, publicKey *felt.Felt, chainId string, client NonceManagerClient) error
}

var _ NonceManager = (*nonceManager)(nil)
Expand Down Expand Up @@ -64,7 +64,7 @@ func (nm *nonceManager) HealthReport() map[string]error {
return map[string]error{nm.Name(): nm.starter.Healthy()}
}

func (nm *nonceManager) Sync(ctx context.Context, address *felt.Felt, chainId string, client NonceManagerClient) error {
func (nm *nonceManager) Sync(ctx context.Context, address *felt.Felt, publicKey *felt.Felt, chainId string, client NonceManagerClient) error {
if err := nm.validate(address, chainId); err != nil {
return err
}
Expand All @@ -76,7 +76,7 @@ func (nm *nonceManager) Sync(ctx context.Context, address *felt.Felt, chainId st
return err
}

nm.n[address.String()][chainId] = n
nm.n[publicKey.String()][chainId] = n

return nil
}
Expand All @@ -101,40 +101,40 @@ func (nm *nonceManager) Register(ctx context.Context, addr *felt.Felt, publicKey
return nil
}

func (nm *nonceManager) NextSequence(addr *felt.Felt, chainId string) (*felt.Felt, error) {
if err := nm.validate(addr, chainId); err != nil {
func (nm *nonceManager) NextSequence(publicKey *felt.Felt, chainId string) (*felt.Felt, error) {
if err := nm.validate(publicKey, chainId); err != nil {
return nil, err
}

nm.lock.RLock()
defer nm.lock.RUnlock()
return nm.n[addr.String()][chainId], nil
return nm.n[publicKey.String()][chainId], nil
}

func (nm *nonceManager) IncrementNextSequence(addr *felt.Felt, chainId string, currentNonce *felt.Felt) error {
if err := nm.validate(addr, chainId); err != nil {
func (nm *nonceManager) IncrementNextSequence(publicKey *felt.Felt, chainId string, currentNonce *felt.Felt) error {
if err := nm.validate(publicKey, chainId); err != nil {
return err
}

nm.lock.Lock()
defer nm.lock.Unlock()
n := nm.n[addr.String()][chainId]
n := nm.n[publicKey.String()][chainId]
if n.Cmp(currentNonce) != 0 {
return fmt.Errorf("mismatched nonce for %s: %s (expected) != %s (got)", addr, n, currentNonce)
return fmt.Errorf("mismatched nonce for %s: %s (expected) != %s (got)", publicKey, n, currentNonce)
}
one := new(felt.Felt).SetUint64(1)
nm.n[addr.String()][chainId] = new(felt.Felt).Add(n, one)
nm.n[publicKey.String()][chainId] = new(felt.Felt).Add(n, one)
return nil
}

func (nm *nonceManager) validate(addr *felt.Felt, chainId string) error {
func (nm *nonceManager) validate(publicKey *felt.Felt, chainId string) error {
nm.lock.RLock()
defer nm.lock.RUnlock()
if _, exists := nm.n[addr.String()]; !exists {
return fmt.Errorf("nonce tracking does not exist for key: %s", addr.String())
if _, exists := nm.n[publicKey.String()]; !exists {
return fmt.Errorf("nonce tracking does not exist for key: %s", publicKey.String())
}
if _, exists := nm.n[addr.String()][chainId]; !exists {
return fmt.Errorf("nonce does not exist for key: %s and chain: %s", addr.String(), chainId)
if _, exists := nm.n[publicKey.String()][chainId]; !exists {
return fmt.Errorf("nonce does not exist for key: %s and chain: %s", publicKey.String(), chainId)
}
return nil
}
Loading
Loading