Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove unnecessary DBVersion param. #2400

Merged
merged 2 commits into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions wallet/udb/stake.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2015-2017 The Decred developers
// Copyright (c) 2015-2024 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.

Expand Down Expand Up @@ -105,7 +105,7 @@ func (s *StakeStore) insertSStx(ns walletdb.ReadWriteBucket, sstx *dcrutil.Tx) e
}

// Add the SStx to the database.
err := putSStxRecord(ns, record, DBVersion)
err := putSStxRecord(ns, record)
if err != nil {
return err
}
Expand Down Expand Up @@ -170,7 +170,7 @@ func (s *StakeStore) dumpSStxHashesForAddress(ns walletdb.ReadBucket, addr stdad

// Access the database and store the result locally.
for _, h := range allTickets {
thisHash160, p2sh, err := fetchSStxRecordSStxTicketHash160(ns, &h, DBVersion)
thisHash160, p2sh, err := fetchSStxRecordSStxTicketHash160(ns, &h)
if err != nil {
return nil, errors.E(errors.IO, err)
}
Expand Down Expand Up @@ -198,7 +198,7 @@ func (s *StakeStore) DumpSStxHashesForAddress(ns walletdb.ReadBucket, addr stdad
// sstxAddress returns the address for a given ticket.
func (s *StakeStore) sstxAddress(ns walletdb.ReadBucket, hash *chainhash.Hash) (stdaddr.Address, error) {
// Access the database and store the result locally.
thisHash160, p2sh, err := fetchSStxRecordSStxTicketHash160(ns, hash, DBVersion)
thisHash160, p2sh, err := fetchSStxRecordSStxTicketHash160(ns, hash)
if err != nil {
return nil, err
}
Expand Down
97 changes: 9 additions & 88 deletions wallet/udb/stakedb.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2015-2017 The Decred developers
// Copyright (c) 2015-2024 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.

Expand Down Expand Up @@ -166,17 +166,9 @@ func deserializeSStxRecord(serializedSStxRecord []byte, dbVersion uint32) (*sstx

// deserializeSStxTicketHash160 deserializes and returns a 20 byte script
// hash for a ticket's 0th output.
func deserializeSStxTicketHash160(serializedSStxRecord []byte, dbVersion uint32) (hash160 []byte, p2sh bool, err error) {
var pkscriptLocOffset int
var txOffset int
switch {
case dbVersion < 3:
pkscriptLocOffset = 8 // After transaction size
txOffset = 8 + 4 + 1 + stake.MaxSingleBytePushLength
case dbVersion >= 3:
pkscriptLocOffset = 0
txOffset = 4
}
func deserializeSStxTicketHash160(serializedSStxRecord []byte) (hash160 []byte, p2sh bool, err error) {
const pkscriptLocOffset = 0
const txOffset = 4

pkscriptLoc := int(binary.LittleEndian.Uint32(serializedSStxRecord[pkscriptLocOffset:])) + txOffset

Expand Down Expand Up @@ -205,75 +197,7 @@ func deserializeSStxTicketHash160(serializedSStxRecord []byte, dbVersion uint32)
}

// serializeSSTxRecord returns the serialization of the passed txrecord row.
func serializeSStxRecord(record *sstxRecord, dbVersion uint32) ([]byte, error) {
switch {
case dbVersion < 3:
msgTx := record.tx.MsgTx()
msgTxSize := int64(msgTx.SerializeSize())

size := 0

// tx tree is implicit (stake)

// size of msgTx (recast to int64)
size += int64Size

// byte index of the ticket pk script
size += int32Size

// intended votebits length (uint8)
size += int8Size

// intended votebits (75 bytes)
size += stake.MaxSingleBytePushLength

// msgTx size is variable.
size += int(msgTxSize)

// timestamp (int64)
size += int64Size

buf := make([]byte, size)

curPos := 0

// Write msgTx size (as a uint64).
binary.LittleEndian.PutUint64(buf[curPos:curPos+int64Size], uint64(msgTxSize))
curPos += int64Size

// Write the pkScript loc for the ticket output as a uint32.
pkScrLoc := msgTx.PkScriptLocs()
binary.LittleEndian.PutUint32(buf[curPos:curPos+int32Size], uint32(pkScrLoc[0]))
curPos += int32Size

// Write the intended votebits length (uint8). Hardcode the uint16
// size for now.
buf[curPos] = byte(int16Size + len(record.voteBitsExt))
curPos += int8Size

// Write the first two bytes for the intended votebits (75 bytes max),
// then write the extended vote bits.
binary.LittleEndian.PutUint16(buf[curPos:curPos+int16Size], record.voteBits)
curPos += int16Size
copy(buf[curPos:], record.voteBitsExt)
curPos += stake.MaxSingleBytePushLength - 2

// Serialize and write transaction.
var b bytes.Buffer
b.Grow(msgTx.SerializeSize())
err := msgTx.Serialize(&b)
if err != nil {
return buf, err
}
copy(buf[curPos:curPos+int(msgTxSize)], b.Bytes())
curPos += int(msgTxSize)

// Write received unix time (int64).
binary.LittleEndian.PutUint64(buf[curPos:curPos+int64Size], uint64(record.ts.Unix()))

return buf, nil

case dbVersion >= 3:
func serializeSStxRecord(record *sstxRecord) ([]byte, error) {
tx := record.tx.MsgTx()
txSize := tx.SerializeSize()

Expand All @@ -287,9 +211,6 @@ func serializeSStxRecord(record *sstxRecord, dbVersion uint32) ([]byte, error) {
binary.LittleEndian.PutUint64(buf[4+txSize:], uint64(record.ts.Unix()))
return buf, nil

default:
panic("unreachable")
}
}

// stakeStoreExists returns whether or not the stake store has already
Expand All @@ -315,7 +236,7 @@ func fetchSStxRecord(ns walletdb.ReadBucket, hash *chainhash.Hash, dbVersion uin

// fetchSStxRecordSStxTicketHash160 retrieves a ticket 0th output script or
// pubkeyhash from the sstx records bucket with the given hash.
func fetchSStxRecordSStxTicketHash160(ns walletdb.ReadBucket, hash *chainhash.Hash, dbVersion uint32) (hash160 []byte, p2sh bool, err error) {
func fetchSStxRecordSStxTicketHash160(ns walletdb.ReadBucket, hash *chainhash.Hash) (hash160 []byte, p2sh bool, err error) {
bucket := ns.NestedReadBucket(sstxRecordsBucketName)

key := hash[:]
Expand All @@ -324,15 +245,15 @@ func fetchSStxRecordSStxTicketHash160(ns walletdb.ReadBucket, hash *chainhash.Ha
return nil, false, errors.E(errors.NotExist, errors.Errorf("no ticket purchase %v", hash))
}

return deserializeSStxTicketHash160(val, dbVersion)
return deserializeSStxTicketHash160(val)
}

// putSStxRecord inserts a given SStx record to the SStxrecords bucket.
func putSStxRecord(ns walletdb.ReadWriteBucket, record *sstxRecord, dbVersion uint32) error {
func putSStxRecord(ns walletdb.ReadWriteBucket, record *sstxRecord) error {
bucket := ns.NestedReadWriteBucket(sstxRecordsBucketName)

// Write the serialized txrecord keyed by the tx hash.
serializedSStxRecord, err := serializeSStxRecord(record, dbVersion)
serializedSStxRecord, err := serializeSStxRecord(record)
if err != nil {
return errors.E(errors.IO, err)
}
Expand Down
2 changes: 1 addition & 1 deletion wallet/udb/upgrades.go
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ func votingPreferencesUpgrade(tx walletdb.ReadWriteTx, publicPassphrase []byte,
ticketPurchases[hash] = ticketPurchase
}
for _, ticketPurchase := range ticketPurchases {
err := putSStxRecord(stakemgrBucket, ticketPurchase, newVersion)
err := putSStxRecord(stakemgrBucket, ticketPurchase)
if err != nil {
return err
}
Expand Down
Loading