-
Notifications
You must be signed in to change notification settings - Fork 0
/
fnochain.go
149 lines (135 loc) · 3.78 KB
/
fnochain.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// Copyright (c) 2019 The Fonero developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
// fnochain wraps codechain to handle the mainnet and testnet repositories.
package main
import (
"bytes"
"errors"
"flag"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/frankbraun/codechain/hashchain"
)
const (
mainnetDir = ".codechain_mainnet"
testnetDir = ".codechain_testnet"
)
var treehash = "undefined"
func callCodechain(args []string) error {
cmd := exec.Command("codechain", args...)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}
func lastTreeHash(dir string) (string, error) {
c, err := hashchain.ReadFile(filepath.Join(dir, "hashchain"))
if err != nil {
return "", err
}
defer c.Close()
return c.LastTreeHash(), nil
}
func readSourceLineComment() (string, error) {
buf, err := ioutil.ReadFile(filepath.Join(testnetDir, "hashchain"))
if err != nil {
return "", err
}
lines := bytes.Split(buf, []byte("\n"))
line := bytes.SplitN(lines[len(lines)-2], []byte(" "), 7)
return string(line[6]), nil
}
func run(mainnet, testnet bool, args []string) error {
fmt.Printf("_binary_treehash=%s\n", treehash)
// check if we are publishing on both nets
var sameOrigin bool
if !mainnet && !testnet && args[0] == "publish" {
testnetTreeHash, err := lastTreeHash(testnetDir)
if err != nil {
return err
}
fmt.Printf("testnet_treehash=%s\n", testnetTreeHash)
mainnetTreeHash, err := lastTreeHash(mainnetDir)
if err != nil {
return err
}
fmt.Printf("mainnet_treehash=%s\n", mainnetTreeHash)
if testnetTreeHash == mainnetTreeHash {
sameOrigin = true
}
}
// run codechain for testnet first
if testnet || !mainnet {
fmt.Printf("--------------------------------------------------------------------------------\n")
fmt.Printf("$ env CODECHAIN_DIR=%s CODECHAIN_EXCLUDE=%s codechain %s\n",
testnetDir, mainnetDir, strings.Join(args, " "))
if err := os.Setenv("CODECHAIN_DIR", testnetDir); err != nil {
return err
}
if err := os.Setenv("CODECHAIN_EXCLUDE", mainnetDir); err != nil {
return err
}
if err := callCodechain(args); err != nil {
return err
}
}
// now run codechain for mainnet
if mainnet || !testnet {
if args[0] == "publish" {
if sameOrigin {
// we just published a new source version and both previous
// versions are the same, read comment from hashchain
msg, err := readSourceLineComment()
if err != nil {
return err
}
// publish with same comment on mainnet
args = append(args, "-y", "-m", msg)
} else {
fmt.Println("originating tree hashes differ, review again")
}
}
fmt.Printf("--------------------------------------------------------------------------------\n")
fmt.Printf("$ env CODECHAIN_DIR=%s CODECHAIN_EXCLUDE=%s codechain %s\n",
mainnetDir, testnetDir, strings.Join(args, " "))
if err := os.Setenv("CODECHAIN_DIR", mainnetDir); err != nil {
return err
}
if err := os.Setenv("CODECHAIN_EXCLUDE", testnetDir); err != nil {
return err
}
if err := callCodechain(args); err != nil {
return err
}
}
return nil
}
func fatal(err error) {
fmt.Fprintf(os.Stderr, "%s: error: %s\n", os.Args[0], err)
os.Exit(1)
}
func usage() {
fmt.Fprintf(os.Stderr, "Usage: %s [options] codechain_command [codechain_options]\n", os.Args[0])
flag.PrintDefaults()
os.Exit(2)
}
func main() {
mainnet := flag.Bool("mainnet", false, "Call codechain only for mainnet")
testnet := flag.Bool("testnet", false, "Call codechain only for testnet")
flag.Usage = usage
flag.Parse()
if flag.NArg() < 1 {
usage()
}
if *mainnet && *testnet {
fatal(errors.New("-mainnet and -testnet exclude each other"))
}
if err := run(*mainnet, *testnet, flag.Args()); err != nil {
fatal(err)
}
}