<commit_before>// +build sync

package main

import (
	"os/exec"

	"testing"
)

func TestSyncFiles(t *testing.T) {
	println("TestSyncFiles")

	exec.Command("sh", "-c",
		"rm -rf src dst;" +
		"mkdir src dst;" +
		"echo aaa > src/a;" +
		"cp -p src/a dst;" +
		"echo bbb > src/b;" +
		"echo ccc > dst/c;" +
		"echo eee > src/e;" +
		"touch -A -0100 src/e;" +
		"touch dst/e;" +
		"echo fff > dst/f;" +
		"touch -A -0200 dst/f;" +
		"touch src/f;",
	).Run()

	src := FSDir("src")
	dst := FSDir("dst")

	fs, err := SyncFiles(SyncModeBegin, &dst, nil)
	if err != nil {
		panic(err)
	}

	fs, err = SyncFiles(SyncModeSend, &src, fs)
	if err != nil {
		panic(err)
	}

	fs, err = SyncFiles(SyncModeBoth, &dst, fs)
	if err != nil {
		panic(err)
	}

	fs, err = SyncFiles(SyncModeReceive, &src, fs)
	if err != nil {
		panic(err)
	}
}
<commit_msg>use strings.Join in TestSyncFiles<commit_after>// +build sync

package main

import (
	"os/exec"
	"strings"

	"testing"
)

func TestSyncFiles(t *testing.T) {
	println("TestSyncFiles")

	exec.Command("sh", "-c", strings.Join([]string{
		"rm -rf src dst",
		"mkdir src dst",
		"echo aaa > src/a",
		"cp -p src/a dst",
		"echo bbb > src/b",
		"echo ccc > dst/c",
		"echo eee > src/e",
		"touch -A -0100 src/e",
		"touch dst/e",
		"echo fff > dst/f",
		"touch -A -0200 dst/f",
		"touch src/f",
	}, ";")).Run()

	src := FSDir("src")
	dst := FSDir("dst")

	fs, err := SyncFiles(SyncModeBegin, &dst, nil)
	if err != nil {
		panic(err)
	}

	fs, err = SyncFiles(SyncModeSend, &src, fs)
	if err != nil {
		panic(err)
	}

	fs, err = SyncFiles(SyncModeBoth, &dst, fs)
	if err != nil {
		panic(err)
	}

	fs, err = SyncFiles(SyncModeReceive, &src, fs)
	if err != nil {
		panic(err)
	}
}
<|endoftext|>


<commit_before>package livequery

func queryHash(query M) string {
	return ""
}

func matchesQuery(object, query M) bool {

	if className, ok := query["className"]; ok {
		if object["className"].(string) != className {
			return false
		}
		return matchesQuery(object, query["where"].(map[string]interface{}))
	}

	for field, constraints := range query {
		if matchesKeyConstraints(object, field, constraints) == false {
			return false
		}
	}

	return true
}

func matchesKeyConstraints(object M, key string, constraints interface{}) bool {
	if key == "$or" {
		if querys, ok := constraints.([]interface{}); ok {
			for _, query := range querys {
				if q, ok := query.(map[string]interface{}); ok {
					if matchesQuery(object, q) {
						return true
					}
				}
			}
			return false
		}
		return false
	}

	// 不支持 relatedTo
	if key == "$relatedTo" {
		return false
	}

	// 只支持 key == "$or" 时，constraints 为数组的情况
	if _, ok := constraints.([]interface{}); ok {
		return false
	}

	var constraint M
	if v, ok := constraints.(map[string]interface{}); ok {
		constraint = v
	} else {
		if objects, ok := object[key].([]interface{}); ok {
			for _, o := range objects {
				if equalObject(o, constraints) {
					return true
				}
			}
			return false
		}
		return equalObject(object[key], constraints)
	}

	objectType := constraint["__type"].(string)
	if objectType != "" {

	}

	return true
}

// equalObject 仅比较基础类型：string float64 bool
func equalObject(i1, i2 interface{}) bool {
	if v1, ok := i1.(string); ok {
		if v2, ok := i2.(string); ok {
			return v1 == v2
		}
	}

	if v1, ok := i1.(float64); ok {
		if v2, ok := i2.(float64); ok {
			return v1 == v2
		}
	}

	if v1, ok := i1.(bool); ok {
		if v2, ok := i2.(bool); ok {
			return v1 == v2
		}
	}

	return false
}
<commit_msg>完成 equalObject<commit_after>package livequery

func queryHash(query M) string {
	return ""
}

func matchesQuery(object, query M) bool {

	if className, ok := query["className"]; ok {
		if object["className"].(string) != className {
			return false
		}
		return matchesQuery(object, query["where"].(map[string]interface{}))
	}

	for field, constraints := range query {
		if matchesKeyConstraints(object, field, constraints) == false {
			return false
		}
	}

	return true
}

func matchesKeyConstraints(object M, key string, constraints interface{}) bool {
	if key == "$or" {
		if querys, ok := constraints.([]interface{}); ok {
			for _, query := range querys {
				if q, ok := query.(map[string]interface{}); ok {
					if matchesQuery(object, q) {
						return true
					}
				}
			}
			return false
		}
		return false
	}

	// 不支持 relatedTo
	if key == "$relatedTo" {
		return false
	}

	// 只支持 key == "$or" 时，constraints 为数组的情况
	if _, ok := constraints.([]interface{}); ok {
		return false
	}

	var constraint M
	if v, ok := constraints.(map[string]interface{}); ok {
		constraint = v
	} else {
		if objects, ok := object[key].([]interface{}); ok {
			for _, o := range objects {
				if equalObject(o, constraints) {
					return true
				}
			}
			return false
		}
		return equalObject(object[key], constraints)
	}

	objectType := constraint["__type"].(string)
	if objectType != "" {

	}

	return true
}

// equalObject 仅比较基础类型：string float64 bool slice map
func equalObject(i1, i2 interface{}) bool {
	if v1, ok := i1.(string); ok {
		if v2, ok := i2.(string); ok {
			return v1 == v2
		}
		return false
	}

	if v1, ok := i1.(float64); ok {
		if v2, ok := i2.(float64); ok {
			return v1 == v2
		}
		return false
	}

	if v1, ok := i1.(bool); ok {
		if v2, ok := i2.(bool); ok {
			return v1 == v2
		}
		return false
	}

	if v1, ok := i1.([]interface{}); ok {
		if v2, ok := i2.([]interface{}); ok {
			if len(v1) != len(v2) {
				return false
			}
			for i := 0; i < len(v1); i++ {
				if equalObject(v1[i], v2[i]) == false {
					return false
				}
			}
			return true
		}
		return false
	}

	if v1, ok := i1.(map[string]interface{}); ok {
		if v2, ok := i2.(map[string]interface{}); ok {
			if len(v1) != len(v2) {
				return false
			}
			for k := range v1 {
				if equalObject(v1[k], v2[k]) == false {
					return false
				}
			}
			return true
		}
		return false
	}

	return false
}
<|endoftext|>


<commit_before>package filesystem

import (
	"context"
	"fmt"
	"io"
	"log"
	"time"

	pb "github.com/apoydence/loggrebutterfly/pb/v1"
	"github.com/apoydence/petasos/reader"
	"github.com/apoydence/petasos/router"
	"google.golang.org/grpc"
)

type FileSystem struct {
	masterClient pb.MasterClient

	routes map[string]clientInfo
}

type clientInfo struct {
	client pb.DataNodeClient
	closer io.Closer
}

func New(masterAddr string) *FileSystem {
	return &FileSystem{
		masterClient: setupMasterClient(masterAddr),
	}
}

func (f *FileSystem) List() (files []string, err error) {
	if err := f.setupRoutes(); err != nil {
		return nil, err
	}

	for name, _ := range f.routes {
		files = append(files, name)
	}

	return files, nil
}

func (f *FileSystem) Writer(name string) (writer router.Writer, err error) {
	client, ok := f.fetchRoute(name)
	if !ok {
		return nil, fmt.Errorf("unknown file: %s", name)
	}

	wrapper := &senderWrapper{
		client: client,
		reset: func() {
			r := f.routes
			f.routes = nil

			for _, client := range r {
				client.closer.Close()
			}
		},
	}

	return wrapper, nil
}

func (f *FileSystem) Reader(name string) (reader reader.Reader, err error) {
	client, ok := f.fetchRoute(name)
	if !ok {
		return nil, fmt.Errorf("unknown file: %s", name)
	}

	ctx, cancel := context.WithCancel(context.Background())
	rx, err := client.Read(ctx, &pb.ReadInfo{Name: name})
	if err != nil {
		return nil, err
	}

	return &receiverWrapper{rx: rx, cancel: cancel}, nil
}

func (f *FileSystem) setupRoutes() error {
	if f.routes != nil {
		return nil
	}

	files, err := f.list()
	if err != nil {
		return err
	}

	f.routes = make(map[string]clientInfo)

	for _, file := range files {
		f.routes[file.Name] = setupDataClient(file.Leader)
	}

	return nil
}

func (f *FileSystem) fetchRoute(name string) (client pb.DataNodeClient, ok bool) {
	if err := f.setupRoutes(); err != nil {
		return nil, false
	}

	info, ok := f.routes[name]
	return info.client, ok
}

func (f *FileSystem) list() (files []*pb.RouteInfo, err error) {
	ctx, _ := context.WithDeadline(context.Background(), time.Now().Add(5*time.Second))
	resp, err := f.masterClient.Routes(ctx, new(pb.RoutesInfo))
	if err != nil {
		return nil, err
	}

	return resp.Routes, nil
}

func setupMasterClient(addr string) pb.MasterClient {
	conn, err := grpc.Dial(addr, grpc.WithInsecure())
	if err != nil {
		log.Fatalf("unable to connect to master: %s", err)
	}
	return pb.NewMasterClient(conn)
}

func setupDataClient(addr string) clientInfo {
	conn, err := grpc.Dial(addr, grpc.WithInsecure())
	if err != nil {
		log.Fatalf("unable to connect to master: %s", err)
	}
	return clientInfo{
		client: pb.NewDataNodeClient(conn),
		closer: conn,
	}
}

type senderWrapper struct {
	client pb.DataNodeClient
	err    error
	reset  func()
}

func (w *senderWrapper) Write(data []byte) error {
	if w.err != nil {
		return w.err
	}

	ctx, _ := context.WithDeadline(context.Background(), time.Now().Add(time.Second))
	_, w.err = w.client.Write(ctx, &pb.WriteInfo{Payload: data})

	if w.err != nil {
		w.reset()
		return w.err
	}

	return nil
}

type receiverWrapper struct {
	rx     pb.DataNode_ReadClient
	cancel func()
}

func (w *receiverWrapper) Read() ([]byte, error) {
	data, err := w.rx.Recv()
	if err != nil && grpc.ErrorDesc(err) == "EOF" {
		return nil, io.EOF
	}

	if err != nil {
		return nil, err
	}

	return data.Payload, nil
}

func (w *receiverWrapper) Close() {
	w.cancel()
}
<commit_msg>More descriptive errors in client<commit_after>package filesystem

import (
	"context"
	"fmt"
	"io"
	"log"
	"time"

	pb "github.com/apoydence/loggrebutterfly/pb/v1"
	"github.com/apoydence/petasos/reader"
	"github.com/apoydence/petasos/router"
	"google.golang.org/grpc"
)

type FileSystem struct {
	masterClient pb.MasterClient

	routes map[string]clientInfo
}

type clientInfo struct {
	addr   string
	client pb.DataNodeClient
	closer io.Closer
}

func New(masterAddr string) *FileSystem {
	return &FileSystem{
		masterClient: setupMasterClient(masterAddr),
	}
}

func (f *FileSystem) List() (files []string, err error) {
	if err := f.setupRoutes(); err != nil {
		return nil, err
	}

	for name, _ := range f.routes {
		files = append(files, name)
	}

	return files, nil
}

func (f *FileSystem) Writer(name string) (writer router.Writer, err error) {
	client, ok := f.fetchRoute(name)
	if !ok {
		return nil, fmt.Errorf("unknown file: %s", name)
	}

	wrapper := &senderWrapper{
		client: client.client,
		addr:   client.addr,
		reset: func() {
			r := f.routes
			f.routes = nil

			for _, client := range r {
				client.closer.Close()
			}
		},
	}

	return wrapper, nil
}

func (f *FileSystem) Reader(name string) (reader reader.Reader, err error) {
	client, ok := f.fetchRoute(name)
	if !ok {
		return nil, fmt.Errorf("unknown file: %s", name)
	}

	ctx, cancel := context.WithCancel(context.Background())
	rx, err := client.client.Read(ctx, &pb.ReadInfo{Name: name})
	if err != nil {
		return nil, err
	}

	return &receiverWrapper{rx: rx, cancel: cancel, addr: client.addr}, nil
}

func (f *FileSystem) setupRoutes() error {
	if f.routes != nil {
		return nil
	}

	files, err := f.list()
	if err != nil {
		return err
	}

	f.routes = make(map[string]clientInfo)

	for _, file := range files {
		f.routes[file.Name] = setupDataClient(file.Leader)
	}

	return nil
}

func (f *FileSystem) fetchRoute(name string) (client clientInfo, ok bool) {
	if err := f.setupRoutes(); err != nil {
		return clientInfo{}, false
	}

	info, ok := f.routes[name]
	return info, ok
}

func (f *FileSystem) list() (files []*pb.RouteInfo, err error) {
	ctx, _ := context.WithDeadline(context.Background(), time.Now().Add(5*time.Second))
	resp, err := f.masterClient.Routes(ctx, new(pb.RoutesInfo))
	if err != nil {
		return nil, err
	}

	return resp.Routes, nil
}

func setupMasterClient(addr string) pb.MasterClient {
	conn, err := grpc.Dial(addr, grpc.WithInsecure())
	if err != nil {
		log.Fatalf("unable to connect to master: %s", err)
	}
	return pb.NewMasterClient(conn)
}

func setupDataClient(addr string) clientInfo {
	conn, err := grpc.Dial(addr, grpc.WithInsecure())
	if err != nil {
		log.Fatalf("unable to connect to master: %s", err)
	}
	return clientInfo{
		addr:   addr,
		client: pb.NewDataNodeClient(conn),
		closer: conn,
	}
}

type senderWrapper struct {
	addr   string
	client pb.DataNodeClient
	err    error
	reset  func()
}

func (w *senderWrapper) Write(data []byte) error {
	if w.err != nil {
		return w.err
	}

	ctx, _ := context.WithDeadline(context.Background(), time.Now().Add(time.Second))
	_, w.err = w.client.Write(ctx, &pb.WriteInfo{Payload: data})

	if w.err != nil {
		w.reset()
		return fmt.Errorf("[WRITE TO %s]: %s", w.addr, w.err)
	}

	return nil
}

type receiverWrapper struct {
	addr   string
	rx     pb.DataNode_ReadClient
	cancel func()
}

func (w *receiverWrapper) Read() ([]byte, error) {
	data, err := w.rx.Recv()
	if err != nil && grpc.ErrorDesc(err) == "EOF" {
		return nil, io.EOF
	}

	if err != nil {
		return nil, fmt.Errorf("[READ FROM %s]: %s", w.addr, err)
	}

	return data.Payload, nil
}

func (w *receiverWrapper) Close() {
	w.cancel()
}
<|endoftext|>


<commit_before>package pgtype

import (
	"database/sql/driver"
	"encoding/binary"
	"math"
	"strconv"

	"github.com/jackc/pgx/pgio"
	"github.com/pkg/errors"
)

type Int8 struct {
	Int    int64
	Status Status
}

func (dst *Int8) Set(src interface{}) error {
	if src == nil {
		*dst = Int8{Status: Null}
		return nil
	}

	switch value := src.(type) {
	case int8:
		*dst = Int8{Int: int64(value), Status: Present}
	case uint8:
		*dst = Int8{Int: int64(value), Status: Present}
	case int16:
		*dst = Int8{Int: int64(value), Status: Present}
	case uint16:
		*dst = Int8{Int: int64(value), Status: Present}
	case int32:
		*dst = Int8{Int: int64(value), Status: Present}
	case uint32:
		*dst = Int8{Int: int64(value), Status: Present}
	case int64:
		*dst = Int8{Int: int64(value), Status: Present}
	case uint64:
		if value > math.MaxInt64 {
			return errors.Errorf("%d is greater than maximum value for Int8", value)
		}
		*dst = Int8{Int: int64(value), Status: Present}
	case int:
		if int64(value) < math.MinInt64 {
			return errors.Errorf("%d is greater than maximum value for Int8", value)
		}
		if int64(value) > math.MaxInt64 {
			return errors.Errorf("%d is greater than maximum value for Int8", value)
		}
		*dst = Int8{Int: int64(value), Status: Present}
	case uint:
		if uint64(value) > math.MaxInt64 {
			return errors.Errorf("%d is greater than maximum value for Int8", value)
		}
		*dst = Int8{Int: int64(value), Status: Present}
	case string:
		num, err := strconv.ParseInt(value, 10, 64)
		if err != nil {
			return err
		}
		*dst = Int8{Int: num, Status: Present}
	default:
		if originalSrc, ok := underlyingNumberType(src); ok {
			return dst.Set(originalSrc)
		}
		return errors.Errorf("cannot convert %v to Int8", value)
	}

	return nil
}

func (dst *Int8) Get() interface{} {
	switch dst.Status {
	case Present:
		return dst.Int
	case Null:
		return nil
	default:
		return dst.Status
	}
}

func (src *Int8) AssignTo(dst interface{}) error {
	return int64AssignTo(int64(src.Int), src.Status, dst)
}

func (dst *Int8) DecodeText(ci *ConnInfo, src []byte) error {
	if src == nil {
		*dst = Int8{Status: Null}
		return nil
	}

	n, err := strconv.ParseInt(string(src), 10, 64)
	if err != nil {
		return err
	}

	*dst = Int8{Int: n, Status: Present}
	return nil
}

func (dst *Int8) DecodeBinary(ci *ConnInfo, src []byte) error {
	if src == nil {
		*dst = Int8{Status: Null}
		return nil
	}

	if len(src) != 8 {
		return errors.Errorf("invalid length for int8: %v", len(src))
	}

	n := int64(binary.BigEndian.Uint64(src))

	*dst = Int8{Int: n, Status: Present}
	return nil
}

func (src *Int8) EncodeText(ci *ConnInfo, buf []byte) ([]byte, error) {
	switch src.Status {
	case Null:
		return nil, nil
	case Undefined:
		return nil, errUndefined
	}

	return append(buf, strconv.FormatInt(src.Int, 10)...), nil
}

func (src *Int8) EncodeBinary(ci *ConnInfo, buf []byte) ([]byte, error) {
	switch src.Status {
	case Null:
		return nil, nil
	case Undefined:
		return nil, errUndefined
	}

	return pgio.AppendInt64(buf, src.Int), nil
}

// Scan implements the database/sql Scanner interface.
func (dst *Int8) Scan(src interface{}) error {
	if src == nil {
		*dst = Int8{Status: Null}
		return nil
	}

	switch src := src.(type) {
	case int64:
		*dst = Int8{Int: src, Status: Present}
		return nil
	case string:
		return dst.DecodeText(nil, []byte(src))
	case []byte:
		srcCopy := make([]byte, len(src))
		copy(srcCopy, src)
		return dst.DecodeText(nil, srcCopy)
	}

	return errors.Errorf("cannot scan %T", src)
}

// Value implements the database/sql/driver Valuer interface.
func (src *Int8) Value() (driver.Value, error) {
	switch src.Status {
	case Present:
		return int64(src.Int), nil
	case Null:
		return nil, nil
	default:
		return nil, errUndefined
	}
}

func (src *Int8) MarshalJSON() ([]byte, error) {
	switch src.Status {
	case Present:
		return []byte(strconv.FormatInt(src.Int, 10)), nil
	case Null:
		return []byte("null"), nil
	case Undefined:
		return nil, errUndefined
	}

	return nil, errBadStatus
}

func (dst *Int8) UnmarshalJSON(b []byte) error {
	var n int64
	err := json.Unmarshal(b, &n)
	if err != nil {
		return err
	}

	*dst = Int8{Int: n, Status: Present}

	return nil
}
<commit_msg>Import encoding/json package<commit_after>package pgtype

import (
	"database/sql/driver"
	"encoding/binary"
	"encoding/json"
	"math"
	"strconv"

	"github.com/jackc/pgx/pgio"
	"github.com/pkg/errors"
)

type Int8 struct {
	Int    int64
	Status Status
}

func (dst *Int8) Set(src interface{}) error {
	if src == nil {
		*dst = Int8{Status: Null}
		return nil
	}

	switch value := src.(type) {
	case int8:
		*dst = Int8{Int: int64(value), Status: Present}
	case uint8:
		*dst = Int8{Int: int64(value), Status: Present}
	case int16:
		*dst = Int8{Int: int64(value), Status: Present}
	case uint16:
		*dst = Int8{Int: int64(value), Status: Present}
	case int32:
		*dst = Int8{Int: int64(value), Status: Present}
	case uint32:
		*dst = Int8{Int: int64(value), Status: Present}
	case int64:
		*dst = Int8{Int: int64(value), Status: Present}
	case uint64:
		if value > math.MaxInt64 {
			return errors.Errorf("%d is greater than maximum value for Int8", value)
		}
		*dst = Int8{Int: int64(value), Status: Present}
	case int:
		if int64(value) < math.MinInt64 {
			return errors.Errorf("%d is greater than maximum value for Int8", value)
		}
		if int64(value) > math.MaxInt64 {
			return errors.Errorf("%d is greater than maximum value for Int8", value)
		}
		*dst = Int8{Int: int64(value), Status: Present}
	case uint:
		if uint64(value) > math.MaxInt64 {
			return errors.Errorf("%d is greater than maximum value for Int8", value)
		}
		*dst = Int8{Int: int64(value), Status: Present}
	case string:
		num, err := strconv.ParseInt(value, 10, 64)
		if err != nil {
			return err
		}
		*dst = Int8{Int: num, Status: Present}
	default:
		if originalSrc, ok := underlyingNumberType(src); ok {
			return dst.Set(originalSrc)
		}
		return errors.Errorf("cannot convert %v to Int8", value)
	}

	return nil
}

func (dst *Int8) Get() interface{} {
	switch dst.Status {
	case Present:
		return dst.Int
	case Null:
		return nil
	default:
		return dst.Status
	}
}

func (src *Int8) AssignTo(dst interface{}) error {
	return int64AssignTo(int64(src.Int), src.Status, dst)
}

func (dst *Int8) DecodeText(ci *ConnInfo, src []byte) error {
	if src == nil {
		*dst = Int8{Status: Null}
		return nil
	}

	n, err := strconv.ParseInt(string(src), 10, 64)
	if err != nil {
		return err
	}

	*dst = Int8{Int: n, Status: Present}
	return nil
}

func (dst *Int8) DecodeBinary(ci *ConnInfo, src []byte) error {
	if src == nil {
		*dst = Int8{Status: Null}
		return nil
	}

	if len(src) != 8 {
		return errors.Errorf("invalid length for int8: %v", len(src))
	}

	n := int64(binary.BigEndian.Uint64(src))

	*dst = Int8{Int: n, Status: Present}
	return nil
}

func (src *Int8) EncodeText(ci *ConnInfo, buf []byte) ([]byte, error) {
	switch src.Status {
	case Null:
		return nil, nil
	case Undefined:
		return nil, errUndefined
	}

	return append(buf, strconv.FormatInt(src.Int, 10)...), nil
}

func (src *Int8) EncodeBinary(ci *ConnInfo, buf []byte) ([]byte, error) {
	switch src.Status {
	case Null:
		return nil, nil
	case Undefined:
		return nil, errUndefined
	}

	return pgio.AppendInt64(buf, src.Int), nil
}

// Scan implements the database/sql Scanner interface.
func (dst *Int8) Scan(src interface{}) error {
	if src == nil {
		*dst = Int8{Status: Null}
		return nil
	}

	switch src := src.(type) {
	case int64:
		*dst = Int8{Int: src, Status: Present}
		return nil
	case string:
		return dst.DecodeText(nil, []byte(src))
	case []byte:
		srcCopy := make([]byte, len(src))
		copy(srcCopy, src)
		return dst.DecodeText(nil, srcCopy)
	}

	return errors.Errorf("cannot scan %T", src)
}

// Value implements the database/sql/driver Valuer interface.
func (src *Int8) Value() (driver.Value, error) {
	switch src.Status {
	case Present:
		return int64(src.Int), nil
	case Null:
		return nil, nil
	default:
		return nil, errUndefined
	}
}

func (src *Int8) MarshalJSON() ([]byte, error) {
	switch src.Status {
	case Present:
		return []byte(strconv.FormatInt(src.Int, 10)), nil
	case Null:
		return []byte("null"), nil
	case Undefined:
		return nil, errUndefined
	}

	return nil, errBadStatus
}

func (dst *Int8) UnmarshalJSON(b []byte) error {
	var n int64
	err := json.Unmarshal(b, &n)
	if err != nil {
		return err
	}

	*dst = Int8{Int: n, Status: Present}

	return nil
}
<|endoftext|>


<commit_before>// Copyright 2010 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

/*
Package cgo contains runtime support for code generated
by the cgo tool.  See the documentation for the cgo command
for details on using cgo.
*/
package cgo

/*

#cgo darwin,!arm64 LDFLAGS: -lpthread
#cgo darwin,arm64 LDFLAGS: -framework CoreFoundation
#cgo dragonfly LDFLAGS: -lpthread
#cgo freebsd LDFLAGS: -lpthread
#cgo android LDFLAGS: -llog
#cgo !android,linux LDFLAGS: -lpthread
#cgo netbsd LDFLAGS: -lpthread
#cgo openbsd LDFLAGS: -lpthread
#cgo aix LDFLAGS: -Wl,-berok
#cgo solaris LDFLAGS: -lxnet
#cgo illumos LDFLAGS: -lsocket

// Issue 35247.
#cgo darwin CFLAGS: -Wno-nullability-completeness

#cgo CFLAGS: -Wall -Werror

#cgo solaris CPPFLAGS: -D_POSIX_PTHREAD_SEMANTICS

*/
import "C"
<commit_msg>runtime/cgo: remove stdlib.h warning workaround on darwin<commit_after>// Copyright 2010 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

/*
Package cgo contains runtime support for code generated
by the cgo tool.  See the documentation for the cgo command
for details on using cgo.
*/
package cgo

/*

#cgo darwin,!arm64 LDFLAGS: -lpthread
#cgo darwin,arm64 LDFLAGS: -framework CoreFoundation
#cgo dragonfly LDFLAGS: -lpthread
#cgo freebsd LDFLAGS: -lpthread
#cgo android LDFLAGS: -llog
#cgo !android,linux LDFLAGS: -lpthread
#cgo netbsd LDFLAGS: -lpthread
#cgo openbsd LDFLAGS: -lpthread
#cgo aix LDFLAGS: -Wl,-berok
#cgo solaris LDFLAGS: -lxnet
#cgo illumos LDFLAGS: -lsocket

#cgo CFLAGS: -Wall -Werror

#cgo solaris CPPFLAGS: -D_POSIX_PTHREAD_SEMANTICS

*/
import "C"
<|endoftext|>


<commit_before>// Copyright 2012 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// Lock-free stack.
// Initialize head to 0, compare with 0 to test for emptiness.
// The stack does not keep pointers to nodes,
// so they can be garbage collected if there are no other pointers to nodes.
// The following code runs only on g0 stack.

package runtime

import (
	"runtime/internal/atomic"
	"unsafe"
)

func lfstackpush(head *uint64, node *lfnode) {
	node.pushcnt++
	new := lfstackPack(node, node.pushcnt)
	if node1 := lfstackUnpack(new); node1 != node {
		print("runtime: lfstackpush invalid packing: node=", node, " cnt=", hex(node.pushcnt), " packed=", hex(new), " -> node=", node1, "\n")
		throw("lfstackpush")
	}
	for {
		old := atomic.Load64(head)
		node.next = old
		if atomic.Cas64(head, old, new) {
			break
		}
	}
}

func lfstackpop(head *uint64) unsafe.Pointer {
	for {
		old := atomic.Load64(head)
		if old == 0 {
			return nil
		}
		node := lfstackUnpack(old)
		next := atomic.Load64(&node.next)
		if atomic.Cas64(head, old, next) {
			return unsafe.Pointer(node)
		}
	}
}
<commit_msg>runtime: fix stale comment in lfstack<commit_after>// Copyright 2012 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// Lock-free stack.
// Initialize head to 0, compare with 0 to test for emptiness.
// The stack does not keep pointers to nodes,
// so they can be garbage collected if there are no other pointers to nodes.
// The following code runs only in non-preemptible contexts.

package runtime

import (
	"runtime/internal/atomic"
	"unsafe"
)

func lfstackpush(head *uint64, node *lfnode) {
	node.pushcnt++
	new := lfstackPack(node, node.pushcnt)
	if node1 := lfstackUnpack(new); node1 != node {
		print("runtime: lfstackpush invalid packing: node=", node, " cnt=", hex(node.pushcnt), " packed=", hex(new), " -> node=", node1, "\n")
		throw("lfstackpush")
	}
	for {
		old := atomic.Load64(head)
		node.next = old
		if atomic.Cas64(head, old, new) {
			break
		}
	}
}

func lfstackpop(head *uint64) unsafe.Pointer {
	for {
		old := atomic.Load64(head)
		if old == 0 {
			return nil
		}
		node := lfstackUnpack(old)
		next := atomic.Load64(&node.next)
		if atomic.Cas64(head, old, next) {
			return unsafe.Pointer(node)
		}
	}
}
<|endoftext|>


<commit_before>package task

import (
	"bufio"
	"fmt"
	ptylib "github.com/kr/pty"
	"golang.org/x/crypto/ssh/terminal"
	"os"
	"os/exec"
)

// Task is a running process inside a PTY. Use Spawn to create a new Task.
// Interact with a task by reading from its Events channel, and writing to its
// Input channel.
type Task struct {
	cmd *exec.Cmd
	pty *os.File
	// Send a byte slice to this channel to write to the process's pty.
	Input chan<- []byte
	// Output will emit Event structs as events (like process output or process
	// termination) occurr.
	Output <-chan *Event
}

func (task *Task) String() string {
	return fmt.Sprintf("%T{%p '%+v'}", task, task, task.cmd.Path)
}

// Kill kills the task. Returns nil if the task is not running.
func (task *Task) Kill() error {
	if task.cmd.Process == nil {
		return nil
	}

	return task.cmd.Process.Kill()
}

// Spawn a Cmd into a PTY. Returns a Task, so that you can communicate with
// the process
func Spawn(cmd *exec.Cmd, splitter bufio.SplitFunc) (*Task, error) {
	pty, err := ptylib.Start(cmd)
	if err != nil {
		// TODO: wrap error.
		return nil, err
	}

	// if we don't make the terminal raw, it will echo all input back to us.
	_, err = terminal.MakeRaw(int(pty.Fd()))
	if err != nil {
		// TODO: wrap error.
		return nil, err
	}

	scanner := bufio.NewScanner(pty)
	scanner.Split(splitter)

	fromProcess := make(chan *Event)
	toProcess := make(chan []byte)

	task := &Task{
		cmd:    cmd,
		pty:    pty,
		Input:  toProcess,
		Output: fromProcess,
	}

	go emitEvents(task, scanner, fromProcess, toProcess)
	go sendInput(pty, toProcess)

	return task, nil
}

func emitEvents(task *Task, scanner *bufio.Scanner, out chan<- *Event, closeOnEnd chan []byte) {
	for scanner.Scan() {
		output := &Output{scanner.Text()}
		out <- &Event{task, output}
	}

	if err := scanner.Err(); err != nil {
		// TODO: something more sensible than panic
		// send error on the channel?
		panic(err)
	}

	exit := task.cmd.Wait()
	// TODO: should this be last, and go after the send?
	close(closeOnEnd)

	out <- &Event{task, &Ended{exit}}
	close(out)
}

func sendInput(pty *os.File, in <-chan []byte) {
	for input := range in {
		_, err := pty.Write(input)
		if err != nil {
			// TODO: something more sensible than panic
			// TODO: can we always write? What if the process is dead and the PTY closes??
			panic(err)
		}
	}
}
<commit_msg>task: add Respawn() method<commit_after>package task

import (
	"bufio"
	"fmt"
	ptylib "github.com/kr/pty"
	"golang.org/x/crypto/ssh/terminal"
	"os"
	"os/exec"
)

// Task is a running process inside a PTY. Use Spawn to create a new Task.
// Interact with a task by reading from its Events channel, and writing to its
// Input channel.
type Task struct {
	cmd *exec.Cmd
	pty *os.File
	// Send a byte slice to this channel to write to the process's pty.
	Input chan<- []byte
	// Output will emit Event structs as events (like process output or process
	// termination) occurr.
	Output    <-chan *Event
	splitFunc bufio.SplitFunc
}

func (task *Task) String() string {
	return fmt.Sprintf("%T{%p '%+v'}", task, task, task.cmd.Path)
}

// Kill kills the task. Returns nil if the task is not running.
func (task *Task) Kill() error {
	if task.cmd.Process == nil {
		return nil
	}

	return task.cmd.Process.Kill()
}

// Spawn a Cmd into a PTY. Returns a Task, so that you can communicate with
// the process
func Spawn(cmd *exec.Cmd, splitter bufio.SplitFunc) (*Task, error) {
	pty, err := ptylib.Start(cmd)
	if err != nil {
		// TODO: wrap error.
		return nil, err
	}

	// if we don't make the terminal raw, it will echo all input back to us.
	_, err = terminal.MakeRaw(int(pty.Fd()))
	if err != nil {
		// TODO: wrap error.
		return nil, err
	}

	scanner := bufio.NewScanner(pty)
	scanner.Split(splitter)

	fromProcess := make(chan *Event)
	toProcess := make(chan []byte)

	task := &Task{
		cmd:       cmd,
		pty:       pty,
		splitFunc: splitter,
		Input:     toProcess,
		Output:    fromProcess,
	}

	go emitEvents(task, scanner, fromProcess, toProcess)
	go sendInput(pty, toProcess)

	return task, nil
}

// Respawn spawns a new task with a duplicate of this task's command.
func (task *Task) Respawn() (*Task, error) {
	return Spawn(
		&exec.Cmd{
			Path:        task.cmd.Path,
			Args:        task.cmd.Args,
			Env:         task.cmd.Env,
			Dir:         task.cmd.Dir,
			ExtraFiles:  task.cmd.ExtraFiles,
			SysProcAttr: task.cmd.SysProcAttr,
		},
		task.splitFunc,
	)
}

func emitEvents(task *Task, scanner *bufio.Scanner, out chan<- *Event, closeOnEnd chan []byte) {
	for scanner.Scan() {
		output := &Output{scanner.Text()}
		out <- &Event{task, output}
	}

	if err := scanner.Err(); err != nil {
		// TODO: something more sensible than panic
		// send error on the channel?
		panic(err)
	}

	exit := task.cmd.Wait()
	// TODO: should this be last, and go after the send?
	close(closeOnEnd)

	out <- &Event{task, &Ended{exit}}
	close(out)
}

func sendInput(pty *os.File, in <-chan []byte) {
	for input := range in {
		_, err := pty.Write(input)
		if err != nil {
			// TODO: something more sensible than panic
			// TODO: can we always write? What if the process is dead and the PTY closes??
			panic(err)
		}
	}
}
<|endoftext|>


<commit_before>package main

import (
	"bufio"
	"flag"
	"fmt"
	"math"
	"os"
	"strconv"
	"strings"

	log "github.com/Sirupsen/logrus"
	"github.com/gizak/termui"
)

var (
	colorForeground = termui.ColorWhite
)

// TODO:
// - Different chart types
// - Check sizing

func drawTermChart(label []string, data []int) {
	// Determine width of chart
	dataPoints := len(data)
	barWidth := 4
	barGap := 1
	width := (barWidth * dataPoints) + (barGap * (dataPoints - 2))

	// If we can fit it all in one screen
	if width < 200 {
		bc := termui.NewBarChart()
		bclabels := label
		bc.Data = data
		bc.Height = 30
		bc.BarWidth = barWidth
		bc.BarGap = barGap
		bc.DataLabels = bclabels
		bc.TextColor = colorForeground
		bc.BarColor = termui.ColorBlue
		bc.NumColor = colorForeground
		termui.Body.AddRows(
			termui.NewRow(
				termui.NewCol(12, 0, bc),
			))
		termui.Body.Align()
		termui.Render(termui.Body)
	} else {
		requiredCharts := int(math.Ceil(float64(width) / 200.0))
		fmt.Println("Number of charts required:", requiredCharts)
		var datas = make([][]int, requiredCharts)
		var labels = make([][]string, requiredCharts)
		// Split the data
		for i := 0; i < dataPoints; i++ {
			chartNo := (i % requiredCharts)
			datas[chartNo] = append(datas[chartNo], data[i])
			labels[chartNo] = append(labels[chartNo], label[i])
		}
		// Create the charts
		barCharts := make([]termui.Bufferer, requiredCharts)
		for z := 0; z < requiredCharts; z++ {
			bc := termui.NewBarChart()
			bclabels := labels[z]
			bc.Data = datas[z]
			bc.Height = 30
			bc.BarWidth = int(float64(barWidth) * 1.5)
			bc.BarGap = barGap
			bc.DataLabels = bclabels
			bc.TextColor = colorForeground
			bc.BarColor = termui.ColorBlue
			bc.NumColor = colorForeground
			bc.SetY(z * 30)
			barCharts[z] = bc
			termui.Body.AddRows(
				termui.NewRow(
					termui.NewCol(12, 0, bc),
				),
			)
		}
		termui.Body.Align()
		termui.Render(termui.Body)
	}

	termui.Handle("/sys/kbd/q", func(termui.Event) {
		termui.StopLoop()
	})
	termui.Loop()

}

func tryDetectDelimeter(l string) string {
	var bd string
	md := 0
	ds := []string{",", "|", "\t", "."}
	for _, d := range ds {
		c := strings.Split(l, d)
		if len(c) > md {
			md = len(c)
			bd = d
		}
	}
	return bd
}

func tryDetectColTypes(l string, d string) []string {
	cols := strings.Split(l, d)
	colTypes := make([]string, len(cols))
	for i, col := range cols {
		_, err := strconv.ParseInt(col, 10, 64)
		if err == nil {
			colTypes[i] = "int"
			continue
		}
		_, err = strconv.ParseFloat(col, 10)
		if err == nil {
			colTypes[i] = "float"
			continue
		}
		_, err = strconv.ParseBool(col)
		if err == nil {
			colTypes[i] = "bool"
			continue
		}
		colTypes[i] = "string"
	}
	return colTypes
}

func mustParseFlags() {
	var theme string
	flag.StringVar(&theme, "theme", "dark", "color theme to use; one of: light, dark")
	flag.Parse()
	if theme != "light" && theme != "dark" {
		log.WithFields(log.Fields{"value": theme}).Fatal("unsupported theme name")
	}

	if theme == "light" {
		colorForeground = termui.ColorBlack
	}
}

func main() {
	log.SetFormatter(&log.JSONFormatter{})
	if err := termui.Init(); err != nil {
		log.WithField("value", err).Fatal("Failed to start termui")
	}
	defer termui.Close()

	var (
		data   []int
		labels []string
		delim  string
		rows   int
	)
	mustParseFlags()

	x := bufio.NewScanner(os.Stdin)
	for {
		z := x.Scan()
		if z == false {
			break
		}

		line := x.Text()
		if rows == 0 {
			delim = tryDetectDelimeter(line)
		}
		values := strings.Split(line, delim)
		intVal, err := strconv.ParseInt(values[1], 10, 64)
		if err != nil {
			log.WithField("value", values[1]).Fatal("Failed to convert input data to int")
		}

		labels = append(labels, values[0])
		data = append(data, int(intVal))

		rows++
	}

	drawTermChart(labels, data)
}
<commit_msg>Sizing based on the available terminal height<commit_after>package main

import (
	"bufio"
	"flag"
	"fmt"
	"math"
	"os"
	"strconv"
	"strings"

	log "github.com/Sirupsen/logrus"
	"github.com/gizak/termui"
)

var (
	colorForeground = termui.ColorWhite
)

// TODO:
// - Different chart types
// - Check sizing

func drawTermChart(label []string, data []int) {
	// Determine width of chart
	dataPoints := len(data)
	barWidth := 4
	barGap := 1
	width := (barWidth * dataPoints) + (barGap * (dataPoints - 2))
	termWidth := termui.TermWidth()
	termHeight := termui.TermHeight()

	// If we can fit it all in one screen
	if width < termWidth {
		bc := termui.NewBarChart()
		bc.Data = data
		bc.Height = termHeight
		bc.Width = termWidth
		bc.BarWidth = barWidth
		bc.BarGap = barGap
		bc.DataLabels = label
		bc.TextColor = colorForeground
		bc.BarColor = termui.ColorBlue
		bc.NumColor = colorForeground
		termui.Body.AddRows(
			termui.NewRow(
				termui.NewCol(12, 0, bc),
			))
		termui.Body.Align()
		termui.Render(termui.Body)
	} else {
		requiredCharts := int(math.Ceil(float64(width) / 200.0))
		fmt.Println("Number of charts required:", requiredCharts)
		var datas = make([][]int, requiredCharts)
		var labels = make([][]string, requiredCharts)
		// Split the data
		for i := 0; i < dataPoints; i++ {
			chartNo := (i % requiredCharts)
			datas[chartNo] = append(datas[chartNo], data[i])
			labels[chartNo] = append(labels[chartNo], label[i])
		}
		// Create the charts
		barCharts := make([]termui.Bufferer, requiredCharts)
		for z := 0; z < requiredCharts; z++ {
			bc := termui.NewBarChart()
			bclabels := labels[z]
			bc.Data = datas[z]
			bc.Height = termHeight / requiredCharts
			bc.BarWidth = int(float64(barWidth) * 1.5)
			bc.BarGap = barGap
			bc.DataLabels = bclabels
			bc.TextColor = colorForeground
			bc.BarColor = termui.ColorBlue
			bc.NumColor = colorForeground
			bc.SetY(z * 30)
			barCharts[z] = bc
			termui.Body.AddRows(
				termui.NewRow(
					termui.NewCol(12, 0, bc),
				),
			)
		}
		termui.Body.Align()
		termui.Render(termui.Body)
	}

}

func tryDetectDelimeter(l string) string {
	var bd string
	md := 0
	ds := []string{",", "|", "\t", "."}
	for _, d := range ds {
		c := strings.Split(l, d)
		if len(c) > md {
			md = len(c)
			bd = d
		}
	}
	return bd
}

func tryDetectColTypes(l string, d string) []string {
	cols := strings.Split(l, d)
	colTypes := make([]string, len(cols))
	for i, col := range cols {
		_, err := strconv.ParseInt(col, 10, 64)
		if err == nil {
			colTypes[i] = "int"
			continue
		}
		_, err = strconv.ParseFloat(col, 10)
		if err == nil {
			colTypes[i] = "float"
			continue
		}
		_, err = strconv.ParseBool(col)
		if err == nil {
			colTypes[i] = "bool"
			continue
		}
		colTypes[i] = "string"
	}
	return colTypes
}

func mustParseFlags() {
	var theme string
	flag.StringVar(&theme, "theme", "dark", "color theme to use; one of: light, dark")
	flag.Parse()
	if theme != "light" && theme != "dark" {
		log.WithFields(log.Fields{"value": theme}).Fatal("unsupported theme name")
	}

	if theme == "light" {
		colorForeground = termui.ColorBlack
	}
}

func main() {
	log.SetFormatter(&log.JSONFormatter{})
	if err := termui.Init(); err != nil {
		log.WithField("value", err).Fatal("Failed to start termui")
	}
	defer termui.Close()

	var (
		data   []int
		labels []string
		delim  string
		rows   int
	)
	mustParseFlags()

	x := bufio.NewScanner(os.Stdin)
	for {
		z := x.Scan()
		if z == false {
			break
		}

		line := x.Text()
		if rows == 0 {
			delim = tryDetectDelimeter(line)
		}
		values := strings.Split(line, delim)
		intVal, err := strconv.ParseInt(values[1], 10, 64)
		if err != nil {
			log.WithField("value", values[1]).Fatal("Failed to convert input data to int")
		}

		labels = append(labels, values[0])
		data = append(data, int(intVal))

		rows++
	}

	drawTermChart(labels, data)
	termui.Handle("/sys/kbd/q", func(termui.Event) {
		termui.StopLoop()
	})
	termui.Loop()

}
<|endoftext|>


<commit_before>package ristretto255

import (
	"bytes"
	"encoding/hex"
	"testing"

	"github.com/gtank/ristretto255/internal/edwards25519"
	"github.com/gtank/ristretto255/internal/radix51"
)

func assertFeEqual(value, expect *radix51.FieldElement) {
	if value.Equal(expect) == 1 {
		return
	} else {
		panic("failed equality assertion")
	}
}

type sqrtRatioTest struct {
	u, v     *radix51.FieldElement
	sqrt     *radix51.FieldElement
	choice   int
	negative int
}

// These tests can be found in curve25519-dalek's 'field.rs'
func TestSqrtRatioM1(t *testing.T) {
	var (
		zero, one = radix51.Zero, radix51.One

		// Two is nonsquare in our field, 4 is square
		two  = new(radix51.FieldElement).Add(one, one)
		four = new(radix51.FieldElement).Add(two, two)

		// 2*i
		twoTimesSqrtM1 = new(radix51.FieldElement).Mul(two, sqrtM1)

		sqrt2i = fieldElementFromDecimal(
			"38214883241950591754978413199355411911188925816896391856984770930832735035196")

		invSqrt4 = fieldElementFromDecimal(
			"28948022309329048855892746252171976963317496166410141009864396001978282409974")
	)

	// Check the construction of those magic numbers.
	assertFeEqual(new(radix51.FieldElement).Mul(sqrt2i, sqrt2i), twoTimesSqrtM1)
	assertFeEqual(new(radix51.FieldElement).Mul(new(radix51.FieldElement).Square(invSqrt4), four), one)

	var tests = []sqrtRatioTest{
		{u: zero, v: zero, sqrt: zero, choice: 1, negative: 0},    // 0
		{u: one, v: zero, sqrt: zero, choice: 0, negative: 0},     // 1
		{u: two, v: one, sqrt: sqrt2i, choice: 0, negative: 0},    // 2
		{u: four, v: one, sqrt: two, choice: 1, negative: 0},      // 3
		{u: one, v: four, sqrt: invSqrt4, choice: 1, negative: 0}, // 4
	}

	for idx, tt := range tests {
		sqrt := new(radix51.FieldElement)
		choice := feSqrtRatio(sqrt, tt.u, tt.v)
		if choice != tt.choice || sqrt.Equal(tt.sqrt) != 1 || sqrt.IsNegative() != tt.negative {
			t.Errorf("Failed test %d", idx)
			t.Logf("Got {u: %v, v: %v, sqrt: %v, choice: %d, neg: %d}", tt.u, tt.v, sqrt, choice, sqrt.IsNegative())
		}
	}
}

var (
	// The encoding of Ristretto element that can be represented internally by the Curve25519 base point.
	compressedRistrettoBasepoint, _ = hex.DecodeString("e2f2ae0a6abc4e71a884a961c500515f58e30b6aa582dd8db6a65945e08d2d76")

	// The representative Ristretto basepoint in extended coordinates.
	ristrettoBasepoint = Element{r: edwards25519.ExtendedGroupElement{
		X: radix51.FieldElement([5]uint64{426475514619346, 2063872706840040, 14628272888959, 107677749330612, 288339085807592}),
		Y: radix51.FieldElement([5]uint64{1934594822876571, 2049809580636559, 1991994783322914, 1758681962032007, 380046701118659}),
		Z: radix51.FieldElement([5]uint64{1, 0, 0, 0, 0}),
		T: radix51.FieldElement([5]uint64{410445769351754, 2235400917701188, 1495825632738689, 1351628537510093, 430502003771208}),
	}}
)

func TestRistrettoBasepointRoundTrip(t *testing.T) {
	decodedBasepoint := &Element{}
	err := decodedBasepoint.Decode(compressedRistrettoBasepoint)
	if err != nil {
		t.Fatal(err)
	}

	if decodedBasepoint.Equal(&ristrettoBasepoint) != 1 {
		t.Error("decode succeeded, but got wrong point")
	}

	roundtripBasepoint := decodedBasepoint.Encode(nil)
	if !bytes.Equal(compressedRistrettoBasepoint, roundtripBasepoint) {
		t.Error("decode<>encode roundtrip produced different results")
	}

	encodedBasepoint := ristrettoBasepoint.Encode(nil)
	if !bytes.Equal(compressedRistrettoBasepoint, encodedBasepoint) {
		t.Error("point encode produced different results")
	}
}

func TestRistrettoRandomRoundtrip(t *testing.T) {
	// TODO quickcheck
}

func TestRistrettoSmallMultiplesTestVectors(t *testing.T) {
	var testVectors = [16]string{
		// This is the identity point
		"0000000000000000000000000000000000000000000000000000000000000000",
		// This is the basepoint
		"e2f2ae0a6abc4e71a884a961c500515f58e30b6aa582dd8db6a65945e08d2d76",
		// These are small multiples of the basepoint
		"6a493210f7499cd17fecb510ae0cea23a110e8d5b901f8acadd3095c73a3b919",
		"94741f5d5d52755ece4f23f044ee27d5d1ea1e2bd196b462166b16152a9d0259",
		"da80862773358b466ffadfe0b3293ab3d9fd53c5ea6c955358f568322daf6a57",
		"e882b131016b52c1d3337080187cf768423efccbb517bb495ab812c4160ff44e",
		"f64746d3c92b13050ed8d80236a7f0007c3b3f962f5ba793d19a601ebb1df403",
		"44f53520926ec81fbd5a387845beb7df85a96a24ece18738bdcfa6a7822a176d",
		"903293d8f2287ebe10e2374dc1a53e0bc887e592699f02d077d5263cdd55601c",
		"02622ace8f7303a31cafc63f8fc48fdc16e1c8c8d234b2f0d6685282a9076031",
		"20706fd788b2720a1ed2a5dad4952b01f413bcf0e7564de8cdc816689e2db95f",
		"bce83f8ba5dd2fa572864c24ba1810f9522bc6004afe95877ac73241cafdab42",
		"e4549ee16b9aa03099ca208c67adafcafa4c3f3e4e5303de6026e3ca8ff84460",
		"aa52e000df2e16f55fb1032fc33bc42742dad6bd5a8fc0be0167436c5948501f",
		"46376b80f409b29dc2b5f6f0c52591990896e5716f41477cd30085ab7f10301e",
		"e0c418f7c8d9c4cdd7395b93ea124f3ad99021bb681dfc3302a9d99a2e53e64e",
	}

	basepointMultiple := Element{}
	basepointMultiple.Zero()

	for i := range testVectors {
		// Grab the bytes of the encoding
		encoding, err := hex.DecodeString(testVectors[i])
		if err != nil {
			t.Fatalf("#%d: bad hex encoding in test vector: %v", i, err)
		}

		// Decode the test vector to a ristretto255 element
		decodedPoint := Element{}
		err = decodedPoint.Decode(encoding)
		if err != nil {
			t.Error("Could not decode test vector")
		}
		// Re-encode and check round trips
		roundtripEncoding := decodedPoint.Encode(nil)
		if !bytes.Equal(encoding, roundtripEncoding) {
			t.Errorf("#%d: decode<>encode roundtrip failed", i)
		}

		// Check that the test vector encodes i * B
		if basepointMultiple.Equal(&decodedPoint) != 1 {
			t.Errorf("decoded small multiple %d * B is not %d * B", i, i)
		}
		// Ensure basepointMultiple = i * B in the next iteration
		basepointMultiple.Add(&basepointMultiple, &ristrettoBasepoint)
	}
}
<commit_msg>Update ristretto255_test.go<commit_after>package ristretto255

import (
	"bytes"
	"encoding/hex"
	"testing"

	"github.com/gtank/ristretto255/internal/edwards25519"
	"github.com/gtank/ristretto255/internal/radix51"
)

func assertFeEqual(value, expect *radix51.FieldElement) {
	if value.Equal(expect) == 1 {
		return
	} else {
		panic("failed equality assertion")
	}
}

type sqrtRatioTest struct {
	u, v     *radix51.FieldElement
	sqrt     *radix51.FieldElement
	choice   int
	negative int
}

// These tests can be found in curve25519-dalek's 'field.rs'
func TestSqrtRatioM1(t *testing.T) {
	var (
		zero, one = radix51.Zero, radix51.One

		// Two is nonsquare in our field, 4 is square
		two  = new(radix51.FieldElement).Add(one, one)
		four = new(radix51.FieldElement).Add(two, two)

		// 2*i
		twoTimesSqrtM1 = new(radix51.FieldElement).Mul(two, sqrtM1)

		sqrt2i = fieldElementFromDecimal(
			"38214883241950591754978413199355411911188925816896391856984770930832735035196")

		invSqrt4 = fieldElementFromDecimal(
			"28948022309329048855892746252171976963317496166410141009864396001978282409974")
	)

	// Check the construction of those magic numbers.
	assertFeEqual(new(radix51.FieldElement).Mul(sqrt2i, sqrt2i), twoTimesSqrtM1)
	assertFeEqual(new(radix51.FieldElement).Mul(new(radix51.FieldElement).Square(invSqrt4), four), one)

	var tests = []sqrtRatioTest{
		{u: zero, v: zero, sqrt: zero, choice: 1, negative: 0},    // 0
		{u: one, v: zero, sqrt: zero, choice: 0, negative: 0},     // 1
		{u: two, v: one, sqrt: sqrt2i, choice: 0, negative: 0},    // 2
		{u: four, v: one, sqrt: two, choice: 1, negative: 0},      // 3
		{u: one, v: four, sqrt: invSqrt4, choice: 1, negative: 0}, // 4
	}

	for idx, tt := range tests {
		sqrt := new(radix51.FieldElement)
		choice := feSqrtRatio(sqrt, tt.u, tt.v)
		if choice != tt.choice || sqrt.Equal(tt.sqrt) != 1 || sqrt.IsNegative() != tt.negative {
			t.Errorf("Failed test %d", idx)
			t.Logf("Got {u: %v, v: %v, sqrt: %v, choice: %d, neg: %d}", tt.u, tt.v, sqrt, choice, sqrt.IsNegative())
		}
	}
}

var (
	// The encoding of Ristretto element that can be represented internally by the Curve25519 base point.
	compressedRistrettoBasepoint, _ = hex.DecodeString("e2f2ae0a6abc4e71a884a961c500515f58e30b6aa582dd8db6a65945e08d2d76")

	// The representative Ristretto basepoint in extended coordinates.
	ristrettoBasepoint = Element{r: edwards25519.ExtendedGroupElement{
		X: radix51.FieldElement([5]uint64{426475514619346, 2063872706840040, 14628272888959, 107677749330612, 288339085807592}),
		Y: radix51.FieldElement([5]uint64{1934594822876571, 2049809580636559, 1991994783322914, 1758681962032007, 380046701118659}),
		Z: radix51.FieldElement([5]uint64{1, 0, 0, 0, 0}),
		T: radix51.FieldElement([5]uint64{410445769351754, 2235400917701188, 1495825632738689, 1351628537510093, 430502003771208}),
	}}
)

func TestRistrettoBasepointRoundTrip(t *testing.T) {
	decodedBasepoint := &Element{}
	err := decodedBasepoint.Decode(compressedRistrettoBasepoint)
	if err != nil {
		t.Fatal(err)
	}

	if decodedBasepoint.Equal(&ristrettoBasepoint) != 1 {
		t.Error("decode succeeded, but got wrong point")
	}

	roundtripBasepoint := decodedBasepoint.Encode(nil)
	if !bytes.Equal(compressedRistrettoBasepoint, roundtripBasepoint) {
		t.Error("decode<>encode roundtrip produced different results")
	}

	encodedBasepoint := ristrettoBasepoint.Encode(nil)
	if !bytes.Equal(compressedRistrettoBasepoint, encodedBasepoint) {
		t.Error("point encode produced different results")
	}
}

func TestRistrettoRandomRoundtrip(t *testing.T) {
	// TODO quickcheck
}

func TestRistrettoSmallMultiplesTestVectors(t *testing.T) {
	var testVectors = [16]string{
		// This is the identity point
		"0000000000000000000000000000000000000000000000000000000000000000",
		// This is the basepoint
		"e2f2ae0a6abc4e71a884a961c500515f58e30b6aa582dd8db6a65945e08d2d76",
		// These are small multiples of the basepoint
		"6a493210f7499cd17fecb510ae0cea23a110e8d5b901f8acadd3095c73a3b919",
		"94741f5d5d52755ece4f23f044ee27d5d1ea1e2bd196b462166b16152a9d0259",
		"da80862773358b466ffadfe0b3293ab3d9fd53c5ea6c955358f568322daf6a57",
		"e882b131016b52c1d3337080187cf768423efccbb517bb495ab812c4160ff44e",
		"f64746d3c92b13050ed8d80236a7f0007c3b3f962f5ba793d19a601ebb1df403",
		"44f53520926ec81fbd5a387845beb7df85a96a24ece18738bdcfa6a7822a176d",
		"903293d8f2287ebe10e2374dc1a53e0bc887e592699f02d077d5263cdd55601c",
		"02622ace8f7303a31cafc63f8fc48fdc16e1c8c8d234b2f0d6685282a9076031",
		"20706fd788b2720a1ed2a5dad4952b01f413bcf0e7564de8cdc816689e2db95f",
		"bce83f8ba5dd2fa572864c24ba1810f9522bc6004afe95877ac73241cafdab42",
		"e4549ee16b9aa03099ca208c67adafcafa4c3f3e4e5303de6026e3ca8ff84460",
		"aa52e000df2e16f55fb1032fc33bc42742dad6bd5a8fc0be0167436c5948501f",
		"46376b80f409b29dc2b5f6f0c52591990896e5716f41477cd30085ab7f10301e",
		"e0c418f7c8d9c4cdd7395b93ea124f3ad99021bb681dfc3302a9d99a2e53e64e",
	}

	basepointMultiple := Element{}
	basepointMultiple.Zero()

	for i := range testVectors {
		// Grab the bytes of the encoding
		encoding, err := hex.DecodeString(testVectors[i])
		if err != nil {
			t.Fatalf("#%d: bad hex encoding in test vector: %v", i, err)
		}

		// Decode the test vector to a ristretto255 element
		decodedPoint := Element{}
		err = decodedPoint.Decode(encoding)
		if err != nil {
			t.Fatalf("#%d: could not decode test vector: %v", i, err)
		}
		// Re-encode and check round trips
		roundtripEncoding := decodedPoint.Encode(nil)
		if !bytes.Equal(encoding, roundtripEncoding) {
			t.Errorf("#%d: decode<>encode roundtrip failed", i)
		}

		// Check that the test vector encodes i * B
		if basepointMultiple.Equal(&decodedPoint) != 1 {
			t.Errorf("decoded small multiple %d * B is not %d * B", i, i)
		}
		// Ensure basepointMultiple = i * B in the next iteration
		basepointMultiple.Add(&basepointMultiple, &ristrettoBasepoint)
	}
}
<|endoftext|>


<commit_before>// Copyright 2012 Dorival de Moraes Pedroso. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package rnd

import (
	"sort"
	"testing"
	"time"

	"github.com/cpmech/gosl/chk"
	"github.com/cpmech/gosl/io"
	"github.com/cpmech/gosl/utl"
)

const NSAMPLES = 1000

//const NSAMPLES = 10

func Test_GOint01(tst *testing.T) {

	//verbose()
	chk.PrintTitle("GOint01. integers")

	Init(1234)

	nints := 10
	vals := make([]int, NSAMPLES)

	// using Int
	t0 := time.Now()
	for i := 0; i < NSAMPLES; i++ {
		vals[i] = Int(0, nints-1)
	}
	io.Pforan("time elapsed = %v\n", time.Now().Sub(t0))

	hist := IntHistogram{Stations: utl.IntRange(nints + 1)}
	hist.Count(vals, true)
	io.Pfyel(TextHist(hist.GenLabels("%d"), hist.Counts, 60))

	// using Ints
	t0 = time.Now()
	Ints(vals, 0, nints-1)
	io.Pforan("time elapsed = %v\n", time.Now().Sub(t0))

	hist.Count(vals, true)
	io.Pfcyan(TextHist(hist.GenLabels("%d"), hist.Counts, 60))
}

func Test_MTint01(tst *testing.T) {

	//verbose()
	chk.PrintTitle("MTint01. integers (Mersenne Twister)")

	Init(1234)

	nints := 10
	vals := make([]int, NSAMPLES)

	// using MTint
	t0 := time.Now()
	for i := 0; i < NSAMPLES; i++ {
		vals[i] = MTint(0, nints-1)
	}
	io.Pforan("time elapsed = %v\n", time.Now().Sub(t0))

	hist := IntHistogram{Stations: utl.IntRange(nints + 1)}
	hist.Count(vals, true)
	io.Pfyel(TextHist(hist.GenLabels("%d"), hist.Counts, 60))

	// using MTints
	t0 = time.Now()
	MTints(vals, 0, nints-1)
	io.Pforan("time elapsed = %v\n", time.Now().Sub(t0))

	hist.Count(vals, true)
	io.Pfcyan(TextHist(hist.GenLabels("%d"), hist.Counts, 60))
}

func Test_GOflt01(tst *testing.T) {

	//verbose()
	chk.PrintTitle("GOflt01. float64")

	Init(1234)

	xmin := 10.0
	xmax := 20.0
	vals := make([]float64, NSAMPLES)

	// using Float64
	t0 := time.Now()
	for i := 0; i < NSAMPLES; i++ {
		vals[i] = Float64(xmin, xmax)
	}
	io.Pforan("time elapsed = %v\n", time.Now().Sub(t0))

	hist := Histogram{Stations: []float64{10, 12.5, 15, 17.5, 20}}
	hist.Count(vals, true)
	io.Pfpink(TextHist(hist.GenLabels("%4g"), hist.Counts, 60))

	// using Float64s
	t0 = time.Now()
	Float64s(vals, xmin, xmax)
	io.Pforan("time elapsed = %v\n", time.Now().Sub(t0))

	hist.Count(vals, true)
	io.Pfblue2(TextHist(hist.GenLabels("%4g"), hist.Counts, 60))
}

func Test_MTflt01(tst *testing.T) {

	//verbose()
	chk.PrintTitle("MTflt01. float64 (Mersenne Twister)")

	Init(1234)

	xmin := 10.0
	xmax := 20.0
	vals := make([]float64, NSAMPLES)

	// using MTfloat64
	t0 := time.Now()
	for i := 0; i < NSAMPLES; i++ {
		vals[i] = MTfloat64(xmin, xmax)
	}
	io.Pforan("time elapsed = %v\n", time.Now().Sub(t0))

	hist := Histogram{Stations: []float64{10, 12.5, 15, 17.5, 20}}
	hist.Count(vals, true)
	io.Pfpink(TextHist(hist.GenLabels("%4g"), hist.Counts, 60))

	// using MTfloat64s
	t0 = time.Now()
	MTfloat64s(vals, xmin, xmax)
	io.Pforan("time elapsed = %v\n", time.Now().Sub(t0))

	hist.Count(vals, true)
	io.Pfblue2(TextHist(hist.GenLabels("%4g"), hist.Counts, 60))
}

func Test_flip01(tst *testing.T) {

	//verbose()
	chk.PrintTitle("flip01")

	Init(1234)

	p := 0.5
	nsamples := 100
	ntrue := 0
	nfalse := 0
	for i := 0; i < nsamples; i++ {
		if FlipCoin(p) {
			ntrue++
		} else {
			nfalse++
		}
	}

	io.Pforan("ntrue  = %v (42)\n", ntrue)
	io.Pforan("nfalse = %v (58)\n", nfalse)
}

func Test_GOshuffleInts01(tst *testing.T) {

	//verbose()
	chk.PrintTitle("GOshuffleInts01")

	Init(0)

	n := 10
	nums := utl.IntRange(n)
	io.Pfgreen("before = %v\n", nums)
	IntShuffle(nums)
	io.Pfcyan("after  = %v\n", nums)

	sort.Ints(nums)
	io.Pforan("sorted = %v\n", nums)
	chk.Ints(tst, "nums", nums, utl.IntRange(n))

	shufled := IntGetShuffled(nums)
	io.Pfyel("shufled = %v\n", shufled)
	sort.Ints(shufled)
	chk.Ints(tst, "shufled", shufled, utl.IntRange(n))
}

func Test_MTshuffleInts01(tst *testing.T) {

	//verbose()
	chk.PrintTitle("MTshuffleInts01. Mersenne Twister")

	Init(0)

	n := 10
	nums := utl.IntRange(n)
	io.Pfgreen("before = %v\n", nums)
	MTintShuffle(nums)
	io.Pfcyan("after  = %v\n", nums)

	sort.Ints(nums)
	io.Pforan("sorted = %v\n", nums)
	chk.Ints(tst, "nums", nums, utl.IntRange(n))
}

func Test_getunique01(tst *testing.T) {

	//verbose()
	chk.PrintTitle("getunique01")

	Init(0)

	nsel := 5 // number of selections
	size := 10
	nums := utl.IntRange(size)
	hist := IntHistogram{Stations: utl.IntRange(size + 5)}
	sel := IntGetUnique(nums, nsel)
	io.Pfgreen("nums = %v\n", nums)
	io.Pfcyan("sel  = %v\n", sel)
	for i := 0; i < NSAMPLES; i++ {
		sel := IntGetUnique(nums, nsel)
		check_repeated(sel)
		hist.Count(sel, false)
		//io.Pfgrey("sel  = %v\n", sel)
	}

	io.Pf(TextHist(hist.GenLabels("%d"), hist.Counts, 60))
}

func Test_getunique02(tst *testing.T) {

	//verbose()
	chk.PrintTitle("getunique02")

	Init(0)

	nsel := 5 // number of selections
	start := 2
	size := 10
	hist := IntHistogram{Stations: utl.IntRange(size + 3)}
	sel := IntGetUniqueN(start, size, nsel)
	io.Pfcyan("sel  = %v\n", sel)
	for i := 0; i < NSAMPLES; i++ {
		sel := IntGetUniqueN(start, size, nsel)
		check_repeated(sel)
		hist.Count(sel, false)
		//io.Pfgrey("sel  = %v\n", sel)
	}

	io.Pf(TextHist(hist.GenLabels("%d"), hist.Counts, 60))
}

func check_repeated(v []int) {
	for i := 1; i < len(v); i++ {
		if v[i] == v[i-1] {
			chk.Panic("there are repeated entries in v = %v", v)
		}
	}
}
<commit_msg>type fixed<commit_after>// Copyright 2012 Dorival de Moraes Pedroso. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package rnd

import (
	"sort"
	"testing"
	"time"

	"github.com/cpmech/gosl/chk"
	"github.com/cpmech/gosl/io"
	"github.com/cpmech/gosl/utl"
)

const NSAMPLES = 1000

//const NSAMPLES = 10

func Test_GOint01(tst *testing.T) {

	//verbose()
	chk.PrintTitle("GOint01. integers")

	Init(1234)

	nints := 10
	vals := make([]int, NSAMPLES)

	// using Int
	t0 := time.Now()
	for i := 0; i < NSAMPLES; i++ {
		vals[i] = Int(0, nints-1)
	}
	io.Pforan("time elapsed = %v\n", time.Now().Sub(t0))

	hist := IntHistogram{Stations: utl.IntRange(nints + 1)}
	hist.Count(vals, true)
	io.Pfyel(TextHist(hist.GenLabels("%d"), hist.Counts, 60))

	// using Ints
	t0 = time.Now()
	Ints(vals, 0, nints-1)
	io.Pforan("time elapsed = %v\n", time.Now().Sub(t0))

	hist.Count(vals, true)
	io.Pfcyan(TextHist(hist.GenLabels("%d"), hist.Counts, 60))
}

func Test_MTint01(tst *testing.T) {

	//verbose()
	chk.PrintTitle("MTint01. integers (Mersenne Twister)")

	Init(1234)

	nints := 10
	vals := make([]int, NSAMPLES)

	// using MTint
	t0 := time.Now()
	for i := 0; i < NSAMPLES; i++ {
		vals[i] = MTint(0, nints-1)
	}
	io.Pforan("time elapsed = %v\n", time.Now().Sub(t0))

	hist := IntHistogram{Stations: utl.IntRange(nints + 1)}
	hist.Count(vals, true)
	io.Pfyel(TextHist(hist.GenLabels("%d"), hist.Counts, 60))

	// using MTints
	t0 = time.Now()
	MTints(vals, 0, nints-1)
	io.Pforan("time elapsed = %v\n", time.Now().Sub(t0))

	hist.Count(vals, true)
	io.Pfcyan(TextHist(hist.GenLabels("%d"), hist.Counts, 60))
}

func Test_GOflt01(tst *testing.T) {

	//verbose()
	chk.PrintTitle("GOflt01. float64")

	Init(1234)

	xmin := 10.0
	xmax := 20.0
	vals := make([]float64, NSAMPLES)

	// using Float64
	t0 := time.Now()
	for i := 0; i < NSAMPLES; i++ {
		vals[i] = Float64(xmin, xmax)
	}
	io.Pforan("time elapsed = %v\n", time.Now().Sub(t0))

	hist := Histogram{Stations: []float64{10, 12.5, 15, 17.5, 20}}
	hist.Count(vals, true)
	io.Pfpink(TextHist(hist.GenLabels("%4g"), hist.Counts, 60))

	// using Float64s
	t0 = time.Now()
	Float64s(vals, xmin, xmax)
	io.Pforan("time elapsed = %v\n", time.Now().Sub(t0))

	hist.Count(vals, true)
	io.Pfblue2(TextHist(hist.GenLabels("%4g"), hist.Counts, 60))
}

func Test_MTflt01(tst *testing.T) {

	//verbose()
	chk.PrintTitle("MTflt01. float64 (Mersenne Twister)")

	Init(1234)

	xmin := 10.0
	xmax := 20.0
	vals := make([]float64, NSAMPLES)

	// using MTfloat64
	t0 := time.Now()
	for i := 0; i < NSAMPLES; i++ {
		vals[i] = MTfloat64(xmin, xmax)
	}
	io.Pforan("time elapsed = %v\n", time.Now().Sub(t0))

	hist := Histogram{Stations: []float64{10, 12.5, 15, 17.5, 20}}
	hist.Count(vals, true)
	io.Pfpink(TextHist(hist.GenLabels("%4g"), hist.Counts, 60))

	// using MTfloat64s
	t0 = time.Now()
	MTfloat64s(vals, xmin, xmax)
	io.Pforan("time elapsed = %v\n", time.Now().Sub(t0))

	hist.Count(vals, true)
	io.Pfblue2(TextHist(hist.GenLabels("%4g"), hist.Counts, 60))
}

func Test_flip01(tst *testing.T) {

	//verbose()
	chk.PrintTitle("flip01")

	Init(1234)

	p := 0.5
	nsamples := 100
	ntrue := 0
	nfalse := 0
	for i := 0; i < nsamples; i++ {
		if FlipCoin(p) {
			ntrue++
		} else {
			nfalse++
		}
	}

	io.Pforan("ntrue  = %v (42)\n", ntrue)
	io.Pforan("nfalse = %v (58)\n", nfalse)
}

func Test_GOshuffleInts01(tst *testing.T) {

	//verbose()
	chk.PrintTitle("GOshuffleInts01")

	Init(0)

	n := 10
	nums := utl.IntRange(n)
	io.Pfgreen("before = %v\n", nums)
	IntShuffle(nums)
	io.Pfcyan("after  = %v\n", nums)

	sort.Ints(nums)
	io.Pforan("sorted = %v\n", nums)
	chk.Ints(tst, "nums", nums, utl.IntRange(n))

	shufled := IntGetShuffled(nums)
	io.Pfyel("shufled = %v\n", shufled)
	sort.Ints(shufled)
	chk.Ints(tst, "shufled", shufled, utl.IntRange(n))
}

func Test_MTshuffleInts01(tst *testing.T) {

	//verbose()
	chk.PrintTitle("MTshuffleInts01. Mersenne Twister")

	Init(0)

	n := 10
	nums := utl.IntRange(n)
	io.Pfgreen("before = %v\n", nums)
	MTintShuffle(nums)
	io.Pfcyan("after  = %v\n", nums)

	sort.Ints(nums)
	io.Pforan("sorted = %v\n", nums)
	chk.Ints(tst, "nums", nums, utl.IntRange(n))
}

func Test_getunique01(tst *testing.T) {

	//verbose()
	chk.PrintTitle("getunique01")

	Init(0)

	nsel := 5 // number of selections
	size := 10
	nums := utl.IntRange(size)
	hist := IntHistogram{Stations: utl.IntRange(size + 5)}
	sel := IntGetUnique(nums, nsel)
	io.Pfgreen("nums = %v\n", nums)
	io.Pfcyan("sel  = %v\n", sel)
	for i := 0; i < NSAMPLES; i++ {
		sel := IntGetUnique(nums, nsel)
		check_repeated(sel)
		hist.Count(sel, false)
		//io.Pfgrey("sel  = %v\n", sel)
	}

	io.Pf(TextHist(hist.GenLabels("%d"), hist.Counts, 60))
}

func Test_getunique02(tst *testing.T) {

	//verbose()
	chk.PrintTitle("getunique02")

	Init(0)

	nsel := 5 // number of selections
	start := 2
	endp1 := 10
	hist := IntHistogram{Stations: utl.IntRange(endp1 + 3)}
	sel := IntGetUniqueN(start, endp1, nsel)
	io.Pfcyan("sel  = %v\n", sel)
	for i := 0; i < NSAMPLES; i++ {
		sel := IntGetUniqueN(start, endp1, nsel)
		check_repeated(sel)
		hist.Count(sel, false)
		//io.Pfgrey("sel  = %v\n", sel)
	}

	io.Pf(TextHist(hist.GenLabels("%d"), hist.Counts, 60))
}

func check_repeated(v []int) {
	for i := 1; i < len(v); i++ {
		if v[i] == v[i-1] {
			chk.Panic("there are repeated entries in v = %v", v)
		}
	}
}
<|endoftext|>


<commit_before>package namer

import (
	"encoding/json"
	"fmt"
	"io"
	"io/ioutil"
	"net/http"
	"net/url"
	"strings"
)

// XXX later we should add support for parsing dtabs

type (
	Version       string
	VersionedDtab struct {
		Version Version `json:"version"`
		Dtab    Dtab    `json:"dtab"`
	}

	Controller interface {
		List() ([]string, error)
		Get(name string) (*VersionedDtab, error)
		Create(name string, dtabstr string) (Version, error)
		Delete(name string) error
		Update(name string, dtabstr string, version Version) (Version, error)
	}

	httpController struct {
		baseURL *url.URL
		client  *http.Client
	}
)

func NewHttpController(baseURL *url.URL, client *http.Client) Controller {
	return &httpController{baseURL, client}
}

func (ctl *httpController) dtabRequest(method, name string, data io.Reader) (*http.Request, error) {
	u := *ctl.baseURL
	if name == "" {
		u.Path = "/api/1/dtabs"
	} else {
		u.Path = fmt.Sprintf("/api/1/dtabs/%s", name)
	}
	return http.NewRequest(method, u.String(), data)
}

func (ctl *httpController) List() ([]string, error) {
	req, err := ctl.dtabRequest("GET", "", nil)
	if err != nil {
		return nil, err
	}
	req.Header.Set("Accept", "application/json")

	rsp, err := ctl.client.Do(req)
	if err != nil {
		return nil, err
	}
	defer drainAndClose(rsp)

	switch rsp.StatusCode {
	case http.StatusOK:
		var names []string
		if err := json.NewDecoder(rsp.Body).Decode(&names); err != nil {
			return nil, err
		}

		return names, nil

	default:
		return nil, fmt.Errorf("unexpected response: %s", rsp.Status)
	}
}

func (ctl *httpController) Get(name string) (*VersionedDtab, error) {
	req, err := ctl.dtabRequest("GET", name, nil)
	if err != nil {
		return nil, err
	}
	req.Header.Set("Accept", "application/json")

	rsp, err := ctl.client.Do(req)
	if err != nil {
		return nil, err
	}
	defer drainAndClose(rsp)

	switch rsp.StatusCode {
	case http.StatusOK:
		dtab := VersionedDtab{
			Version: Version(rsp.Header.Get("ETag")),
		}
		if err := json.NewDecoder(rsp.Body).Decode(&dtab.Dtab); err != nil {
			return nil, err
		}
		return &dtab, nil

	default:
		return nil, fmt.Errorf("unexpected response: %s", rsp.Status)
	}
}

func isJson(str string) bool {
	return len(str) > 0 && (str[0:1] == "{" || str[0:1] == "[")
}

func (ctl *httpController) Create(name, dtabstr string) (Version, error) {
	emptyVersion := Version("")
	var req *http.Request
	if isJson(dtabstr) {
		var vdtab VersionedDtab
		if err := json.Unmarshal([]byte(dtabstr), &vdtab); err != nil {
			return emptyVersion, err
		}
		dtab, err := json.Marshal(vdtab.Dtab)
		if err != nil {
			return emptyVersion, err
		}
		req, err = ctl.dtabRequest("POST", name, strings.NewReader(string(dtab)))
		if err != nil {
			return emptyVersion, err
		}
		req.Header.Set("Content-Type", "application/json")
	} else {
		req, err := ctl.dtabRequest("POST", name, strings.NewReader(dtabstr))
		if err != nil {
			return emptyVersion, err
		}
		req.Header.Set("Content-Type", "application/dtab")
	}

	rsp, err := ctl.client.Do(req)
	if err != nil {
		return emptyVersion, err
	}
	defer drainAndClose(rsp)

	switch rsp.StatusCode {
	case http.StatusOK, http.StatusCreated, http.StatusNoContent:
		v := Version(rsp.Header.Get("ETag"))
		return v, nil

	default:
		return emptyVersion, fmt.Errorf("unexpected response: %s", rsp.Status)
	}
}

func (ctl *httpController) Delete(name string) error {

	req, err := ctl.dtabRequest("DELETE", name, nil)
	if err != nil {
		return err
	}
	rsp, err := ctl.client.Do(req)
	if err != nil {
		return err
	}
	defer drainAndClose(rsp)

	switch rsp.StatusCode {
	case http.StatusOK, http.StatusCreated, http.StatusNoContent:
		return nil

	default:
		return fmt.Errorf("unexpected response: %s", rsp.Status)
	}
}

func (ctl *httpController) Update(name, dtabstr string, version Version) (Version, error) {
	useJson := isJson(dtabstr)
	if useJson {
		var vdtab VersionedDtab
		if err := json.Unmarshal([]byte(dtabstr), &vdtab); err != nil {
			return Version(""), err
		}

		dtab, err := json.Marshal(vdtab.Dtab)
		if err != nil {
			return Version(""), err
		}

		dtabstr = string(dtab)
		if vdtab.Version != "" {
			version = vdtab.Version
		}
	}

	req, err := ctl.dtabRequest("PUT", name, strings.NewReader(dtabstr))
	if err != nil {
		return Version(""), err
	}
	if version != "" {
		req.Header.Set("If-Match", string(version))
	}

	if useJson {
		req.Header.Set("Content-Type", "application/json")
	} else {
		req.Header.Set("Content-Type", "application/dtab")
	}

	rsp, err := ctl.client.Do(req)
	if err != nil {
		return Version(""), err
	}
	defer drainAndClose(rsp)

	switch rsp.StatusCode {
	case http.StatusOK, http.StatusCreated, http.StatusNoContent:
		v := Version(rsp.Header.Get("ETag"))
		return v, nil

	default:
		return Version(""), fmt.Errorf("unexpected response: %s", rsp.Status)
	}
}

func drainAndClose(resp *http.Response) {
	if resp != nil {
		io.Copy(ioutil.Discard, resp.Body)
		resp.Body.Close()
	}
}
<commit_msg>Fix a scope bug where req would revert to nil when creating a dtab causing a panic (#15)<commit_after>package namer

import (
	"encoding/json"
	"fmt"
	"io"
	"io/ioutil"
	"net/http"
	"net/url"
	"strings"
)

// XXX later we should add support for parsing dtabs

type (
	Version       string
	VersionedDtab struct {
		Version Version `json:"version"`
		Dtab    Dtab    `json:"dtab"`
	}

	Controller interface {
		List() ([]string, error)
		Get(name string) (*VersionedDtab, error)
		Create(name string, dtabstr string) (Version, error)
		Delete(name string) error
		Update(name string, dtabstr string, version Version) (Version, error)
	}

	httpController struct {
		baseURL *url.URL
		client  *http.Client
	}
)

func NewHttpController(baseURL *url.URL, client *http.Client) Controller {
	return &httpController{baseURL, client}
}

func (ctl *httpController) dtabRequest(method, name string, data io.Reader) (*http.Request, error) {
	u := *ctl.baseURL
	if name == "" {
		u.Path = "/api/1/dtabs"
	} else {
		u.Path = fmt.Sprintf("/api/1/dtabs/%s", name)
	}
	return http.NewRequest(method, u.String(), data)
}

func (ctl *httpController) List() ([]string, error) {
	req, err := ctl.dtabRequest("GET", "", nil)
	if err != nil {
		return nil, err
	}
	req.Header.Set("Accept", "application/json")

	rsp, err := ctl.client.Do(req)
	if err != nil {
		return nil, err
	}
	defer drainAndClose(rsp)

	switch rsp.StatusCode {
	case http.StatusOK:
		var names []string
		if err := json.NewDecoder(rsp.Body).Decode(&names); err != nil {
			return nil, err
		}

		return names, nil

	default:
		return nil, fmt.Errorf("unexpected response: %s", rsp.Status)
	}
}

func (ctl *httpController) Get(name string) (*VersionedDtab, error) {
	req, err := ctl.dtabRequest("GET", name, nil)
	if err != nil {
		return nil, err
	}
	req.Header.Set("Accept", "application/json")

	rsp, err := ctl.client.Do(req)
	if err != nil {
		return nil, err
	}
	defer drainAndClose(rsp)

	switch rsp.StatusCode {
	case http.StatusOK:
		dtab := VersionedDtab{
			Version: Version(rsp.Header.Get("ETag")),
		}
		if err := json.NewDecoder(rsp.Body).Decode(&dtab.Dtab); err != nil {
			return nil, err
		}
		return &dtab, nil

	default:
		return nil, fmt.Errorf("unexpected response: %s", rsp.Status)
	}
}

func isJson(str string) bool {
	return len(str) > 0 && (str[0:1] == "{" || str[0:1] == "[")
}

func (ctl *httpController) Create(name, dtabstr string) (Version, error) {
	emptyVersion := Version("")
	var req *http.Request
	var err error
	if isJson(dtabstr) {
		var vdtab VersionedDtab
		if err := json.Unmarshal([]byte(dtabstr), &vdtab); err != nil {
			return emptyVersion, err
		}
		dtab, err := json.Marshal(vdtab.Dtab)
		if err != nil {
			return emptyVersion, err
		}
		req, err = ctl.dtabRequest("POST", name, strings.NewReader(string(dtab)))
		if err != nil {
			return emptyVersion, err
		}
		req.Header.Set("Content-Type", "application/json")
	} else {
		req, err = ctl.dtabRequest("POST", name, strings.NewReader(dtabstr))
		if err != nil {
			return emptyVersion, err
		}
		req.Header.Set("Content-Type", "application/dtab")
	}

	rsp, err := ctl.client.Do(req)
	if err != nil {
		return emptyVersion, err
	}
	defer drainAndClose(rsp)

	switch rsp.StatusCode {
	case http.StatusOK, http.StatusCreated, http.StatusNoContent:
		v := Version(rsp.Header.Get("ETag"))
		return v, nil

	default:
		return emptyVersion, fmt.Errorf("unexpected response: %s", rsp.Status)
	}
}

func (ctl *httpController) Delete(name string) error {

	req, err := ctl.dtabRequest("DELETE", name, nil)
	if err != nil {
		return err
	}
	rsp, err := ctl.client.Do(req)
	if err != nil {
		return err
	}
	defer drainAndClose(rsp)

	switch rsp.StatusCode {
	case http.StatusOK, http.StatusCreated, http.StatusNoContent:
		return nil

	default:
		return fmt.Errorf("unexpected response: %s", rsp.Status)
	}
}

func (ctl *httpController) Update(name, dtabstr string, version Version) (Version, error) {
	useJson := isJson(dtabstr)
	if useJson {
		var vdtab VersionedDtab
		if err := json.Unmarshal([]byte(dtabstr), &vdtab); err != nil {
			return Version(""), err
		}

		dtab, err := json.Marshal(vdtab.Dtab)
		if err != nil {
			return Version(""), err
		}

		dtabstr = string(dtab)
		if vdtab.Version != "" {
			version = vdtab.Version
		}
	}

	req, err := ctl.dtabRequest("PUT", name, strings.NewReader(dtabstr))
	if err != nil {
		return Version(""), err
	}
	if version != "" {
		req.Header.Set("If-Match", string(version))
	}

	if useJson {
		req.Header.Set("Content-Type", "application/json")
	} else {
		req.Header.Set("Content-Type", "application/dtab")
	}

	rsp, err := ctl.client.Do(req)
	if err != nil {
		return Version(""), err
	}
	defer drainAndClose(rsp)

	switch rsp.StatusCode {
	case http.StatusOK, http.StatusCreated, http.StatusNoContent:
		v := Version(rsp.Header.Get("ETag"))
		return v, nil

	default:
		return Version(""), fmt.Errorf("unexpected response: %s", rsp.Status)
	}
}

func drainAndClose(resp *http.Response) {
	if resp != nil {
		io.Copy(ioutil.Discard, resp.Body)
		resp.Body.Close()
	}
}
<|endoftext|>


<commit_before>package ethchain

import (
	"fmt"
	"math/big"

	"github.com/ethereum/go-ethereum/ethstate"
	"github.com/ethereum/go-ethereum/ethtrie"
	"github.com/ethereum/go-ethereum/ethutil"
	"github.com/ethereum/go-ethereum/vm"
)

/*
 * The State transitioning model
 *
 * A state transition is a change made when a transaction is applied to the current world state
 * The state transitioning model does all all the necessary work to work out a valid new state root.
 * 1) Nonce handling
 * 2) Pre pay / buy gas of the coinbase (miner)
 * 3) Create a new state object if the recipient is \0*32
 * 4) Value transfer
 * == If contract creation ==
 * 4a) Attempt to run transaction data
 * 4b) If valid, use result as code for the new state object
 * == end ==
 * 5) Run Script section
 * 6) Derive new state root
 */
type StateTransition struct {
	coinbase, receiver []byte
	tx                 *Transaction
	gas, gasPrice      *big.Int
	value              *big.Int
	data               []byte
	state              *ethstate.State
	block              *Block

	cb, rec, sen *ethstate.StateObject
}

func NewStateTransition(coinbase *ethstate.StateObject, tx *Transaction, state *ethstate.State, block *Block) *StateTransition {
	return &StateTransition{coinbase.Address(), tx.Recipient, tx, new(big.Int), new(big.Int).Set(tx.GasPrice), tx.Value, tx.Data, state, block, coinbase, nil, nil}
}

func (self *StateTransition) Coinbase() *ethstate.StateObject {
	if self.cb != nil {
		return self.cb
	}

	self.cb = self.state.GetOrNewStateObject(self.coinbase)
	return self.cb
}
func (self *StateTransition) Sender() *ethstate.StateObject {
	if self.sen != nil {
		return self.sen
	}

	self.sen = self.state.GetOrNewStateObject(self.tx.Sender())

	return self.sen
}
func (self *StateTransition) Receiver() *ethstate.StateObject {
	if self.tx != nil && self.tx.CreatesContract() {
		return nil
	}

	if self.rec != nil {
		return self.rec
	}

	self.rec = self.state.GetOrNewStateObject(self.tx.Recipient)
	return self.rec
}

func (self *StateTransition) UseGas(amount *big.Int) error {
	if self.gas.Cmp(amount) < 0 {
		return OutOfGasError()
	}
	self.gas.Sub(self.gas, amount)

	return nil
}

func (self *StateTransition) AddGas(amount *big.Int) {
	self.gas.Add(self.gas, amount)
}

func (self *StateTransition) BuyGas() error {
	var err error

	sender := self.Sender()
	if sender.Balance().Cmp(self.tx.GasValue()) < 0 {
		return fmt.Errorf("Insufficient funds to pre-pay gas. Req %v, has %v", self.tx.GasValue(), sender.Balance())
	}

	coinbase := self.Coinbase()
	err = coinbase.BuyGas(self.tx.Gas, self.tx.GasPrice)
	if err != nil {
		return err
	}

	self.AddGas(self.tx.Gas)
	sender.SubAmount(self.tx.GasValue())

	return nil
}

func (self *StateTransition) RefundGas() {
	coinbase, sender := self.Coinbase(), self.Sender()
	coinbase.RefundGas(self.gas, self.tx.GasPrice)

	// Return remaining gas
	remaining := new(big.Int).Mul(self.gas, self.tx.GasPrice)
	sender.AddAmount(remaining)
}

func (self *StateTransition) preCheck() (err error) {
	var (
		tx     = self.tx
		sender = self.Sender()
	)

	// Make sure this transaction's nonce is correct
	if sender.Nonce != tx.Nonce {
		return NonceError(tx.Nonce, sender.Nonce)
	}

	// Pre-pay gas / Buy gas of the coinbase account
	if err = self.BuyGas(); err != nil {
		return err
	}

	return nil
}

func (self *StateTransition) TransitionState() (err error) {
	statelogger.Debugf("(~) %x\n", self.tx.Hash())

	defer func() {
		if r := recover(); r != nil {
			statelogger.Infoln(r)
			err = fmt.Errorf("state transition err %v", r)
		}
	}()

	// XXX Transactions after this point are considered valid.
	if err = self.preCheck(); err != nil {
		return
	}

	var (
		tx       = self.tx
		sender   = self.Sender()
		receiver *ethstate.StateObject
	)

	defer self.RefundGas()

	// Increment the nonce for the next transaction
	sender.Nonce += 1

	// Transaction gas
	if err = self.UseGas(vm.GasTx); err != nil {
		return
	}

	// Pay data gas
	dataPrice := big.NewInt(int64(len(self.data)))
	dataPrice.Mul(dataPrice, vm.GasData)
	if err = self.UseGas(dataPrice); err != nil {
		return
	}

	if sender.Balance().Cmp(self.value) < 0 {
		return fmt.Errorf("Insufficient funds to transfer value. Req %v, has %v", self.value, sender.Balance)
	}

	var snapshot *ethstate.State
	// If the receiver is nil it's a contract (\0*32).
	if tx.CreatesContract() {
		// Subtract the (irreversible) amount from the senders account
		sender.SubAmount(self.value)

		snapshot = self.state.Copy()

		// Create a new state object for the contract
		receiver := MakeContract(tx, self.state)
		self.rec = receiver
		if receiver == nil {
			return fmt.Errorf("Unable to create contract")
		}

		// Add the amount to receivers account which should conclude this transaction
		receiver.AddAmount(self.value)
	} else {
		receiver = self.Receiver()

		// Subtract the amount from the senders account
		sender.SubAmount(self.value)
		// Add the amount to receivers account which should conclude this transaction
		receiver.AddAmount(self.value)

		snapshot = self.state.Copy()
	}

	msg := self.state.Manifest().AddMessage(&ethstate.Message{
		To: receiver.Address(), From: sender.Address(),
		Input:  self.tx.Data,
		Origin: sender.Address(),
		Block:  self.block.Hash(), Timestamp: self.block.Time, Coinbase: self.block.Coinbase, Number: self.block.Number,
		Value: self.value,
	})

	// Process the init code and create 'valid' contract
	if IsContractAddr(self.receiver) {
		// Evaluate the initialization script
		// and use the return value as the
		// script section for the state object.
		self.data = nil

		code, err := self.Eval(msg, receiver.Init(), receiver)
		if err != nil {
			self.state.Set(snapshot)

			return fmt.Errorf("Error during init execution %v", err)
		}

		receiver.Code = code
		msg.Output = code
	} else {
		if len(receiver.Code) > 0 {
			ret, err := self.Eval(msg, receiver.Code, receiver)
			if err != nil {
				self.state.Set(snapshot)

				return fmt.Errorf("Error during code execution %v", err)
			}

			msg.Output = ret
		} else {
			// Add default LOG
			// PUSH1 1 CALLER ADD LOG1
			addr := ethutil.BigD(sender.Address())
			addr.Add(addr, ethutil.Big1)
			tx.addLog(vm.Log{sender.Address(), []*big.Int{addr}, nil})
		}
	}

	return
}

func (self *StateTransition) Eval(msg *ethstate.Message, script []byte, context *ethstate.StateObject) (ret []byte, err error) {
	var (
		transactor    = self.Sender()
		state         = self.state
		env           = NewEnv(state, self.tx, self.block)
		callerClosure = vm.NewClosure(msg, transactor, context, script, self.gas, self.gasPrice)
	)

	//vm := vm.New(env, vm.Type(ethutil.Config.VmType))
	evm := vm.New(env, vm.DebugVmTy)

	ret, _, err = callerClosure.Call(evm, self.tx.Data)

	return
}

// Converts an transaction in to a state object
func MakeContract(tx *Transaction, state *ethstate.State) *ethstate.StateObject {
	// Create contract if there's no recipient
	if tx.IsContract() {
		addr := tx.CreationAddress(state)

		contract := state.GetOrNewStateObject(addr)
		contract.InitCode = tx.Data
		contract.State = ethstate.New(ethtrie.New(ethutil.Config.Db, ""))

		return contract
	}

	return nil
}
<commit_msg>Do not return error on VM exception<commit_after>package ethchain

import (
	"fmt"
	"math/big"

	"github.com/ethereum/go-ethereum/ethstate"
	"github.com/ethereum/go-ethereum/ethtrie"
	"github.com/ethereum/go-ethereum/ethutil"
	"github.com/ethereum/go-ethereum/vm"
)

/*
 * The State transitioning model
 *
 * A state transition is a change made when a transaction is applied to the current world state
 * The state transitioning model does all all the necessary work to work out a valid new state root.
 * 1) Nonce handling
 * 2) Pre pay / buy gas of the coinbase (miner)
 * 3) Create a new state object if the recipient is \0*32
 * 4) Value transfer
 * == If contract creation ==
 * 4a) Attempt to run transaction data
 * 4b) If valid, use result as code for the new state object
 * == end ==
 * 5) Run Script section
 * 6) Derive new state root
 */
type StateTransition struct {
	coinbase, receiver []byte
	tx                 *Transaction
	gas, gasPrice      *big.Int
	value              *big.Int
	data               []byte
	state              *ethstate.State
	block              *Block

	cb, rec, sen *ethstate.StateObject
}

func NewStateTransition(coinbase *ethstate.StateObject, tx *Transaction, state *ethstate.State, block *Block) *StateTransition {
	return &StateTransition{coinbase.Address(), tx.Recipient, tx, new(big.Int), new(big.Int).Set(tx.GasPrice), tx.Value, tx.Data, state, block, coinbase, nil, nil}
}

func (self *StateTransition) Coinbase() *ethstate.StateObject {
	if self.cb != nil {
		return self.cb
	}

	self.cb = self.state.GetOrNewStateObject(self.coinbase)
	return self.cb
}
func (self *StateTransition) Sender() *ethstate.StateObject {
	if self.sen != nil {
		return self.sen
	}

	self.sen = self.state.GetOrNewStateObject(self.tx.Sender())

	return self.sen
}
func (self *StateTransition) Receiver() *ethstate.StateObject {
	if self.tx != nil && self.tx.CreatesContract() {
		return nil
	}

	if self.rec != nil {
		return self.rec
	}

	self.rec = self.state.GetOrNewStateObject(self.tx.Recipient)
	return self.rec
}

func (self *StateTransition) UseGas(amount *big.Int) error {
	if self.gas.Cmp(amount) < 0 {
		return OutOfGasError()
	}
	self.gas.Sub(self.gas, amount)

	return nil
}

func (self *StateTransition) AddGas(amount *big.Int) {
	self.gas.Add(self.gas, amount)
}

func (self *StateTransition) BuyGas() error {
	var err error

	sender := self.Sender()
	if sender.Balance().Cmp(self.tx.GasValue()) < 0 {
		return fmt.Errorf("Insufficient funds to pre-pay gas. Req %v, has %v", self.tx.GasValue(), sender.Balance())
	}

	coinbase := self.Coinbase()
	err = coinbase.BuyGas(self.tx.Gas, self.tx.GasPrice)
	if err != nil {
		return err
	}

	self.AddGas(self.tx.Gas)
	sender.SubAmount(self.tx.GasValue())

	return nil
}

func (self *StateTransition) RefundGas() {
	coinbase, sender := self.Coinbase(), self.Sender()
	coinbase.RefundGas(self.gas, self.tx.GasPrice)

	// Return remaining gas
	remaining := new(big.Int).Mul(self.gas, self.tx.GasPrice)
	sender.AddAmount(remaining)
}

func (self *StateTransition) preCheck() (err error) {
	var (
		tx     = self.tx
		sender = self.Sender()
	)

	// Make sure this transaction's nonce is correct
	if sender.Nonce != tx.Nonce {
		return NonceError(tx.Nonce, sender.Nonce)
	}

	// Pre-pay gas / Buy gas of the coinbase account
	if err = self.BuyGas(); err != nil {
		return err
	}

	return nil
}

func (self *StateTransition) TransitionState() (err error) {
	statelogger.Debugf("(~) %x\n", self.tx.Hash())

	// XXX Transactions after this point are considered valid.
	if err = self.preCheck(); err != nil {
		return
	}

	var (
		tx       = self.tx
		sender   = self.Sender()
		receiver *ethstate.StateObject
	)

	defer self.RefundGas()

	// Increment the nonce for the next transaction
	sender.Nonce += 1

	// Transaction gas
	if err = self.UseGas(vm.GasTx); err != nil {
		return
	}

	// Pay data gas
	dataPrice := big.NewInt(int64(len(self.data)))
	dataPrice.Mul(dataPrice, vm.GasData)
	if err = self.UseGas(dataPrice); err != nil {
		return
	}

	if sender.Balance().Cmp(self.value) < 0 {
		return fmt.Errorf("Insufficient funds to transfer value. Req %v, has %v", self.value, sender.Balance)
	}

	var snapshot *ethstate.State
	// If the receiver is nil it's a contract (\0*32).
	if tx.CreatesContract() {
		// Subtract the (irreversible) amount from the senders account
		sender.SubAmount(self.value)

		snapshot = self.state.Copy()

		// Create a new state object for the contract
		receiver = MakeContract(tx, self.state)
		self.rec = receiver
		if receiver == nil {
			return fmt.Errorf("Unable to create contract")
		}

		// Add the amount to receivers account which should conclude this transaction
		receiver.AddAmount(self.value)
	} else {
		receiver = self.Receiver()

		// Subtract the amount from the senders account
		sender.SubAmount(self.value)
		// Add the amount to receivers account which should conclude this transaction
		receiver.AddAmount(self.value)

		snapshot = self.state.Copy()
	}

	msg := self.state.Manifest().AddMessage(&ethstate.Message{
		To: receiver.Address(), From: sender.Address(),
		Input:  self.tx.Data,
		Origin: sender.Address(),
		Block:  self.block.Hash(), Timestamp: self.block.Time, Coinbase: self.block.Coinbase, Number: self.block.Number,
		Value: self.value,
	})

	// Process the init code and create 'valid' contract
	if IsContractAddr(self.receiver) {
		// Evaluate the initialization script
		// and use the return value as the
		// script section for the state object.
		self.data = nil

		code, evmerr := self.Eval(msg, receiver.Init(), receiver)
		if evmerr != nil {
			self.state.Set(snapshot)

			statelogger.Debugf("Error during init execution %v", evmerr)
		}

		receiver.Code = code
		msg.Output = code
	} else {
		if len(receiver.Code) > 0 {
			ret, evmerr := self.Eval(msg, receiver.Code, receiver)
			if evmerr != nil {
				self.state.Set(snapshot)

				statelogger.Debugf("Error during code execution %v", evmerr)
			}

			msg.Output = ret
		} else {
			// Add default LOG
			// PUSH1 1 CALLER ADD LOG1
			addr := ethutil.BigD(sender.Address())
			addr.Add(addr, ethutil.Big1)
			tx.addLog(vm.Log{sender.Address(), []*big.Int{addr}, nil})
		}
	}

	return
}

func (self *StateTransition) Eval(msg *ethstate.Message, script []byte, context *ethstate.StateObject) (ret []byte, err error) {
	var (
		transactor    = self.Sender()
		state         = self.state
		env           = NewEnv(state, self.tx, self.block)
		callerClosure = vm.NewClosure(msg, transactor, context, script, self.gas, self.gasPrice)
	)

	//vm := vm.New(env, vm.Type(ethutil.Config.VmType))
	evm := vm.New(env, vm.DebugVmTy)

	ret, _, err = callerClosure.Call(evm, self.tx.Data)

	return
}

// Converts an transaction in to a state object
func MakeContract(tx *Transaction, state *ethstate.State) *ethstate.StateObject {
	addr := tx.CreationAddress(state)

	contract := state.GetOrNewStateObject(addr)
	contract.InitCode = tx.Data
	contract.State = ethstate.New(ethtrie.New(ethutil.Config.Db, ""))

	return contract
}
<|endoftext|>


<commit_before><commit_msg>remove extra word from say<commit_after><|endoftext|>


<commit_before>package ebs

import (
	"fmt"

	"github.com/aws/aws-sdk-go/aws"
	"github.com/aws/aws-sdk-go/service/ec2"
	"github.com/mitchellh/multistep"
	"github.com/mitchellh/packer/builder/amazon/common"
	"github.com/mitchellh/packer/packer"
)

// stepCleanupVolumes cleans up any orphaned volumes that were not designated to
// remain after termination of the instance. These volumes are typically ones
// that are marked as "delete on terminate:false" in the source_ami of a build.
type stepCleanupVolumes struct {
	BlockDevices common.BlockDevices
}

func (s *stepCleanupVolumes) Run(state multistep.StateBag) multistep.StepAction {
	// stepCleanupVolumes is for Cleanup only
	return multistep.ActionContinue
}

func (s *stepCleanupVolumes) Cleanup(state multistep.StateBag) {
	ec2conn := state.Get("ec2").(*ec2.EC2)
	instanceRaw := state.Get("instance")
	var instance *ec2.Instance
	if instanceRaw != nil {
		instance = instanceRaw.(*ec2.Instance)
	}
	ui := state.Get("ui").(packer.Ui)
	if instance == nil {
		ui.Say("No volumes to clean up, skipping")
		return
	}

	ui.Say("Cleaning up any extra volumes...")

	// We don't actually care about the value here, but we need Set behavior
	save := make(map[string]struct{})
	for _, b := range s.BlockDevices.AMIMappings {
		if !b.DeleteOnTermination {
			save[b.DeviceName] = struct{}{}
		}
	}

	for _, b := range s.BlockDevices.LaunchMappings {
		if !b.DeleteOnTermination {
			save[b.DeviceName] = struct{}{}
		}
	}

	// Collect Volume information from the cached Instance as a map of volume-id
	// to device name, to compare with save list above
	var vl []*string
	volList := make(map[string]string)
	for _, bdm := range instance.BlockDeviceMappings {
		if bdm.Ebs != nil {
			vl = append(vl, bdm.Ebs.VolumeId)
			volList[*bdm.Ebs.VolumeId] = *bdm.DeviceName
		}
	}

	// Using the volume list from the cached Instance, check with AWS for up to
	// date information on them
	resp, err := ec2conn.DescribeVolumes(&ec2.DescribeVolumesInput{
		Filters: []*ec2.Filter{
			{
				Name:   aws.String("volume-id"),
				Values: vl,
			},
		},
	})

	if err != nil {
		ui.Say(fmt.Sprintf("Error describing volumes: %s", err))
		return
	}

	// If any of the returned volumes are in a "deleting" stage or otherwise not
	// available, remove them from the list of volumes
	for _, v := range resp.Volumes {
		if v.State != nil && *v.State != "available" {
			delete(volList, *v.VolumeId)
		}
	}

	if len(resp.Volumes) == 0 {
		ui.Say("No volumes to clean up, skipping")
		return
	}

	// Filter out any devices marked for saving
	for saveName := range save {
		for volKey, volName := range volList {
			if volName == saveName {
				delete(volList, volKey)
			}
		}
	}

	// Destroy remaining volumes
	for k := range volList {
		ui.Say(fmt.Sprintf("Destroying volume (%s)...", k))
		_, err := ec2conn.DeleteVolume(&ec2.DeleteVolumeInput{VolumeId: aws.String(k)})
		if err != nil {
			ui.Say(fmt.Sprintf("Error deleting volume: %s", k))
		}

	}
}
<commit_msg>Output error causing ebs volume deletion failure<commit_after>package ebs

import (
	"fmt"

	"github.com/aws/aws-sdk-go/aws"
	"github.com/aws/aws-sdk-go/service/ec2"
	"github.com/mitchellh/multistep"
	"github.com/mitchellh/packer/builder/amazon/common"
	"github.com/mitchellh/packer/packer"
)

// stepCleanupVolumes cleans up any orphaned volumes that were not designated to
// remain after termination of the instance. These volumes are typically ones
// that are marked as "delete on terminate:false" in the source_ami of a build.
type stepCleanupVolumes struct {
	BlockDevices common.BlockDevices
}

func (s *stepCleanupVolumes) Run(state multistep.StateBag) multistep.StepAction {
	// stepCleanupVolumes is for Cleanup only
	return multistep.ActionContinue
}

func (s *stepCleanupVolumes) Cleanup(state multistep.StateBag) {
	ec2conn := state.Get("ec2").(*ec2.EC2)
	instanceRaw := state.Get("instance")
	var instance *ec2.Instance
	if instanceRaw != nil {
		instance = instanceRaw.(*ec2.Instance)
	}
	ui := state.Get("ui").(packer.Ui)
	if instance == nil {
		ui.Say("No volumes to clean up, skipping")
		return
	}

	ui.Say("Cleaning up any extra volumes...")

	// We don't actually care about the value here, but we need Set behavior
	save := make(map[string]struct{})
	for _, b := range s.BlockDevices.AMIMappings {
		if !b.DeleteOnTermination {
			save[b.DeviceName] = struct{}{}
		}
	}

	for _, b := range s.BlockDevices.LaunchMappings {
		if !b.DeleteOnTermination {
			save[b.DeviceName] = struct{}{}
		}
	}

	// Collect Volume information from the cached Instance as a map of volume-id
	// to device name, to compare with save list above
	var vl []*string
	volList := make(map[string]string)
	for _, bdm := range instance.BlockDeviceMappings {
		if bdm.Ebs != nil {
			vl = append(vl, bdm.Ebs.VolumeId)
			volList[*bdm.Ebs.VolumeId] = *bdm.DeviceName
		}
	}

	// Using the volume list from the cached Instance, check with AWS for up to
	// date information on them
	resp, err := ec2conn.DescribeVolumes(&ec2.DescribeVolumesInput{
		Filters: []*ec2.Filter{
			{
				Name:   aws.String("volume-id"),
				Values: vl,
			},
		},
	})

	if err != nil {
		ui.Say(fmt.Sprintf("Error describing volumes: %s", err))
		return
	}

	// If any of the returned volumes are in a "deleting" stage or otherwise not
	// available, remove them from the list of volumes
	for _, v := range resp.Volumes {
		if v.State != nil && *v.State != "available" {
			delete(volList, *v.VolumeId)
		}
	}

	if len(resp.Volumes) == 0 {
		ui.Say("No volumes to clean up, skipping")
		return
	}

	// Filter out any devices marked for saving
	for saveName := range save {
		for volKey, volName := range volList {
			if volName == saveName {
				delete(volList, volKey)
			}
		}
	}

	// Destroy remaining volumes
	for k := range volList {
		ui.Say(fmt.Sprintf("Destroying volume (%s)...", k))
		_, err := ec2conn.DeleteVolume(&ec2.DeleteVolumeInput{VolumeId: aws.String(k)})
		if err != nil {
			ui.Say(fmt.Sprintf("Error deleting volume: %s", k))
			ui.Say(fmt.Sprintf("The error is: %s", err))
		}

	}
}
<|endoftext|>