From 7e44773d21f80084f6f916bab8c6d25fdf867108 Mon Sep 17 00:00:00 2001 From: jholdstock Date: Tue, 3 Sep 2024 10:33:16 +0100 Subject: [PATCH] udb: Remove unnecessary DBVersion param. serializeSStxRecord implemented two different behaviours based on the provided database version parameter, however all instances of the param are hard-coded and only one of the behaviours is ever invoked. This has a nice side-effect of removing a panic. --- wallet/udb/stake.go | 2 +- wallet/udb/stakedb.go | 77 ++---------------------------------------- wallet/udb/upgrades.go | 2 +- 3 files changed, 5 insertions(+), 76 deletions(-) diff --git a/wallet/udb/stake.go b/wallet/udb/stake.go index 9b258ab30..605320e3e 100644 --- a/wallet/udb/stake.go +++ b/wallet/udb/stake.go @@ -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 } diff --git a/wallet/udb/stakedb.go b/wallet/udb/stakedb.go index fb865d9a8..a1bcca05e 100644 --- a/wallet/udb/stakedb.go +++ b/wallet/udb/stakedb.go @@ -197,75 +197,7 @@ func deserializeSStxTicketHash160(serializedSStxRecord []byte) (hash160 []byte, } // 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() @@ -279,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 @@ -320,11 +249,11 @@ func fetchSStxRecordSStxTicketHash160(ns walletdb.ReadBucket, hash *chainhash.Ha } // 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) } diff --git a/wallet/udb/upgrades.go b/wallet/udb/upgrades.go index b8e4ca8d7..2211491a3 100644 --- a/wallet/udb/upgrades.go +++ b/wallet/udb/upgrades.go @@ -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 }