Skip to content

Commit

Permalink
Change DB schemas
Browse files Browse the repository at this point in the history
  • Loading branch information
mike76-dev committed Feb 25, 2024
1 parent c9ed857 commit b56cf33
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 73 deletions.
16 changes: 8 additions & 8 deletions init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ CREATE TABLE cs_consistency (

CREATE TABLE cs_sfpool (
id INT NOT NULL AUTO_INCREMENT,
bytes VARBINARY(24) NOT NULL,
bytes BLOB NOT NULL,
PRIMARY KEY (id)
);

Expand All @@ -68,7 +68,7 @@ CREATE TABLE cs_changelog (
CREATE TABLE cs_dsco (
height BIGINT UNSIGNED NOT NULL,
scoid BINARY(32) NOT NULL,
bytes VARBINARY(56) NOT NULL,
bytes BLOB NOT NULL,
PRIMARY KEY (scoid ASC)
);

Expand All @@ -93,7 +93,7 @@ CREATE TABLE cs_oak_init (

CREATE TABLE cs_sco (
scoid BINARY(32) NOT NULL,
bytes VARBINARY(56) NOT NULL,
bytes BLOB NOT NULL,
PRIMARY KEY (scoid ASC)
);

Expand All @@ -105,7 +105,7 @@ CREATE TABLE cs_fc (

CREATE TABLE cs_sfo (
sfoid BINARY(32) NOT NULL,
bytes VARBINARY(80) NOT NULL,
bytes BLOB NOT NULL,
PRIMARY KEY (sfoid ASC)
);

Expand Down Expand Up @@ -136,7 +136,7 @@ CREATE TABLE cs_map (

CREATE TABLE cs_cl (
ceid BINARY(32) NOT NULL,
bytes VARBINARY(1024) NOT NULL,
bytes BLOB NOT NULL,
PRIMARY KEY (ceid ASC)
);

Expand Down Expand Up @@ -213,13 +213,13 @@ CREATE TABLE wt_addr (

CREATE TABLE wt_sco (
scoid BINARY(32) NOT NULL,
bytes VARBINARY(56) NOT NULL,
bytes BLOB NOT NULL,
PRIMARY KEY (scoid ASC)
);

CREATE TABLE wt_sfo (
sfoid BINARY(32) NOT NULL,
bytes VARBINARY(80) NOT NULL,
bytes BLOB NOT NULL,
PRIMARY KEY (sfoid ASC)
);

Expand All @@ -240,7 +240,7 @@ CREATE TABLE wt_info (
cc BINARY(32) NOT NULL,
height BIGINT UNSIGNED NOT NULL,
encrypted BLOB NOT NULL,
sfpool VARBINARY(24) NOT NULL,
sfpool BLOB NOT NULL,
salt BINARY(32) NOT NULL,
progress BIGINT UNSIGNED NOT NULL,
seed BLOB NOT NULL,
Expand Down
5 changes: 1 addition & 4 deletions modules/consensus/applytransaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"database/sql"
"errors"
"io"

"github.com/mike76-dev/sia-satellite/modules"

Expand Down Expand Up @@ -275,9 +274,7 @@ func applyArbitraryData(tx *sql.Tx, pb *processedBlock, t types.Transaction) err
for _, arb := range t.ArbitraryData {
if bytes.HasPrefix(arb, types.SpecifierFoundation[:]) {
var update types.FoundationAddressUpdate
var buf bytes.Buffer
buf.Write(arb[16:])
d := types.NewDecoder(io.LimitedReader{R: &buf, N: int64(len(arb)) - 16})
d := types.NewBufDecoder(arb[16:])
update.DecodeFrom(d)
if err := d.Err(); err != nil {
return err
Expand Down
10 changes: 3 additions & 7 deletions modules/consensus/consistency.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package consensus

import (
"bytes"
"database/sql"
"errors"
"fmt"
"io"

"github.com/mike76-dev/sia-satellite/modules"

Expand Down Expand Up @@ -94,7 +92,7 @@ func checkDSCOs(tx *sql.Tx) {
var height uint64
var scoid types.SiacoinOutputID
id := make([]byte, 32)
scoBytes := make([]byte, 0, 56)
var scoBytes []byte
if err := rows.Scan(&height, &id, &scoBytes); err != nil {
rows.Close()
manageErr(tx, err)
Expand All @@ -115,9 +113,7 @@ func checkDSCOs(tx *sql.Tx) {

// Sum the funds.
var sco types.SiacoinOutput
var buf bytes.Buffer
buf.Write(scoBytes)
d := types.NewDecoder(io.LimitedReader{R: &buf, N: int64(len(scoBytes))})
d := types.NewBufDecoder(scoBytes)
sco.DecodeFrom(d)
if err := d.Err(); err != nil {
rows.Close()
Expand Down Expand Up @@ -145,7 +141,7 @@ func checkDSCOs(tx *sql.Tx) {
// Check that all of the correct heights are represented.
currentHeight := blockHeight(tx)
expectedBuckets := 0
for i := currentHeight + 1; i <= currentHeight + modules.MaturityDelay; i++ {
for i := currentHeight + 1; i <= currentHeight+modules.MaturityDelay; i++ {
if i < modules.MaturityDelay {
continue
}
Expand Down
49 changes: 19 additions & 30 deletions modules/consensus/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"database/sql"
"encoding/binary"
"errors"
"io"
"time"

"github.com/mike76-dev/sia-satellite/modules"
Expand Down Expand Up @@ -166,8 +165,7 @@ func findBlockByID(tx *sql.Tx, id types.BlockID) (*processedBlock, bool, error)
return nil, false, err
}
pb := new(processedBlock)
buf := bytes.NewBuffer(pbBytes)
d := types.NewDecoder(io.LimitedReader{R: buf, N: int64(len(pbBytes))})
d := types.NewBufDecoder(pbBytes)
pb.DecodeFrom(d)
return pb, true, d.Err()
}
Expand Down Expand Up @@ -279,13 +277,12 @@ func saveConsensusChange(tx *sql.Tx, ceid modules.ConsensusChangeID, cn changeNo

// loadConsensusChange retrieves the consensus change node from the database.
func loadConsensusChange(tx *sql.Tx, ceid modules.ConsensusChangeID) (cn changeNode, err error) {
cnBytes := make([]byte, 0, 1024)
var cnBytes []byte
err = tx.QueryRow("SELECT bytes FROM cs_cl WHERE ceid = ?", ceid[:]).Scan(&cnBytes)
if err != nil {
return
}
buf := bytes.NewBuffer(cnBytes)
d := types.NewDecoder(io.LimitedReader{R: buf, N: int64(len(cnBytes))})
d := types.NewBufDecoder(cnBytes)
cn.DecodeFrom(d)
return cn, d.Err()
}
Expand Down Expand Up @@ -316,13 +313,12 @@ func countDelayedSiacoins(tx *sql.Tx) (total types.Currency, err error) {

for rows.Next() {
var dsco types.SiacoinOutput
dscoBytes := make([]byte, 0, 56)
var dscoBytes []byte
if err = rows.Scan(&dscoBytes); err != nil {
rows.Close()
return types.ZeroCurrency, err
}
buf := bytes.NewBuffer(dscoBytes)
d := types.NewDecoder(io.LimitedReader{R: buf, N: int64(len(dscoBytes))})
d := types.NewBufDecoder(dscoBytes)
dsco.DecodeFrom(d)
if err = d.Err(); err != nil {
rows.Close()
Expand All @@ -344,13 +340,12 @@ func countSiacoins(tx *sql.Tx) (total types.Currency, err error) {

for rows.Next() {
var sco types.SiacoinOutput
scoBytes := make([]byte, 0, 56)
var scoBytes []byte
if err = rows.Scan(&scoBytes); err != nil {
rows.Close()
return types.ZeroCurrency, err
}
buf := bytes.NewBuffer(scoBytes)
d := types.NewDecoder(io.LimitedReader{R: buf, N: int64(len(scoBytes))})
d := types.NewBufDecoder(scoBytes)
sco.DecodeFrom(d)
if err = d.Err(); err != nil {
rows.Close()
Expand All @@ -377,8 +372,7 @@ func countFileContractPayouts(tx *sql.Tx) (total types.Currency, err error) {
rows.Close()
return types.ZeroCurrency, err
}
buf := bytes.NewBuffer(fcBytes)
d := types.NewDecoder(io.LimitedReader{R: buf, N: int64(len(fcBytes))})
d := types.NewBufDecoder(fcBytes)
fc.DecodeFrom(d)
if err = d.Err(); err != nil {
rows.Close()
Expand All @@ -404,13 +398,12 @@ func countSiafundClaims(tx *sql.Tx) (total types.Currency, err error) {
}

for rows.Next() {
sfoBytes := make([]byte, 0, 80)
var sfoBytes []byte
if err = rows.Scan(&sfoBytes); err != nil {
rows.Close()
return types.ZeroCurrency, err
}
buf := bytes.NewBuffer(sfoBytes)
d := types.NewDecoder(io.LimitedReader{R: buf, N: int64(len(sfoBytes))})
d := types.NewBufDecoder(sfoBytes)
var value, claimStart types.Currency
value.DecodeFrom(d)
(&types.Address{}).DecodeFrom(d)
Expand All @@ -436,13 +429,12 @@ func countSiafunds(tx *sql.Tx) (total uint64, err error) {

for rows.Next() {
var sfo types.SiafundOutput
sfoBytes := make([]byte, 0, 80)
var sfoBytes []byte
if err = rows.Scan(&sfoBytes); err != nil {
rows.Close()
return 0, err
}
buf := bytes.NewBuffer(sfoBytes)
d := types.NewDecoder(io.LimitedReader{R: buf, N: int64(len(sfoBytes))})
d := types.NewBufDecoder(sfoBytes)
sfo.DecodeFrom(d)
if err = d.Err(); err != nil {
rows.Close()
Expand All @@ -458,16 +450,15 @@ func countSiafunds(tx *sql.Tx) (total uint64, err error) {
// findSiacoinOutput tries to find a Siacoin output with the given ID in the
// consensus set.
func findSiacoinOutput(tx *sql.Tx, scoid types.SiacoinOutputID) (sco types.SiacoinOutput, exists bool, err error) {
scoBytes := make([]byte, 0, 56)
var scoBytes []byte
err = tx.QueryRow("SELECT bytes FROM cs_sco WHERE scoid = ?", scoid[:]).Scan(&scoBytes)
if errors.Is(err, sql.ErrNoRows) {
return types.SiacoinOutput{}, false, nil
}
if err != nil {
return types.SiacoinOutput{}, false, err
}
buf := bytes.NewBuffer(scoBytes)
d := types.NewDecoder(io.LimitedReader{R: buf, N: int64(len(scoBytes))})
d := types.NewBufDecoder(scoBytes)
sco.DecodeFrom(d)
return sco, true, d.Err()
}
Expand All @@ -491,16 +482,15 @@ func removeSiacoinOutput(tx *sql.Tx, id types.SiacoinOutputID) error {
// findSiafundOutput tries to find a Siafund output with the given ID in the
// consensus set.
func findSiafundOutput(tx *sql.Tx, sfoid types.SiafundOutputID) (sfo types.SiafundOutput, claimStart types.Currency, exists bool, err error) {
sfoBytes := make([]byte, 0, 80)
var sfoBytes []byte
err = tx.QueryRow("SELECT bytes FROM cs_sfo WHERE sfoid = ?", sfoid[:]).Scan(&sfoBytes)
if errors.Is(err, sql.ErrNoRows) {
return types.SiafundOutput{}, types.Currency{}, false, nil
}
if err != nil {
return types.SiafundOutput{}, types.Currency{}, false, err
}
buf := bytes.NewBuffer(sfoBytes)
d := types.NewDecoder(io.LimitedReader{R: buf, N: int64(len(sfoBytes))})
d := types.NewBufDecoder(sfoBytes)
var val types.Currency
val.DecodeFrom(d)
sfo.Value = val.Lo
Expand Down Expand Up @@ -545,8 +535,7 @@ func findFileContract(tx *sql.Tx, fcid types.FileContractID) (fc types.FileContr
if err != nil {
return types.FileContract{}, false, err
}
buf := bytes.NewBuffer(fcBytes)
d := types.NewDecoder(io.LimitedReader{R: buf, N: int64(len(fcBytes))})
d := types.NewBufDecoder(fcBytes)
fc.DecodeFrom(d)
return fc, true, d.Err()
}
Expand Down Expand Up @@ -589,13 +578,13 @@ func removeFileContract(tx *sql.Tx, id types.FileContractID) error {
// getSiafundPool returns the current value of the Siafund pool. No error is
// returned as the Siafund pool should always be available.
func getSiafundPool(tx *sql.Tx) (pool types.Currency) {
poolBytes := make([]byte, 0, 24)
var poolBytes []byte
err := tx.QueryRow("SELECT bytes FROM cs_sfpool WHERE id = 1").Scan(&poolBytes)
if err != nil {
return types.ZeroCurrency
}

d := types.NewDecoder(io.LimitedReader{R: bytes.NewBuffer(poolBytes), N: int64(len(poolBytes))})
d := types.NewBufDecoder(poolBytes)
pool.DecodeFrom(d)
if err := d.Err(); err != nil {
return types.ZeroCurrency
Expand Down
7 changes: 2 additions & 5 deletions modules/consensus/maintenance.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package consensus

import (
"bytes"
"database/sql"
"errors"
"io"

"github.com/mike76-dev/sia-satellite/modules"

Expand Down Expand Up @@ -90,16 +88,15 @@ func applyMaturedSiacoinOutputs(tx *sql.Tx, pb *processedBlock) error {
for rows.Next() {
var scoid types.SiacoinOutputID
id := make([]byte, 32)
scoBytes := make([]byte, 0, 56)
var scoBytes []byte
var sco types.SiacoinOutput
if err := rows.Scan(&id, &scoBytes); err != nil {
rows.Close()
return err
}

copy(scoid[:], id[:])
buf := bytes.NewBuffer(scoBytes)
d := types.NewDecoder(io.LimitedReader{R: buf, N: int64(len(scoBytes))})
d := types.NewBufDecoder(scoBytes)
sco.DecodeFrom(d)
if err := d.Err(); err != nil {
rows.Close()
Expand Down
4 changes: 1 addition & 3 deletions modules/transactionpool/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"database/sql"
"errors"
"io"

"github.com/mike76-dev/sia-satellite/modules"

Expand Down Expand Up @@ -79,8 +78,7 @@ func (tp *TransactionPool) getFeeMedian() (medianPersist, error) {
}

var mp medianPersist
buf := bytes.NewBuffer(medianBytes)
d := types.NewDecoder(io.LimitedReader{R: buf, N: int64(len(medianBytes))})
d := types.NewBufDecoder(medianBytes)
mp.DecodeFrom(d)
if err := d.Err(); err != nil {
return medianPersist{}, modules.AddContext(err, "unable to unmarshal median data")
Expand Down
Loading

0 comments on commit b56cf33

Please sign in to comment.