Skip to content

Commit

Permalink
Merge pull request #16 from SiaFoundation/alex/test-v2-transition
Browse files Browse the repository at this point in the history
manager: add height and mine to height, add transition network node
  • Loading branch information
n8maninger authored Jan 21, 2025
2 parents eac2e7a + d8bfd86 commit 1063b65
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 1 deletion.
11 changes: 11 additions & 0 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,23 @@ import (
"go.sia.tech/core/types"
)

// HeightResponse is the response type for [GET] /height.
type HeightResponse struct {
Height uint64 `json:"height"`
}

// MineRequest is the request type for [POST] /mine.
type MineRequest struct {
Blocks int `json:"blocks"`
Address types.Address `json:"address"`
}

// MineToRequest is the request type for [POST] /mine/to.
type MineToRequest struct {
TargetHeight uint64 `json:"targetHeight"`
Address types.Address `json:"address"`
}

// A ProxyResponse is the response for a proxied API request from a node.
type ProxyResponse struct {
NodeID nodes.NodeID `json:"nodeID"`
Expand Down
29 changes: 28 additions & 1 deletion api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package api
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"

Expand Down Expand Up @@ -40,6 +41,7 @@ type (
Nodes() []nodes.Node

MineBlocks(ctx context.Context, n int, rewardAddress types.Address) error
MineBlocksToHeight(ctx context.Context, targetHeight uint64, rewardAddress types.Address) error
ProxyRequest(ctx context.Context, filter, httpMethod, path string, r io.Reader) ([]nodes.ProxyResponse, error)
}

Expand All @@ -63,6 +65,21 @@ func (srv *server) postMine(jc jape.Context) {
jc.Check("failed to mine", srv.nodes.MineBlocks(jc.Request.Context(), req.Blocks, req.Address))
}

func (srv *server) postMineTo(jc jape.Context) {
var req MineToRequest
if err := jc.Decode(&req); err != nil {
return
}

currentTip := srv.chain.Tip()
if req.TargetHeight <= currentTip.Height {
jc.Error(fmt.Errorf("target height %v is not greater than current height %v", req.TargetHeight, currentTip.Height), http.StatusBadRequest)
return
}

jc.Check("failed to mine to height", srv.nodes.MineBlocksToHeight(jc.Request.Context(), req.TargetHeight, req.Address))
}

func (srv *server) proxyNodeAPI(jc jape.Context) {
var nodeID string
if err := jc.DecodeParam("id", &nodeID); err != nil {
Expand Down Expand Up @@ -101,6 +118,13 @@ func (srv *server) proxyNodeAPI(jc jape.Context) {
}
}

func (srv *server) getHeight(jc jape.Context) {
tip := srv.chain.Tip()
jc.Encode(HeightResponse{
Height: tip.Height,
})
}

// Handler returns an http.Handler that serves the API.
func Handler(cm ChainManager, s Syncer, n Nodes, log *zap.Logger) http.Handler {
srv := &server{
Expand All @@ -120,6 +144,9 @@ func Handler(cm ChainManager, s Syncer, n Nodes, log *zap.Logger) http.Handler {
"PATCH /nodes/proxy/:id/*path": srv.proxyNodeAPI,
"DELETE /nodes/proxy/:id/*path": srv.proxyNodeAPI,

"POST /mine": srv.postMine,
"POST /mine/to": srv.postMineTo,
"POST /mine": srv.postMine,

"GET /height": srv.getHeight,
})
}
3 changes: 3 additions & 0 deletions cmd/clusterd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@ func main() {
case "v2":
n.HardforkV2.AllowHeight = 2
n.HardforkV2.RequireHeight = 3
case "transition":
n.HardforkV2.AllowHeight = 200 // attainable and after 144
n.HardforkV2.RequireHeight = 300
default:
log.Fatal("invalid network", zap.String("network", network))
}
Expand Down
21 changes: 21 additions & 0 deletions nodes/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,27 @@ func (m *Manager) MineBlocks(ctx context.Context, n int, rewardAddress types.Add
return nil
}

// MineBlocksToHeight mines blocks until the target height is reached.
func (m *Manager) MineBlocksToHeight(ctx context.Context, targetHeight uint64, rewardAddress types.Address) error {
log := m.log.Named("mineToHeight")
currentHeight := m.chain.Tip().Height

log.Debug("mining to height",
zap.Uint64("currentHeight", currentHeight),
zap.Uint64("targetHeight", targetHeight))

for currentHeight < targetHeight {
if err := m.MineBlocks(ctx, 1, rewardAddress); err != nil {
return fmt.Errorf("failed to mine to height %v: %w", targetHeight, err)
}
currentHeight = m.chain.Tip().Height
}

log.Debug("finished mining to height",
zap.Uint64("height", currentHeight))
return nil
}

func createNodeDir(baseDir string, id NodeID) (dir string, err error) {
dir = filepath.Join(baseDir, id.String())
if err := os.MkdirAll(dir, 0700); err != nil {
Expand Down

0 comments on commit 1063b65

Please sign in to comment.