-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
store all custodian account and nodes updates
- Loading branch information
1 parent
10f1321
commit 2777bc1
Showing
7 changed files
with
77 additions
and
26 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
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 |
---|---|---|
@@ -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) | ||
} |
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