-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7534013
commit 4267153
Showing
9 changed files
with
539 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package rank | ||
|
||
import ( | ||
. "github.com/cybercongress/cyberd/cosmos/poc/app/storage" | ||
"math" | ||
) | ||
|
||
const ( | ||
d = 0.85 | ||
tolerance = 0.1 | ||
) | ||
|
||
func CalculateRank(data *InMemoryStorage) []float64 { | ||
|
||
size := data.GetCidsCount() | ||
|
||
if size == 0 { | ||
return []float64{} | ||
} | ||
inverseOfSize := 1.0 / float64(size) | ||
|
||
prevrank := data.GetRank() | ||
zeros := make([]float64, size-len(prevrank)) | ||
prevrank = append(prevrank, zeros...) | ||
|
||
tOverSize := (1.0 - d) / float64(size) | ||
danglingNodes := calculateDanglingNodes(data) | ||
|
||
for i := range danglingNodes { | ||
prevrank[i] = inverseOfSize | ||
} | ||
|
||
change := 2.0 | ||
|
||
var rank []float64 | ||
for change > tolerance { | ||
rank = step(tOverSize, prevrank, danglingNodes, data) | ||
change = calculateChange(prevrank, rank) | ||
prevrank = rank | ||
} | ||
|
||
return rank | ||
} | ||
|
||
func calculateDanglingNodes(data *InMemoryStorage) []int64 { | ||
|
||
outLinks := data.GetOutLinks() | ||
danglingNodes := make([]int64, 0, len(outLinks)) | ||
|
||
for i, cidLinks := range outLinks { | ||
if len(cidLinks) == 0 { | ||
danglingNodes = append(danglingNodes, int64(i)) | ||
} | ||
} | ||
|
||
return danglingNodes | ||
} | ||
|
||
func step(tOverSize float64, prevrank []float64, danglingNodes []int64, data *InMemoryStorage) []float64 { | ||
|
||
innerProduct := 0.0 | ||
for _, danglingNode := range danglingNodes { | ||
innerProduct += prevrank[danglingNode] | ||
} | ||
|
||
innerProductOverSize := innerProduct / float64(len(prevrank)) | ||
rank := make([]float64, len(prevrank)) | ||
|
||
for i, inLinksForI := range data.GetInLinks() { | ||
ksum := 0.0 | ||
|
||
for j := range inLinksForI { | ||
linkStake := float64(data.GetOverallLinkStake(CidNumber(j), CidNumber(i))) | ||
jCidOutStake := float64(data.GetOverallOutLinksStake(CidNumber(j))) | ||
ksum += prevrank[j] * (linkStake / jCidOutStake) | ||
} | ||
|
||
rank[i] = d*(ksum+innerProductOverSize) + tOverSize | ||
} | ||
return rank | ||
} | ||
|
||
func calculateChange(prevrank, rank []float64) float64 { | ||
|
||
acc := 0.0 | ||
for i, pForI := range prevrank { | ||
acc += math.Abs(pForI - rank[i]) | ||
} | ||
|
||
return acc | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package storage | ||
|
||
import ( | ||
"encoding/binary" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
) | ||
|
||
var lastCidNumber = []byte("cyberd_cids_count") | ||
var lastAppHash = []byte("cyberd_app_hash") | ||
|
||
type MainStorage struct { | ||
key *sdk.KVStoreKey | ||
} | ||
|
||
func NewMainStorage(key *sdk.KVStoreKey) MainStorage { | ||
return MainStorage{key: key} | ||
} | ||
|
||
// returns overall added cids count | ||
func (ms MainStorage) GetCidsCount(ctx sdk.Context) uint64 { | ||
|
||
mainStore := ctx.KVStore(ms.key) | ||
lastIndexAsBytes := mainStore.Get(lastCidNumber) | ||
|
||
if lastIndexAsBytes == nil { | ||
return 0 | ||
} | ||
|
||
return binary.LittleEndian.Uint64(lastIndexAsBytes) + 1 | ||
} | ||
|
||
func (ms MainStorage) SetLastCidIndex(ctx sdk.Context, cidsCount []byte) { | ||
|
||
mainStore := ctx.KVStore(ms.key) | ||
mainStore.Set(lastCidNumber, cidsCount) | ||
} | ||
|
||
func (ms MainStorage) GetAppHash(ctx sdk.Context) []byte { | ||
|
||
store := ctx.KVStore(ms.key) | ||
return store.Get(lastAppHash) | ||
} | ||
|
||
func (ms MainStorage) StoreAppHash(ctx sdk.Context, hash []byte) { | ||
|
||
store := ctx.KVStore(ms.key) | ||
store.Set(lastAppHash, hash) | ||
} |
Oops, something went wrong.