Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
Signed-off-by: p4u <[email protected]>
  • Loading branch information
p4u committed Jun 19, 2024
1 parent 0f2fb1a commit fb096fb
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 9 deletions.
22 changes: 15 additions & 7 deletions census.go
Original file line number Diff line number Diff line change
Expand Up @@ -1196,6 +1196,9 @@ func (v *vocdoniHandler) processCensusRecords(records [][]string, progress chan
if weightRecord == "" {
weightRecord = "1"
}
if weightRecord == "0" {
log.Warnf("address %s has weight %s", address, weightRecord)
}
weight, ok = new(big.Int).SetString(weightRecord, 10)
if !ok {
log.Warnf("invalid weight for address %s: %s", address, weightRecord)
Expand Down Expand Up @@ -1297,12 +1300,14 @@ func (v *vocdoniHandler) processCensusRecords(records [][]string, progress chan
// the weight is the sum of the weights of all the addresses of the user
weight := new(big.Int).SetUint64(0)
for _, addr := range user.Addresses {
weightAddress, ok := addressMap[common.HexToAddress(addr).Hex()]
weightAddress, ok := addressMap[helpers.NormalizeAddressString(addr)]
if ok {
weight = weight.Add(weight, weightAddress)
}
}

if weight.Cmp(big.NewInt(0)) == 0 {
log.Warnw("user address not found on addressMap ", "address", addr)
}
for _, signer := range user.Signers {
signerBytes, err := hex.DecodeString(strings.TrimPrefix(signer, "0x"))
if err != nil {
Expand Down Expand Up @@ -1353,19 +1358,19 @@ func (v *vocdoniHandler) processCensusRecords(records [][]string, progress chan
userData.FID,
userData.Username,
userData.Displayname,
userData.VerificationsAddresses,
helpers.NormalizeAddressStringSlice(userData.VerificationsAddresses),
userData.Signers,
userData.CustodyAddress,
helpers.NormalizeAddressString(userData.CustodyAddress),
0,
); err != nil {
return nil, 0, err
}
} else {
log.Debugw("updating user on database", "fid", userData.FID)
dbUser.Addresses = userData.VerificationsAddresses
dbUser.Addresses = helpers.NormalizeAddressStringSlice(userData.VerificationsAddresses)
dbUser.Username = userData.Username
dbUser.Signers = userData.Signers
dbUser.CustodyAddress = userData.CustodyAddress
dbUser.CustodyAddress = helpers.NormalizeAddressString(userData.CustodyAddress)
if err := v.db.UpdateUser(dbUser); err != nil {
return nil, 0, err
}
Expand All @@ -1374,11 +1379,14 @@ func (v *vocdoniHandler) processCensusRecords(records [][]string, progress chan
// the weight is the sum of the weights of all the addresses of the user
weight := new(big.Int).SetUint64(0)
for _, addr := range userData.VerificationsAddresses {
weightAddress, ok := addressMap[common.HexToAddress(addr).Hex()]
weightAddress, ok := addressMap[helpers.NormalizeAddressString(addr)]
if ok {
weight = weight.Add(weight, weightAddress)
}
}
if weight.Cmp(big.NewInt(0)) == 0 {
log.Warnw("user address not found on addressMap ", "address", userData.VerificationsAddresses)
}

// Add the user to the participants list (with all the signers)
for _, signer := range userData.Signers {
Expand Down
18 changes: 18 additions & 0 deletions helpers/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"math/big"

"github.com/ethereum/go-ethereum/common"
"go.vocdoni.io/dvote/api"
"go.vocdoni.io/dvote/log"
)
Expand Down Expand Up @@ -127,3 +128,20 @@ func UnpackMetadata(metadata any) *api.ElectionDescription {
}
return desc
}

// NormalizeAddressString converts an Ethereum address to its normalized form.
func NormalizeAddressString(address string) string {
return common.HexToAddress(address).Hex()
}

// NormalizeAddressStringSlice converts a slice of Ethereum addresses to their normalized form.
func NormalizeAddressStringSlice(addresses []string) []string {
normalizedAddresses := make([]string, 0, len(addresses))
for _, address := range addresses {
normalizedAddress := NormalizeAddressString(address)
if normalizedAddress != "" {
normalizedAddresses = append(normalizedAddresses, normalizedAddress)
}
}
return normalizedAddresses
}
4 changes: 2 additions & 2 deletions mongo/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ func (ms *MongoStorage) UsersWithPendingProfile() ([]uint64, error) {
return users, nil
}

// UserByAddress returns the user that has the given address. If the user is not found, it returns an error.
// UserByAddress returns the user that has the given address (case insensitive). If the user is not found, it returns an error.
func (ms *MongoStorage) UserByAddress(address string) (*User, error) {
ms.keysLock.RLock()
defer ms.keysLock.RUnlock()
Expand All @@ -170,7 +170,7 @@ func (ms *MongoStorage) UserByAddress(address string) (*User, error) {
defer cancel()
var userByAddress User
if err := ms.users.FindOne(ctx, bson.M{
"addresses": bson.M{"$in": []string{address}},
"addresses": bson.M{"$elemMatch": bson.M{"$regex": address, "$options": "i"}},
}).Decode(&userByAddress); err != nil {
return nil, ErrUserUnknown
}
Expand Down

0 comments on commit fb096fb

Please sign in to comment.