2017-05-16 12:09:52 +00:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"reflect"
|
|
|
|
"runtime/debug"
|
|
|
|
"time"
|
|
|
|
|
2018-03-20 18:35:28 +00:00
|
|
|
"github.com/ethereum/go-ethereum/log"
|
2018-01-26 05:59:21 +00:00
|
|
|
"github.com/pborman/uuid"
|
2017-05-16 12:09:52 +00:00
|
|
|
"github.com/status-im/status-go/static"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// MessageIDKey is a key for message ID
|
|
|
|
// This ID is required to track from which chat a given send transaction request is coming.
|
|
|
|
MessageIDKey = contextKey("message_id")
|
|
|
|
)
|
|
|
|
|
|
|
|
type contextKey string // in order to make sure that our context key does not collide with keys from other packages
|
|
|
|
|
2018-03-20 18:35:28 +00:00
|
|
|
// All general log messages in this package should be routed through this logger.
|
|
|
|
var logger = log.New("package", "status-go/geth/common")
|
|
|
|
|
2017-11-07 17:46:11 +00:00
|
|
|
// ImportTestAccount imports keystore from static resources, see "static/keys" folder
|
2017-05-16 12:09:52 +00:00
|
|
|
func ImportTestAccount(keystoreDir, accountFile string) error {
|
|
|
|
// make sure that keystore folder exists
|
|
|
|
if _, err := os.Stat(keystoreDir); os.IsNotExist(err) {
|
2018-01-17 16:46:21 +00:00
|
|
|
os.MkdirAll(keystoreDir, os.ModePerm) // nolint: errcheck, gas
|
2017-05-16 12:09:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
dst := filepath.Join(keystoreDir, accountFile)
|
2017-11-07 17:46:11 +00:00
|
|
|
err := ioutil.WriteFile(dst, static.MustAsset("keys/"+accountFile), 0644)
|
|
|
|
if err != nil {
|
2018-03-20 18:35:28 +00:00
|
|
|
logger.Warn("cannot copy test account PK", "error", err)
|
2017-05-16 12:09:52 +00:00
|
|
|
}
|
|
|
|
|
2017-11-07 17:46:11 +00:00
|
|
|
return err
|
2017-05-16 12:09:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// PanicAfter throws panic() after waitSeconds, unless abort channel receives notification
|
|
|
|
func PanicAfter(waitSeconds time.Duration, abort chan struct{}, desc string) {
|
|
|
|
go func() {
|
|
|
|
select {
|
|
|
|
case <-abort:
|
|
|
|
return
|
|
|
|
case <-time.After(waitSeconds):
|
|
|
|
panic("whatever you were doing takes toooo long: " + desc)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
// MessageIDFromContext returns message id from context (if exists)
|
|
|
|
func MessageIDFromContext(ctx context.Context) string {
|
|
|
|
if ctx == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
if messageID, ok := ctx.Value(MessageIDKey).(string); ok {
|
|
|
|
return messageID
|
|
|
|
}
|
|
|
|
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
// ParseJSONArray parses JSON array into Go array of string
|
|
|
|
func ParseJSONArray(items string) ([]string, error) {
|
|
|
|
var parsedItems []string
|
|
|
|
err := json.Unmarshal([]byte(items), &parsedItems)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return parsedItems, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fatalf is used to halt the execution.
|
|
|
|
// When called the function prints stack end exits.
|
|
|
|
// Failure is logged into both StdErr and StdOut.
|
|
|
|
func Fatalf(reason interface{}, args ...interface{}) {
|
|
|
|
// decide on output stream
|
|
|
|
w := io.MultiWriter(os.Stdout, os.Stderr)
|
2018-01-17 16:46:21 +00:00
|
|
|
outf, _ := os.Stdout.Stat() // nolint: gas
|
|
|
|
errf, _ := os.Stderr.Stat() // nolint: gas
|
2017-05-16 12:09:52 +00:00
|
|
|
if outf != nil && errf != nil && os.SameFile(outf, errf) {
|
|
|
|
w = os.Stderr
|
|
|
|
}
|
|
|
|
|
|
|
|
// find out whether error or string has been passed as a reason
|
|
|
|
r := reflect.ValueOf(reason)
|
|
|
|
if r.Kind() == reflect.String {
|
Remove //nolint: gas directives from fmt.Fprintf #590 (#656)
Summary:
Filter out gas linter error checks for fmt.Fprintf commands. This required defining a custom linter around gas that additionally included the offending code.
Notes:
Gas format, without piping it through gometalinter, gives output like this:
$ gas -fmt=csv geth/jail/console/console.go
geth/jail/console/console.go,21,Errors unhandled.,LOW,HIGH,"fmt.Fprintf(w, ""%s: %s"", consoleEventName, formatForConsole(fn.ArgumentList))"
Gometalinter, by default, does not grab the line of code when it filters gas errors. To resolve this, I created a wrapper around gas (I wasn't sure what to call this "gas wrapper", I opted for gasv2, open to other names).
The first part of the regular expression was taken directly from gometalinter (see https://github.com/alecthomas/gometalinter/blob/master/linters.go#L236), and I then appended ,\".*\" to additionally grab the line of code of the offending line. Lastly, I excluded ".*Errors unhandled.*fmt.Fprintf.*" to filter out only fmt.Fprintf errors around omitted errors.
Also as a result of this change, gas lint output will now include the offending code.
Closes #590
2018-02-14 17:58:20 +00:00
|
|
|
fmt.Fprintf(w, "Fatal Failure: %v\n%v\n", reason.(string), args)
|
2017-05-16 12:09:52 +00:00
|
|
|
} else {
|
Remove //nolint: gas directives from fmt.Fprintf #590 (#656)
Summary:
Filter out gas linter error checks for fmt.Fprintf commands. This required defining a custom linter around gas that additionally included the offending code.
Notes:
Gas format, without piping it through gometalinter, gives output like this:
$ gas -fmt=csv geth/jail/console/console.go
geth/jail/console/console.go,21,Errors unhandled.,LOW,HIGH,"fmt.Fprintf(w, ""%s: %s"", consoleEventName, formatForConsole(fn.ArgumentList))"
Gometalinter, by default, does not grab the line of code when it filters gas errors. To resolve this, I created a wrapper around gas (I wasn't sure what to call this "gas wrapper", I opted for gasv2, open to other names).
The first part of the regular expression was taken directly from gometalinter (see https://github.com/alecthomas/gometalinter/blob/master/linters.go#L236), and I then appended ,\".*\" to additionally grab the line of code of the offending line. Lastly, I excluded ".*Errors unhandled.*fmt.Fprintf.*" to filter out only fmt.Fprintf errors around omitted errors.
Also as a result of this change, gas lint output will now include the offending code.
Closes #590
2018-02-14 17:58:20 +00:00
|
|
|
fmt.Fprintf(w, "Fatal Failure: %v\n", reason.(error))
|
2017-05-16 12:09:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
debug.PrintStack()
|
|
|
|
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2018-01-26 05:59:21 +00:00
|
|
|
|
|
|
|
// CreateTransaction returns a transaction object.
|
|
|
|
func CreateTransaction(ctx context.Context, args SendTxArgs) *QueuedTx {
|
|
|
|
return &QueuedTx{
|
|
|
|
ID: QueuedTxID(uuid.New()),
|
|
|
|
Context: ctx,
|
|
|
|
Args: args,
|
2018-01-05 20:58:17 +00:00
|
|
|
Result: make(chan TransactionResult, 1),
|
2018-01-26 05:59:21 +00:00
|
|
|
}
|
|
|
|
}
|