Skip to content

Commit

Permalink
read latest mint with graph timestamp
Browse files Browse the repository at this point in the history
  • Loading branch information
vanessaviolet committed Jul 7, 2023
1 parent 967269d commit f4081cb
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 47 deletions.
2 changes: 1 addition & 1 deletion common/mint.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (tx *VersionedTransaction) validateMint(store DataStore) error {
return fmt.Errorf("invalid mint group %s", mint.Group)
}

dist, err := store.ReadLastMintDistribution()
dist, err := store.ReadLastMintDistribution(^uint64(0))
if err != nil {
return err
}
Expand Down
40 changes: 40 additions & 0 deletions common/store.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package common

import "github.com/MixinNetwork/mixin/crypto"

type UTXOKeysReader interface {
ReadUTXOKeys(hash crypto.Hash, index int) (*UTXOKeys, error)
}

type UTXOLockReader interface {
ReadUTXOLock(hash crypto.Hash, index int) (*UTXOWithLock, error)
CheckDepositInput(deposit *DepositData, tx crypto.Hash) error
ReadLastMintDistribution(ts uint64) (*MintDistribution, error)
}

type UTXOLocker interface {
LockUTXOs(inputs []*Input, tx crypto.Hash, fork bool) error
LockDepositInput(deposit *DepositData, tx crypto.Hash, fork bool) error
LockMintInput(mint *MintData, tx crypto.Hash, fork bool) error
}

type GhostChecker interface {
CheckGhost(key crypto.Key) (*crypto.Hash, error)
}

type NodeReader interface {
ReadAllNodes(offset uint64, withState bool) []*Node
ReadTransaction(hash crypto.Hash) (*VersionedTransaction, string, error)
}

type DomainReader interface {
ReadDomains() []*Domain
}

type DataStore interface {
UTXOLockReader
UTXOLocker
GhostChecker
NodeReader
DomainReader
}
2 changes: 1 addition & 1 deletion common/transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -749,7 +749,7 @@ func (store storeImpl) LockDepositInput(deposit *DepositData, tx crypto.Hash, fo
return nil
}

func (store storeImpl) ReadLastMintDistribution() (*MintDistribution, error) {
func (store storeImpl) ReadLastMintDistribution(ts uint64) (*MintDistribution, error) {
return nil, nil
}

Expand Down
37 changes: 0 additions & 37 deletions common/utxo.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,43 +22,6 @@ type UTXOKeys struct {
Keys []*crypto.Key
}

type UTXOKeysReader interface {
ReadUTXOKeys(hash crypto.Hash, index int) (*UTXOKeys, error)
}

type UTXOLockReader interface {
ReadUTXOLock(hash crypto.Hash, index int) (*UTXOWithLock, error)
CheckDepositInput(deposit *DepositData, tx crypto.Hash) error
ReadLastMintDistribution() (*MintDistribution, error)
}

type UTXOLocker interface {
LockUTXOs(inputs []*Input, tx crypto.Hash, fork bool) error
LockDepositInput(deposit *DepositData, tx crypto.Hash, fork bool) error
LockMintInput(mint *MintData, tx crypto.Hash, fork bool) error
}

type GhostChecker interface {
CheckGhost(key crypto.Key) (*crypto.Hash, error)
}

type NodeReader interface {
ReadAllNodes(offset uint64, withState bool) []*Node
ReadTransaction(hash crypto.Hash) (*VersionedTransaction, string, error)
}

type DomainReader interface {
ReadDomains() []*Domain
}

type DataStore interface {
UTXOLockReader
UTXOLocker
GhostChecker
NodeReader
DomainReader
}

func (tx *VersionedTransaction) UnspentOutputs() []*UTXOWithLock {
var utxos []*UTXOWithLock
for i, out := range tx.Outputs {
Expand Down
5 changes: 4 additions & 1 deletion kernel/election.go
Original file line number Diff line number Diff line change
Expand Up @@ -359,10 +359,13 @@ func (node *Node) validateNodeAcceptSnapshot(s *common.Snapshot, tx *common.Vers

func (node *Node) reloadConsensusState(s *common.Snapshot, tx *common.VersionedTransaction) error {
if tx.TransactionType() == common.TransactionTypeMint {
mint, err := node.persistStore.ReadLastMintDistribution()
mint, err := node.persistStore.ReadLastMintDistribution(s.Timestamp)
if err != nil {
return err
}
if mint.Batch < node.LastMint {
panic(node.LastMint)
}
node.LastMint = mint.Batch
return nil
}
Expand Down
6 changes: 3 additions & 3 deletions kernel/mint.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ func (node *Node) tryToSlashLegacyLightPool(batch uint64, amount common.Integer,
}

func (node *Node) PoolSize() (common.Integer, error) {
dist, err := node.persistStore.ReadLastMintDistribution()
dist, err := node.persistStore.ReadLastMintDistribution(uint64(clock.Now().UnixNano()))
if err != nil {
return common.Zero, err
}
Expand Down Expand Up @@ -429,7 +429,7 @@ func (node *Node) checkUniversalMintPossibility(timestamp uint64, validateOnly b
pool = pool.Div(MintYearShares)
total := pool.Div(MintYearBatches)

dist, err := node.persistStore.ReadLastMintDistribution()
dist, err := node.persistStore.ReadLastMintDistribution(timestamp)
if err != nil {
logger.Verbosef("ReadLastMintDistribution ERROR %s\n", err)
return 0, common.Zero
Expand Down Expand Up @@ -480,7 +480,7 @@ func (node *Node) checkLegacyMintPossibility(timestamp uint64, validateOnly bool
light := total.Div(10)
full := light.Mul(9)

dist, err := node.persistStore.ReadLastMintDistribution()
dist, err := node.persistStore.ReadLastMintDistribution(timestamp)
if err != nil {
logger.Verbosef("ReadLastMintDistribution ERROR %s\n", err)
return 0, common.Zero
Expand Down
2 changes: 1 addition & 1 deletion kernel/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func SetupNode(custom *config.Custom, persistStore storage.Store, cacheStore *ri

node.LoadNodeConfig()

mint, err := node.persistStore.ReadLastMintDistribution()
mint, err := node.persistStore.ReadLastMintDistribution(uint64(clock.Now().UnixNano()))
if err != nil {
return nil, fmt.Errorf("ReadLastMintDistribution() => %v", err)
}
Expand Down
4 changes: 2 additions & 2 deletions storage/badger_mint.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func (s *BadgerStore) ReadMintDistributions(offset, count uint64) ([]*common.Min
return mints, transactions, nil
}

func (s *BadgerStore) ReadLastMintDistribution() (*common.MintDistribution, error) {
func (s *BadgerStore) ReadLastMintDistribution(ts uint64) (*common.MintDistribution, error) {
s.mutex.RLock()
defer s.mutex.RUnlock()

Expand All @@ -75,7 +75,7 @@ func (s *BadgerStore) ReadLastMintDistribution() (*common.MintDistribution, erro
it := txn.NewIterator(opts)
defer it.Close()

it.Seek(graphMintKey(^uint64(0)))
it.Seek(graphMintKey(ts))
for ; it.Valid(); it.Next() {
item := it.Item()
key := item.KeyCopy(nil)
Expand Down
2 changes: 1 addition & 1 deletion storage/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ type Store interface {
CacheRetrieveTransactions(limit int) ([]*common.VersionedTransaction, error)
CacheRemoveTransactions([]crypto.Hash) error

ReadLastMintDistribution() (*common.MintDistribution, error)
ReadLastMintDistribution(ts uint64) (*common.MintDistribution, error)
LockMintInput(mint *common.MintData, tx crypto.Hash, fork bool) error
ReadMintDistributions(offset, count uint64) ([]*common.MintDistribution, []*common.VersionedTransaction, error)
ReadSnapshotWorksForNodeRound(nodeId crypto.Hash, round uint64) ([]*common.SnapshotWork, error)
Expand Down

0 comments on commit f4081cb

Please sign in to comment.