Skip to content

Commit

Permalink
add all linked address for weight
Browse files Browse the repository at this point in the history
Signed-off-by: p4u <[email protected]>
  • Loading branch information
p4u committed Apr 25, 2024
1 parent 72fcfa9 commit 043e65e
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 22 deletions.
57 changes: 37 additions & 20 deletions census.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ type FarcasterParticipant struct {
PubKey []byte `json:"pubkey"`
Weight *big.Int `json:"weight"`
Username string `json:"username"`
FID uint64 `json:"fid"`
}

// CreateCensus creates a new census from a list of participants.
Expand Down Expand Up @@ -254,17 +255,14 @@ func (v *vocdoniHandler) censusCSV(msg *apirest.APIdata, ctx *httprouter.HTTPCon
return
}
// since each participant can have multiple signers, we need to get the unique usernames
uniqueParticipantsMap := make(map[string]string)
uniqueParticipantsMap := make(map[string]*big.Int)
totalWeight := new(big.Int).SetUint64(0)
for _, p := range participants {
if _, ok := uniqueParticipantsMap[p.Username]; ok {
// if the username is already in the map, skip
// if the username is already in the map, continue
continue
}
if p.Weight == nil {
p.Weight = big.NewInt(1)
}
uniqueParticipantsMap[p.Username] = p.Weight.String()
uniqueParticipantsMap[p.Username] = p.Weight
totalWeight.Add(totalWeight, p.Weight)
}
uniqueParticipants := []string{}
Expand Down Expand Up @@ -705,18 +703,15 @@ func (v *vocdoniHandler) censusTokenAirstack(tokens []*CensusToken, tokenType in
}

// since each participant can have multiple signers, we need to get the unique usernames
uniqueParticipantsMap := make(map[string]string)
uniqueParticipantsMap := make(map[string]*big.Int)
totalWeight := new(big.Int).SetUint64(0)
for _, p := range participants {
if _, ok := uniqueParticipantsMap[p.Username]; ok {
// if the username is already in the map, skip
// if the username is already in the map, continue
continue
}
if p.Weight == nil {
p.Weight = big.NewInt(1)
}
uniqueParticipantsMap[p.Username] = p.Weight
totalWeight.Add(totalWeight, p.Weight)
uniqueParticipantsMap[p.Username] = p.Weight.String()
}
uniqueParticipants := []string{}
for k := range uniqueParticipantsMap {
Expand Down Expand Up @@ -801,10 +796,10 @@ func (v *vocdoniHandler) censusWarpcastChannel(censusID types.HexBytes, channelI
v.censusCreationMap.Store(censusID.String(), CensusInfo{Error: err.Error()})
return
}
uniqueParticipantsMap := map[string]string{}
uniqueParticipantsMap := make(map[string]*big.Int)
for _, p := range participants {
if _, ok := uniqueParticipantsMap[p.Username]; !ok {
uniqueParticipantsMap[p.Username] = "1"
uniqueParticipantsMap[p.Username] = new(big.Int).SetUint64(1)
}
}
for username := range uniqueParticipantsMap {
Expand Down Expand Up @@ -1039,6 +1034,10 @@ func (v *vocdoniHandler) processCensusRecords(records [][]string, progress chan
log.Warnf("invalid record: %v", record)
continue
}
// If the weight is not provided, set it to 1
if weightRecord == "" {
weightRecord = "1"
}
weight, ok = new(big.Int).SetString(weightRecord, 10)
if !ok {
log.Warnf("invalid weight for address %s: %s", address, weightRecord)
Expand Down Expand Up @@ -1135,6 +1134,22 @@ func (v *vocdoniHandler) processCensusRecords(records [][]string, progress chan
pendingAddressesCh <- addr
return
}

// find the addres on the map to get the weight
// 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()]
if ok {
weight = weight.Add(weight, weightAddress)
}
}

if weight.Uint64() == 0 {
log.Warnf("weight not found for user %s", user.Username)
return
}

for _, signer := range user.Signers {
signerBytes, err := hex.DecodeString(strings.TrimPrefix(signer, "0x"))
if err != nil {
Expand All @@ -1143,8 +1158,9 @@ func (v *vocdoniHandler) processCensusRecords(records [][]string, progress chan
}
participantsCh <- &FarcasterParticipant{
PubKey: signerBytes,
Weight: addressMap[addr],
Weight: weight,
Username: user.Username,
FID: user.UserID,
}
}
processedAddresses.Add(1)
Expand Down Expand Up @@ -1202,16 +1218,16 @@ func (v *vocdoniHandler) processCensusRecords(records [][]string, progress chan
}
}
// find the addres on the map to get the weight
var weight *big.Int
var ok bool
// 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 {
weight, ok = addressMap[common.HexToAddress(addr).Hex()]
weightAddress, ok := addressMap[common.HexToAddress(addr).Hex()]
if ok {
break
weight = weight.Add(weight, weightAddress)
}
}

if weight == nil {
if weight.Uint64() == 0 {
log.Warnf("weight not found for user %s with address %v", userData.Username, userData.VerificationsAddresses)
continue
}
Expand All @@ -1226,6 +1242,7 @@ func (v *vocdoniHandler) processCensusRecords(records [][]string, progress chan
PubKey: signerBytes,
Weight: weight,
Username: userData.Username,
FID: userData.FID,
})
}
count++
Expand Down
9 changes: 7 additions & 2 deletions mongo/census.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,22 @@ func (ms *MongoStorage) AddCensus(censusID types.HexBytes, userFID uint64) error
}

// AddParticipantsToCensus updates a census document with participants and their associated values.
func (ms *MongoStorage) AddParticipantsToCensus(censusID types.HexBytes, participants map[string]string,
func (ms *MongoStorage) AddParticipantsToCensus(censusID types.HexBytes, participants map[string]*big.Int,
fromTotalAddresses uint32, totalWeight *big.Int, censusURI string,
) error {
ms.keysLock.Lock()
defer ms.keysLock.Unlock()

participantsString := map[string]string{}
for k, v := range participants {
participantsString[k] = v.String()
}

ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
update := bson.M{
"$set": bson.M{
"participants": participants,
"participants": participantsString,
"fromTotalAddresses": fromTotalAddresses,
"totalWeight": totalWeight.String(),
"url": censusURI,
Expand Down

0 comments on commit 043e65e

Please sign in to comment.