Skip to content

Commit

Permalink
Revert "refactor: optimized GetSortedRevealedValues (#1173)"
Browse files Browse the repository at this point in the history
This reverts commit 9842a61.
  • Loading branch information
Yashk767 committed May 20, 2024
1 parent 55f126e commit d9d5c63
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 119 deletions.
91 changes: 22 additions & 69 deletions cmd/propose.go
Original file line number Diff line number Diff line change
Expand Up @@ -391,92 +391,45 @@ func (*UtilsStruct) GetSortedRevealedValues(client *ethclient.Client, blockNumbe
return nil, err
}
log.Debugf("GetSortedRevealedValues: Revealed Data: %+v", assignedAsset)

var wg sync.WaitGroup
resultsChan := make(chan *types.AssetResult, len(assignedAsset))

for _, asset := range assignedAsset {
wg.Add(1)
go processAsset(asset, resultsChan, &wg)
}

wg.Wait()
close(resultsChan)

revealedValuesWithIndex := make(map[uint16][]*big.Int)
voteWeights := make(map[string]*big.Int)
influenceSum := make(map[uint16]*big.Int)

for result := range resultsChan {
for leafId, values := range result.RevealedValuesWithIndex {
revealedValuesWithIndex[leafId] = append(revealedValuesWithIndex[leafId], values...)
}
for value, weight := range result.VoteWeights {
if voteWeights[value] == nil {
voteWeights[value] = weight
log.Debug("Calculating sorted revealed values, vote weights and influence sum...")
for _, asset := range assignedAsset {
for _, assetValue := range asset.RevealedValues {
if revealedValuesWithIndex[assetValue.LeafId] == nil {
revealedValuesWithIndex[assetValue.LeafId] = []*big.Int{assetValue.Value}
} else {
voteWeights[value].Add(voteWeights[value], weight)
if !utils.ContainsBigInteger(revealedValuesWithIndex[assetValue.LeafId], assetValue.Value) {
revealedValuesWithIndex[assetValue.LeafId] = append(revealedValuesWithIndex[assetValue.LeafId], assetValue.Value)
}
}
}
for leafId, sum := range result.InfluenceSum {
if influenceSum[leafId] == nil {
influenceSum[leafId] = sum
} else {
influenceSum[leafId].Add(influenceSum[leafId], sum)
//Calculate vote weights
if voteWeights[assetValue.Value.String()] == nil {
voteWeights[assetValue.Value.String()] = big.NewInt(0)
}
voteWeights[assetValue.Value.String()] = big.NewInt(0).Add(voteWeights[assetValue.Value.String()], asset.Influence)

//Calculate influence sum
if influenceSum[assetValue.LeafId] == nil {
influenceSum[assetValue.LeafId] = big.NewInt(0)
}
influenceSum[assetValue.LeafId] = big.NewInt(0).Add(influenceSum[assetValue.LeafId], asset.Influence)
}
}

for _, values := range revealedValuesWithIndex {
sort.Slice(values, func(i, j int) bool {
return values[i].Cmp(values[j]) == -1
//sort revealed values
for _, element := range revealedValuesWithIndex {
sort.Slice(element, func(i, j int) bool {
return element[i].Cmp(element[j]) == -1
})
}

return &types.RevealedDataMaps{
SortedRevealedValues: revealedValuesWithIndex,
VoteWeights: voteWeights,
InfluenceSum: influenceSum,
}, nil
}

func processAsset(asset types.RevealedStruct, resultsChan chan<- *types.AssetResult, wg *sync.WaitGroup) {
defer wg.Done()

revealedValuesWithIndex := make(map[uint16][]*big.Int)
voteWeights := make(map[string]*big.Int)
influenceSum := make(map[uint16]*big.Int)

for _, assetValue := range asset.RevealedValues {
leafId := assetValue.LeafId
valueStr := assetValue.Value.String()
influence := asset.Influence

// Append the leaf value to the revealed values slice if it's not already present
if !utils.ContainsBigInteger(revealedValuesWithIndex[leafId], assetValue.Value) {
revealedValuesWithIndex[leafId] = append(revealedValuesWithIndex[leafId], assetValue.Value)
}

// Calculate vote weights
if voteWeights[valueStr] == nil {
voteWeights[valueStr] = big.NewInt(0)
}
voteWeights[valueStr] = voteWeights[valueStr].Add(voteWeights[valueStr], influence)

// Calculate influence sum
if influenceSum[leafId] == nil {
influenceSum[leafId] = big.NewInt(0)
}
influenceSum[leafId] = influenceSum[leafId].Add(influenceSum[leafId], influence)
}

resultsChan <- &types.AssetResult{
RevealedValuesWithIndex: revealedValuesWithIndex,
VoteWeights: voteWeights,
InfluenceSum: influenceSum,
}
}

//This function returns the medians, idsRevealedInThisEpoch and revealedDataMaps
func (*UtilsStruct) MakeBlock(client *ethclient.Client, blockNumber *big.Int, epoch uint32, rogueData types.Rogue) ([]*big.Int, []uint16, *types.RevealedDataMaps, error) {
log.Debugf("MakeBlock: Calling GetSortedRevealedValues with arguments blockNumber = %s, epoch = %d", blockNumber, epoch)
Expand Down
53 changes: 9 additions & 44 deletions cmd/reveal.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@ import (
"razor/pkg/bindings"
"razor/utils"
"strings"
"sync"

"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common"
Types "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethclient"
)

Expand Down Expand Up @@ -137,25 +135,14 @@ func (*UtilsStruct) IndexRevealEventsOfCurrentEpoch(client *ethclient.Client, bl
if err != nil {
return nil, err
}

revealedDataChan := make(chan types.RevealedStruct, len(logs))
errorChan := make(chan error, len(logs))
var wg sync.WaitGroup

var revealedData []types.RevealedStruct
for _, vLog := range logs {
wg.Add(1)
go func(vLog Types.Log) {
defer wg.Done()
data, unpackErr := abiUtils.Unpack(contractAbi, "Revealed", vLog.Data)
if unpackErr != nil {
errorChan <- unpackErr
return
}

if epoch != data[0].(uint32) {
return
}

data, unpackErr := abiUtils.Unpack(contractAbi, "Revealed", vLog.Data)
if unpackErr != nil {
log.Debug(unpackErr)
continue
}
if epoch == data[0].(uint32) {
treeValues := data[2].([]struct {
LeafId uint16 `json:"leafId"`
Value *big.Int `json:"value"`
Expand All @@ -171,31 +158,9 @@ func (*UtilsStruct) IndexRevealEventsOfCurrentEpoch(client *ethclient.Client, bl
RevealedValues: revealedValues,
Influence: data[1].(*big.Int),
}

revealedDataChan <- consolidatedRevealedData
}(vLog)
}

wg.Wait()
close(revealedDataChan)
close(errorChan)

var revealedData []types.RevealedStruct
for {
select {
case data, ok := <-revealedDataChan:
if ok {
revealedData = append(revealedData, data)
}
case err, ok := <-errorChan:
if ok {
log.Debug(err)
}
}
if len(revealedDataChan) == 0 && len(errorChan) == 0 {
break
revealedData = append(revealedData, consolidatedRevealedData)
}
}

log.Debug("IndexRevealEventsOfCurrentEpoch: Revealed values: ", revealedData)
return revealedData, nil
}
6 changes: 0 additions & 6 deletions core/types/assets.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,6 @@ type Asset struct {
Collection bindings.StructsCollection
}

type AssetResult struct {
RevealedValuesWithIndex map[uint16][]*big.Int
VoteWeights map[string]*big.Int
InfluenceSum map[uint16]*big.Int
}

type Locks struct {
Amount *big.Int
UnlockAfter *big.Int
Expand Down

0 comments on commit d9d5c63

Please sign in to comment.