Skip to content

Commit

Permalink
store all custodian account and nodes updates
Browse files Browse the repository at this point in the history
  • Loading branch information
vanessaviolet committed Jul 7, 2023
1 parent 10f1321 commit 2777bc1
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 26 deletions.
8 changes: 6 additions & 2 deletions common/custodian.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@ package common

import "fmt"

type CustodianInfo struct {
type CustodianNode struct {
Custodian Address
Payee Address
Extra []byte
}

func (ci *CustodianInfo) Validate() error {
func (ci *CustodianNode) validate() error {
panic(0)
}

func (tx *Transaction) parseCustodianUpdateNodesExtra() (*Address, []*CustodianNode, error) {
panic(0)
}

Expand Down
8 changes: 6 additions & 2 deletions common/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,14 @@ func (tx *SignedTransaction) getExtraLimit() int {
if len(out.Keys) != 1 {
return ExtraSizeGeneralLimit
}
if out.Type != OutputTypeScript {
if out.Script.String() != "fffe40" {
return ExtraSizeGeneralLimit
}
if out.Script.String() != "fffe40" {
switch out.Type {
case OutputTypeScript:
case OutputTypeCustodianUpdateNodes:
return ExtraSizeStorageCapacity
default:
return ExtraSizeGeneralLimit
}
step := NewIntegerFromString(ExtraStoragePriceStep)
Expand Down
2 changes: 1 addition & 1 deletion kernel/mint.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func (node *Node) MintLoop() {
case <-node.done:
return
case <-ticker.C:
ca, err := node.persistStore.ReadCustodianAccount()
ca, _, err := node.persistStore.ReadCustodianAccount()
if err != nil {
panic(err)
}
Expand Down
75 changes: 60 additions & 15 deletions storage/badger_custodian.go
Original file line number Diff line number Diff line change
@@ -1,31 +1,76 @@
package storage

import (
"encoding/binary"

"github.com/MixinNetwork/mixin/common"
"github.com/dgraph-io/badger/v4"
)

func (s *BadgerStore) writeCustodianNodes(txn *badger.Txn, snap *common.Snapshot, custodian *common.Address, nodes []*common.CustodianInfo) error {
panic(0)
}

func (s *BadgerStore) ReadCustodianAccount() (*common.Address, error) {
func (s *BadgerStore) ReadCustodianAccount() (*common.Address, uint64, error) {
txn := s.snapshotsDB.NewTransaction(false)
defer txn.Discard()

item, err := txn.Get([]byte(graphPrefixCustodianAccount))
if err == badger.ErrKeyNotFound {
return nil, nil
} else if err != nil {
return nil, err
return s.readCustodianAccount(txn)
}

func (s *BadgerStore) readCustodianAccount(txn *badger.Txn) (*common.Address, uint64, error) {
opts := badger.DefaultIteratorOptions
opts.PrefetchValues = true
opts.Reverse = true

it := txn.NewIterator(opts)
defer it.Close()

it.Seek(graphCustodianAccountKey(^uint64(0)))
if it.ValidForPrefix([]byte(graphPrefixCustodianAccount)) {
key := it.Item().KeyCopy(nil)
ts := graphCustodianAccountTimestamp(key)
val, err := it.Item().ValueCopy(nil)
if err != nil {
return nil, 0, err
}
addr, err := common.NewAddressFromString(string(val))
return &addr, ts, err
}
val, err := item.ValueCopy(nil)

return nil, 0, nil
}

func (s *BadgerStore) writeCustodianNodes(txn *badger.Txn, snap *common.Snapshot, custodian *common.Address, nodes []*common.CustodianNode) error {
old, ts, err := s.readCustodianAccount(txn)
if err != nil {
return nil, err
return err
}
switch {
case ts == 0 && old != nil:
panic(snap.Hash.String())
case ts != 0 && old == nil:
panic(snap.Hash.String())
case old == nil && ts == 0:
case ts > snap.Timestamp:
panic(snap.Hash.String())
case ts == snap.Timestamp && custodian.String() == old.String():
return nil
case ts == snap.Timestamp && custodian.String() != old.String():
panic(snap.Hash.String())
case ts < snap.Timestamp:
}
addr, err := common.NewAddressFromString(string(val))

key := graphCustodianAccountKey(snap.Timestamp)
err = txn.Set(key, []byte(custodian.String()))
if err != nil {
return nil, err
return err
}
return &addr, nil
panic(0)
}

func graphCustodianAccountKey(ts uint64) []byte {
key := []byte(graphPrefixCustodianAccount)
return binary.BigEndian.AppendUint64(key, ts)
}

func graphCustodianAccountTimestamp(key []byte) uint64 {
ts := key[len(graphPrefixCustodianAccount):]
return binary.BigEndian.Uint64(ts)
}
2 changes: 1 addition & 1 deletion storage/badger_graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const (
graphPrefixWorkSnapshot = "WORKSNAPSHOT"
graphPrefixSpaceCheckpoint = "SPACECHECKPOINT"
graphPrefixSpaceQueue = "SPACEQUEUE"
graphPrefixCustodianAccount = "CUSTODIAN:ACCOUNT"
graphPrefixCustodianAccount = "CUSTODIAN:ACCOUNT:"
)

func (s *BadgerStore) RemoveGraphEntries(prefix string) (int, error) {
Expand Down
6 changes: 2 additions & 4 deletions storage/badger_topology.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,6 @@ func (s *BadgerStore) ReadSnapshotsSinceTopology(topologyOffset, count uint64) (
}

func (s *BadgerStore) TopologySequence() uint64 {
var sequence uint64

txn := s.snapshotsDB.NewTransaction(false)
defer txn.Discard()

Expand All @@ -132,9 +130,9 @@ func (s *BadgerStore) TopologySequence() uint64 {
it.Seek(graphTopologyKey(^uint64(0)))
if it.ValidForPrefix([]byte(graphPrefixTopology)) {
key := it.Item().KeyCopy(nil)
sequence = graphTopologyOrder(key)
return graphTopologyOrder(key)
}
return sequence
return 0
}

func writeTopology(txn *badger.Txn, snap *common.SnapshotWithTopologicalOrder) error {
Expand Down
2 changes: 1 addition & 1 deletion storage/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type Store interface {
ReadLink(from, to crypto.Hash) (uint64, error)
WriteSnapshot(*common.SnapshotWithTopologicalOrder, []crypto.Hash) error
ReadDomains() []*common.Domain
ReadCustodianAccount() (*common.Address, error)
ReadCustodianAccount() (*common.Address, uint64, error)

CachePutTransaction(tx *common.VersionedTransaction) error
CacheGetTransaction(hash crypto.Hash) (*common.VersionedTransaction, error)
Expand Down

0 comments on commit 2777bc1

Please sign in to comment.