Skip to content

Commit

Permalink
Replace base58 lib with properly designed one
Browse files Browse the repository at this point in the history
Removed caching as it didn't appear to imply a clear performance gain.
  • Loading branch information
bisgardo committed Jul 6, 2022
1 parent 0ad6761 commit 807feaa
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 44 deletions.
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ require (
golang.org/x/sys v0.0.0-20211205182925-97ca703d548d // indirect
)

require github.com/anaskhan96/base58check v0.0.0-20181220122047-b05365d494c4
require (
github.com/btcsuite/btcd/btcutil v1.1.1
)

replace github.com/coinbase/rosetta-sdk-go => github.com/Concordium/rosetta-sdk-go v0.7.11-0.20220706095914-7942ab456d4f
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,15 @@ github.com/boltdb/bolt v1.3.1/go.mod h1:clJnj/oiGkjum5o1McbSZDSLxVThjynRyGBgiAx2
github.com/btcsuite/btcd v0.0.0-20190315201642-aa6e0f35703c/go.mod h1:DrZx5ec/dmnfpw9KyYoQyYo7d0KEvTkk/5M/vbZjAr8=
github.com/btcsuite/btcd v0.20.1-beta/go.mod h1:wVuoA8VJLEcwgqHBwHmzLRazpKxTv13Px/pDuV7OomQ=
github.com/btcsuite/btcd v0.21.0-beta.0.20201114000516-e9c7a5ac6401/go.mod h1:Sv4JPQ3/M+teHz9Bo5jBpkNcP0x6r7rdihlNL/7tTAs=
github.com/btcsuite/btcd v0.22.0-beta.0.20220111032746-97732e52810c/go.mod h1:tjmYdS6MLJ5/s0Fj4DbLgSbDHbEqLJrtnHecBFkdz5M=
github.com/btcsuite/btcd v0.22.1 h1:CnwP9LM/M9xuRrGSCGeMVs9iv09uMqwsVX7EeIpgV2c=
github.com/btcsuite/btcd v0.22.1/go.mod h1:wqgTSL29+50LRkmOVknEdmt8ZojIzhuWvgu/iptuN7Y=
github.com/btcsuite/btcd/btcec/v2 v2.1.1/go.mod h1:ctjw4H1kknNJmRN4iP1R7bTQ+v3GJkZBd6mui8ZsAZE=
github.com/btcsuite/btcd/btcec/v2 v2.2.0/go.mod h1:U7MHm051Al6XmscBQ0BoNydpOTsFAn707034b5nY8zU=
github.com/btcsuite/btcd/btcutil v1.0.0/go.mod h1:Uoxwv0pqYWhD//tfTiipkxNfdhG9UrLwaeswfjfdF0A=
github.com/btcsuite/btcd/btcutil v1.1.1 h1:hDcDaXiP0uEzR8Biqo2weECKqEw0uHDZ9ixIWevVQqY=
github.com/btcsuite/btcd/btcutil v1.1.1/go.mod h1:nbKlBMNm9FGsdvKvu0essceubPiAcI57pYBNnsLAa34=
github.com/btcsuite/btcd/chaincfg/chainhash v1.0.0/go.mod h1:7SFka0XMvUgj3hfZtydOrQY2mwhPclbT2snogU7SQQc=
github.com/btcsuite/btcd/chaincfg/chainhash v1.0.1 h1:q0rUy8C/TYNBQS1+CGKw68tLOFYSNEs0TFnxxnS9+4U=
github.com/btcsuite/btcd/chaincfg/chainhash v1.0.1/go.mod h1:7SFka0XMvUgj3hfZtydOrQY2mwhPclbT2snogU7SQQc=
github.com/btcsuite/btclog v0.0.0-20170628155309-84c8d2346e9f/go.mod h1:TdznJufoqS23FtqVCzL0ZqgP5MqXbb4fg/WgDys70nA=
Expand Down
53 changes: 10 additions & 43 deletions pkg/tester/data_concordium.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@ package tester

import (
"context"
"encoding/hex"
"log"
"sync"

"github.com/anaskhan96/base58check"
"github.com/btcsuite/btcd/btcutil/base58"
"github.com/coinbase/rosetta-sdk-go/syncer"
"github.com/coinbase/rosetta-sdk-go/types"
)
Expand All @@ -15,15 +11,12 @@ import (
// by rewriting all account addresses into a "normalized" form (the zero'th alias).
// This is necessary for DataTester to do correct accounting.
type concordiumHelper struct {
helper syncer.Helper
cacheLock sync.RWMutex
cache map[string]string
helper syncer.Helper
}

func newConcordiumHelper(h syncer.Helper) *concordiumHelper {
return &concordiumHelper{
helper: h,
cache: make(map[string]string),
}
}

Expand All @@ -46,46 +39,20 @@ func (h *concordiumHelper) Block(ctx context.Context, network *types.NetworkIden

func (h *concordiumHelper) normalizeAddress(account *types.AccountIdentifier) {
if account != nil {
h.cacheLock.RLock()
a, ok := h.cache[account.Address]
h.cacheLock.RUnlock()
if !ok {
a = addressToAliasZero(account.Address)
h.cacheLock.Lock()
h.cache[account.Address] = a
h.cacheLock.Unlock()
}
account.Address = a
account.Address = addressToAliasZero(account.Address)
}
}

func addressToAliasZero(addr string) string {
// Lots of redundant encoding/decoding back and forth due to the design choices of the base58check lib.
decodedStr, err := base58check.Decode(addr)
if err != nil {
addrBytes, versionByte, err := base58.CheckDecode(addr)
if err != nil || len(addrBytes) != 32 {
// Non-Base58Check address is likely virtual (see https://github.com/Concordium/concordium-rosetta/#identifiers);
// return unchanged.
return addr
}
decodedBytes, err := hex.DecodeString(decodedStr)
if err != nil {
// Never happens as it just inverts the hex encode steps done in base58check.Decode.
log.Fatal(err)
}
versionBytes, addrBytes := decodedBytes[0:1], decodedBytes[1:]
versionStr := hex.EncodeToString(versionBytes)

// Zero out last 3 bytes.
if len(addrBytes) == 32 {
addrBytes[29] = 0
addrBytes[30] = 0
addrBytes[31] = 0
}

addr0, err := base58check.Encode(versionStr, hex.EncodeToString(addrBytes))
if err != nil {
// Never happens as base58check.Encode just inverts the hex encode steps above.
log.Fatal(err)
}
return addr0
// Zero out last 3 bytes and re-encode.
addrBytes[29] = 0
addrBytes[30] = 0
addrBytes[31] = 0
return base58.CheckEncode(addrBytes, versionByte)
}

0 comments on commit 807feaa

Please sign in to comment.