Skip to content

Commit

Permalink
scripts: keystone: Allow overriding PublicKeys.json/NodeList.txt loca…
Browse files Browse the repository at this point in the history
…tion
  • Loading branch information
archseer committed Aug 8, 2024
1 parent a48c0a6 commit 2013be4
Show file tree
Hide file tree
Showing 13 changed files with 133 additions and 53 deletions.
1 change: 1 addition & 0 deletions core/scripts/keystone/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
!*-sample.sh
keystone
.cache/
artefacts/
49 changes: 34 additions & 15 deletions core/scripts/keystone/src/01_deploy_contracts_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,12 @@ func (g *deployContracts) Run(args []string) {
skipFunding := fs.Bool("skipfunding", false, "skip funding the transmitters")
onlySetConfig := fs.Bool("onlysetconfig", false, "set the config on the OCR3 contract without deploying the contracts or funding transmitters")
dryRun := fs.Bool("dryrun", false, "dry run, don't actually deploy the contracts and do not fund transmitters")
publicKeys := fs.String("publickeys", "", "Custom public keys json location")
nodeList := fs.String("nodes", "", "Custom node list location")
artefactsDir := fs.String("artefacts", "", "Custom artefacts directory location")

err := fs.Parse(args)

if err != nil ||
*ocrConfigFile == "" || ocrConfigFile == nil ||
*ethUrl == "" || ethUrl == nil ||
Expand All @@ -63,11 +67,21 @@ func (g *deployContracts) Run(args []string) {
os.Exit(1)
}

if *artefactsDir == "" {
*artefactsDir = defaultArtefactsDir
}
if *publicKeys == "" {
*publicKeys = defaultPublicKeys
}
if *nodeList == "" {
*nodeList = defaultNodeList
}

os.Setenv("ETH_URL", *ethUrl)
os.Setenv("ETH_CHAIN_ID", fmt.Sprintf("%d", *chainID))
os.Setenv("ACCOUNT_KEY", *accountKey)

deploy(*ocrConfigFile, *skipFunding, *dryRun, *onlySetConfig)
deploy(*nodeList, *publicKeys, *ocrConfigFile, *skipFunding, *dryRun, *onlySetConfig, *artefactsDir)
}

// deploy does the following:
Expand All @@ -77,16 +91,20 @@ func (g *deployContracts) Run(args []string) {
// 4. Writes the deployed contract addresses to a file
// 5. Funds the transmitters
func deploy(
nodeList string,
publicKeys string,
configFile string,
skipFunding bool,
dryRun bool,
onlySetConfig bool,
artefacts string,
) {
env := helpers.SetupEnv(false)
ocrConfig := generateOCR3Config(
nodeList,
configFile,
env.ChainID,
".cache/PublicKeys.json",
publicKeys,
)

if dryRun {
Expand All @@ -96,11 +114,11 @@ func deploy(

if onlySetConfig {
fmt.Println("Skipping deployment of contracts and skipping funding transmitters, only setting config")
setOCR3Config(env, ocrConfig)
setOCR3Config(env, ocrConfig, artefacts)
return
}

if ContractsAlreadyDeployed() {
if ContractsAlreadyDeployed(artefacts) {
fmt.Println("Contracts already deployed")
return
}
Expand All @@ -118,10 +136,10 @@ func deploy(
jsonBytes, err := json.Marshal(contracts)
PanicErr(err)

err = os.WriteFile(DeployedContractsFilePath(), jsonBytes, 0600)
err = os.WriteFile(DeployedContractsFilePath(artefacts), jsonBytes, 0600)
PanicErr(err)

setOCR3Config(env, ocrConfig)
setOCR3Config(env, ocrConfig, artefacts)

if skipFunding {
fmt.Println("Skipping funding transmitters")
Expand All @@ -139,8 +157,9 @@ func deploy(
func setOCR3Config(
env helpers.Environment,
ocrConfig orc2drOracleConfig,
artefacts string,
) {
loadedContracts, err := LoadDeployedContracts()
loadedContracts, err := LoadDeployedContracts(artefacts)
PanicErr(err)

ocrContract, err := ocr3_capability.NewOCR3Capability(loadedContracts.OCRContract, env.Ec)
Expand All @@ -161,16 +180,16 @@ func setOCR3Config(
loadedContracts.SetConfigTxBlock = receipt.BlockNumber.Uint64()
jsonBytes, err := json.Marshal(loadedContracts)
PanicErr(err)
err = os.WriteFile(DeployedContractsFilePath(), jsonBytes, 0600)
err = os.WriteFile(DeployedContractsFilePath(artefacts), jsonBytes, 0600)
PanicErr(err)
}

func LoadDeployedContracts() (deployedContracts, error) {
if !ContractsAlreadyDeployed() {
func LoadDeployedContracts(artefacts string) (deployedContracts, error) {
if !ContractsAlreadyDeployed(artefacts) {
return deployedContracts{}, fmt.Errorf("no deployed contracts found, run deploy first")
}

jsonBytes, err := os.ReadFile(DeployedContractsFilePath())
jsonBytes, err := os.ReadFile(DeployedContractsFilePath(artefacts))
if err != nil {
return deployedContracts{}, err
}
Expand All @@ -180,13 +199,13 @@ func LoadDeployedContracts() (deployedContracts, error) {
return contracts, err
}

func ContractsAlreadyDeployed() bool {
_, err := os.Stat(DeployedContractsFilePath())
func ContractsAlreadyDeployed(artefacts string) bool {
_, err := os.Stat(DeployedContractsFilePath(artefacts))
return err == nil
}

func DeployedContractsFilePath() string {
return filepath.Join(artefactsDir, deployedContractsJSON)
func DeployedContractsFilePath(artefacts string) string {
return filepath.Join(artefacts, deployedContractsJSON)
}

func DeployForwarder(e helpers.Environment) *forwarder.KeystoneForwarder {
Expand Down
29 changes: 24 additions & 5 deletions core/scripts/keystone/src/02_deploy_jobspecs_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ import (
"github.com/smartcontractkit/chainlink/v2/core/cmd"
)

type deployJobSpecs struct {
}
type deployJobSpecs struct{}

func NewDeployJobSpecsCommand() *deployJobSpecs {
return &deployJobSpecs{}
Expand All @@ -32,6 +31,11 @@ func (g *deployJobSpecs) Run(args []string) {
chainID := fs.Int64("chainid", 11155111, "chain id")
p2pPort := fs.Int64("p2pport", 6690, "p2p port")
onlyReplay := fs.Bool("onlyreplay", false, "only replay the block from the OCR3 contract setConfig transaction")
templatesLocation := fs.String("templates", "", "Custom templates location")
nodeList := fs.String("nodes", "", "Custom node list location")
publicKeys := fs.String("publickeys", "", "Custom public keys json location")
artefactsDir := fs.String("artefacts", "", "Custom artefacts directory location")

err := fs.Parse(args)
if err != nil || chainID == nil || *chainID == 0 || p2pPort == nil || *p2pPort == 0 || onlyReplay == nil {
fs.Usage()
Expand All @@ -43,12 +47,27 @@ func (g *deployJobSpecs) Run(args []string) {
fmt.Println("Deploying OCR3 job specs")
}

nodes := downloadNodeAPICredentialsDefault()
deployedContracts, err := LoadDeployedContracts()
if *artefactsDir == "" {
*artefactsDir = defaultArtefactsDir
}
if *publicKeys == "" {
*publicKeys = defaultPublicKeys
}
if *nodeList == "" {
*nodeList = defaultNodeList
}
if *templatesLocation == "" {
*templatesLocation = "templates"
}

nodes := downloadNodeAPICredentials(*nodeList)
deployedContracts, err := LoadDeployedContracts(*artefactsDir)
PanicErr(err)

jobspecs := genSpecs(
".cache/PublicKeys.json", ".cache/NodeList.txt", "templates",
*publicKeys,
*nodeList,
*templatesLocation,
*chainID, *p2pPort, deployedContracts.OCRContract.Hex(),
)
flattenedSpecs := []hostSpec{jobspecs.bootstrap}
Expand Down
29 changes: 21 additions & 8 deletions core/scripts/keystone/src/03_gen_crib_cluster_overrides_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ import (
helpers "github.com/smartcontractkit/chainlink/core/scripts/common"
)

type generateCribClusterOverrides struct {
}
type generateCribClusterOverrides struct{}

func NewGenerateCribClusterOverridesCommand() *generateCribClusterOverrides {
return &generateCribClusterOverrides{}
Expand All @@ -24,25 +23,39 @@ func (g *generateCribClusterOverrides) Run(args []string) {
fs := flag.NewFlagSet(g.Name(), flag.ContinueOnError)
chainID := fs.Int64("chainid", 11155111, "chain id")
outputPath := fs.String("outpath", "../crib", "the path to output the generated overrides")
publicKeys := fs.String("publickeys", "", "Custom public keys json location")
nodeList := fs.String("nodes", "", "Custom node list location")
artefactsDir := fs.String("artefacts", "", "Custom artefacts directory location")

deployedContracts, err := LoadDeployedContracts()
helpers.PanicErr(err)
templatesDir := "templates"
err = fs.Parse(args)
err := fs.Parse(args)
if err != nil || outputPath == nil || *outputPath == "" || chainID == nil || *chainID == 0 {
fs.Usage()
os.Exit(1)
}

lines := generateCribConfig(".cache/PublicKeys.json", chainID, templatesDir, deployedContracts.ForwarderContract.Hex())
if *artefactsDir == "" {
*artefactsDir = defaultArtefactsDir
}
if *publicKeys == "" {
*publicKeys = defaultPublicKeys
}
if *nodeList == "" {
*nodeList = defaultNodeList
}

deployedContracts, err := LoadDeployedContracts(*artefactsDir)
helpers.PanicErr(err)

lines := generateCribConfig(*nodeList, *publicKeys, chainID, templatesDir, deployedContracts.ForwarderContract.Hex())

cribOverridesStr := strings.Join(lines, "\n")
err = os.WriteFile(filepath.Join(*outputPath, "crib-cluster-overrides.yaml"), []byte(cribOverridesStr), 0600)
helpers.PanicErr(err)
}

func generateCribConfig(pubKeysPath string, chainID *int64, templatesDir string, forwarderAddress string) []string {
nca := downloadNodePubKeys(*chainID, pubKeysPath)
func generateCribConfig(nodeList string, pubKeysPath string, chainID *int64, templatesDir string, forwarderAddress string) []string {
nca := downloadNodePubKeys(nodeList, *chainID, pubKeysPath)
nodeAddresses := []string{}

for _, node := range nca[1:] {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func TestGenerateCribConfig(t *testing.T) {
forwarderAddress := "0x1234567890abcdef"
publicKeysPath := "./testdata/PublicKeys.json"

lines := generateCribConfig(publicKeysPath, &chainID, templatesDir, forwarderAddress)
lines := generateCribConfig(defaultNodeList, publicKeysPath, &chainID, templatesDir, forwarderAddress)

snaps.MatchSnapshot(t, strings.Join(lines, "\n"))
}
25 changes: 21 additions & 4 deletions core/scripts/keystone/src/04_delete_ocr3_jobs_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import (
"encoding/json"
"flag"
"fmt"
"os"

"github.com/urfave/cli"

helpers "github.com/smartcontractkit/chainlink/core/scripts/common"
)

type deleteJobs struct {
}
type deleteJobs struct{}

type OCRSpec struct {
ContractID string
Expand Down Expand Up @@ -43,9 +43,26 @@ func (g *deleteJobs) Name() string {
}

func (g *deleteJobs) Run(args []string) {
deployedContracts, err := LoadDeployedContracts()
fs := flag.NewFlagSet(g.Name(), flag.ContinueOnError)
nodeList := fs.String("nodes", "", "Custom node list location")
artefactsDir := fs.String("artefacts", "", "Custom artefacts directory location")

err := fs.Parse(args)
if err != nil {
fs.Usage()
os.Exit(1)
}

if *artefactsDir == "" {
*artefactsDir = defaultArtefactsDir
}
if *nodeList == "" {
*nodeList = defaultNodeList
}

deployedContracts, err := LoadDeployedContracts(*artefactsDir)
helpers.PanicErr(err)
nodes := downloadNodeAPICredentialsDefault()
nodes := downloadNodeAPICredentials(*nodeList)

for _, node := range nodes {
output := &bytes.Buffer{}
Expand Down
9 changes: 6 additions & 3 deletions core/scripts/keystone/src/06_deploy_workflows_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ import (
helpers "github.com/smartcontractkit/chainlink/core/scripts/common"
)

type deployWorkflows struct {
}
type deployWorkflows struct{}

func NewDeployWorkflowsCommand() *deployWorkflows {
return &deployWorkflows{}
Expand All @@ -26,15 +25,19 @@ func (g *deployWorkflows) Name() string {
func (g *deployWorkflows) Run(args []string) {
fs := flag.NewFlagSet(g.Name(), flag.ContinueOnError)
workflowFile := fs.String("workflow", "workflow.yml", "path to workflow file")
nodeList := fs.String("nodes", "", "Custom node list location")
err := fs.Parse(args)
if err != nil || workflowFile == nil || *workflowFile == "" {
fs.Usage()
os.Exit(1)
}
if *nodeList == "" {
*nodeList = defaultNodeList
}
fmt.Println("Deploying workflows")

// use a separate list
nodes := downloadNodeAPICredentialsDefault()
nodes := downloadNodeAPICredentials(*nodeList)

if _, err = os.Stat(*workflowFile); err != nil {
PanicErr(errors.New("toml file does not exist"))
Expand Down
19 changes: 16 additions & 3 deletions core/scripts/keystone/src/07_delete_workflows_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import (
"encoding/json"
"flag"
"fmt"
"os"

"github.com/urfave/cli"

helpers "github.com/smartcontractkit/chainlink/core/scripts/common"
)

type deleteWorkflows struct {
}
type deleteWorkflows struct{}

func NewDeleteWorkflowsCommand() *deleteWorkflows {
return &deleteWorkflows{}
Expand All @@ -23,7 +23,20 @@ func (g *deleteWorkflows) Name() string {
}

func (g *deleteWorkflows) Run(args []string) {
nodes := downloadNodeAPICredentialsDefault()
fs := flag.NewFlagSet(g.Name(), flag.ExitOnError)
nodeList := fs.String("nodes", "", "Custom node list location")

err := fs.Parse(args)
if err != nil {
fs.Usage()
os.Exit(1)
}

if *nodeList == "" {
*nodeList = defaultNodeList
}

nodes := downloadNodeAPICredentials(*nodeList)

for _, node := range nodes {
output := &bytes.Buffer{}
Expand Down
2 changes: 1 addition & 1 deletion core/scripts/keystone/src/88_gen_jobspecs.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func genSpecs(
ocrConfigContractAddress string,
) donHostSpec {
nodes := downloadNodeAPICredentials(nodeListPath)
nca := downloadNodePubKeys(chainID, pubkeysPath)
nca := downloadNodePubKeys(nodeListPath, chainID, pubkeysPath)
bootstrapNode := nca[0]

bootstrapSpecLines, err := readLines(filepath.Join(templatesDir, bootstrapSpecTemplate))
Expand Down
4 changes: 2 additions & 2 deletions core/scripts/keystone/src/88_gen_ocr3_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,10 @@ func mustReadConfig(fileName string) (output TopLevelConfigSource) {
return mustParseJSON[TopLevelConfigSource](fileName)
}

func generateOCR3Config(configFile string, chainID int64, pubKeysPath string) orc2drOracleConfig {
func generateOCR3Config(nodeList string, configFile string, chainID int64, pubKeysPath string) orc2drOracleConfig {
topLevelCfg := mustReadConfig(configFile)
cfg := topLevelCfg.OracleConfig
nca := downloadNodePubKeys(chainID, pubKeysPath)
nca := downloadNodePubKeys(nodeList, chainID, pubKeysPath)

onchainPubKeys := []common.Address{}
for _, n := range nca {
Expand Down
Loading

0 comments on commit 2013be4

Please sign in to comment.