Skip to content

Commit

Permalink
all: update core
Browse files Browse the repository at this point in the history
  • Loading branch information
n8maninger committed Dec 3, 2024
1 parent 6172c9c commit e086953
Show file tree
Hide file tree
Showing 12 changed files with 69 additions and 182 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.DS_Store
.vscode
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ toolchain go1.23.2

require (
go.etcd.io/bbolt v1.3.11
go.sia.tech/core v0.6.2
go.sia.tech/core v0.7.1-0.20241203043244-c435a355b1da
go.sia.tech/mux v1.3.0
go.uber.org/zap v1.27.0
golang.org/x/crypto v0.29.0
Expand Down
6 changes: 2 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@ github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKs
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
go.etcd.io/bbolt v1.3.11 h1:yGEzV1wPz2yVCLsD8ZAiGHhHVlczyC9d1rP43/VCRJ0=
go.etcd.io/bbolt v1.3.11/go.mod h1:dksAq7YMXoljX0xu6VF5DMZGbhYYoLUalEiSySYAS4I=
go.sia.tech/core v0.6.1 h1:eaExM2E2eNr43su2XDkY5J24E3F54YGS7hcC3WtVjVk=
go.sia.tech/core v0.6.1/go.mod h1:P3C1BWa/7J4XgdzWuaYHBvLo2RzZ0UBaJM4TG1GWB2g=
go.sia.tech/core v0.6.2 h1:8NEjxyD93A+EhZopsBy/LvuHH+zUSjRNKnf9rXgtIwU=
go.sia.tech/core v0.6.2/go.mod h1:4v+aT/33857tMfqa5j5OYlAoLsoIrd4d7qMlgeP+VGk=
go.sia.tech/core v0.7.1-0.20241203043244-c435a355b1da h1:taO86czGly5SIb8UswVI2W7rmxhmv9G4C93zoAwtfxk=
go.sia.tech/core v0.7.1-0.20241203043244-c435a355b1da/go.mod h1:4v+aT/33857tMfqa5j5OYlAoLsoIrd4d7qMlgeP+VGk=
go.sia.tech/mux v1.3.0 h1:hgR34IEkqvfBKUJkAzGi31OADeW2y7D6Bmy/Jcbop9c=
go.sia.tech/mux v1.3.0/go.mod h1:I46++RD4beqA3cW9Xm9SwXbezwPqLvHhVs9HLpDtt58=
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
Expand Down
15 changes: 1 addition & 14 deletions miner.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package coreutils

import (
"encoding/binary"
"time"

"go.sia.tech/core/consensus"
Expand All @@ -12,22 +11,10 @@ import (
// FindBlockNonce attempts to find a nonce for b that meets the PoW target.
func FindBlockNonce(cs consensus.State, b *types.Block, timeout time.Duration) bool {
b.Nonce = 0
buf := make([]byte, 32+8+8+32)
binary.LittleEndian.PutUint64(buf[32:], b.Nonce)
binary.LittleEndian.PutUint64(buf[40:], uint64(b.Timestamp.Unix()))
if b.V2 != nil {
copy(buf[:32], "sia/id/block|")
copy(buf[48:], b.V2.Commitment[:])
} else {
root := b.MerkleRoot()
copy(buf[:32], b.ParentID[:])
copy(buf[48:], root[:])
}
factor := cs.NonceFactor()
startBlock := time.Now()
for types.BlockID(types.HashBytes(buf)).CmpWork(cs.ChildTarget) < 0 {
for b.ID().CmpWork(cs.ChildTarget) < 0 {
b.Nonce += factor
binary.LittleEndian.PutUint64(buf[32:], b.Nonce)
if time.Since(startBlock) > timeout {
return false
}
Expand Down
27 changes: 19 additions & 8 deletions rhp/v4/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,11 +246,10 @@ func RPCWriteSector(ctx context.Context, t TransportClient, prices rhp4.HostPric
req := rhp4.RPCWriteSectorRequest{
Prices: prices,
Token: token,
Duration: duration,
DataLength: length,
}

if err := req.Validate(t.PeerKey(), req.Duration); err != nil {
if err := req.Validate(t.PeerKey()); err != nil {
return RPCWriteSectorResult{}, fmt.Errorf("invalid request: %w", err)
}

Expand Down Expand Up @@ -287,7 +286,7 @@ func RPCWriteSector(ctx context.Context, t TransportClient, prices rhp4.HostPric

return RPCWriteSectorResult{
Root: resp.Root,
Usage: prices.RPCWriteSectorCost(uint64(length), duration),
Usage: prices.RPCWriteSectorCost(uint64(length)),
}, nil
}

Expand Down Expand Up @@ -498,7 +497,7 @@ func RPCSectorRoots(ctx context.Context, t TransportClient, cs consensus.State,
RenterSignature: revision.RenterSignature,
}

if err := req.Validate(contract.Revision.HostPublicKey, revision, length); err != nil {
if err := req.Validate(contract.Revision.HostPublicKey, revision); err != nil {
return RPCSectorRootsResult{}, fmt.Errorf("invalid request: %w", err)
}

Expand Down Expand Up @@ -727,10 +726,14 @@ func RPCRenewContract(ctx context.Context, t TransportClient, tp TxPool, signer
// sign the renewal
renewalSigHash := cs.RenewalSigHash(renewal)
renewal.RenterSignature = signer.SignHash(renewalSigHash)
// sign the contract
contractSigHash := cs.ContractSigHash(renewal.NewContract)
renewal.NewContract.RenterSignature = signer.SignHash(contractSigHash)

// send the renter signatures
renterPolicyResp := rhp4.RPCRenewContractSecondResponse{
RenterRenewalSignature: renewal.RenterSignature,
RenterRenewalSignature: renewal.RenterSignature,
RenterContractSignature: renewal.NewContract.RenterSignature,
}
for _, si := range renewalTxn.SiacoinInputs[:len(req.RenterInputs)] {
renterPolicyResp.RenterSatisfiedPolicies = append(renterPolicyResp.RenterSatisfiedPolicies, si.SatisfiedPolicy)
Expand Down Expand Up @@ -760,7 +763,9 @@ func RPCRenewContract(ctx context.Context, t TransportClient, tp TxPool, signer

// validate the host signature
if !existing.HostPublicKey.VerifyHash(renewalSigHash, hostRenewal.HostSignature) {
return RPCRenewContractResult{}, errors.New("invalid host signature")
return RPCRenewContractResult{}, errors.New("invalid host renewal signature")
} else if !existing.HostPublicKey.VerifyHash(contractSigHash, hostRenewal.NewContract.HostSignature) {
return RPCRenewContractResult{}, errors.New("invalid host contract signature")
}
return RPCRenewContractResult{
Contract: ContractRevision{
Expand Down Expand Up @@ -852,10 +857,14 @@ func RPCRefreshContract(ctx context.Context, t TransportClient, tp TxPool, signe
// sign the renewal
renewalSigHash := cs.RenewalSigHash(renewal)
renewal.RenterSignature = signer.SignHash(renewalSigHash)
// sign the new contract
contractSigHash := cs.ContractSigHash(renewal.NewContract)
renewal.NewContract.RenterSignature = signer.SignHash(contractSigHash)

// send the renter signatures
renterPolicyResp := rhp4.RPCRefreshContractSecondResponse{
RenterRenewalSignature: renewal.RenterSignature,
RenterRenewalSignature: renewal.RenterSignature,
RenterContractSignature: renewal.NewContract.RenterSignature,
}
for _, si := range renewalTxn.SiacoinInputs[:len(req.RenterInputs)] {
renterPolicyResp.RenterSatisfiedPolicies = append(renterPolicyResp.RenterSatisfiedPolicies, si.SatisfiedPolicy)
Expand Down Expand Up @@ -885,7 +894,9 @@ func RPCRefreshContract(ctx context.Context, t TransportClient, tp TxPool, signe

// validate the host signature
if !existing.HostPublicKey.VerifyHash(renewalSigHash, hostRenewal.HostSignature) {
return RPCRefreshContractResult{}, errors.New("invalid host signature")
return RPCRefreshContractResult{}, errors.New("invalid host renewal signature")
} else if !existing.HostPublicKey.VerifyHash(contractSigHash, hostRenewal.NewContract.HostSignature) {
return RPCRefreshContractResult{}, errors.New("invalid host contract signature")
}
return RPCRefreshContractResult{
Contract: ContractRevision{
Expand Down
28 changes: 0 additions & 28 deletions rhp/v4/rpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,6 @@ func TestSettings(t *testing.T) {
WalletAddress: w.Address(),
MaxCollateral: types.Siacoins(10000),
MaxContractDuration: 1000,
MaxSectorDuration: 3 * 144,
MaxSectorBatchSize: 100,
RemainingStorage: 100 * proto4.SectorSize,
TotalStorage: 100 * proto4.SectorSize,
Prices: proto4.HostPrices{
Expand Down Expand Up @@ -216,8 +214,6 @@ func TestFormContract(t *testing.T) {
WalletAddress: w.Address(),
MaxCollateral: types.Siacoins(10000),
MaxContractDuration: 1000,
MaxSectorDuration: 3 * 144,
MaxSectorBatchSize: 100,
RemainingStorage: 100 * proto4.SectorSize,
TotalStorage: 100 * proto4.SectorSize,
Prices: proto4.HostPrices{
Expand Down Expand Up @@ -275,8 +271,6 @@ func TestFormContractBasis(t *testing.T) {
WalletAddress: w.Address(),
MaxCollateral: types.Siacoins(10000),
MaxContractDuration: 1000,
MaxSectorDuration: 3 * 144,
MaxSectorBatchSize: 100,
RemainingStorage: 100 * proto4.SectorSize,
TotalStorage: 100 * proto4.SectorSize,
Prices: proto4.HostPrices{
Expand Down Expand Up @@ -333,8 +327,6 @@ func TestRPCRefresh(t *testing.T) {
WalletAddress: w.Address(),
MaxCollateral: types.Siacoins(10000),
MaxContractDuration: 1000,
MaxSectorDuration: 3 * 144,
MaxSectorBatchSize: 100,
RemainingStorage: 100 * proto4.SectorSize,
TotalStorage: 100 * proto4.SectorSize,
Prices: proto4.HostPrices{
Expand Down Expand Up @@ -446,8 +438,6 @@ func TestRPCRenew(t *testing.T) {
WalletAddress: w.Address(),
MaxCollateral: types.Siacoins(10000),
MaxContractDuration: 1000,
MaxSectorDuration: 3 * 144,
MaxSectorBatchSize: 100,
RemainingStorage: 100 * proto4.SectorSize,
TotalStorage: 100 * proto4.SectorSize,
Prices: proto4.HostPrices{
Expand Down Expand Up @@ -607,8 +597,6 @@ func TestAccounts(t *testing.T) {
WalletAddress: w.Address(),
MaxCollateral: types.Siacoins(10000),
MaxContractDuration: 1000,
MaxSectorDuration: 3 * 144,
MaxSectorBatchSize: 100,
RemainingStorage: 100 * proto4.SectorSize,
TotalStorage: 100 * proto4.SectorSize,
Prices: proto4.HostPrices{
Expand Down Expand Up @@ -708,8 +696,6 @@ func TestReadWriteSector(t *testing.T) {
WalletAddress: w.Address(),
MaxCollateral: types.Siacoins(10000),
MaxContractDuration: 1000,
MaxSectorDuration: 3 * 144,
MaxSectorBatchSize: 100,
RemainingStorage: 100 * proto4.SectorSize,
TotalStorage: 100 * proto4.SectorSize,
Prices: proto4.HostPrices{
Expand Down Expand Up @@ -804,8 +790,6 @@ func TestAppendSectors(t *testing.T) {
WalletAddress: w.Address(),
MaxCollateral: types.Siacoins(10000),
MaxContractDuration: 1000,
MaxSectorDuration: 3 * 144,
MaxSectorBatchSize: 100,
RemainingStorage: 100 * proto4.SectorSize,
TotalStorage: 100 * proto4.SectorSize,
Prices: proto4.HostPrices{
Expand Down Expand Up @@ -921,8 +905,6 @@ func TestVerifySector(t *testing.T) {
WalletAddress: w.Address(),
MaxCollateral: types.Siacoins(10000),
MaxContractDuration: 1000,
MaxSectorDuration: 3 * 144,
MaxSectorBatchSize: 100,
RemainingStorage: 100 * proto4.SectorSize,
TotalStorage: 100 * proto4.SectorSize,
Prices: proto4.HostPrices{
Expand Down Expand Up @@ -1014,8 +996,6 @@ func TestRPCFreeSectors(t *testing.T) {
WalletAddress: w.Address(),
MaxCollateral: types.Siacoins(10000),
MaxContractDuration: 1000,
MaxSectorDuration: 3 * 144,
MaxSectorBatchSize: 100,
RemainingStorage: 100 * proto4.SectorSize,
TotalStorage: 100 * proto4.SectorSize,
Prices: proto4.HostPrices{
Expand Down Expand Up @@ -1137,8 +1117,6 @@ func TestRPCSectorRoots(t *testing.T) {
WalletAddress: w.Address(),
MaxCollateral: types.Siacoins(10000),
MaxContractDuration: 1000,
MaxSectorDuration: 3 * 144,
MaxSectorBatchSize: 100,
RemainingStorage: 100 * proto4.SectorSize,
TotalStorage: 100 * proto4.SectorSize,
Prices: proto4.HostPrices{
Expand Down Expand Up @@ -1247,8 +1225,6 @@ func BenchmarkWrite(b *testing.B) {
WalletAddress: w.Address(),
MaxCollateral: types.Siacoins(10000),
MaxContractDuration: 1000,
MaxSectorDuration: 3 * 144,
MaxSectorBatchSize: 100,
RemainingStorage: 100 * proto4.SectorSize,
TotalStorage: 100 * proto4.SectorSize,
Prices: proto4.HostPrices{
Expand Down Expand Up @@ -1337,8 +1313,6 @@ func BenchmarkRead(b *testing.B) {
WalletAddress: w.Address(),
MaxCollateral: types.Siacoins(10000),
MaxContractDuration: 1000,
MaxSectorDuration: 3 * 144,
MaxSectorBatchSize: 100,
RemainingStorage: 100 * proto4.SectorSize,
TotalStorage: 100 * proto4.SectorSize,
Prices: proto4.HostPrices{
Expand Down Expand Up @@ -1439,8 +1413,6 @@ func BenchmarkContractUpload(b *testing.B) {
WalletAddress: w.Address(),
MaxCollateral: types.Siacoins(10000),
MaxContractDuration: 1000,
MaxSectorDuration: 3 * 144,
MaxSectorBatchSize: 100,
RemainingStorage: 100 * proto4.SectorSize,
TotalStorage: 100 * proto4.SectorSize,
Prices: proto4.HostPrices{
Expand Down
44 changes: 26 additions & 18 deletions rhp/v4/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,9 +232,7 @@ func (s *Server) handleRPCWriteSector(stream net.Conn) error {
var req rhp4.RPCWriteSectorRequest
if err := rhp4.ReadRequest(stream, &req); err != nil {
return errorDecodingError("failed to read request: %v", err)
}
settings := s.settings.RHP4Settings()
if err := req.Validate(s.hostKey.PublicKey(), settings.MaxSectorDuration); err != nil {
} else if err := req.Validate(s.hostKey.PublicKey()); err != nil {
return errorBadRequest("request invalid: %v", err)
}
prices := req.Prices
Expand All @@ -253,12 +251,12 @@ func (s *Server) handleRPCWriteSector(stream net.Conn) error {
return errorDecodingError("failed to read sector data: %v", err)
}

usage := prices.RPCWriteSectorCost(req.DataLength, req.Duration)
usage := prices.RPCWriteSectorCost(req.DataLength)
if err = s.contractor.DebitAccount(req.Token.Account, usage); err != nil {
return fmt.Errorf("failed to debit account: %w", err)
}

if err := s.sectors.StoreSector(root, &sector, req.Duration); err != nil {
if err := s.sectors.StoreSector(root, &sector, prices.TipHeight+rhp4.TempSectorDuration); err != nil {
return fmt.Errorf("failed to store sector: %w", err)
}
return rhp4.WriteResponse(stream, &rhp4.RPCWriteSectorResponse{
Expand All @@ -283,9 +281,7 @@ func (s *Server) handleRPCFreeSectors(stream net.Conn) error {
}

fc := state.Revision

settings := s.settings.RHP4Settings()
if err := req.Validate(s.hostKey.PublicKey(), fc, settings.MaxSectorBatchSize); err != nil {
if err := req.Validate(s.hostKey.PublicKey(), fc); err != nil {
return errorBadRequest("request invalid: %v", err)
}
prices := req.Prices
Expand Down Expand Up @@ -347,8 +343,7 @@ func (s *Server) handleRPCAppendSectors(stream net.Conn) error {
return errorDecodingError("failed to read request: %v", err)
}

settings := s.settings.RHP4Settings()
if err := req.Validate(s.hostKey.PublicKey(), settings.MaxSectorBatchSize); err != nil {
if err := req.Validate(s.hostKey.PublicKey()); err != nil {
return errorBadRequest("request invalid: %v", err)
}

Expand Down Expand Up @@ -483,8 +478,7 @@ func (s *Server) handleRPCSectorRoots(stream net.Conn) error {
defer unlock()

// validate the request fields
settings := s.settings.RHP4Settings()
if err := req.Validate(s.hostKey.PublicKey(), state.Revision, settings.MaxSectorBatchSize); err != nil {
if err := req.Validate(s.hostKey.PublicKey(), state.Revision); err != nil {
return rhp4.NewRPCError(rhp4.ErrorCodeBadRequest, err.Error())
}
prices := req.Prices
Expand Down Expand Up @@ -693,7 +687,7 @@ func (s *Server) handleRPCRefreshContract(stream net.Conn) error {

// validate the request
settings := s.settings.RHP4Settings()
if err := req.Validate(s.hostKey.PublicKey(), state.Revision.ExpirationHeight, settings.MaxCollateral); err != nil {
if err := req.Validate(s.hostKey.PublicKey(), state.Revision.TotalCollateral, state.Revision.ExpirationHeight, settings.MaxCollateral); err != nil {
return rhp4.NewRPCError(rhp4.ErrorCodeBadRequest, err.Error())
}

Expand Down Expand Up @@ -782,15 +776,22 @@ func (s *Server) handleRPCRefreshContract(stream net.Conn) error {
// validate the renter's signature
renewalSigHash := cs.RenewalSigHash(renewal)
if !existing.RenterPublicKey.VerifyHash(renewalSigHash, renterSigResp.RenterRenewalSignature) {
return rhp4.ErrInvalidSignature
return fmt.Errorf("failed to validate renter renewal signature: %w", rhp4.ErrInvalidSignature)
}
renewal.RenterSignature = renterSigResp.RenterRenewalSignature
renewal.HostSignature = s.hostKey.SignHash(renewalSigHash)

contractSigHash := cs.ContractSigHash(renewal.NewContract)
if !existing.RenterPublicKey.VerifyHash(contractSigHash, renterSigResp.RenterContractSignature) {
return fmt.Errorf("failed to validate renter contract signature: %w", rhp4.ErrInvalidSignature)
}
renewal.NewContract.RenterSignature = renterSigResp.RenterContractSignature
renewal.NewContract.HostSignature = s.hostKey.SignHash(contractSigHash)

// apply the renter's signatures
for i, policy := range renterSigResp.RenterSatisfiedPolicies {
renewalTxn.SiacoinInputs[i].SatisfiedPolicy = policy
}
renewal.HostSignature = s.hostKey.SignHash(renewalSigHash)

// add the renter's parents to our transaction pool to ensure they are valid
// and update the proofs.
Expand Down Expand Up @@ -849,7 +850,7 @@ func (s *Server) handleRPCRenewContract(stream net.Conn) error {
tip := s.chain.Tip()

// validate the request
if err := req.Validate(s.hostKey.PublicKey(), tip, state.Revision.ProofHeight, settings.MaxCollateral, settings.MaxContractDuration); err != nil {
if err := req.Validate(s.hostKey.PublicKey(), tip, state.Revision.Filesize, state.Revision.ProofHeight, settings.MaxCollateral, settings.MaxContractDuration); err != nil {
return rhp4.NewRPCError(rhp4.ErrorCodeBadRequest, err.Error())
}

Expand Down Expand Up @@ -944,15 +945,22 @@ func (s *Server) handleRPCRenewContract(stream net.Conn) error {
// validate the renter's signature
renewalSigHash := cs.RenewalSigHash(renewal)
if !existing.RenterPublicKey.VerifyHash(renewalSigHash, renterSigResp.RenterRenewalSignature) {
return rhp4.ErrInvalidSignature
return fmt.Errorf("failed to validate renewal signature: %w", rhp4.ErrInvalidSignature)
}
renewal.RenterSignature = renterSigResp.RenterRenewalSignature
renewal.HostSignature = s.hostKey.SignHash(renewalSigHash)

contractSighash := cs.ContractSigHash(renewal.NewContract)
if !existing.RenterPublicKey.VerifyHash(contractSighash, renterSigResp.RenterContractSignature) {
return fmt.Errorf("failed to validate contract signature: %w", rhp4.ErrInvalidSignature)
}
renewal.NewContract.RenterSignature = renterSigResp.RenterContractSignature
renewal.NewContract.HostSignature = s.hostKey.SignHash(contractSighash)

// apply the renter's signatures
for i, policy := range renterSigResp.RenterSatisfiedPolicies {
renewalTxn.SiacoinInputs[i].SatisfiedPolicy = policy
}
renewal.HostSignature = s.hostKey.SignHash(renewalSigHash)

// add the renter's parents to our transaction pool to ensure they are valid
// and update the proofs.
Expand Down
Loading

0 comments on commit e086953

Please sign in to comment.