Skip to content

Commit

Permalink
fix: faster recursive address replacement
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonathansumner committed Apr 11, 2024
1 parent 75d7e2f commit 760f0ae
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 56 deletions.
2 changes: 1 addition & 1 deletion app/prefix.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
)

const (
AccountAddressPrefix = "fetch"
AccountAddressPrefix = "asi"
)

var (
Expand Down
119 changes: 64 additions & 55 deletions cmd/fetchd/cmd/ASIGenesisUpgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@ import (
"github.com/btcsuite/btcutil/bech32"
"github.com/cosmos/cosmos-sdk/codec"
cosmostypes "github.com/cosmos/cosmos-sdk/types"
"github.com/spf13/cobra"
"github.com/tendermint/tendermint/types"
"regexp"
"strings"
"sync"

"github.com/spf13/cobra"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
Expand Down Expand Up @@ -157,72 +155,65 @@ func ASIGenesisUpgradeReplaceAddresses(jsonString *string) {
replaceAddresses(ConsAddressPrefix, jsonString)
}

type AddressInfo struct {
Original string
Modified string
}

func replaceAddresses(addressTypePrefix string, jsonString *string) {
re := regexp.MustCompile(fmt.Sprintf(`"%s%s1([%s]{%d})"`, OldAddrPrefix, addressTypePrefix, Bech32Chars, AddrDataLength+AddrChecksumLength))
matches := re.FindAllString(*jsonString, -1)

addressChan := make(chan AddressInfo, len(matches))

var wg sync.WaitGroup
wg.Add(len(matches))
var jsonData map[string]interface{}
err := json.Unmarshal([]byte(*jsonString), &jsonData)
if err != nil {
panic(err)
}

replacements := make(map[string]string, len(matches))
for _, match := range matches {
go func(match string) {
defer wg.Done()

matchedAddr := strings.ReplaceAll(match, `"`, "")
_, decodedAddrData, err := bech32.Decode(matchedAddr)
if err != nil {
panic(err)
}

newAddress, err := bech32.Encode(NewAddrPrefix+addressTypePrefix, decodedAddrData)
if err != nil {
panic(err)
}
matchedAddr := strings.ReplaceAll(match, `"`, "")
_, decodedAddrData, err := bech32.Decode(matchedAddr)
if err != nil {
panic(err)
}

err = cosmostypes.VerifyAddressFormat(decodedAddrData)
if err != nil {
panic(err)
}
newAddress, err := bech32.Encode(NewAddrPrefix+addressTypePrefix, decodedAddrData)
if err != nil {
panic(err)
}

switch addressTypePrefix {
case AccAddressPrefix:
_, err = cosmostypes.AccAddressFromBech32(newAddress)
case ValAddressPrefix:
_, err = cosmostypes.ValAddressFromBech32(newAddress)
case ConsAddressPrefix:
_, err = cosmostypes.ConsAddressFromBech32(newAddress)
default:
panic("invalid address type prefix")
}
if err != nil {
panic(err)
}
err = cosmostypes.VerifyAddressFormat(decodedAddrData)
if err != nil {
panic(err)
}

addressChan <- AddressInfo{Original: matchedAddr, Modified: newAddress}
}(match)
switch addressTypePrefix {
case AccAddressPrefix:
_, err = cosmostypes.AccAddressFromBech32(newAddress)
case ValAddressPrefix:
_, err = cosmostypes.ValAddressFromBech32(newAddress)
case ConsAddressPrefix:
_, err = cosmostypes.ConsAddressFromBech32(newAddress)
default:
panic("invalid address type prefix")
}
if err != nil {
panic(err)
}
replacements[matchedAddr] = newAddress
}

go func() {
wg.Wait()
close(addressChan)
}()
crawlJson(jsonData, func(data *interface{}) {
if str, ok := (*data).(string); ok {
if !re.MatchString(fmt.Sprintf(`"%s"`, str)) || len(str) > 200 {
return
}

modifiedAddresses := make(map[string]string)
for addressInfo := range addressChan {
modifiedAddresses[addressInfo.Original] = addressInfo.Modified
}
*data = replacements[str]
}
})

//TODO: This is not feasible time-wise - we need to come up with a quicker method
for originalAddr, modifiedAddr := range modifiedAddresses {
*jsonString = strings.ReplaceAll(*jsonString, originalAddr, fmt.Sprintf(`"%s"`, modifiedAddr))
modifiedJSON, err := json.Marshal(jsonData)
if err != nil {
panic(err)
}
*jsonString = string(modifiedJSON)
}

func ASIGenesisUpgradeWithdrawIBCChannelsBalances() {
Expand Down Expand Up @@ -260,3 +251,21 @@ type regexPair struct {
pattern string
replacement string
}

func crawlJson(data interface{}, strHandler func(*interface{})) {
switch value := data.(type) {
case string:
if strHandler != nil {
strHandler(&data)
}
case []interface{}:
for _, item := range value {
crawlJson(item, strHandler)
}
case map[string]interface{}:
for _, item := range value {
crawlJson(item, strHandler)
}
default:
}
}

0 comments on commit 760f0ae

Please sign in to comment.