-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from jsign/jsign/satellite
Support offline contributions, configurable sequencer URL & direct hex-encoded entropy flag
- Loading branch information
Showing
11 changed files
with
320 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,7 @@ test: | |
.PHONY: test | ||
|
||
build: | ||
go build -o kzgcli ./cmd/kzgcli | ||
go build ./cmd/kzgcli | ||
.PHONY: build | ||
|
||
bench: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/hex" | ||
"fmt" | ||
"io" | ||
"log" | ||
"os" | ||
"strings" | ||
|
||
"github.com/jsign/go-kzg-ceremony-client/contribution" | ||
"github.com/jsign/go-kzg-ceremony-client/extrand" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var offlineContributeCmd = &cobra.Command{ | ||
Use: "contribute <path-current-state-file> <path-contribution-file>", | ||
Short: "Opens a file with the current state of the ceremony, makes the contribution, and saves the new state to a file.", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
if len(args) != 2 { | ||
log.Fatalf("two arguments expected") | ||
} | ||
|
||
urlrand, err := cmd.Flags().GetString("urlrand") | ||
if err != nil { | ||
log.Fatalf("get --urlrand flag value: %s", err) | ||
} | ||
var extRandomness [][]byte | ||
if urlrand != "" { | ||
fmt.Printf("Pulling entropy from %s... ", urlrand) | ||
urlBytes, err := extrand.GetFromURL(cmd.Context(), urlrand) | ||
if err != nil { | ||
log.Fatalf("get bytes from url: %s", err) | ||
} | ||
fmt.Printf("Got it! (length: %d)\n", len(urlBytes)) | ||
extRandomness = append(extRandomness, urlBytes) | ||
} | ||
hexEntropy, err := cmd.Flags().GetString("hex-entropy") | ||
if err != nil { | ||
log.Fatalf("get --hex-entropy flag value: %s", err) | ||
} | ||
if hexEntropy != "" { | ||
hexEntropy := strings.TrimPrefix(hexEntropy, "0x") | ||
hb, err := hex.DecodeString(hexEntropy) | ||
if err != nil { | ||
log.Fatalf("decoding hex entropy: %s", err) | ||
} | ||
extRandomness = append(extRandomness, hb) | ||
} | ||
|
||
fmt.Printf("Opening and parsing offline current state file...") | ||
f, err := os.Open(args[0]) | ||
if err != nil { | ||
log.Fatalf("opening current state file at %s: %s", args[0], err) | ||
} | ||
defer f.Close() | ||
|
||
bytes, err := io.ReadAll(f) | ||
if err != nil { | ||
log.Fatalf("reading current state file: %s", err) | ||
} | ||
contributionBatch, err := contribution.DecodeBatchContribution(bytes) | ||
if err != nil { | ||
log.Fatalf("deserializing file content: %s", err) | ||
} | ||
fmt.Printf("OK\nCalculating contribution... ") | ||
|
||
if err := contributionBatch.Contribute(extRandomness...); err != nil { | ||
log.Fatalf("failed on calculating contribution: %s", err) | ||
} | ||
|
||
nbytes, err := contribution.Encode(contributionBatch, true) | ||
if err != nil { | ||
log.Fatalf("encoding contribution: %s", err) | ||
} | ||
|
||
if err := os.WriteFile(args[1], nbytes, os.ModePerm); err != nil { | ||
log.Fatalf("writing contribution file to %s: %s", args[1], err) | ||
} | ||
|
||
fmt.Printf("OK\nSuccess, saved contribution in %s\n", args[1]) | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"os" | ||
|
||
"github.com/jsign/go-kzg-ceremony-client/contribution" | ||
"github.com/jsign/go-kzg-ceremony-client/sequencerclient" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var offlineDownloadStateCmd = &cobra.Command{ | ||
Use: "download-state <path>", | ||
Short: "Downloads the current state of the ceremony, and saves it in a file", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
if len(args) != 1 { | ||
log.Fatalf("one argument exected") | ||
} | ||
sequencerURL, err := cmd.Flags().GetString("sequencer-url") | ||
if err != nil { | ||
log.Fatalf("get --sequencer-url flag value: %s", err) | ||
} | ||
client, err := sequencerclient.New(sequencerURL) | ||
if err != nil { | ||
log.Fatalf("creating sequencer client: %s", err) | ||
} | ||
|
||
fmt.Printf("Downloading current state... ") | ||
transcript, err := client.GetCurrentTranscript(cmd.Context()) | ||
if err != nil { | ||
log.Fatalf("getting current transcript: %s", err) | ||
} | ||
fmt.Printf("OK\n") | ||
|
||
bc := contribution.BatchContribution{ | ||
Contributions: make([]contribution.Contribution, len(transcript.Transcripts)), | ||
} | ||
for i, transcript := range transcript.Transcripts { | ||
bc.Contributions[i].NumG1Powers = transcript.NumG1Powers | ||
bc.Contributions[i].NumG2Powers = transcript.NumG2Powers | ||
bc.Contributions[i].PowersOfTau = transcript.PowersOfTau | ||
} | ||
|
||
fmt.Printf("Encoding and saving to %s... ", args[0]) | ||
bytes, err := contribution.Encode(&bc, true) | ||
if err != nil { | ||
log.Fatalf("encoding current state to json: %s", err) | ||
} | ||
|
||
if err := os.WriteFile(args[0], bytes, os.ModePerm); err != nil { | ||
log.Fatalf("writing current state to file: %s", err) | ||
} | ||
|
||
fmt.Printf("OK\nSaved current state in %s\n", args[0]) | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"log" | ||
"os" | ||
"time" | ||
|
||
"github.com/jsign/go-kzg-ceremony-client/contribution" | ||
"github.com/jsign/go-kzg-ceremony-client/sequencerclient" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var offlineSendContributionCmd = &cobra.Command{ | ||
Use: "send-contribution <path-contribution-file>", | ||
Short: "Sends a previously generated contribution to the sequencer", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
if len(args) != 1 { | ||
log.Fatalf("one argument expected") | ||
} | ||
|
||
sessionID, err := cmd.Flags().GetString("session-id") | ||
if err != nil { | ||
log.Fatalf("get --session-id flag value: %s", err) | ||
} | ||
if sessionID == "" { | ||
log.Fatalf("the session id can't be empty") | ||
} | ||
|
||
contributionBytes, err := os.ReadFile(args[0]) | ||
if err != nil { | ||
log.Fatalf("reading contribution file: %s", err) | ||
} | ||
contributionBatch, err := contribution.DecodeBatchContribution(contributionBytes) | ||
if err != nil { | ||
log.Fatalf("decoding contribution file: %s", err) | ||
} | ||
|
||
sequencerURL, err := cmd.Flags().GetString("sequencer-url") | ||
if err != nil { | ||
log.Fatalf("get --sequencer-url flag value: %s", err) | ||
} | ||
client, err := sequencerclient.New(sequencerURL) | ||
if err != nil { | ||
log.Fatalf("creating sequencer client: %s", err) | ||
} | ||
|
||
for { | ||
_, ok, err := client.TryContribute(cmd.Context(), sessionID) | ||
if err != nil { | ||
fmt.Printf("%v Waiting for our turn failed (err: %s), retrying in %v...\n", time.Now().Format("2006-01-02 15:04:05"), err, tryContributeAttemptDelay) | ||
time.Sleep(tryContributeAttemptDelay) | ||
continue | ||
} | ||
if !ok { | ||
fmt.Printf("%v Can't enter the lobby, are you sure we're on your reserved slot? Waiting %v for retrying...\n", time.Now().Format("2006-01-02 15:04:05"), tryContributeAttemptDelay) | ||
time.Sleep(tryContributeAttemptDelay) | ||
continue | ||
} | ||
break | ||
} | ||
|
||
fmt.Printf("Sending our precomputed contribution %s to the sequencer...\n", args[0]) | ||
var contributionReceipt *sequencerclient.ContributionReceipt | ||
for { | ||
var err error | ||
contributionReceipt, err = client.Contribute(cmd.Context(), sessionID, contributionBatch) | ||
if err != nil { | ||
fmt.Printf("Failed sending contribution!: %s\n", err) | ||
fmt.Printf("Retrying sending contribution in %v\n", sendContributionRetryDelay) | ||
time.Sleep(sendContributionRetryDelay) | ||
continue | ||
} | ||
break | ||
} | ||
|
||
// Persist the receipt and contribution. | ||
receiptJSON, _ := json.Marshal(contributionReceipt) | ||
if err := os.WriteFile(fmt.Sprintf("contribution_receipt_%s.json", sessionID), receiptJSON, os.ModePerm); err != nil { | ||
log.Fatalf("failed to save the contribution receipt (err: %s), printing to stdout as last resort: %s", err, receiptJSON) | ||
} | ||
ourContributionBatchJSON, _ := contribution.Encode(contributionBatch, true) | ||
if err := os.WriteFile(fmt.Sprintf("my_contribution_%s.json", sessionID), ourContributionBatchJSON, os.ModePerm); err != nil { | ||
log.Fatalf("failed to save the contribution (err: %s), printing to stdout as last resort: %s", err, ourContributionBatchJSON) | ||
} | ||
|
||
fmt.Printf("Success!\n") | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.