Skip to content

Commit

Permalink
allow future round spaces aggregation
Browse files Browse the repository at this point in the history
  • Loading branch information
tristenblakely committed Aug 20, 2023
1 parent fe99da3 commit 3bf81fc
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 24 deletions.
51 changes: 37 additions & 14 deletions kernel/mint.go
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,19 @@ func (node *Node) ListMintWorks(batch uint64) (map[crypto.Hash][2]uint64, error)
return works, err
}

func (node *Node) ListRoundSpaces(cids []crypto.Hash, day uint64) (map[crypto.Hash][]*common.RoundSpace, error) {
epoch := node.Epoch / (uint64(time.Hour) * 24)
spaces := make(map[crypto.Hash][]*common.RoundSpace)
for _, id := range cids {
ns, err := node.persistStore.ReadNodeRoundSpacesForBatch(id, day-epoch)
if err != nil {
return nil, err
}
spaces[id] = ns
}
return spaces, nil
}

// a = average work
// for x > 7a, y = 2a
// for 7a > x > a, y = 1/6x + 5/6a
Expand Down Expand Up @@ -571,10 +584,20 @@ func (node *Node) distributeKernelMintByWorks(accepted []*CNode, base common.Int
if err != nil {
return nil, err
}
spaces, err := node.ListRoundSpaces(cids, day-1)
if err != nil {
return nil, err
}

var valid int
var min, max, total common.Integer
var minW, maxW, totalW common.Integer
for _, m := range mints {
ns := spaces[m.IdForNetwork]
if len(ns) > 0 {
// TODO use this for universal mint distributions
logger.Printf("node spaces %s %d %d\n", m.IdForNetwork, ns[0].Batch, len(ns))
}

w := works[m.IdForNetwork]
m.Work = common.NewInteger(w[0]).Mul(120).Div(100)
sign := common.NewInteger(w[1])
Expand All @@ -585,29 +608,29 @@ func (node *Node) distributeKernelMintByWorks(accepted []*CNode, base common.Int
continue
}
valid += 1
if min.Sign() == 0 {
min = m.Work
} else if m.Work.Cmp(min) < 0 {
min = m.Work
if minW.Sign() == 0 {
minW = m.Work
} else if m.Work.Cmp(minW) < 0 {
minW = m.Work
}
if m.Work.Cmp(max) > 0 {
max = m.Work
if m.Work.Cmp(maxW) > 0 {
maxW = m.Work
}
total = total.Add(m.Work)
totalW = totalW.Add(m.Work)
}
if valid < thr {
return nil, fmt.Errorf("distributeKernelMintByWorks not valid %d %d %d %d",
day, len(mints), thr, valid)
}

total = total.Sub(min).Sub(max)
avg := total.Div(valid - 2)
totalW = totalW.Sub(minW).Sub(maxW)
avg := totalW.Div(valid - 2)
if avg.Sign() == 0 {
return nil, fmt.Errorf("distributeKernelMintByWorks not valid %d %d %d %d",
day, len(mints), thr, valid)
}

total = common.NewInteger(0)
totalW = common.NewInteger(0)
upper, lower := avg.Mul(7), avg.Div(7)
for _, m := range mints {
if m.Work.Cmp(upper) >= 0 {
Expand All @@ -617,11 +640,11 @@ func (node *Node) distributeKernelMintByWorks(accepted []*CNode, base common.Int
} else if m.Work.Cmp(lower) <= 0 {
m.Work = avg.Div(7)
}
total = total.Add(m.Work)
totalW = totalW.Add(m.Work)
}

for _, m := range mints {
rat := m.Work.Ration(total)
rat := m.Work.Ration(totalW)
m.Work = rat.Product(base)
}
return mints, nil
Expand Down Expand Up @@ -651,7 +674,7 @@ func (node *Node) validateWorksAndSpacesAggregator(cids []crypto.Hash, thr int,
epoch := node.Epoch / (uint64(time.Hour) * 24)
batch := day - epoch
for _, s := range spaces {
if s.Batch == batch {
if s.Batch >= batch {
spacesAgg += 1
}
}
Expand Down
6 changes: 0 additions & 6 deletions kernel/space.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,6 @@ func (chain *Chain) AggregateRoundSpace() {

since := checkTime - chain.node.Epoch
batch := uint64(since/3600000000000) / 24
if batch > chain.node.LastMint+1 {
logger.Printf("AggregateRoundSpace(%s) ERROR batch future %d %d %s\n",
chain.ChainId, batch, chain.node.LastMint, time.Unix(0, int64(checkTime)))
chain.waitOrDone(wait)
continue
}
space := &common.RoundSpace{
NodeId: chain.ChainId,
Batch: batch,
Expand Down
9 changes: 5 additions & 4 deletions storage/badger_space.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func (s *BadgerStore) ListAggregatedRoundSpaceCheckpoints(cids []crypto.Hash) (m

spaces := make(map[crypto.Hash]*common.RoundSpace)
for _, id := range cids {
batch, round, err := s.ReadRoundSpaceCheckpoint(id)
batch, round, err := readRoundSpaceCheckpoint(txn, id)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -50,16 +50,17 @@ func (s *BadgerStore) ReadNodeRoundSpacesForBatch(nodeId crypto.Hash, batch uint
if err != nil {
return nil, err
}
if bytes.Compare(nodeId[:], item.Key()[:32]) != 0 {
off := len(graphPrefixSpaceQueue)
if bytes.Compare(nodeId[:], item.Key()[off:off+32]) != 0 {
panic(nodeId)
}
if binary.BigEndian.Uint64(item.Key()[32:40]) != batch {
if binary.BigEndian.Uint64(item.Key()[off+32:off+40]) != batch {
panic(batch)
}
space := &common.RoundSpace{
NodeId: nodeId,
Batch: batch,
Round: binary.BigEndian.Uint64(item.Key()[40:]),
Round: binary.BigEndian.Uint64(item.Key()[off+40:]),
Duration: binary.BigEndian.Uint64(val),
}
spaces = append(spaces, space)
Expand Down

0 comments on commit 3bf81fc

Please sign in to comment.