Skip to content

Commit

Permalink
skip credit mint works of last round of the day
Browse files Browse the repository at this point in the history
  • Loading branch information
vanessaviolet committed Jan 30, 2024
1 parent cec26d8 commit 4d7a13d
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 29 deletions.
1 change: 1 addition & 0 deletions kernel/hack.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ package kernel
const (
mainnetConsensusOperationElectionForkAt = uint64(1701388800000000000)
mainnetConsensusNodeRemovalTimeForkAt = uint64(1706400000000000000)
mainnetMintDayGapSkipForkBatch = uint64(1800)
mainnetNodeRemovalHackSnapshotHash = "b5a9ab66e3b5d24328f8f87bc38e90f0c426dc38413200bb8ecf7f5b8607a5f9"
)
40 changes: 24 additions & 16 deletions kernel/mint.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ func (chain *Chain) AggregateMintWork() {
// Another fix is to utilize the light node to reference the node removal
// and incentivize the first light nodes that do this.
// we don't care the round state final or cache, it must has subsequent snapshots
if !chain.checkRoundMature(round) {
mts, ok := chain.checkRoundMature(round)
if !ok {
chain.waitOrDone(wait)
continue
}
Expand All @@ -61,11 +62,13 @@ func (chain *Chain) AggregateMintWork() {
logger.Printf("AggregateMintWork(%s) ERROR ReadSnapshotsForNodeRound %s\n", chain.ChainId, err.Error())
continue
}
if len(snapshots) == 0 {
chain.waitOrDone(wait)
continue
day := uint64(time.Hour) * 24
rd := snapshots[0].Timestamp / day
md := mts / day
if rd > md {
panic(fmt.Errorf("AggregateMintWork(%s) %d %d %d", chain.ChainId, round, rd, md))
}
err = chain.writeRoundWork(round, snapshots)
err = chain.writeRoundWork(round, snapshots, rd == md)
if err != nil {
panic(err)
}
Expand All @@ -79,23 +82,28 @@ func (chain *Chain) AggregateMintWork() {
logger.Printf("AggregateMintWork(%s) end with %d\n", chain.ChainId, round)
}

func (chain *Chain) checkRoundMature(round uint64) bool {
crn := chain.State.CacheRound.Number
if crn < round {
panic(fmt.Errorf("AggregateMintWork(%s) waiting %d %d", chain.ChainId, crn, round))
func (chain *Chain) checkRoundMature(round uint64) (uint64, bool) {
cache := chain.State.CacheRound
if cache.Number < round {
panic(fmt.Errorf("AggregateMintWork(%s) waiting %d %d", chain.ChainId, cache.Number, round))
}
if cache.Number == round {
return 0, false
}
if crn == round {
return false
if cache.Number > round+1 {
return chain.State.FinalRound.Start, true
}
if crn == round+1 {
return len(chain.State.CacheRound.Snapshots) > 0
if len(cache.Snapshots) < 1 {
return 0, false
}
return true
return cache.Snapshots[0].Timestamp, true
}

func (chain *Chain) writeRoundWork(round uint64, works []*common.SnapshotWork) error {
func (chain *Chain) writeRoundWork(round uint64, works []*common.SnapshotWork, credit bool) error {
credit = credit || (chain.node.IdForNetwork.String() == config.KernelNetworkId &&
(works[0].Timestamp-chain.node.Epoch)/(uint64(time.Hour)*24) < mainnetMintDayGapSkipForkBatch)
for chain.running {
err := chain.persistStore.WriteRoundWork(chain.ChainId, round, works)
err := chain.persistStore.WriteRoundWork(chain.ChainId, round, works, credit)
if err == nil {
return nil
}
Expand Down
18 changes: 9 additions & 9 deletions kernel/mint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,10 @@ func TestUniversalMintTransaction(t *testing.T) {
timestamp := uint64(clock.Now().UnixNano())
for i := 0; i < 2; i++ {
snapshots := testBuildMintSnapshots(signers, tr.round, timestamp)
err = node.persistStore.WriteRoundWork(node.IdForNetwork, tr.round, snapshots)
err = node.persistStore.WriteRoundWork(node.IdForNetwork, tr.round, snapshots, true)
require.Nil(err)
for j := 1; j < 2*len(signers)/3+1; j++ {
err = node.persistStore.WriteRoundWork(signers[j], tr.round, snapshots)
err = node.persistStore.WriteRoundWork(signers[j], tr.round, snapshots, true)
require.Nil(err)
}

Expand Down Expand Up @@ -199,10 +199,10 @@ func TestMintWorks(t *testing.T) {
leaders := len(signers)*2/3 + 1
for i := 0; i < 2; i++ {
snapshots := testBuildMintSnapshots(signers[1:], 0, timestamp)
err = node.persistStore.WriteRoundWork(node.IdForNetwork, 0, snapshots)
err = node.persistStore.WriteRoundWork(node.IdForNetwork, 0, snapshots, true)
require.Nil(err)
for j := 1; j < leaders; j++ {
err = node.persistStore.WriteRoundWork(signers[j], 0, snapshots)
err = node.persistStore.WriteRoundWork(signers[j], 0, snapshots, true)
require.Nil(err)
}

Expand Down Expand Up @@ -231,7 +231,7 @@ func TestMintWorks(t *testing.T) {

timestamp = uint64(clock.Now().UnixNano())
snapshots := testBuildMintSnapshots(signers[1:], 1, timestamp)
err = node.persistStore.WriteRoundWork(node.IdForNetwork, 1, snapshots[:98])
err = node.persistStore.WriteRoundWork(node.IdForNetwork, 1, snapshots[:98], true)
require.Nil(err)

works, err := node.persistStore.ListNodeWorks(signers, uint32(snapshots[0].Timestamp/uint64(time.Hour*24)))
Expand All @@ -258,10 +258,10 @@ func TestMintWorks(t *testing.T) {
require.Nil(err)
require.Equal(uint64(1), offset)

err = node.persistStore.WriteRoundWork(node.IdForNetwork, 1, snapshots)
err = node.persistStore.WriteRoundWork(node.IdForNetwork, 1, snapshots, true)
require.Nil(err)
for i := 1; i < leaders; i++ {
err = node.persistStore.WriteRoundWork(signers[i], 1, nil)
err = node.persistStore.WriteRoundWork(signers[i], 1, nil, true)
require.Nil(err)
}

Expand Down Expand Up @@ -291,10 +291,10 @@ func TestMintWorks(t *testing.T) {

timestamp = uint64(clock.Now().Add(24 * time.Hour).UnixNano())
snapshots = testBuildMintSnapshots(signers[1:], 2, timestamp)
err = node.persistStore.WriteRoundWork(node.IdForNetwork, 2, snapshots[:10])
err = node.persistStore.WriteRoundWork(node.IdForNetwork, 2, snapshots[:10], true)
require.Nil(err)
for i := 1; i < leaders; i++ {
err = node.persistStore.WriteRoundWork(signers[i], 2, snapshots[:10])
err = node.persistStore.WriteRoundWork(signers[i], 2, snapshots[:10], true)
require.Nil(err)
}

Expand Down
7 changes: 6 additions & 1 deletion kernel/self.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,16 @@ func (node *Node) lockAndPersistTransaction(tx *common.VersionedTransaction, fin
}

func (node *Node) validateKernelSnapshot(s *common.Snapshot, tx *common.VersionedTransaction, finalized bool) error {
if finalized && node.networkId.String() == config.KernelNetworkId && s.Timestamp < mainnetConsensusOperationElectionForkAt {
if finalized && node.networkId.String() == config.KernelNetworkId &&
s.Timestamp < mainnetConsensusOperationElectionForkAt {
return nil
}
switch tx.TransactionType() {
case common.TransactionTypeMint:
if finalized && tx.Inputs[0].Mint.Batch < mainnetMintDayGapSkipForkBatch &&
node.IdForNetwork.String() == config.KernelNetworkId {
return nil
}
err := node.validateMintSnapshot(s, tx)
if err != nil {
logger.Printf("validateMintSnapshot ERROR %v %s %s\n",
Expand Down
4 changes: 2 additions & 2 deletions storage/badger_work.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func (s *BadgerStore) ListNodeWorks(cids []crypto.Hash, day uint32) (map[crypto.
return works, nil
}

func (s *BadgerStore) WriteRoundWork(nodeId crypto.Hash, round uint64, snapshots []*common.SnapshotWork) error {
func (s *BadgerStore) WriteRoundWork(nodeId crypto.Hash, round uint64, snapshots []*common.SnapshotWork, credit bool) error {
return s.snapshotsDB.Update(func(txn *badger.Txn) error {
offKey := graphWorkOffsetKey(nodeId)
off, osm, err := graphReadWorkOffset(txn, offKey)
Expand Down Expand Up @@ -133,7 +133,7 @@ func (s *BadgerStore) WriteRoundWork(nodeId crypto.Hash, round uint64, snapshots
if err != nil || len(fresh) == 0 {
return err
}
if len(fresh[0].Signers) == 0 {
if len(fresh[0].Signers) == 0 || !credit {
return nil
}

Expand Down
2 changes: 1 addition & 1 deletion storage/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ type Store interface {
ListWorkOffsets(cids []crypto.Hash) (map[crypto.Hash]uint64, error)
ListNodeWorks(cids []crypto.Hash, day uint32) (map[crypto.Hash][2]uint64, error)
ReadWorkOffset(nodeId crypto.Hash) (uint64, error)
WriteRoundWork(nodeId crypto.Hash, round uint64, snapshots []*common.SnapshotWork) error
WriteRoundWork(nodeId crypto.Hash, round uint64, snapshots []*common.SnapshotWork, credit bool) error

ReadRoundSpaceCheckpoint(nodeId crypto.Hash) (uint64, uint64, error)
WriteRoundSpaceAndState(space *common.RoundSpace) error
Expand Down

0 comments on commit 4d7a13d

Please sign in to comment.