github.com/ari-anchor/sei-tendermint@v0.0.0-20230519144642-dc826b7b56bb/scripts/confix/plan.go (about)

     1  package main
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  	"fmt"
     7  	"strings"
     8  
     9  	"github.com/creachadair/tomledit"
    10  	"github.com/creachadair/tomledit/parser"
    11  	"github.com/creachadair/tomledit/transform"
    12  )
    13  
    14  // The plan is the sequence of transformation steps that should be applied, in
    15  // the given order, to convert a configuration file to be compatible with the
    16  // current version of the config grammar.
    17  //
    18  // Transformation steps are specific to the target config version.  For this
    19  // reason, you must exercise caution when backporting changes to this script
    20  // into older releases.
    21  var plan = transform.Plan{
    22  	{
    23  		// Since https://github.com/ari-anchor/sei-tendermint/pull/5777.
    24  		Desc: "Rename everything from snake_case to kebab-case",
    25  		T:    transform.SnakeToKebab(),
    26  	},
    27  	{
    28  		// [fastsync]  renamed in https://github.com/ari-anchor/sei-tendermint/pull/6896.
    29  		// [blocksync] removed in https://github.com/ari-anchor/sei-tendermint/pull/7159.
    30  		Desc: "Remove [fastsync] and [blocksync] sections",
    31  		T: transform.Func(func(_ context.Context, doc *tomledit.Document) error {
    32  			doc.First("fast-sync").Remove()
    33  			transform.FindTable(doc, "fastsync").Remove()
    34  			transform.FindTable(doc, "blocksync").Remove()
    35  			return nil
    36  		}),
    37  		ErrorOK: true,
    38  	},
    39  	{
    40  		// Since https://github.com/ari-anchor/sei-tendermint/pull/6241.
    41  		Desc: `Add top-level mode setting (default "full")`,
    42  		T: transform.EnsureKey(nil, &parser.KeyValue{
    43  			Block: parser.Comments{"Mode of Node: full | validator | seed"},
    44  			Name:  parser.Key{"mode"},
    45  			Value: parser.MustValue(`"full"`),
    46  		}),
    47  		ErrorOK: true,
    48  	},
    49  	{
    50  		// Since https://github.com/ari-anchor/sei-tendermint/pull/7121.
    51  		Desc: "Remove gRPC settings from the [rpc] section",
    52  		T: transform.Func(func(_ context.Context, doc *tomledit.Document) error {
    53  			doc.First("rpc", "grpc-laddr").Remove()
    54  			doc.First("rpc", "grpc-max-open-connections").Remove()
    55  			return nil
    56  		}),
    57  	},
    58  	{
    59  		// Since https://github.com/ari-anchor/sei-tendermint/pull/8217.
    60  		Desc: "Remove per-node consensus timeouts (converted to consensus parameters)",
    61  		T: transform.Remove(
    62  			parser.Key{"consensus", "skip-timeout-commit"},
    63  			parser.Key{"consensus", "timeout-commit"},
    64  			parser.Key{"consensus", "timeout-precommit"},
    65  			parser.Key{"consensus", "timeout-precommit-delta"},
    66  			parser.Key{"consensus", "timeout-prevote"},
    67  			parser.Key{"consensus", "timeout-prevote-delta"},
    68  			parser.Key{"consensus", "timeout-propose"},
    69  			parser.Key{"consensus", "timeout-propose-delta"},
    70  		),
    71  		ErrorOK: true,
    72  	},
    73  	{
    74  		// Removed wal-dir: https://github.com/ari-anchor/sei-tendermint/pull/6396.
    75  		// Removed version: https://github.com/ari-anchor/sei-tendermint/pull/7171.
    76  		Desc: "Remove vestigial mempool.wal-dir settings",
    77  		T: transform.Remove(
    78  			parser.Key{"mempool", "wal-dir"},
    79  			parser.Key{"mempool", "version"},
    80  		),
    81  		ErrorOK: true,
    82  	},
    83  	{
    84  		// Since https://github.com/ari-anchor/sei-tendermint/pull/6323.
    85  		Desc: "Add new [p2p] queue-type setting",
    86  		T: transform.EnsureKey(parser.Key{"p2p"}, &parser.KeyValue{
    87  			Block: parser.Comments{"Select the p2p internal queue"},
    88  			Name:  parser.Key{"queue-type"},
    89  			Value: parser.MustValue(`"priority"`),
    90  		}),
    91  		ErrorOK: true,
    92  	},
    93  	{
    94  		// Since https://github.com/ari-anchor/sei-tendermint/pull/6353.
    95  		Desc: "Add [p2p] connection count and rate limit settings",
    96  		T: transform.Func(func(_ context.Context, doc *tomledit.Document) error {
    97  			tab := transform.FindTable(doc, "p2p")
    98  			if tab == nil {
    99  				return errors.New("p2p table not found")
   100  			}
   101  			transform.InsertMapping(tab.Section, &parser.KeyValue{
   102  				Block: parser.Comments{"Maximum number of connections (inbound and outbound)."},
   103  				Name:  parser.Key{"max-connections"},
   104  				Value: parser.MustValue("64"),
   105  			}, false)
   106  			transform.InsertMapping(tab.Section, &parser.KeyValue{
   107  				Block: parser.Comments{
   108  					"Rate limits the number of incoming connection attempts per IP address.",
   109  				},
   110  				Name:  parser.Key{"max-incoming-connection-attempts"},
   111  				Value: parser.MustValue("100"),
   112  			}, false)
   113  			return nil
   114  		}),
   115  	},
   116  	{
   117  		// Added "chunk-fetchers" https://github.com/ari-anchor/sei-tendermint/pull/6566.
   118  		// This value was backported into v0.34.11 (modulo casing).
   119  		// Renamed to "fetchers"  https://github.com/ari-anchor/sei-tendermint/pull/6587.
   120  		Desc: "Rename statesync.chunk-fetchers to statesync.fetchers",
   121  		T: transform.Func(func(ctx context.Context, doc *tomledit.Document) error {
   122  			// If the key already exists, rename it preserving its value.
   123  			if found := doc.First("statesync", "chunk-fetchers"); found != nil {
   124  				found.KeyValue.Name = parser.Key{"fetchers"}
   125  				return nil
   126  			}
   127  
   128  			// Otherwise, add it.
   129  			return transform.EnsureKey(parser.Key{"statesync"}, &parser.KeyValue{
   130  				Block: parser.Comments{
   131  					"The number of concurrent chunk and block fetchers to run (default: 4).",
   132  				},
   133  				Name:  parser.Key{"fetchers"},
   134  				Value: parser.MustValue("4"),
   135  			})(ctx, doc)
   136  		}),
   137  	},
   138  	{
   139  		// Since https://github.com/ari-anchor/sei-tendermint/pull/6807.
   140  		// Backported into v0.34.13 (modulo casing).
   141  		Desc: "Add statesync.use-p2p setting",
   142  		T: transform.EnsureKey(parser.Key{"statesync"}, &parser.KeyValue{
   143  			Block: parser.Comments{
   144  				"# State sync uses light client verification to verify state. This can be done either through the",
   145  				"# P2P layer or RPC layer. Set this to true to use the P2P layer. If false (default), RPC layer",
   146  				"# will be used.",
   147  			},
   148  			Name:  parser.Key{"use-p2p"},
   149  			Value: parser.MustValue("false"),
   150  		}),
   151  	},
   152  	{
   153  		// Since https://github.com/ari-anchor/sei-tendermint/pull/6462.
   154  		Desc: "Move priv-validator settings under [priv-validator]",
   155  		T: transform.Func(func(_ context.Context, doc *tomledit.Document) error {
   156  			const pvPrefix = "priv-validator-"
   157  
   158  			var found []*tomledit.Entry
   159  			doc.Global.Scan(func(key parser.Key, e *tomledit.Entry) bool {
   160  				if len(key) == 1 && strings.HasPrefix(key[0], pvPrefix) {
   161  					found = append(found, e)
   162  				}
   163  				return true
   164  			})
   165  			if len(found) == 0 {
   166  				return nil // nothing to do
   167  			}
   168  
   169  			// Now that we know we have work to do, find the target table.
   170  			var sec *tomledit.Section
   171  			if dst := transform.FindTable(doc, "priv-validator"); dst == nil {
   172  				// If the table doesn't exist, create it. Old config files
   173  				// probably will not have it, so plug in the comment too.
   174  				sec = &tomledit.Section{
   175  					Heading: &parser.Heading{
   176  						Block: parser.Comments{
   177  							"#######################################################",
   178  							"###       Priv Validator Configuration              ###",
   179  							"#######################################################",
   180  						},
   181  						Name: parser.Key{"priv-validator"},
   182  					},
   183  				}
   184  				doc.Sections = append(doc.Sections, sec)
   185  			} else {
   186  				sec = dst.Section
   187  			}
   188  
   189  			for _, e := range found {
   190  				e.Remove()
   191  				e.Name = parser.Key{strings.TrimPrefix(e.Name[0], pvPrefix)}
   192  				sec.Items = append(sec.Items, e.KeyValue)
   193  			}
   194  			return nil
   195  		}),
   196  	},
   197  	{
   198  		// Since https://github.com/ari-anchor/sei-tendermint/pull/6411.
   199  		Desc: "Convert tx-index.indexer from a string to a list of strings",
   200  		T: transform.Func(func(ctx context.Context, doc *tomledit.Document) error {
   201  			idx := doc.First("tx-index", "indexer")
   202  			if idx == nil {
   203  				// No previous indexer setting: Default to ["null"] per #8222.
   204  				return transform.EnsureKey(parser.Key{"tx-index"}, &parser.KeyValue{
   205  					Block: parser.Comments{"The backend database list to back the indexer."},
   206  					Name:  parser.Key{"indexer"},
   207  					Value: parser.MustValue(`["null"]`),
   208  				})(ctx, doc)
   209  			}
   210  
   211  			// Versions prior to v0.35 had a string value here, v0.35 and onward
   212  			// use an array of strings.
   213  			switch idx.KeyValue.Value.X.(type) {
   214  			case parser.Array:
   215  				// OK, this is already up-to-date.
   216  				return nil
   217  			case parser.Token:
   218  				// Wrap the value in a single-element array.
   219  				idx.KeyValue.Value.X = parser.Array{idx.KeyValue.Value}
   220  				return nil
   221  			}
   222  			return fmt.Errorf("unrecognized value: %v", idx.KeyValue)
   223  		}),
   224  	},
   225  	{
   226  		// Since https://github.com/ari-anchor/sei-tendermint/pull/8514.
   227  		Desc:    "Remove the recheck option from the [mempool] section",
   228  		T:       transform.Remove(parser.Key{"mempool", "recheck"}),
   229  		ErrorOK: true,
   230  	},
   231  	{
   232  		// Since https://github.com/ari-anchor/sei-tendermint/pull/8654.
   233  		Desc:    "Remove the seeds option from the [p2p] section",
   234  		T:       transform.Remove(parser.Key{"p2p", "seeds"}),
   235  		ErrorOK: true,
   236  	},
   237  }