Polishing & documenting, add compile script

This commit is contained in:
2022-12-10 10:59:48 +01:00
parent 797d4c50a7
commit a514115905
10 changed files with 195 additions and 144 deletions

View File

@@ -5,88 +5,80 @@ import (
"encoding/json"
"fmt"
"io/ioutil"
"kzgceremony"
"kzgceremony/client"
"os"
"strings"
"time"
kzgceremony "github.com/arnaucube/eth-kzg-ceremony-alt"
"github.com/arnaucube/eth-kzg-ceremony-alt/client"
"github.com/fatih/color"
flag "github.com/spf13/pflag"
)
type Config struct {
sequencerURL string
randomness string
sleepTime uint64
}
func main() {
fmt.Println("eth-kzg-ceremony-alt")
fmt.Printf("====================\n\n")
red := color.New(color.FgRed)
redB := color.New(color.FgRed, color.Bold)
cyan := color.New(color.FgCyan)
cyanB := color.New(color.FgCyan, color.Bold)
green := color.New(color.FgHiGreen)
greenB := color.New(color.FgHiGreen, color.Bold)
config := Config{}
flag.StringVarP(&config.sequencerURL, "url", "u",
var sequencerURL string
var randomness string
var sleepTime uint64
flag.StringVarP(&sequencerURL, "url", "u",
"https://kzg-ceremony-sequencer-dev.fly.dev", "sequencer url")
flag.StringVarP(&config.randomness, "rand", "r",
"", "randomness")
flag.Uint64VarP(&config.sleepTime, "sleeptime", "s",
flag.StringVarP(&randomness, "rand", "r",
"", fmt.Sprintf("randomness, needs to be bigger than %d", kzgceremony.MinRandomnessLen))
flag.Uint64VarP(&sleepTime, "sleeptime", "s",
10, "time (seconds) sleeping before trying again to be the next contributor")
flag.CommandLine.SortFlags = false
flag.Parse()
c := client.NewClient(config.sequencerURL)
c := client.NewClient(sequencerURL)
// get status
msgStatus, err := c.GetCurrentStatus()
if err != nil {
red.Println(err)
os.Exit(1)
printErrAndExit(err)
}
fmt.Println(msgStatus)
if config.randomness == "" {
cyanB.Println("To contribute to the ceremony, please set your randomness. Use -h to show the available flags.")
if randomness == "" {
_, _ =
cyanB.Println("To contribute to the ceremony, please set your randomness. Use -h to show the available flags.")
os.Exit(0)
}
if len([]byte(config.randomness)) < kzgceremony.MinRandomnessLen {
redB.Printf("Randomness must be longer than %d, current length: %d\n",
kzgceremony.MinRandomnessLen, len([]byte(config.randomness)))
if len([]byte(randomness)) < kzgceremony.MinRandomnessLen {
_, _ = redB.Printf("Randomness must be longer than %d, current length: %d\n",
kzgceremony.MinRandomnessLen, len([]byte(randomness)))
os.Exit(1)
}
// Auth
msgReqLink, err := c.GetRequestLink()
if err != nil {
red.Println(err)
os.Exit(1)
printErrAndExit(err)
}
green.Printf("Please go to\n%s\n and authenticate with Github.\n", msgReqLink.GithubAuthURL)
_, _ = green.Printf("Please go to\n%s\n and authenticate with Github.\n", msgReqLink.GithubAuthURL)
fmt.Println("(currently only Github auth is supported)")
greenB.Printf("Paste here the RawData from the auth answer:\n")
_, _ = greenB.Printf("Paste here the RawData from the auth answer:\n")
s, err := readInput()
if err != nil {
red.Println(err)
os.Exit(1)
printErrAndExit(err)
}
var authMsg client.MsgAuthCallback
if err = json.Unmarshal([]byte(s), &authMsg); err != nil {
red.Println(err)
os.Exit(1)
printErrAndExit(err)
}
fmt.Print("Parsed auth msg: ")
cyan.Printf("%#v\n", authMsg)
_, _ = cyan.Printf("%#v\n", authMsg)
// TODO this will be only triggered by a flag
// msg, err := c.PostAbortContribution(authMsg.SessionID)
@@ -104,14 +96,14 @@ func main() {
var retry bool
prevBatchContribution, retry, err = c.PostTryContribute(authMsg.SessionID)
if err != nil {
red.Println(err)
_, _ = cyan.Println(err)
}
if !retry {
break
}
fmt.Printf("%s try_contribute unsuccessful, going to sleep %d seconds\n",
time.Now().Format("2006-01-02 15:04:05"), config.sleepTime)
time.Sleep(time.Duration(config.sleepTime) * time.Second)
time.Now().Format("2006-01-02 15:04:05"), sleepTime)
time.Sleep(time.Duration(sleepTime) * time.Second)
}
// get latest state
@@ -122,48 +114,48 @@ func main() {
// }
fmt.Println("starting to compute new contribution")
newBatchContribution, err := prevBatchContribution.Contribute([]byte(config.randomness))
newBatchContribution, err := prevBatchContribution.Contribute([]byte(randomness))
if err != nil {
red.Println(err)
os.Exit(1)
printErrAndExit(err)
}
// store contribution
fmt.Println("storing contribution.json")
b, err := json.Marshal(newBatchContribution)
if err != nil {
red.Println(err)
os.Exit(1)
printErrAndExit(err)
}
err = ioutil.WriteFile("contribution.json", b, 0600)
if err != nil {
red.Println(err)
os.Exit(1)
printErrAndExit(err)
}
// send contribution
fmt.Println("sending contribution")
receipt, err := c.PostContribute(authMsg.SessionID, newBatchContribution)
if err != nil {
red.Println(err)
os.Exit(1)
printErrAndExit(err)
}
fmt.Println("Receipt:")
green.Println(receipt)
_, _ = green.Println(receipt)
// store receipt
fmt.Println("storing contribution_receipt.json")
b, err = json.Marshal(receipt)
if err != nil {
red.Println(err)
os.Exit(1)
printErrAndExit(err)
}
err = ioutil.WriteFile("contribution_receipt.json", b, 0600)
if err != nil {
red.Println(err)
os.Exit(1)
printErrAndExit(err)
}
}
func printErrAndExit(err error) {
red := color.New(color.FgRed)
_, _ = red.Println(err)
os.Exit(1)
}
func readInput() (string, error) {
reader := bufio.NewReader(os.Stdin)
input, err := reader.ReadString('\n')