From 807feaafc641f26c5fa891f09ff3690a6d6548e5 Mon Sep 17 00:00:00 2001 From: Michael Bisgaard Olesen Date: Wed, 6 Jul 2022 15:34:10 +0200 Subject: [PATCH] Replace base58 lib with properly designed one Removed caching as it didn't appear to imply a clear performance gain. --- go.mod | 4 ++- go.sum | 6 ++++ pkg/tester/data_concordium.go | 53 +++++++---------------------------- 3 files changed, 19 insertions(+), 44 deletions(-) diff --git a/go.mod b/go.mod index 89a97c72..8a40d6c2 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index b56190a7..446ceb36 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/pkg/tester/data_concordium.go b/pkg/tester/data_concordium.go index 3ffc62a6..1cd8229e 100644 --- a/pkg/tester/data_concordium.go +++ b/pkg/tester/data_concordium.go @@ -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" ) @@ -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), } } @@ -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) }