From 6dd5417223180700acd83bbe5d4d168d767edfc9 Mon Sep 17 00:00:00 2001 From: joanestebanr <129153821+joanestebanr@users.noreply.github.com> Date: Mon, 22 Jul 2024 18:02:21 +0200 Subject: [PATCH] add translator to override DAC members URLs --- cmd/run.go | 5 ++ common/config.go | 5 +- .../datacommittee/datacommittee.go | 8 +++ sequencesender/txbuilder/banana_validium.go | 3 +- translator/config.go | 11 +++ translator/translator.go | 5 ++ translator/translator_impl.go | 69 +++++++++++++++++++ 7 files changed, 104 insertions(+), 2 deletions(-) create mode 100644 translator/config.go create mode 100644 translator/translator.go create mode 100644 translator/translator_impl.go diff --git a/cmd/run.go b/cmd/run.go index 6ce6bb36..25f29654 100644 --- a/cmd/run.go +++ b/cmd/run.go @@ -23,6 +23,7 @@ import ( "github.com/0xPolygon/cdk/sequencesender/txbuilder" "github.com/0xPolygon/cdk/state" "github.com/0xPolygon/cdk/state/pgstatestorage" + "github.com/0xPolygon/cdk/translator" ethtxman "github.com/0xPolygonHermez/zkevm-ethtx-manager/etherman" "github.com/0xPolygonHermez/zkevm-ethtx-manager/etherman/etherscan" "github.com/jackc/pgx/v4/pgxpool" @@ -183,6 +184,9 @@ func newDataAvailability(c config.Config, etherman *etherman.Client) (*dataavail if !c.Common.IsValidiumMode { return nil, nil } + translator := translator.NewTranslatorImpl() + log.Infof("Translator rules: %v", c.Common.Translator) + translator.AddConfigRules(c.Common.Translator) // Backend specific config daProtocolName, err := etherman.GetDAProtocolName() @@ -210,6 +214,7 @@ func newDataAvailability(c config.Config, etherman *etherman.Client) (*dataavail dacAddr, pk, dataCommitteeClient.NewFactory(), + translator, ) if err != nil { return nil, err diff --git a/common/config.go b/common/config.go index 0fdf14d4..d8f2d1ce 100644 --- a/common/config.go +++ b/common/config.go @@ -1,8 +1,11 @@ package common +import "github.com/0xPolygon/cdk/translator" + type Config struct { // IsValidiumMode has the value true if the sequence sender is running in validium mode. IsValidiumMode bool `mapstructure:"IsValidiumMode"` // Contract Versions: elderberry, banana - ContractVersions string `mapstructure:"ContractVersions"` + ContractVersions string `mapstructure:"ContractVersions"` + Translator translator.Config `mapstructure:"Translator"` } diff --git a/dataavailability/datacommittee/datacommittee.go b/dataavailability/datacommittee/datacommittee.go index c29cd622..bfe9393a 100644 --- a/dataavailability/datacommittee/datacommittee.go +++ b/dataavailability/datacommittee/datacommittee.go @@ -14,6 +14,7 @@ import ( daTypes "github.com/0xPolygon/cdk-data-availability/types" "github.com/0xPolygon/cdk/etherman" "github.com/0xPolygon/cdk/log" + "github.com/0xPolygonHermez/zkevm-synchronizer-l1/translator" "github.com/ethereum/go-ethereum/accounts/abi/bind" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/crypto" @@ -22,6 +23,7 @@ import ( ) const unexpectedHashTemplate = "missmatch on transaction data. Expected hash %s, actual hash: %s" +const translateContextName = "dataCommittee" // DataCommitteeMember represents a member of the Data Committee type DataCommitteeMember struct { @@ -45,6 +47,7 @@ type Backend struct { committeeMembers []DataCommitteeMember selectedCommitteeMember int ctx context.Context + Translator translator.Translator } // New creates an instance of Backend @@ -53,6 +56,7 @@ func New( dataCommitteeAddr common.Address, privKey *ecdsa.PrivateKey, dataCommitteeClientFactory client.Factory, + translator translator.Translator, ) (*Backend, error) { ethClient, err := ethclient.Dial(l1RPCURL) if err != nil { @@ -70,6 +74,7 @@ func New( privKey: privKey, dataCommitteeClientFactory: dataCommitteeClientFactory, ctx: context.Background(), + Translator: translator, }, nil } @@ -377,6 +382,9 @@ func (d *Backend) getCurrentDataCommitteeMembers() ([]DataCommitteeMember, error if err != nil { return nil, fmt.Errorf("error getting Members %d from L1 SC: %w", i, err) } + if d.Translator != nil { + member.Url = d.Translator.Translate(translateContextName, member.Url) + } members = append(members, DataCommitteeMember{ Addr: member.Addr, URL: member.Url, diff --git a/sequencesender/txbuilder/banana_validium.go b/sequencesender/txbuilder/banana_validium.go index 08d1750e..d926143a 100644 --- a/sequencesender/txbuilder/banana_validium.go +++ b/sequencesender/txbuilder/banana_validium.go @@ -22,7 +22,8 @@ type TxBuilderBananaValidium struct { condNewSeq CondNewSequence } -func NewTxBuilderBananaValidium(rollupContract contracts.RollupBananaType, gerContract contracts.GlobalExitRootBananaType, +func NewTxBuilderBananaValidium(rollupContract contracts.RollupBananaType, + gerContract contracts.GlobalExitRootBananaType, da dataavailability.SequenceSender, opts bind.TransactOpts, sender common.Address, maxBatchesForL1 uint64) *TxBuilderBananaValidium { return &TxBuilderBananaValidium{ TxBuilderBananaBase: *NewTxBuilderBananaBase(rollupContract, gerContract, opts, sender), diff --git a/translator/config.go b/translator/config.go new file mode 100644 index 00000000..f4a37039 --- /dev/null +++ b/translator/config.go @@ -0,0 +1,11 @@ +package translator + +type ConfigRuleFullMatch struct { + ContextName string `mapstructure:"ContextName"` + Old string `mapstructure:"Old"` + New string `mapstructure:"New"` +} + +type Config struct { + FullMatchRules []ConfigRuleFullMatch `mapstructure:"FullMatchRules"` +} diff --git a/translator/translator.go b/translator/translator.go new file mode 100644 index 00000000..ae6e595b --- /dev/null +++ b/translator/translator.go @@ -0,0 +1,5 @@ +package translator + +type Translator interface { + Translate(contextName string, data string) string +} diff --git a/translator/translator_impl.go b/translator/translator_impl.go new file mode 100644 index 00000000..92326460 --- /dev/null +++ b/translator/translator_impl.go @@ -0,0 +1,69 @@ +package translator + +import "github.com/0xPolygonHermez/zkevm-synchronizer-l1/log" + +type TranslatorFullMatchRule struct { + // If null match any context + ContextName *string + // If null match any data + FullMatchString string + NewString string +} + +func (t *TranslatorFullMatchRule) Match(contextName string, data string) bool { + if t.ContextName != nil && *t.ContextName != contextName { + return false + } + if t.FullMatchString != data { + return false + } + return true +} + +func (t *TranslatorFullMatchRule) Translate(contextName string, data string) string { + return t.NewString +} + +func NewTranslatorFullMatchRule(contextName *string, fullMatchString string, newString string) *TranslatorFullMatchRule { + return &TranslatorFullMatchRule{ + ContextName: contextName, + FullMatchString: fullMatchString, + NewString: newString, + } +} + +type TranslatorImpl struct { + FullMatchRules []TranslatorFullMatchRule +} + +func NewTranslatorImpl() *TranslatorImpl { + return &TranslatorImpl{ + FullMatchRules: []TranslatorFullMatchRule{}, + } +} + +func (t *TranslatorImpl) Translate(contextName string, data string) string { + for _, rule := range t.FullMatchRules { + if rule.Match(contextName, data) { + translated := rule.Translate(contextName, data) + log.Debugf("Translated (ctxName=%s) %s to %s", contextName, data, translated) + return translated + } + } + return data +} + +func (t *TranslatorImpl) AddRule(rule TranslatorFullMatchRule) { + t.FullMatchRules = append(t.FullMatchRules, rule) +} + +func (t *TranslatorImpl) AddConfigRules(cfg Config) { + for _, v := range cfg.FullMatchRules { + var contextName *string + if v.ContextName != "" { + contextName = &v.ContextName + } + rule := NewTranslatorFullMatchRule(contextName, v.Old, v.New) + t.AddRule(*rule) + } +}