Skip to content

Commit 6f5ddcd

Browse files
committed
pkg/cosmos/config: config enhancements
1 parent 4af008d commit 6f5ddcd

16 files changed

Lines changed: 914 additions & 517 deletions

File tree

go.mod

Lines changed: 88 additions & 70 deletions
Large diffs are not rendered by default.

go.sum

Lines changed: 239 additions & 197 deletions
Large diffs are not rendered by default.

integration-tests/go.mod

Lines changed: 82 additions & 77 deletions
Large diffs are not rendered by default.

integration-tests/go.sum

Lines changed: 190 additions & 101 deletions
Large diffs are not rendered by default.

pkg/cosmos/CONFIG.md

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
TODO header
2+
## Example
3+
4+
```toml
5+
TODO example
6+
```
7+
8+
## Global
9+
```toml
10+
ChainID = 'Malaga-420' # Example
11+
Enabled = true # Default
12+
Bech32Prefix = 'wasm' # Default
13+
BlockRate = '6s' # Default
14+
BlocksUntilTxTimeout = 30 # Default
15+
ConfirmPollPeriod = '1s' # Default
16+
FallbackGasPrice = '0.015' # Default
17+
GasToken = 'ucosm' # Default
18+
GasLimitMultiplier = '1.5' # Default
19+
MaxMsgsPerBatch = 100 # Default
20+
OCR2CachePollPeriod = '4s' # Default
21+
OCR2CacheTTL = '1m' # Default
22+
TxMsgTimeout = '10m' # Default
23+
```
24+
25+
26+
### ChainID
27+
```toml
28+
ChainID = 'Malaga-420' # Example
29+
```
30+
ChainID TODO
31+
32+
### Enabled
33+
```toml
34+
Enabled = true # Default
35+
```
36+
Enabled TODO
37+
38+
### Bech32Prefix
39+
```toml
40+
Bech32Prefix = 'wasm' # Default
41+
```
42+
Bech32Prefix TODO
43+
44+
### BlockRate
45+
```toml
46+
BlockRate = '6s' # Default
47+
```
48+
BlockRate TODO
49+
50+
### BlocksUntilTxTimeout
51+
```toml
52+
BlocksUntilTxTimeout = 30 # Default
53+
```
54+
BlocksUntilTxTimeout TODO
55+
56+
### ConfirmPollPeriod
57+
```toml
58+
ConfirmPollPeriod = '1s' # Default
59+
```
60+
ConfirmPollPeriod TODO
61+
62+
### FallbackGasPrice
63+
```toml
64+
FallbackGasPrice = '0.015' # Default
65+
```
66+
FallbackGasPrice TODO
67+
68+
### GasToken
69+
```toml
70+
GasToken = 'ucosm' # Default
71+
```
72+
GasToken TODO
73+
74+
### GasLimitMultiplier
75+
```toml
76+
GasLimitMultiplier = '1.5' # Default
77+
```
78+
GasLimitMultiplier TODO
79+
80+
### MaxMsgsPerBatch
81+
```toml
82+
MaxMsgsPerBatch = 100 # Default
83+
```
84+
MaxMsgsPerBatch TODO
85+
86+
### OCR2CachePollPeriod
87+
```toml
88+
OCR2CachePollPeriod = '4s' # Default
89+
```
90+
OCR2CachePollPeriod TODO
91+
92+
### OCR2CacheTTL
93+
```toml
94+
OCR2CacheTTL = '1m' # Default
95+
```
96+
OCR2CacheTTL TODO
97+
98+
### TxMsgTimeout
99+
```toml
100+
TxMsgTimeout = '10m' # Default
101+
```
102+
TxMsgTimeout TODO
103+
104+
## Nodes
105+
```toml
106+
[[Nodes]]
107+
Name = 'primary' # Example
108+
TendermintURL = 'http://tender.mint' # Example
109+
```
110+
111+
112+
### Name
113+
```toml
114+
Name = 'primary' # Example
115+
```
116+
Name TODO
117+
118+
### TendermintURL
119+
```toml
120+
TendermintURL = 'http://tender.mint' # Example
121+
```
122+
TendermintURL TODO
123+

pkg/cosmos/chain.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
sdk "github.com/cosmos/cosmos-sdk/types"
1313
bank "github.com/cosmos/cosmos-sdk/x/bank/types"
1414
"github.com/pelletier/go-toml/v2"
15+
chain_selectors "github.com/smartcontractkit/chain-selectors"
1516

1617
"github.com/smartcontractkit/chainlink-common/pkg/chains"
1718
"github.com/smartcontractkit/chainlink-common/pkg/logger"
@@ -82,6 +83,15 @@ type chain struct {
8283
lggr logger.Logger
8384
}
8485

86+
func (c *chain) GetChainInfo(ctx context.Context) (types.ChainInfo, error) {
87+
return types.ChainInfo{
88+
FamilyName: chain_selectors.FamilyCosmos,
89+
ChainID: c.id,
90+
NetworkName: "", //TODO
91+
NetworkNameFull: "", //TODO
92+
}, nil
93+
}
94+
8595
func newChain(id string, cfg *config.TOMLConfig, ds sqlutil.DataSource, ks loop.Keystore, lggr logger.Logger) (*chain, error) {
8696
lggr = logger.With(lggr, "cosmosChainID", id)
8797
var ch = chain{
@@ -128,6 +138,8 @@ func (c *chain) Reader(name string) (client.Reader, error) {
128138
return c.getClient(name)
129139
}
130140

141+
func (c *chain) Replay(context.Context, string, map[string]any) error { return nil }
142+
131143
// getClient returns a client, optionally requiring a specific node by name.
132144
func (c *chain) getClient(name string) (client.ReaderWriter, error) {
133145
var node db.Node

pkg/cosmos/cmd/chainlink-cosmos/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ type pluginRelayer struct {
5151
ds sqlutil.DataSource
5252
}
5353

54-
func (c *pluginRelayer) NewRelayer(ctx context.Context, config string, keystore loop.Keystore, capRegistry core.CapabilitiesRegistry) (loop.Relayer, error) {
54+
func (c *pluginRelayer) NewRelayer(ctx context.Context, config string, keystore, csaKeystore core.Keystore, capRegistry core.CapabilitiesRegistry) (loop.Relayer, error) {
5555
d := toml.NewDecoder(strings.NewReader(config))
5656
d.DisallowUnknownFields()
5757

pkg/cosmos/cmd/config-docs/main.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package main
2+
3+
import (
4+
"flag"
5+
"fmt"
6+
"log"
7+
"os"
8+
"path"
9+
10+
"github.com/smartcontractkit/chainlink-cosmos/pkg/cosmos/config"
11+
)
12+
13+
var outDir = flag.String("o", "", "output directory")
14+
15+
func main() {
16+
s, err := config.GenerateDocs()
17+
if err != nil {
18+
log.Fatalln("Failed to generate docs:", err)
19+
}
20+
if err = os.WriteFile(path.Join(*outDir, "CONFIG.md"), []byte(s), 0600); err != nil {
21+
fmt.Fprintf(os.Stderr, "failed to write config docs: %v\n", err)
22+
os.Exit(1)
23+
}
24+
}

pkg/cosmos/config/config.go

Lines changed: 21 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -3,51 +3,33 @@ package config
33
import (
44
"errors"
55
"fmt"
6+
"log"
67
"net/url"
78
"slices"
9+
"strings"
810
"time"
911

1012
sdk "github.com/cosmos/cosmos-sdk/types"
1113
"github.com/pelletier/go-toml/v2"
1214
"github.com/shopspring/decimal"
1315

1416
"github.com/smartcontractkit/chainlink-common/pkg/config"
17+
"github.com/smartcontractkit/chainlink-common/pkg/config/configtest"
1518

16-
"github.com/smartcontractkit/chainlink-cosmos/pkg/cosmos/client"
1719
"github.com/smartcontractkit/chainlink-cosmos/pkg/cosmos/db"
1820
)
1921

20-
// Global defaults.
21-
var defaultConfigSet = configSet{
22-
BlockRate: 6 * time.Second,
23-
// ~6s per block, so ~3m until we give up on the tx getting confirmed
24-
// Anecdotally it appears anything more than 4 blocks would be an extremely long wait,
25-
// In practice during the UST depegging and subsequent extreme congestion, we saw
26-
// ~16 block FIFO lineups.
27-
BlocksUntilTxTimeout: 30,
28-
ConfirmPollPeriod: time.Second,
29-
FallbackGasPrice: sdk.MustNewDecFromStr("0.015"),
30-
// This is high since we simulate before signing the transaction.
31-
// There's a chicken and egg problem: need to sign to simulate accurately
32-
// but you need to specify a gas limit when signing.
33-
// TODO: Determine how much gas a signature adds and then
34-
// add that directly so we can be more accurate.
35-
GasLimitMultiplier: client.DefaultGasLimitMultiplier,
36-
// The max gas limit per block is 1_000_000_000
37-
// https://github.com/terra-money/core/blob/d6037b9a12c8bf6b09fe861c8ad93456aac5eebb/app/legacy/migrate.go#L69.
38-
// The max msg size is 10KB https://github.com/terra-money/core/blob/d6037b9a12c8bf6b09fe861c8ad93456aac5eebb/x/wasm/types/params.go#L15.
39-
// Our msgs are only OCR reports for now, which will not exceed that size.
40-
// There appears to be no gas limit per tx, only per block, so theoretically
41-
// we could include 1000 msgs which use up to 1M gas.
42-
// To be conservative and since the number of messages we'd
43-
// have in a batch on average roughly corresponds to the number of terra ocr jobs we're running (do not expect more than 100),
44-
// we can set a max msgs per batch of 100.
45-
MaxMsgsPerBatch: 100,
46-
OCR2CachePollPeriod: 4 * time.Second,
47-
OCR2CacheTTL: time.Minute,
48-
TxMsgTimeout: 10 * time.Minute,
49-
Bech32Prefix: "wasm", // note: this shouldn't be used outside of tests
50-
GasToken: "ucosm", // note: this shouldn't be used outside of tests
22+
var defaults TOMLConfig
23+
24+
func init() {
25+
if err := configtest.DocDefaultsOnly(strings.NewReader(docsTOML), &defaults, config.DecodeTOML); err != nil {
26+
log.Fatalf("Failed to initialize defaults from docs: %v", err)
27+
}
28+
}
29+
30+
func Defaults() (c TOMLConfig) {
31+
c.SetFrom(&defaults)
32+
return
5133
}
5234

5335
type Config interface {
@@ -93,44 +75,6 @@ type Chain struct {
9375
TxMsgTimeout *config.Duration
9476
}
9577

96-
func (c *Chain) SetDefaults() {
97-
if c.Bech32Prefix == nil {
98-
c.Bech32Prefix = &defaultConfigSet.Bech32Prefix
99-
}
100-
if c.BlockRate == nil {
101-
c.BlockRate = config.MustNewDuration(defaultConfigSet.BlockRate)
102-
}
103-
if c.BlocksUntilTxTimeout == nil {
104-
c.BlocksUntilTxTimeout = &defaultConfigSet.BlocksUntilTxTimeout
105-
}
106-
if c.ConfirmPollPeriod == nil {
107-
c.ConfirmPollPeriod = config.MustNewDuration(defaultConfigSet.ConfirmPollPeriod)
108-
}
109-
if c.FallbackGasPrice == nil {
110-
d := decimal.NewFromBigInt(defaultConfigSet.FallbackGasPrice.BigInt(), -sdk.Precision)
111-
c.FallbackGasPrice = &d
112-
}
113-
if c.GasToken == nil {
114-
c.GasToken = &defaultConfigSet.GasToken
115-
}
116-
if c.GasLimitMultiplier == nil {
117-
d := decimal.NewFromFloat(defaultConfigSet.GasLimitMultiplier)
118-
c.GasLimitMultiplier = &d
119-
}
120-
if c.MaxMsgsPerBatch == nil {
121-
c.MaxMsgsPerBatch = &defaultConfigSet.MaxMsgsPerBatch
122-
}
123-
if c.OCR2CachePollPeriod == nil {
124-
c.OCR2CachePollPeriod = config.MustNewDuration(defaultConfigSet.OCR2CachePollPeriod)
125-
}
126-
if c.OCR2CacheTTL == nil {
127-
c.OCR2CacheTTL = config.MustNewDuration(defaultConfigSet.OCR2CacheTTL)
128-
}
129-
if c.TxMsgTimeout == nil {
130-
c.TxMsgTimeout = config.MustNewDuration(defaultConfigSet.TxMsgTimeout)
131-
}
132-
}
133-
13478
type Node struct {
13579
Name *string
13680
TendermintURL *config.URL
@@ -249,6 +193,13 @@ func (c *TOMLConfig) IsEnabled() bool {
249193
return c.Enabled == nil || *c.Enabled
250194
}
251195

196+
func (c *TOMLConfig) SetDefaults() {
197+
def := Defaults()
198+
def.SetFrom(c)
199+
*c = def
200+
201+
}
202+
252203
func (c *TOMLConfig) SetFrom(f *TOMLConfig) {
253204
if f.ChainID != nil {
254205
c.ChainID = f.ChainID

pkg/cosmos/config/config_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
package config
22

33
import (
4+
_ "embed"
45
"reflect"
56
"testing"
7+
"time"
68

79
sdk "github.com/cosmos/cosmos-sdk/types"
810
"github.com/shopspring/decimal"
911
"github.com/stretchr/testify/assert"
1012

1113
"github.com/smartcontractkit/chainlink-common/pkg/config"
14+
"github.com/smartcontractkit/chainlink-common/pkg/config/configtest"
1215

1316
"github.com/smartcontractkit/chainlink-cosmos/pkg/cosmos/db"
1417
)
@@ -94,3 +97,41 @@ func TestCosmosConfig_GetNode(t *testing.T) {
9497
func ptr[T any](t T) *T {
9598
return &t
9699
}
100+
101+
func TestDefaults_fieldsNotNil(t *testing.T) {
102+
configtest.AssertFieldsNotNil(t, Defaults())
103+
}
104+
105+
func TestDocsTOMLComplete(t *testing.T) {
106+
configtest.AssertDocsTOMLComplete[TOMLConfig](t, docsTOML)
107+
}
108+
109+
//go:embed testdata/config-full.toml
110+
var fullTOML string
111+
112+
func TestTOMLConfig_FullMarshal(t *testing.T) {
113+
full := TOMLConfig{
114+
ChainID: ptr("Ibiza-808"),
115+
Enabled: ptr(true),
116+
Chain: Chain{
117+
Bech32Prefix: ptr("wasm"),
118+
BlockRate: config.MustNewDuration(time.Minute),
119+
BlocksUntilTxTimeout: ptr[int64](12),
120+
ConfirmPollPeriod: config.MustNewDuration(time.Second),
121+
FallbackGasPrice: ptr(decimal.RequireFromString("0.001")),
122+
GasToken: ptr("ucosm"),
123+
GasLimitMultiplier: ptr(decimal.RequireFromString("1.2")),
124+
MaxMsgsPerBatch: ptr[int64](17),
125+
OCR2CachePollPeriod: config.MustNewDuration(time.Minute),
126+
OCR2CacheTTL: config.MustNewDuration(time.Hour),
127+
TxMsgTimeout: config.MustNewDuration(time.Second),
128+
},
129+
Nodes: []*Node{
130+
{
131+
Name: ptr("primary"),
132+
TendermintURL: config.MustParseURL("http://columbus.cosmos.com"),
133+
},
134+
},
135+
}
136+
configtest.AssertFullMarshal(t, full, fullTOML)
137+
}

0 commit comments

Comments
 (0)