<commit_before>package protocol

import (
	// "fmt"
	x "github.com/jddixon/xlattice_go"
	. "launchpad.net/gocheck"
	"testing"
)

// gocheck tie-in /////////////////////
func Test(t *testing.T) { TestingT(t) }

type XLSuite struct{}

var _ = Suite(&XLSuite{})

// end gocheck setup //////////////////

func (s *XLSuite) TestConstructors(c *C) {
	rng := x.MakeSimpleRNG()
	tType := uint16(rng.NextInt32(256 * 256))

	const BUF_SIZE = 16
	value := make([]byte, BUF_SIZE)
	rng.NextBytes(&value)

	tlv := new(TLV16)
	err := tlv.Init(tType, nil) // illegal nil data buffer
	c.Assert(err, Not(IsNil))

	err = tlv.Init(tType, &value)
	c.Assert(tType, Equals, tlv.Type())
	c.Assert(BUF_SIZE, Equals, int(tlv.Length()))
	// XXX CAN'T COMPARE []uint8
	// c.Assert(value, Equals, *(tlv.Value()))
}

func (s *XLSuite) TestCFields(c *C) {
	rng := x.MakeSimpleRNG()
	tType := uint16(rng.NextInt32(256 * 256))

	const BUF_SIZE = 16
	value := make([]byte, BUF_SIZE)
	rng.NextBytes(&value)

	tlv := new(TLV16)
	tlv.Init(tType, &value)
	c.Assert(tType, Equals, tlv.Type())
	c.Assert(len(value), Equals, int(tlv.Length()))
	// XXX CAN'T COMPARE []uint8
	// c.Assert(value, Equals, *(tlv.Value()))

}

//// DEBUG
//func dumpBuffer( title string, p*[]byte) {
//    length := len(*p)
//    fmt.Printf("%s ", title)
//    for i := 0; i < length; i++ {
//        fmt.Printf("%02x ", (*p)[i])
//    }
//    fmt.Print("\n")
//}
//// END
func (s *XLSuite) TestReadWrite(c *C) {
	const COUNT = 16
	const MAX_TYPE = 128
	rng := x.MakeSimpleRNG()
	for i := 0; i < COUNT; i++ {
		tType := uint16(rng.NextInt32(MAX_TYPE))
		bufLen := 4 * (1 + int(rng.NextInt32(16)))
		value := make([]byte, bufLen)
		rng.NextBytes(&value) // just adds noise
		tlv := new(TLV16)
		tlv.Init(tType, &value)

		// create a buffer, write TLV16 into it, read it back
		buffer := make([]byte, 4+bufLen)
		// XXX offset should be randomized
		offset := uint16(0)
		tlv.Encode(&buffer, offset)
		decoded := Decode(&buffer, offset)
		c.Assert(tType, Equals, decoded.Type())
		c.Assert(bufLen, Equals, int(decoded.Length()))
		//      // DEBUG
		//      dumpBuffer("value   ", &buffer)
		//      dumpBuffer("encoded ", decoded.Value())
		//      // END
		for j := 0; j < bufLen; j++ {
			c.Assert((*tlv.Value())[j], Equals, (*decoded.Value())[j])
		}
	}
}
<commit_msg>fixed references to x.MakeSimpleRNG which succeed on this machine but fail elsewhere<commit_after>package protocol

import (
	// "fmt"
	"github.com/jddixon/xlattice_go/rnglib"
	. "launchpad.net/gocheck"
	"testing"
)

// gocheck tie-in /////////////////////
func Test(t *testing.T) { TestingT(t) }

type XLSuite struct{}

var _ = Suite(&XLSuite{})

// end gocheck setup //////////////////

func (s *XLSuite) TestConstructors(c *C) {
	rng := rnglib.MakeSimpleRNG()
	tType := uint16(rng.NextInt32(256 * 256))

	const BUF_SIZE = 16
	value := make([]byte, BUF_SIZE)
	rng.NextBytes(&value)

	tlv := new(TLV16)
	err := tlv.Init(tType, nil) // illegal nil data buffer
	c.Assert(err, Not(IsNil))

	err = tlv.Init(tType, &value)
	c.Assert(tType, Equals, tlv.Type())
	c.Assert(BUF_SIZE, Equals, int(tlv.Length()))
	// XXX CAN'T COMPARE []uint8
	// c.Assert(value, Equals, *(tlv.Value()))
}

func (s *XLSuite) TestCFields(c *C) {
	rng := rnglib.MakeSimpleRNG()
	tType := uint16(rng.NextInt32(256 * 256))

	const BUF_SIZE = 16
	value := make([]byte, BUF_SIZE)
	rng.NextBytes(&value)

	tlv := new(TLV16)
	tlv.Init(tType, &value)
	c.Assert(tType, Equals, tlv.Type())
	c.Assert(len(value), Equals, int(tlv.Length()))
	// XXX CAN'T COMPARE []uint8
	// c.Assert(value, Equals, *(tlv.Value()))

}

//// DEBUG
//func dumpBuffer( title string, p*[]byte) {
//    length := len(*p)
//    fmt.Printf("%s ", title)
//    for i := 0; i < length; i++ {
//        fmt.Printf("%02x ", (*p)[i])
//    }
//    fmt.Print("\n")
//}
//// END
func (s *XLSuite) TestReadWrite(c *C) {
	const COUNT = 16
	const MAX_TYPE = 128
	rng := rnglib.MakeSimpleRNG()
	for i := 0; i < COUNT; i++ {
		tType := uint16(rng.NextInt32(MAX_TYPE))
		bufLen := 4 * (1 + int(rng.NextInt32(16)))
		value := make([]byte, bufLen)
		rng.NextBytes(&value) // just adds noise
		tlv := new(TLV16)
		tlv.Init(tType, &value)

		// create a buffer, write TLV16 into it, read it back
		buffer := make([]byte, 4+bufLen)
		// XXX offset should be randomized
		offset := uint16(0)
		tlv.Encode(&buffer, offset)
		decoded := Decode(&buffer, offset)
		c.Assert(tType, Equals, decoded.Type())
		c.Assert(bufLen, Equals, int(decoded.Length()))
		//      // DEBUG
		//      dumpBuffer("value   ", &buffer)
		//      dumpBuffer("encoded ", decoded.Value())
		//      // END
		for j := 0; j < bufLen; j++ {
			c.Assert((*tlv.Value())[j], Equals, (*decoded.Value())[j])
		}
	}
}
<|endoftext|>


<commit_before>package physical

import wrapping "github.com/hashicorp/go-kms-wrapping"

// Entry is used to represent data stored by the physical backend
type Entry struct {
	Key      string
	Value    []byte
	SealWrap bool `json:"seal_wrap,omitempty"`

	// Only used in replication
	ValueHash []byte

	// The bool above is an easy control for whether it should be enabled; it
	// is used to carry information about whether seal wrapping is *desired*
	// regardless of whether it's currently available. The struct below stores
	// needed information when it's actually performed.
	SealWrapInfo *wrapping.EncryptedBlobInfo `json:"seal_wrap_info,omitempty"`
}
<commit_msg>Remove EncryptedBlobInfo in Entry<commit_after>package physical

// Entry is used to represent data stored by the physical backend
type Entry struct {
	Key      string
	Value    []byte
	SealWrap bool `json:"seal_wrap,omitempty"`

	// Only used in replication
	ValueHash []byte
}
<|endoftext|>


<commit_before>package blockchain

// ValidTransaction checks whether a transaction is valid, assuming the
import (
	"crypto/ecdsa"
	"math/big"

	log "github.com/Sirupsen/logrus"
)

// TransactionCode is returned from ValidTransaction.
type TransactionCode uint32

// CloudBaseTransactionCode is returned from ValidCloudBaseTransaction.
type CloudBaseTransactionCode uint32

// GenesisBlockCode is returned from ValidGenesisBlock.
type GenesisBlockCode uint32

// BlockCode is returned from ValidBlock.
type BlockCode uint32

const (
	// ValidTransaction is returned when transaction is valid.
	ValidTransaction TransactionCode = iota
	// NoInputTransaction is returned when transaction has no valid input transaction.
	NoInputTransaction
	// Overspend is returned when transaction outputs exceed transaction inputs.
	Overspend
	// BadSig is returned when the signature verification fails.
	BadSig
	// Respend is returned when inputs have been spent elsewhere in the chain.
	Respend
	// NilTransaction is returned when the transaction pointer is nil.
	NilTransaction
)

const (
	// ValidCloudBaseTransaction is returned when a transaction is a valid
	// CloudBase transaction.
	ValidCloudBaseTransaction CloudBaseTransactionCode = iota
	// BadCloudBaseSender is returned when the sender address in the CloudBase
	// transaction is not a NilAddr.
	BadCloudBaseSender
	// BadCloudBaseInput is returned when all the fields inf the  CloudBase
	// transaction input are not equal to 0.
	BadCloudBaseInput
	// BadCloudBaseOutput is returned when the CloudBase transaction output is
	// invalid.
	BadCloudBaseOutput
	// BadCloudBaseSig is returned when the CloudBase transaction signature is
	// not equal to NilSig.
	BadCloudBaseSig
	// NilCloudBaseTransaction is returned when the CloudBase transaction
	// pointer is nil
	NilCloudBaseTransaction
)

const (
	// ValidGenesisBlock is returned when a block is a valid genesis block.
	ValidGenesisBlock GenesisBlockCode = iota
	// BadGenesisLastBlock is returned when the LastBlock of the genesis block
	// is not equal to 0.
	BadGenesisLastBlock
	// BadGenesisTransactions is returned when the genesis block does not contain
	// exactly 1 transaction, the first CloudBase transaction.
	BadGenesisTransactions
	// BadGenesisCloudBaseTransaction is returned when the transaction in the
	// genesis block is not a valid CloudBase transaction.
	BadGenesisCloudBaseTransaction
	// BadGenesisBlockNumber is returned when the block number in the genesis
	// block is not equal to 0.
	BadGenesisBlockNumber
	// BadGenesisTarget is returned when the genesis block's target is invalid.
	BadGenesisTarget
	// BadGenesisTime is returned when the gensis block's time is invalid.
	BadGenesisTime
	// NilGenesisBlock is returned when the genesis block is equal to nil.
	NilGenesisBlock
)

const (
	// ValidBlock is returned when the block is valid.
	ValidBlock BlockCode = iota
	// BadTransaction is returned when the block contains an invalid
	// transaction.
	BadTransaction
	// BadTime is returned when the block contains an invalid time.
	BadTime
	// BadTarget is returned when the block contains an invalid target.
	BadTarget
	// BadBlockNumber is returned when block number is not one greater than
	// previous block.
	BadBlockNumber
	// BadHash is returned when the block contains incorrect hash.
	BadHash
	// DoubleSpend is returned when two transactions in the block share inputs,
	// but outputs > inputs.
	DoubleSpend
	// BadCloudBaseTransaction is returned when a block does not have a
	// CloudBase transaction as the first transaction in its list of
	// transactions.
	BadCloudBaseTransaction
	// BadGenesisBlock is returned if the block is a genesis block and is
	// invalid.
	BadGenesisBlock
	// NilBlock is returned when the block pointer is nil.
	NilBlock
)

var (
	// MaxTarget is the minimum difficulty
	MaxTarget = new(big.Int).Sub(BigExp(2, 232), big.NewInt(1))
	// MinTarget is the maximum difficulty value
	MinTarget = big.NewInt(1)
)

// ValidTransaction tests whether a transaction valid.
func (bc *BlockChain) ValidTransaction(t *Transaction) (bool, TransactionCode) {

	// Check if the transaction is equal to nil
	if t == nil {
		return false, NilTransaction
	}

	// Find the transaction input in the chain (by hash)
	var input *Transaction
	input = bc.GetInputTransaction(t)
	if input == nil || HashSum(input) != t.Input.Hash {
		return false, NoInputTransaction
	}

	// Check that output to sender in input is equal to outputs in t
	if !input.InputsEqualOutputs(t) {
		return false, Overspend
	}

	// Verify signature of t
	hash := HashSum(t.TxBody)
	if !ecdsa.Verify(t.Sender.Key(), hash.Marshal(), t.Sig.R, t.Sig.S) {
		return false, BadSig
	}

	// Test if identical transaction already exists in chain.
	end := uint32(len(bc.Blocks))
	start := t.Input.BlockNumber
	if exists, _, _ := bc.ContainsTransaction(t, start, end); exists {
		return false, Respend
	}

	return true, ValidTransaction
}

// ValidCloudBase returns true if a transaction is a valid CloudBase transaction
// and false otherwise
func ValidCloudBase(t *Transaction) (bool, CloudBaseTransactionCode) {

	// Check if the CloudBase transaction is equal to nil
	if t == nil {
		return false, NilCloudBaseTransaction
	}

	// Check that the sender address is nil
	if t.Sender != NilAddr {
		return false, BadCloudBaseSender
	}

	// Check that the input is 0
	if t.TxBody.Input.BlockNumber != 0 ||
		t.TxBody.Input.Hash != NilHash ||
		t.Input.Index != 0 {
		return false, BadCloudBaseInput
	}

	// Check that the output is set and that the reward is != 0
	if len(t.Outputs) == 0 ||
		len(t.Outputs) > 1 ||
		t.Outputs[0].Amount == 0 ||
		t.Outputs[0].Recipient == NilAddr {
		return false, BadCloudBaseOutput
	}

	// Asser that the signature is equal to nil
	if t.Sig != NilSig {
		return false, BadCloudBaseSig
	}

	return true, ValidCloudBaseTransaction
}

// ValidGenesisBlock checks whether a block is a valid genesis block.
func (bc *BlockChain) ValidGenesisBlock(gb *Block) (bool, GenesisBlockCode) {

	// Check if the genesis block is equal to nil.
	if gb == nil {
		return false, NilGenesisBlock
	}

	// Check if the genesis block's block number is equal to 0.
	if gb.BlockHeader.BlockNumber != 0 ||
		bc.Blocks[0] != gb {
		return false, BadGenesisBlockNumber
	}

	// Check if the genesis block's last block hash is equal to 0.
	if HashToBigInt(gb.BlockHeader.LastBlock).Cmp(big.NewInt(0)) != 0 {
		return false, BadGenesisLastBlock
	}

	// Check if the size of the transaction list is equal to 1.
	if len(gb.Transactions) != 1 {
		return false, BadGenesisTransactions
	}

	// Check if the transaction is a valid cloud base transaction.
	if valid, code := ValidCloudBase(gb.Transactions[0]); !valid {
		log.Errorf("Invalid CloudBase, CloudBaseTransactionCode: %d", code)
		return false, BadGenesisCloudBaseTransaction
	}

	// Check that the target is within the min and max difficulty levels.
	target := HashToBigInt(gb.Target)
	if target.Cmp(MaxTarget) == 1 || target.Cmp(MinTarget) == -1 {
		return false, BadGenesisTarget
	}

	// Check that time is not greater than current time or equal to 0.
	if uint32(gb.Time) == 0 {
		return false, BadGenesisTime
	}

	return true, ValidGenesisBlock
}

// ValidBlock checks whether a block is valid.
func (bc *BlockChain) ValidBlock(b *Block) (bool, BlockCode) {

	// Check if the block is equal to nil.
	if b == nil {
		return false, NilBlock
	}

	// Check if the block is the genesis block.
	if b.BlockHeader.BlockNumber == 0 || bc.Blocks[0] == b {
		if valid, code := bc.ValidGenesisBlock(b); !valid {
			log.Errorf("Invalid GenesisBlock, GenesisBlockCode: %d", code)
			return false, BadGenesisBlock
		}
		return true, ValidBlock
	}

	// Check that block number between 0 and max blocks.
	ix := b.BlockNumber - 1
	if int(ix) > len(bc.Blocks)-1 || ix < 0 {
		return false, BadBlockNumber
	}

	// Check that block number is one greater than last block
	lastBlock := bc.Blocks[ix]
	if lastBlock.BlockNumber != ix {
		return false, BadBlockNumber
	}

	// Check that the first transaction is a CloudBase transaction
	if valid, code := ValidCloudBase(b.Transactions[0]); !valid {
		log.Errorf("Invalid CloudBase, CloudBaseTransactionCode: %d", code)
		return false, BadCloudBaseTransaction
	}

	// Verify every Transaction in the block.
	for _, t := range b.Transactions[1:] {
		if valid, _ := bc.ValidTransaction(t); !valid {
			return false, BadTransaction
		}
	}

	// Check that the target is within the min and max difficulty levels
	target := HashToBigInt(b.Target)
	if target.Cmp(MaxTarget) == 1 || target.Cmp(MinTarget) == -1 {
		return false, BadTarget
	}

	// Check that time is not greater than current time or equal to 0
	if uint32(b.Time) == 0 {
		return false, BadTime
	}

	// Check that hash of last block is correct
	if HashSum(lastBlock) != b.LastBlock {
		return false, BadHash
	}

	// Check for multiple transactions referencing same input transaction.
	for i, trA := range b.Transactions {
		for j, trB := range b.Transactions {
			if (i != j) && (trA.Input.Hash == trB.Input.Hash) {
				inputTr := bc.GetInputTransaction(trA)
				if !inputTr.InputsEqualOutputs(trA, trB) {
					return false, DoubleSpend
				}
			}
		}
	}

	return true, ValidBlock
}

// BigExp returns an big int pointer with the result set to base**exp,
// if exp <= 0, the result is 1
func BigExp(base, exp int) *big.Int {
	return new(big.Int).Exp(big.NewInt(int64(base)), big.NewInt(int64(exp)), nil)
}
<commit_msg>typo fix<commit_after>package blockchain

// ValidTransaction checks whether a transaction is valid, assuming the
import (
	"crypto/ecdsa"
	"math/big"

	log "github.com/Sirupsen/logrus"
)

// TransactionCode is returned from ValidTransaction.
type TransactionCode uint32

// CloudBaseTransactionCode is returned from ValidCloudBaseTransaction.
type CloudBaseTransactionCode uint32

// GenesisBlockCode is returned from ValidGenesisBlock.
type GenesisBlockCode uint32

// BlockCode is returned from ValidBlock.
type BlockCode uint32

const (
	// ValidTransaction is returned when transaction is valid.
	ValidTransaction TransactionCode = iota
	// NoInputTransaction is returned when transaction has no valid input transaction.
	NoInputTransaction
	// Overspend is returned when transaction outputs exceed transaction inputs.
	Overspend
	// BadSig is returned when the signature verification fails.
	BadSig
	// Respend is returned when inputs have been spent elsewhere in the chain.
	Respend
	// NilTransaction is returned when the transaction pointer is nil.
	NilTransaction
)

const (
	// ValidCloudBaseTransaction is returned when a transaction is a valid
	// CloudBase transaction.
	ValidCloudBaseTransaction CloudBaseTransactionCode = iota
	// BadCloudBaseSender is returned when the sender address in the CloudBase
	// transaction is not a NilAddr.
	BadCloudBaseSender
	// BadCloudBaseInput is returned when all the fields inf the  CloudBase
	// transaction input are not equal to 0.
	BadCloudBaseInput
	// BadCloudBaseOutput is returned when the CloudBase transaction output is
	// invalid.
	BadCloudBaseOutput
	// BadCloudBaseSig is returned when the CloudBase transaction signature is
	// not equal to NilSig.
	BadCloudBaseSig
	// NilCloudBaseTransaction is returned when the CloudBase transaction
	// pointer is nil
	NilCloudBaseTransaction
)

const (
	// ValidGenesisBlock is returned when a block is a valid genesis block.
	ValidGenesisBlock GenesisBlockCode = iota
	// BadGenesisLastBlock is returned when the LastBlock of the genesis block
	// is not equal to 0.
	BadGenesisLastBlock
	// BadGenesisTransactions is returned when the genesis block does not contain
	// exactly 1 transaction, the first CloudBase transaction.
	BadGenesisTransactions
	// BadGenesisCloudBaseTransaction is returned when the transaction in the
	// genesis block is not a valid CloudBase transaction.
	BadGenesisCloudBaseTransaction
	// BadGenesisBlockNumber is returned when the block number in the genesis
	// block is not equal to 0.
	BadGenesisBlockNumber
	// BadGenesisTarget is returned when the genesis block's target is invalid.
	BadGenesisTarget
	// BadGenesisTime is returned when the genesis block's time is invalid.
	BadGenesisTime
	// NilGenesisBlock is returned when the genesis block is equal to nil.
	NilGenesisBlock
)

const (
	// ValidBlock is returned when the block is valid.
	ValidBlock BlockCode = iota
	// BadTransaction is returned when the block contains an invalid
	// transaction.
	BadTransaction
	// BadTime is returned when the block contains an invalid time.
	BadTime
	// BadTarget is returned when the block contains an invalid target.
	BadTarget
	// BadBlockNumber is returned when block number is not one greater than
	// previous block.
	BadBlockNumber
	// BadHash is returned when the block contains incorrect hash.
	BadHash
	// DoubleSpend is returned when two transactions in the block share inputs,
	// but outputs > inputs.
	DoubleSpend
	// BadCloudBaseTransaction is returned when a block does not have a
	// CloudBase transaction as the first transaction in its list of
	// transactions.
	BadCloudBaseTransaction
	// BadGenesisBlock is returned if the block is a genesis block and is
	// invalid.
	BadGenesisBlock
	// NilBlock is returned when the block pointer is nil.
	NilBlock
)

var (
	// MaxTarget is the minimum difficulty
	MaxTarget = new(big.Int).Sub(BigExp(2, 232), big.NewInt(1))
	// MinTarget is the maximum difficulty value
	MinTarget = big.NewInt(1)
)

// ValidTransaction tests whether a transaction valid.
func (bc *BlockChain) ValidTransaction(t *Transaction) (bool, TransactionCode) {

	// Check if the transaction is equal to nil
	if t == nil {
		return false, NilTransaction
	}

	// Find the transaction input in the chain (by hash)
	var input *Transaction
	input = bc.GetInputTransaction(t)
	if input == nil || HashSum(input) != t.Input.Hash {
		return false, NoInputTransaction
	}

	// Check that output to sender in input is equal to outputs in t
	if !input.InputsEqualOutputs(t) {
		return false, Overspend
	}

	// Verify signature of t
	hash := HashSum(t.TxBody)
	if !ecdsa.Verify(t.Sender.Key(), hash.Marshal(), t.Sig.R, t.Sig.S) {
		return false, BadSig
	}

	// Test if identical transaction already exists in chain.
	end := uint32(len(bc.Blocks))
	start := t.Input.BlockNumber
	if exists, _, _ := bc.ContainsTransaction(t, start, end); exists {
		return false, Respend
	}

	return true, ValidTransaction
}

// ValidCloudBase returns true if a transaction is a valid CloudBase transaction
// and false otherwise
func ValidCloudBase(t *Transaction) (bool, CloudBaseTransactionCode) {

	// Check if the CloudBase transaction is equal to nil
	if t == nil {
		return false, NilCloudBaseTransaction
	}

	// Check that the sender address is nil
	if t.Sender != NilAddr {
		return false, BadCloudBaseSender
	}

	// Check that the input is 0
	if t.TxBody.Input.BlockNumber != 0 ||
		t.TxBody.Input.Hash != NilHash ||
		t.Input.Index != 0 {
		return false, BadCloudBaseInput
	}

	// Check that the output is set and that the reward is != 0
	if len(t.Outputs) == 0 ||
		len(t.Outputs) > 1 ||
		t.Outputs[0].Amount == 0 ||
		t.Outputs[0].Recipient == NilAddr {
		return false, BadCloudBaseOutput
	}

	// Asser that the signature is equal to nil
	if t.Sig != NilSig {
		return false, BadCloudBaseSig
	}

	return true, ValidCloudBaseTransaction
}

// ValidGenesisBlock checks whether a block is a valid genesis block.
func (bc *BlockChain) ValidGenesisBlock(gb *Block) (bool, GenesisBlockCode) {

	// Check if the genesis block is equal to nil.
	if gb == nil {
		return false, NilGenesisBlock
	}

	// Check if the genesis block's block number is equal to 0.
	if gb.BlockHeader.BlockNumber != 0 ||
		bc.Blocks[0] != gb {
		return false, BadGenesisBlockNumber
	}

	// Check if the genesis block's last block hash is equal to 0.
	if HashToBigInt(gb.BlockHeader.LastBlock).Cmp(big.NewInt(0)) != 0 {
		return false, BadGenesisLastBlock
	}

	// Check if the size of the transaction list is equal to 1.
	if len(gb.Transactions) != 1 {
		return false, BadGenesisTransactions
	}

	// Check if the transaction is a valid cloud base transaction.
	if valid, code := ValidCloudBase(gb.Transactions[0]); !valid {
		log.Errorf("Invalid CloudBase, CloudBaseTransactionCode: %d", code)
		return false, BadGenesisCloudBaseTransaction
	}

	// Check that the target is within the min and max difficulty levels.
	target := HashToBigInt(gb.Target)
	if target.Cmp(MaxTarget) == 1 || target.Cmp(MinTarget) == -1 {
		return false, BadGenesisTarget
	}

	// Check that time is not greater than current time or equal to 0.
	if uint32(gb.Time) == 0 {
		return false, BadGenesisTime
	}

	return true, ValidGenesisBlock
}

// ValidBlock checks whether a block is valid.
func (bc *BlockChain) ValidBlock(b *Block) (bool, BlockCode) {

	// Check if the block is equal to nil.
	if b == nil {
		return false, NilBlock
	}

	// Check if the block is the genesis block.
	if b.BlockHeader.BlockNumber == 0 || bc.Blocks[0] == b {
		if valid, code := bc.ValidGenesisBlock(b); !valid {
			log.Errorf("Invalid GenesisBlock, GenesisBlockCode: %d", code)
			return false, BadGenesisBlock
		}
		return true, ValidBlock
	}

	// Check that block number between 0 and max blocks.
	ix := b.BlockNumber - 1
	if int(ix) > len(bc.Blocks)-1 || ix < 0 {
		return false, BadBlockNumber
	}

	// Check that block number is one greater than last block
	lastBlock := bc.Blocks[ix]
	if lastBlock.BlockNumber != ix {
		return false, BadBlockNumber
	}

	// Check that the first transaction is a CloudBase transaction
	if valid, code := ValidCloudBase(b.Transactions[0]); !valid {
		log.Errorf("Invalid CloudBase, CloudBaseTransactionCode: %d", code)
		return false, BadCloudBaseTransaction
	}

	// Verify every Transaction in the block.
	for _, t := range b.Transactions[1:] {
		if valid, _ := bc.ValidTransaction(t); !valid {
			return false, BadTransaction
		}
	}

	// Check that the target is within the min and max difficulty levels
	target := HashToBigInt(b.Target)
	if target.Cmp(MaxTarget) == 1 || target.Cmp(MinTarget) == -1 {
		return false, BadTarget
	}

	// Check that time is not greater than current time or equal to 0
	if uint32(b.Time) == 0 {
		return false, BadTime
	}

	// Check that hash of last block is correct
	if HashSum(lastBlock) != b.LastBlock {
		return false, BadHash
	}

	// Check for multiple transactions referencing same input transaction.
	for i, trA := range b.Transactions {
		for j, trB := range b.Transactions {
			if (i != j) && (trA.Input.Hash == trB.Input.Hash) {
				inputTr := bc.GetInputTransaction(trA)
				if !inputTr.InputsEqualOutputs(trA, trB) {
					return false, DoubleSpend
				}
			}
		}
	}

	return true, ValidBlock
}

// BigExp returns an big int pointer with the result set to base**exp,
// if exp <= 0, the result is 1
func BigExp(base, exp int) *big.Int {
	return new(big.Int).Exp(big.NewInt(int64(base)), big.NewInt(int64(exp)), nil)
}
<|endoftext|>


<commit_before>package datasource

import (
	"database/sql/driver"
	"encoding/json"
	"errors"
	"fmt"
	"time"

	"github.com/araddon/dateparse"
	u "github.com/araddon/gou"
)

type TimeValue time.Time

func (m *TimeValue) MarshalJSON() ([]byte, error) {
	by, err := time.Time(*m).MarshalJSON()
	return by, err
}

func (m *TimeValue) UnmarshalJSON(data []byte) error {
	var t time.Time
	err := json.Unmarshal(data, &t)
	if err == nil {
		*m = TimeValue(t)
	}
	return err

}

func (m TimeValue) Value() (driver.Value, error) {
	u.Debugf("Value: %v", m)
	by, err := json.Marshal(time.Time(m))
	return by, err
}

func (m *TimeValue) Scan(src interface{}) error {
	//u.Debugf("scan: '%v'", src)
	var t time.Time
	switch val := src.(type) {
	case string:
		//u.Infof("trying to scan string: '%v'", val)
		t2, err := dateparse.ParseAny(val)
		if err == nil {
			*m = TimeValue(t2)
			return nil
		}
		u.Infof("%v  %v", t2, err)
		err = json.Unmarshal([]byte(val), &t)
		if err == nil {
			*m = TimeValue(t)
		} else {
			u.Warnf("error? %v", err)
			return err
		}
	case []byte:
		t2, err := dateparse.ParseAny(string(val))
		if err == nil {
			*m = TimeValue(t2)
			return nil
		}
		err = json.Unmarshal(val, &t)
		if err == nil {
			*m = TimeValue(t)
		} else {
			return err
		}
	default:
		u.Warnf("unknown type: %T", m)
		return errors.New("Incompatible type for TimeValue")
	}
	return nil
}

func (m *TimeValue) Unmarshal(v interface{}) error {
	u.Warnf("wat? %T %v", v, v)
	//return json.Unmarshal([]byte(*m), v)
	return fmt.Errorf("not implemented")
}

type JsonWrapper json.RawMessage

func (m *JsonWrapper) MarshalJSON() ([]byte, error) { return *m, nil }

// Unmarshall bytes into this typed struct
func (m *JsonWrapper) UnmarshalJSON(data []byte) error {
	if m == nil {
		return errors.New("JsonWrapper must not be nil")
	}
	*m = append((*m)[0:0], data...)
	return nil

}

// This is the go sql/driver interface we need to implement to allow
// conversion back forth
func (m JsonWrapper) Value() (driver.Value, error) {
	var jsonRaw json.RawMessage
	var err = m.Unmarshal(&jsonRaw)
	if err != nil {
		return []byte{}, err
	}
	return []byte(m), nil
}

func (m *JsonWrapper) Scan(src interface{}) error {
	var jsonBytes []byte
	switch src.(type) {
	case string:
		jsonBytes = []byte(src.(string))
	case []byte:
		jsonBytes = src.([]byte)
	default:
		return errors.New("Incompatible type for JsonText")
	}
	*m = JsonWrapper(append((*m)[0:0], jsonBytes...))
	return nil
}

func (m *JsonWrapper) Unmarshal(v interface{}) error {
	return json.Unmarshal([]byte(*m), v)
}
<commit_msg>allow accessing timevalue struct as native time.Time<commit_after>package datasource

import (
	"database/sql/driver"
	"encoding/json"
	"errors"
	"fmt"
	"time"

	"github.com/araddon/dateparse"
	u "github.com/araddon/gou"
)

type TimeValue time.Time

func (m *TimeValue) MarshalJSON() ([]byte, error) {
	by, err := time.Time(*m).MarshalJSON()
	return by, err
}

func (m *TimeValue) UnmarshalJSON(data []byte) error {
	var t time.Time
	err := json.Unmarshal(data, &t)
	if err == nil {
		*m = TimeValue(t)
	}
	return err

}

func (m TimeValue) Value() (driver.Value, error) {
	u.Debugf("Value: %v", m)
	by, err := json.Marshal(time.Time(m))
	return by, err
}

func (m TimeValue) Time() time.Time {
	return time.Time(m)
}

func (m *TimeValue) Scan(src interface{}) error {
	//u.Debugf("scan: '%v'", src)
	var t time.Time
	switch val := src.(type) {
	case string:
		//u.Infof("trying to scan string: '%v'", val)
		t2, err := dateparse.ParseAny(val)
		if err == nil {
			*m = TimeValue(t2)
			return nil
		}
		u.Infof("%v  %v", t2, err)
		err = json.Unmarshal([]byte(val), &t)
		if err == nil {
			*m = TimeValue(t)
		} else {
			u.Warnf("error? %v", err)
			return err
		}
	case []byte:
		t2, err := dateparse.ParseAny(string(val))
		if err == nil {
			*m = TimeValue(t2)
			return nil
		}
		err = json.Unmarshal(val, &t)
		if err == nil {
			*m = TimeValue(t)
		} else {
			return err
		}
	default:
		u.Warnf("unknown type: %T", m)
		return errors.New("Incompatible type for TimeValue")
	}
	return nil
}

func (m *TimeValue) Unmarshal(v interface{}) error {
	u.Warnf("wat? %T %v", v, v)
	//return json.Unmarshal([]byte(*m), v)
	return fmt.Errorf("not implemented")
}

type JsonWrapper json.RawMessage

func (m *JsonWrapper) MarshalJSON() ([]byte, error) { return *m, nil }

// Unmarshall bytes into this typed struct
func (m *JsonWrapper) UnmarshalJSON(data []byte) error {
	if m == nil {
		return errors.New("JsonWrapper must not be nil")
	}
	*m = append((*m)[0:0], data...)
	return nil

}

// This is the go sql/driver interface we need to implement to allow
// conversion back forth
func (m JsonWrapper) Value() (driver.Value, error) {
	var jsonRaw json.RawMessage
	var err = m.Unmarshal(&jsonRaw)
	if err != nil {
		return []byte{}, err
	}
	return []byte(m), nil
}

func (m *JsonWrapper) Scan(src interface{}) error {
	var jsonBytes []byte
	switch src.(type) {
	case string:
		jsonBytes = []byte(src.(string))
	case []byte:
		jsonBytes = src.([]byte)
	default:
		return errors.New("Incompatible type for JsonText")
	}
	*m = JsonWrapper(append((*m)[0:0], jsonBytes...))
	return nil
}

func (m *JsonWrapper) Unmarshal(v interface{}) error {
	return json.Unmarshal([]byte(*m), v)
}
<|endoftext|>


<commit_before>package database

import (
	"time"
	"github.com/emirpasic/gods/maps/hashmap"
	"fmt"
	"github.com/Junbong/mankato-server/db/collection"
	"log"
)

type Database struct {
	createdAt   time.Time
	collections hashmap.Map
}

func New() (*Database) {
	return &Database{createdAt:time.Now(), collections:*hashmap.New()}
}

func (d Database) String() string {
	return fmt.Sprintf("Database{ createdAt:%s, size:%d }", d.createdAt, d.collections.Size())
}

func (d Database) Put(nameOfCollection string, col interface{}) {
	// TODO: make this function thread-safe

	d.collections.Put(nameOfCollection, col)
}

func (d Database) Get(nameOfCollection string) (col interface{}, exists bool) {
	// TODO: make this function thread-safe

	col, exists = d.collections.Get(nameOfCollection)

	if exists {
		col = col.(*collection.Collection)
	}

	return
}

func (d Database) GetOrCreateCollection(nameOfCollection string, createIfNotExists bool) (col interface{}) {
	// TODO: make this function thread-safe

	var exists bool
	col, exists = d.collections.Get(nameOfCollection)

	if !exists && createIfNotExists {
		col = collection.New(nameOfCollection)
		d.Put(nameOfCollection, col)
		log.Println(
			"New collection added [", nameOfCollection,
			"] Total number of collections =", d.collections.Size())
	}

	return
}

func (d Database) Remove(nameOfCollection string) bool {
	// TODO: make this function thread-safe

	before := d.collections.Size()
	d.collections.Remove(nameOfCollection)
	after := d.collections.Size()
	return before != after
}

//func (c Collection) Clear() {
//	// TODO: make this function thread-safe
//
//	c.data.Clear()
//}

//func (c Collection) ContainsKey(key string) (contains bool) {
//	// TODO: make this function thread-safe
//
//	_, contains = c.data.Get(key)
//	return
//}
<commit_msg>Fix build error.<commit_after>package database

import (
	"time"
	"github.com/emirpasic/gods/maps/hashmap"
	"fmt"
	"github.com/Junbong/mankato-server/db/collections"
	"log"
)

type Database struct {
	createdAt   time.Time
	collections hashmap.Map
}

func New() (*Database) {
	return &Database{createdAt:time.Now(), collections:*hashmap.New()}
}

func (d Database) String() string {
	return fmt.Sprintf("Database{ createdAt:%s, size:%d }", d.createdAt, d.collections.Size())
}

func (d Database) Put(nameOfCollection string, col interface{}) {
	// TODO: make this function thread-safe

	d.collections.Put(nameOfCollection, col)
}

func (d Database) Get(nameOfCollection string) (col interface{}, exists bool) {
	// TODO: make this function thread-safe

	col, exists = d.collections.Get(nameOfCollection)

	if exists {
		col = col.(*collection.Collection)
	}

	return
}

func (d Database) GetOrCreateCollection(nameOfCollection string, createIfNotExists bool) (col interface{}) {
	// TODO: make this function thread-safe

	var exists bool
	col, exists = d.collections.Get(nameOfCollection)

	if !exists && createIfNotExists {
		col = collection.New(nameOfCollection)
		d.Put(nameOfCollection, col)
		log.Println(
			"New collection added [", nameOfCollection,
			"] Total number of collections =", d.collections.Size())
	}

	return
}

func (d Database) Remove(nameOfCollection string) bool {
	// TODO: make this function thread-safe

	before := d.collections.Size()
	d.collections.Remove(nameOfCollection)
	after := d.collections.Size()
	return before != after
}

//func (c Collection) Clear() {
//	// TODO: make this function thread-safe
//
//	c.data.Clear()
//}

//func (c Collection) ContainsKey(key string) (contains bool) {
//	// TODO: make this function thread-safe
//
//	_, contains = c.data.Get(key)
//	return
//}
<|endoftext|>


<commit_before>package main

import (
	"database/sql"
	"fmt"

	"time"

	"github.com/pkg/errors"
)

func check3321(db *sql.DB) error {
	// 3.3.2.1 Entries in the WAREHOUSE and DISTRICT tables must satisfy the relationship:
	// W_YTD = sum (D_YTD)

	row := db.QueryRow(`
SELECT COUNT(*)
FROM warehouse
FULL OUTER JOIN
(SELECT d_w_id, SUM(d_ytd) as sum_d_ytd FROM district GROUP BY d_w_id)
ON (w_id = d_w_id)
WHERE w_ytd != sum_d_ytd
`)

	var i int
	if err := row.Scan(&i); err != nil {
		return err
	}

	if i != 0 {
		return errors.Errorf("%d rows returned, expected zero", i)
	}

	return nil
}

func check3322(db *sql.DB) error {
	// Entries in the DISTRICT, ORDER, and NEW-ORDER tables must satisfy the relationship:
	// D_NEXT_O_ID - 1 = max(O_ID) = max(NO_O_ID)

	districtRows, err := db.Query("SELECT d_next_o_id FROM district ORDER BY d_w_id, d_id")
	if err != nil {
		return err
	}
	newOrderRows, err := db.Query(`
SELECT MAX(no_o_id) FROM tpcc.new_order GROUP BY no_d_id, no_w_id ORDER BY no_w_id, no_d_id
`)
	if err != nil {
		return err
	}
	orderRows, err := db.Query(`
SELECT MAX(o_id) FROM tpcc.order GROUP BY o_d_id, o_w_id ORDER BY o_w_id, o_d_id
`)
	if err != nil {
		return err
	}

	var district, newOrder, order float64
	var i int
	for ; districtRows.Next() && newOrderRows.Next() && orderRows.Next(); i++ {
		if err := districtRows.Scan(&district); err != nil {
			return err
		}
		if err := newOrderRows.Scan(&newOrder); err != nil {
			return err
		}
		if err := orderRows.Scan(&order); err != nil {
			return err
		}

		if (order != newOrder) || (order != (district - 1)) {
			return errors.Errorf("inequality at idx %d: order: %d, newOrder: %d, district-1: %d",
				i, order, newOrder, district-1)
		}
	}
	if districtRows.Next() || newOrderRows.Next() || orderRows.Next() {
		return errors.New("length mismatch between rows")
	}
	if err := districtRows.Close(); err != nil {
		return err
	}
	if err := newOrderRows.Close(); err != nil {
		return err
	}
	if err := orderRows.Close(); err != nil {
		return err
	}

	if i == 0 {
		return errors.Errorf("zero rows")
	}

	return nil
}

func check3323(db *sql.DB) error {
	// max(NO_O_ID) - min(NO_O_ID) + 1 = # of rows in new_order for each warehouse/district
	rows, err := db.Query(`
SELECT (VALUES (1)) FULL OUTER JOIN
(SELECT MAX(no_o_id) - MIN(no_o_id) - COUNT(*) AS nod FROM new_order GROUP BY no_w_id, no_d_id)
 WHERE nod != one
`)
	if err != nil {
		return err
	}

	var i int
	var val int
	for ; rows.Next(); i++ {
		if err := rows.Scan(&val); err != nil {
			return err
		}
		if val != -1 {
			return errors.Errorf("MAX(no_o_id) - MIN(no_o_id) - COUNT(rows) must be -1, got %d", val)
		}
	}

	if err := rows.Close(); err != nil {
		return err
	}

	if i == 0 {
		return errors.Errorf("zero rows")
	}

	return nil
}

func check3324(db *sql.DB) error {
	// SUM(O_OL_CNT) = [number of rows in the ORDER-LINE table for this district]

	leftRows, err := db.Query(`
SELECT SUM(o_ol_cnt) FROM tpcc.order GROUP BY o_w_id, o_d_id ORDER BY o_w_id, o_d_id
`)
	if err != nil {
		return err
	}
	rightRows, err := db.Query(`
SELECT COUNT(*) FROM tpcc.order_line GROUP BY ol_w_id, ol_d_id ORDER BY ol_w_id, ol_d_id
`)
	if err != nil {
		return err
	}
	var i int
	var left, right int64
	for ; leftRows.Next() && rightRows.Next(); i++ {
		if err := leftRows.Scan(&left); err != nil {
			return err
		}
		if err := rightRows.Scan(&right); err != nil {
			return err
		}
		if left != right {
			return errors.Errorf("order.SUM(o_ol_cnt): %d != order_line.count(*): %d", left, right)
		}
	}
	if i == 0 {
		return errors.Errorf("0 rows returned")
	}
	if leftRows.Next() || rightRows.Next() {
		return errors.Errorf("length of order.SUM(o_ol_cnt) != order_line.count(*)")
	}

	if err := leftRows.Close(); err != nil {
		return err
	}
	return rightRows.Close()
}

func check3325(db *sql.DB) error {
	// We want the symmetric difference between the sets:
	// (SELECT no_w_id, no_d_id, no_o_id FROM tpcc.new_order)
	// (SELECT o_w_id, o_d_id, o_id FROM tpcc.order@primary WHERE o_carrier_id IS NULL)
	// We achieve this by two EXCEPT ALL queries.

	firstQuery, err := db.Query(`
(SELECT no_w_id, no_d_id, no_o_id FROM tpcc.new_order)
EXCEPT ALL
(SELECT o_w_id, o_d_id, o_id FROM tpcc.order@primary WHERE o_carrier_id IS NULL)`)
	if err != nil {
		return err
	}
	secondQuery, err := db.Query(`
(SELECT o_w_id, o_d_id, o_id FROM tpcc.order@primary WHERE o_carrier_id IS NULL)
EXCEPT ALL
(SELECT no_w_id, no_d_id, no_o_id FROM tpcc.new_order)`)
	if err != nil {
		return err
	}

	if firstQuery.Next() {
		return errors.Errorf("left EXCEPT right returned nonzero results.")
	}

	if secondQuery.Next() {
		return errors.Errorf("right EXCEPT left returned nonzero results.")
	}

	if err := firstQuery.Close(); err != nil {
		return err
	}
	return secondQuery.Close()
}

func check3326(db *sql.DB) error {
	firstQuery, err := db.Query(`
(SELECT o_w_id, o_d_id, o_id, o_ol_cnt FROM tpcc.order
  ORDER BY o_w_id, o_d_id, o_id DESC)
EXCEPT ALL
(SELECT ol_w_id, ol_d_id, ol_o_id, COUNT(*) FROM tpcc.order_line
  GROUP BY (ol_w_id, ol_d_id, ol_o_id)
  ORDER BY ol_w_id, ol_d_id, ol_o_id DESC)`)
	if err != nil {
		return err
	}
	secondQuery, err := db.Query(`
(SELECT ol_w_id, ol_d_id, ol_o_id, COUNT(*) FROM tpcc.order_line
  GROUP BY (ol_w_id, ol_d_id, ol_o_id) ORDER BY ol_w_id, ol_d_id, ol_o_id DESC)
EXCEPT ALL
(SELECT o_w_id, o_d_id, o_id, o_ol_cnt FROM tpcc.order
  ORDER BY o_w_id, o_d_id, o_id DESC)`)
	if err != nil {
		return err
	}

	if firstQuery.Next() {
		return errors.Errorf("left EXCEPT right returned nonzero results")
	}

	if secondQuery.Next() {
		return errors.Errorf("right EXCEPT left returned nonzero results")
	}

	if err := firstQuery.Close(); err != nil {
		return err
	}
	return secondQuery.Close()
}

func checkConsistency(db *sql.DB) bool {
	type check struct {
		name      string
		f         func(db *sql.DB) error
		expensive bool
	}

	checks := []check{
		{"3.3.2.1", check3321, false},
		{"3.3.2.2", check3322, false},
		{"3.3.2.3", check3323, false},
		{"3.3.2.4", check3324, false},
		{"3.3.2.5", check3325, false},
		{"3.3.2.6", check3326, true},
	}
	var errorEncountered bool

	// TODO(arjun): We should run each test in a single transaction as currently
	// we have to shut down load before running the checks.

	for _, check := range checks {
		if check.expensive && !*expensive {
			continue
		}
		start := time.Now()
		fmt.Printf(check.name)
		if err := check.f(db); err != nil {
			fmt.Printf(": error encountered: '%+v'", err)
			errorEncountered = true
		} else {
			fmt.Printf(" passed")
		}

		fmt.Printf(" after %f seconds.\n", time.Since(start).Seconds())
	}
	return errorEncountered
}
<commit_msg>tpcc: fix check 3<commit_after>package main

import (
	"database/sql"
	"fmt"

	"time"

	"github.com/pkg/errors"
)

func check3321(db *sql.DB) error {
	// 3.3.2.1 Entries in the WAREHOUSE and DISTRICT tables must satisfy the relationship:
	// W_YTD = sum (D_YTD)

	row := db.QueryRow(`
SELECT COUNT(*)
FROM warehouse
FULL OUTER JOIN
(SELECT d_w_id, SUM(d_ytd) as sum_d_ytd FROM district GROUP BY d_w_id)
ON (w_id = d_w_id)
WHERE w_ytd != sum_d_ytd
`)

	var i int
	if err := row.Scan(&i); err != nil {
		return err
	}

	if i != 0 {
		return errors.Errorf("%d rows returned, expected zero", i)
	}

	return nil
}

func check3322(db *sql.DB) error {
	// Entries in the DISTRICT, ORDER, and NEW-ORDER tables must satisfy the relationship:
	// D_NEXT_O_ID - 1 = max(O_ID) = max(NO_O_ID)

	districtRows, err := db.Query("SELECT d_next_o_id FROM district ORDER BY d_w_id, d_id")
	if err != nil {
		return err
	}
	newOrderRows, err := db.Query(`
SELECT MAX(no_o_id) FROM tpcc.new_order GROUP BY no_d_id, no_w_id ORDER BY no_w_id, no_d_id
`)
	if err != nil {
		return err
	}
	orderRows, err := db.Query(`
SELECT MAX(o_id) FROM tpcc.order GROUP BY o_d_id, o_w_id ORDER BY o_w_id, o_d_id
`)
	if err != nil {
		return err
	}

	var district, newOrder, order float64
	var i int
	for ; districtRows.Next() && newOrderRows.Next() && orderRows.Next(); i++ {
		if err := districtRows.Scan(&district); err != nil {
			return err
		}
		if err := newOrderRows.Scan(&newOrder); err != nil {
			return err
		}
		if err := orderRows.Scan(&order); err != nil {
			return err
		}

		if (order != newOrder) || (order != (district - 1)) {
			return errors.Errorf("inequality at idx %d: order: %d, newOrder: %d, district-1: %d",
				i, order, newOrder, district-1)
		}
	}
	if districtRows.Next() || newOrderRows.Next() || orderRows.Next() {
		return errors.New("length mismatch between rows")
	}
	if err := districtRows.Close(); err != nil {
		return err
	}
	if err := newOrderRows.Close(); err != nil {
		return err
	}
	if err := orderRows.Close(); err != nil {
		return err
	}

	if i == 0 {
		return errors.Errorf("zero rows")
	}

	return nil
}

func check3323(db *sql.DB) error {
	// max(NO_O_ID) - min(NO_O_ID) + 1 = # of rows in new_order for each warehouse/district
	row := db.QueryRow(`
SELECT COUNT(*) FROM
 (SELECT MAX(no_o_id) - MIN(no_o_id) - COUNT(*) AS nod FROM new_order
  GROUP BY no_w_id, no_d_id)
 WHERE nod != -1
`)

	var i int
	if err := row.Scan(&i); err != nil {
		return err
	}

	if i != 0 {
		return errors.Errorf("%d rows returned, expected zero", i)
	}

	return nil
}

func check3324(db *sql.DB) error {
	// SUM(O_OL_CNT) = [number of rows in the ORDER-LINE table for this district]

	leftRows, err := db.Query(`
SELECT SUM(o_ol_cnt) FROM tpcc.order GROUP BY o_w_id, o_d_id ORDER BY o_w_id, o_d_id
`)
	if err != nil {
		return err
	}
	rightRows, err := db.Query(`
SELECT COUNT(*) FROM tpcc.order_line GROUP BY ol_w_id, ol_d_id ORDER BY ol_w_id, ol_d_id
`)
	if err != nil {
		return err
	}
	var i int
	var left, right int64
	for ; leftRows.Next() && rightRows.Next(); i++ {
		if err := leftRows.Scan(&left); err != nil {
			return err
		}
		if err := rightRows.Scan(&right); err != nil {
			return err
		}
		if left != right {
			return errors.Errorf("order.SUM(o_ol_cnt): %d != order_line.count(*): %d", left, right)
		}
	}
	if i == 0 {
		return errors.Errorf("0 rows returned")
	}
	if leftRows.Next() || rightRows.Next() {
		return errors.Errorf("length of order.SUM(o_ol_cnt) != order_line.count(*)")
	}

	if err := leftRows.Close(); err != nil {
		return err
	}
	return rightRows.Close()
}

func check3325(db *sql.DB) error {
	// We want the symmetric difference between the sets:
	// (SELECT no_w_id, no_d_id, no_o_id FROM tpcc.new_order)
	// (SELECT o_w_id, o_d_id, o_id FROM tpcc.order@primary WHERE o_carrier_id IS NULL)
	// We achieve this by two EXCEPT ALL queries.

	firstQuery, err := db.Query(`
(SELECT no_w_id, no_d_id, no_o_id FROM tpcc.new_order)
EXCEPT ALL
(SELECT o_w_id, o_d_id, o_id FROM tpcc.order@primary WHERE o_carrier_id IS NULL)`)
	if err != nil {
		return err
	}
	secondQuery, err := db.Query(`
(SELECT o_w_id, o_d_id, o_id FROM tpcc.order@primary WHERE o_carrier_id IS NULL)
EXCEPT ALL
(SELECT no_w_id, no_d_id, no_o_id FROM tpcc.new_order)`)
	if err != nil {
		return err
	}

	if firstQuery.Next() {
		return errors.Errorf("left EXCEPT right returned nonzero results.")
	}

	if secondQuery.Next() {
		return errors.Errorf("right EXCEPT left returned nonzero results.")
	}

	if err := firstQuery.Close(); err != nil {
		return err
	}
	return secondQuery.Close()
}

func check3326(db *sql.DB) error {
	firstQuery, err := db.Query(`
(SELECT o_w_id, o_d_id, o_id, o_ol_cnt FROM tpcc.order
  ORDER BY o_w_id, o_d_id, o_id DESC)
EXCEPT ALL
(SELECT ol_w_id, ol_d_id, ol_o_id, COUNT(*) FROM tpcc.order_line
  GROUP BY (ol_w_id, ol_d_id, ol_o_id)
  ORDER BY ol_w_id, ol_d_id, ol_o_id DESC)`)
	if err != nil {
		return err
	}
	secondQuery, err := db.Query(`
(SELECT ol_w_id, ol_d_id, ol_o_id, COUNT(*) FROM tpcc.order_line
  GROUP BY (ol_w_id, ol_d_id, ol_o_id) ORDER BY ol_w_id, ol_d_id, ol_o_id DESC)
EXCEPT ALL
(SELECT o_w_id, o_d_id, o_id, o_ol_cnt FROM tpcc.order
  ORDER BY o_w_id, o_d_id, o_id DESC)`)
	if err != nil {
		return err
	}

	if firstQuery.Next() {
		return errors.Errorf("left EXCEPT right returned nonzero results")
	}

	if secondQuery.Next() {
		return errors.Errorf("right EXCEPT left returned nonzero results")
	}

	if err := firstQuery.Close(); err != nil {
		return err
	}
	return secondQuery.Close()
}

func checkConsistency(db *sql.DB) bool {
	type check struct {
		name      string
		f         func(db *sql.DB) error
		expensive bool
	}

	checks := []check{
		{"3.3.2.1", check3321, false},
		{"3.3.2.2", check3322, false},
		{"3.3.2.3", check3323, false},
		{"3.3.2.4", check3324, false},
		{"3.3.2.5", check3325, false},
		{"3.3.2.6", check3326, true},
	}
	var errorEncountered bool

	// TODO(arjun): We should run each test in a single transaction as currently
	// we have to shut down load before running the checks.

	for _, check := range checks {
		if check.expensive && !*expensive {
			continue
		}
		start := time.Now()
		fmt.Printf(check.name)
		if err := check.f(db); err != nil {
			fmt.Printf(": error encountered: '%+v'", err)
			errorEncountered = true
		} else {
			fmt.Printf(" passed")
		}

		fmt.Printf(" after %f seconds.\n", time.Since(start).Seconds())
	}
	return errorEncountered
}
<|endoftext|>


<commit_before>// Copyright 2017 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package trace

import (
	"strings"

	"golang.org/x/net/context"
	"google.golang.org/api/option"
	"google.golang.org/grpc"
	"google.golang.org/grpc/metadata"
)

const grpcMetadataKey = "stackdriver-trace-context"

// GRPCClientInterceptor returns a grpc.UnaryClientInterceptor that traces all outgoing requests from a gRPC client.
// The calling context should already have a *trace.Span; a child span will be
// created for the outgoing gRPC call. If the calling context doesn't have a span,
// the call will not be traced.
//
// The functionality in gRPC that this feature relies on is currently experimental.
func GRPCClientInterceptor() grpc.UnaryClientInterceptor {
	return grpc.UnaryClientInterceptor(grpcUnaryInterceptor)
}

func grpcUnaryInterceptor(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
	// TODO: also intercept streams.
	span := FromContext(ctx).NewChild(method)
	defer span.Finish()

	if span != nil {
		header := spanHeader(span.trace.traceID, span.span.ParentSpanId, span.trace.globalOptions)
		md, ok := metadata.FromOutgoingContext(ctx)
		if !ok {
			md = metadata.Pairs(grpcMetadataKey, header)
		} else {
			md = md.Copy() // metadata is immutable, copy.
			md[grpcMetadataKey] = []string{header}
		}
		ctx = metadata.NewContext(ctx, md)
	}

	err := invoker(ctx, method, req, reply, cc, opts...)
	if err != nil {
		// TODO: standardize gRPC label names?
		span.SetLabel("error", err.Error())
	}
	return err
}

// GRPCServerInterceptor returns a grpc.UnaryServerInterceptor that enables the tracing of the incoming
// gRPC calls. Incoming call's context can be used to extract the span on servers that enabled this option:
//
//	span := trace.FromContext(ctx)
//
// The functionality in gRPC that this feature relies on is currently experimental.
func GRPCServerInterceptor(tc *Client) grpc.UnaryServerInterceptor {
	return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
		md, _ := metadata.FromIncomingContext(ctx)
		if header, ok := md[grpcMetadataKey]; ok {
			span := tc.SpanFromHeader("", strings.Join(header, ""))
			defer span.Finish()
			ctx = NewContext(ctx, span)
		}
		return handler(ctx, req)
	}
}

// EnableGRPCTracing automatically traces all outgoing gRPC calls from cloud.google.com/go clients.
//
// The functionality in gRPC that this relies on is currently experimental.
//
// Deprecated: Use option.WithGRPCDialOption(grpc.WithUnaryInterceptor(GRPCClientInterceptor())) instead.
var EnableGRPCTracing option.ClientOption = option.WithGRPCDialOption(grpc.WithUnaryInterceptor(GRPCClientInterceptor()))
<commit_msg>trace: make gRPC propagation compatible with Endpoints<commit_after>// Copyright 2017 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package trace

import (
	"strings"

	"golang.org/x/net/context"
	"google.golang.org/api/option"
	"google.golang.org/grpc"
	"google.golang.org/grpc/metadata"
)

const grpcMetadataKey = "x-cloud-trace-context"

// GRPCClientInterceptor returns a grpc.UnaryClientInterceptor that traces all outgoing requests from a gRPC client.
// The calling context should already have a *trace.Span; a child span will be
// created for the outgoing gRPC call. If the calling context doesn't have a span,
// the call will not be traced.
//
// The functionality in gRPC that this feature relies on is currently experimental.
func GRPCClientInterceptor() grpc.UnaryClientInterceptor {
	return grpc.UnaryClientInterceptor(grpcUnaryInterceptor)
}

func grpcUnaryInterceptor(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
	// TODO: also intercept streams.
	span := FromContext(ctx).NewChild(method)
	defer span.Finish()

	if span != nil {
		header := spanHeader(span.trace.traceID, span.span.ParentSpanId, span.trace.globalOptions)
		md, ok := metadata.FromOutgoingContext(ctx)
		if !ok {
			md = metadata.Pairs(grpcMetadataKey, header)
		} else {
			md = md.Copy() // metadata is immutable, copy.
			md[grpcMetadataKey] = []string{header}
		}
		ctx = metadata.NewContext(ctx, md)
	}

	err := invoker(ctx, method, req, reply, cc, opts...)
	if err != nil {
		// TODO: standardize gRPC label names?
		span.SetLabel("error", err.Error())
	}
	return err
}

// GRPCServerInterceptor returns a grpc.UnaryServerInterceptor that enables the tracing of the incoming
// gRPC calls. Incoming call's context can be used to extract the span on servers that enabled this option:
//
//	span := trace.FromContext(ctx)
//
// The functionality in gRPC that this feature relies on is currently experimental.
func GRPCServerInterceptor(tc *Client) grpc.UnaryServerInterceptor {
	return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
		md, _ := metadata.FromIncomingContext(ctx)
		if header, ok := md[grpcMetadataKey]; ok {
			span := tc.SpanFromHeader("", strings.Join(header, ""))
			defer span.Finish()
			ctx = NewContext(ctx, span)
		}
		return handler(ctx, req)
	}
}

// EnableGRPCTracing automatically traces all outgoing gRPC calls from cloud.google.com/go clients.
//
// The functionality in gRPC that this relies on is currently experimental.
//
// Deprecated: Use option.WithGRPCDialOption(grpc.WithUnaryInterceptor(GRPCClientInterceptor())) instead.
var EnableGRPCTracing option.ClientOption = option.WithGRPCDialOption(grpc.WithUnaryInterceptor(GRPCClientInterceptor()))
<|endoftext|>


<commit_before>package steamapi

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

// State represents the state of the tradeoffer, see constants
type State uint

const (
	// ETradeOfferStateCreated /!\ non steam status, used to know the TO has been created
	ETradeOfferStateCreated State = iota
	// ETradeOfferStateInvalid Invalid
	ETradeOfferStateInvalid
	// ETradeOfferStateActive This trade offer has been sent, neither party has acted on it yet.
	ETradeOfferStateActive
	// ETradeOfferStateAccepted The trade offer was accepted by the recipient and items were exchanged.
	ETradeOfferStateAccepted
	// ETradeOfferStateCountered The recipient made a counter offer
	ETradeOfferStateCountered
	// ETradeOfferStateExpired The trade offer was not accepted before the expiration date
	ETradeOfferStateExpired
	// ETradeOfferStateCanceled The sender cancelled the offer
	ETradeOfferStateCanceled
	// ETradeOfferStateDeclined The recipient declined the offer
	ETradeOfferStateDeclined
	// ETradeOfferStateInvalidItems Some of the items in the offer are no longer available
	// (indicated by the missing flag in the output)
	ETradeOfferStateInvalidItems
	// ETradeOfferStateEmailPending The offer hasn't been sent yet and is awaiting email confirmation
	ETradeOfferStateEmailPending
	// ETradeOfferStateEmailCanceled The receiver cancelled the offer via email
	ETradeOfferStateEmailCanceled
)

// CEconAsset represents an asset in steam web api
type CEconAsset struct {
	AppID          uint   `json:",string"`
	ContextID      uint64 `json:",string"`
	AssetID        uint64 `json:",string"`
	CurrencyID     uint64 `json:",string"`
	ClassID        uint64 `json:",string"`
	InstanceID     uint64 `json:",string"`
	Amount         uint64 `json:",string"`
	Missing        bool
	MarketHashName string
}

// CEconTradeOffer represent the to from the steam API
type CEconTradeOffer struct {
	TradeOfferID   uint64 `json:",string"`
	OtherAccountID uint64 `json:"accountid_other"`
	Message        string
	ExpirationTime uint32        `json:"expiration_time"`
	State          State         `json:"trade_offer_state"`
	ToGive         []*CEconAsset `json:"items_to_give"`
	ToReceive      []*CEconAsset `json:"items_to_receive"`
	IsOurs         bool          `json:"is_our_offer"`
	TimeCreated    uint32        `json:"time_created"`
	TimeUpdated    uint32        `json:"time_updated"`
	TradeID        uint64        `json:"tradeid,string"`
}

// CEconTradeOffers represent the list of different tradeoffers types
type CEconTradeOffers struct {
	Sent     []*CEconTradeOffer `json:"trade_offers_sent"`
	Received []*CEconTradeOffer `json:"trade_offers_received"`
}

type ieconGetTradeOffersResponse struct {
	Response struct {
		CEconTradeOffers
	}
}

// IEconGetTradeOffers retrieves a list of tradeoffers
func IEconGetTradeOffers(
	baseSteamAPIURL string,
	apiKey string,
	getSentOffers bool,
	getReceivedOffers bool,
	getDescriptions bool,
	activeOnly bool,
	historicalOnly bool,
	timeHistoricalCutoff int64,
) (*CEconTradeOffers, error) {

	querystring := url.Values{}
	querystring.Add("key", apiKey)
	querystring.Add("get_sent_offers", boolToStr(getSentOffers))
	querystring.Add("get_received_offers", boolToStr(getReceivedOffers))
	querystring.Add("get_descriptions", boolToStr(getDescriptions))
	querystring.Add("language", "en")
	querystring.Add("active_only", boolToStr(activeOnly))
	querystring.Add("historical_only", boolToStr(historicalOnly))
	querystring.Add("time_historical_cutoff", strconv.FormatInt(timeHistoricalCutoff, 10))

	resp, err := http.Get(baseSteamAPIURL + "/IEconService/GetTradeOffers/v0001?" + querystring.Encode())

	if err != nil {
		return nil, fmt.Errorf("tradeoffer IEconGetTradeOffers http.Get: error %v", err)
	}

	if resp.StatusCode != http.StatusOK {
		return nil, fmt.Errorf("tradeoffer IEconGetTradeOffers http.Get: http status %v", resp.Status)
	}

	defer resp.Body.Close()

	tosResp := &ieconGetTradeOffersResponse{}
	err = json.NewDecoder(resp.Body).Decode(tosResp)

	if err != nil {
		return nil, fmt.Errorf("tradeoffer IEconGetTradeOffers Decode: error %v", err)
	}

	return &tosResp.Response.CEconTradeOffers, nil
}

type ieconGetTradeOfferResponse struct {
	Response struct {
		Offer        CEconTradeOffer
		Descriptions []ItemDescription
	}
}

// ItemDescription represents the details about the items unique w classid instanceid
type ItemDescription struct {
	AppID          uint   `json:"appid"`
	ClassID        uint64 `json:"classid,string"`
	InstanceID     uint64 `json:"instanceid,string"`
	MarketHashName string `json:"market_hash_name"`
	IconURL        string `json:"icon_url"`
	NameColor      string `json:"name_color"`
	Name           string `json:"name"`
}

func findMarketHashName(itemD []ItemDescription, appID uint, classID, instanceID uint64) string {
	for _, description := range itemD {
		if description.AppID == appID &&
			description.ClassID == classID &&
			description.InstanceID == instanceID {
			return description.MarketHashName
		}
	}

	return ""
}

// IEconGetTradeOffer retrieves details about a specific tradeoffer
func IEconGetTradeOffer(baseSteamAPIURL string, apiKey string, tradeOfferID uint64) (
	*CEconTradeOffer, error,
) {

	querystring := url.Values{}
	querystring.Add("key", apiKey)
	querystring.Add("format", "json")
	querystring.Add("tradeofferid", strconv.FormatUint(tradeOfferID, 10))
	querystring.Add("language", "en")

	resp, err := http.Get(baseSteamAPIURL + "/IEconService/GetTradeOffer/v1?" + querystring.Encode())
	if err != nil {
		return nil, fmt.Errorf("tradeoffer IEconGetTradeOffer http.Get: error %v", err)
	}
	if resp.StatusCode != http.StatusOK {
		body, errBody := ioutil.ReadAll(resp.Body)
		return nil,
			fmt.Errorf("tradeoffer IEconGetTradeOffer: steam responded with a status %d with the message: %s (%v)",
				resp.StatusCode,
				body,
				errBody,
			)
	}

	defer resp.Body.Close()

	toResp := ieconGetTradeOfferResponse{}
	err = json.NewDecoder(resp.Body).Decode(&toResp)

	if err != nil {
		return nil, fmt.Errorf("tradeoffer IEconGetTradeOffer Decode: error %v", err)
	}

	// If the state is 0, it means there is a mistake
	if toResp.Response.Offer.State == 0 {
		body, errBody := ioutil.ReadAll(resp.Body)
		return nil,
			fmt.Errorf("tradeoffer IEconGetTradeOffer: steam responded with a status %d with the message: %s (%v)",
				resp.StatusCode,
				body,
				errBody,
			)
	}

	for giveIndex, asset := range toResp.Response.Offer.ToGive {
		toResp.Response.Offer.ToGive[giveIndex].MarketHashName =
			findMarketHashName(toResp.Response.Descriptions, asset.AppID, asset.ClassID, asset.InstanceID)
	}

	for receiveIndex, asset := range toResp.Response.Offer.ToReceive {
		toResp.Response.Offer.ToReceive[receiveIndex].MarketHashName =
			findMarketHashName(toResp.Response.Descriptions, asset.AppID, asset.ClassID, asset.InstanceID)
	}

	return &toResp.Response.Offer, nil
}

// IEconActionTradeOffer declines a TO created by someone else
func IEconActionTradeOffer(baseSteamAPIURL string, action string, apiKey string, tradeOfferID uint64) error {

	if action != "Decline" && action != "Cancel" {
		return fmt.Errorf("tradeoffer IEconActionTradeOffer doesn't support %v action", action)
	}
	querystring := url.Values{}
	querystring.Add("key", apiKey)
	querystring.Add("tradeofferid", strconv.FormatUint(tradeOfferID, 10))

	resp, err := http.Get(
		baseSteamAPIURL + "/IEconService/" + action + "TradeOffer/v0001?" + querystring.Encode())

	if err != nil {
		return fmt.Errorf("tradeoffer IEconGetTradeOffer http.Get: error %v", err)
	}
	if resp.StatusCode != http.StatusOK {
		body, errBody := ioutil.ReadAll(resp.Body)
		return fmt.Errorf("tradeoffer IEcon%sTradeOffer: steam responded with a status %d with the message: %s (%v)",
			action,
			resp.StatusCode,
			body,
			errBody,
		)
	}

	err = resp.Body.Close()

	if err != nil {
		return fmt.Errorf("tradeoffer IEcon%sTradeOffer resp.Body.Close(): error %v", action, err)
	}

	return nil

}

func boolToStr(b bool) string {
	if b {
		return "1"
	}

	return "0"

}
<commit_msg>dump the body in case of steamerr<commit_after>package steamapi

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

// State represents the state of the tradeoffer, see constants
type State uint

const (
	// ETradeOfferStateCreated /!\ non steam status, used to know the TO has been created
	ETradeOfferStateCreated State = iota
	// ETradeOfferStateInvalid Invalid
	ETradeOfferStateInvalid
	// ETradeOfferStateActive This trade offer has been sent, neither party has acted on it yet.
	ETradeOfferStateActive
	// ETradeOfferStateAccepted The trade offer was accepted by the recipient and items were exchanged.
	ETradeOfferStateAccepted
	// ETradeOfferStateCountered The recipient made a counter offer
	ETradeOfferStateCountered
	// ETradeOfferStateExpired The trade offer was not accepted before the expiration date
	ETradeOfferStateExpired
	// ETradeOfferStateCanceled The sender cancelled the offer
	ETradeOfferStateCanceled
	// ETradeOfferStateDeclined The recipient declined the offer
	ETradeOfferStateDeclined
	// ETradeOfferStateInvalidItems Some of the items in the offer are no longer available
	// (indicated by the missing flag in the output)
	ETradeOfferStateInvalidItems
	// ETradeOfferStateEmailPending The offer hasn't been sent yet and is awaiting email confirmation
	ETradeOfferStateEmailPending
	// ETradeOfferStateEmailCanceled The receiver cancelled the offer via email
	ETradeOfferStateEmailCanceled
)

// CEconAsset represents an asset in steam web api
type CEconAsset struct {
	AppID          uint   `json:",string"`
	ContextID      uint64 `json:",string"`
	AssetID        uint64 `json:",string"`
	CurrencyID     uint64 `json:",string"`
	ClassID        uint64 `json:",string"`
	InstanceID     uint64 `json:",string"`
	Amount         uint64 `json:",string"`
	Missing        bool
	MarketHashName string
}

// CEconTradeOffer represent the to from the steam API
type CEconTradeOffer struct {
	TradeOfferID   uint64 `json:",string"`
	OtherAccountID uint64 `json:"accountid_other"`
	Message        string
	ExpirationTime uint32        `json:"expiration_time"`
	State          State         `json:"trade_offer_state"`
	ToGive         []*CEconAsset `json:"items_to_give"`
	ToReceive      []*CEconAsset `json:"items_to_receive"`
	IsOurs         bool          `json:"is_our_offer"`
	TimeCreated    uint32        `json:"time_created"`
	TimeUpdated    uint32        `json:"time_updated"`
	TradeID        uint64        `json:"tradeid,string"`
}

// CEconTradeOffers represent the list of different tradeoffers types
type CEconTradeOffers struct {
	Sent     []*CEconTradeOffer `json:"trade_offers_sent"`
	Received []*CEconTradeOffer `json:"trade_offers_received"`
}

type ieconGetTradeOffersResponse struct {
	Response struct {
		CEconTradeOffers
	}
}

// IEconGetTradeOffers retrieves a list of tradeoffers
func IEconGetTradeOffers(
	baseSteamAPIURL string,
	apiKey string,
	getSentOffers bool,
	getReceivedOffers bool,
	getDescriptions bool,
	activeOnly bool,
	historicalOnly bool,
	timeHistoricalCutoff int64,
) (*CEconTradeOffers, error) {

	querystring := url.Values{}
	querystring.Add("key", apiKey)
	querystring.Add("get_sent_offers", boolToStr(getSentOffers))
	querystring.Add("get_received_offers", boolToStr(getReceivedOffers))
	querystring.Add("get_descriptions", boolToStr(getDescriptions))
	querystring.Add("language", "en")
	querystring.Add("active_only", boolToStr(activeOnly))
	querystring.Add("historical_only", boolToStr(historicalOnly))
	querystring.Add("time_historical_cutoff", strconv.FormatInt(timeHistoricalCutoff, 10))

	resp, err := http.Get(baseSteamAPIURL + "/IEconService/GetTradeOffers/v0001?" + querystring.Encode())

	if err != nil {
		return nil, fmt.Errorf("tradeoffer IEconGetTradeOffers http.Get: error %v", err)
	}

	if resp.StatusCode != http.StatusOK {
		return nil, fmt.Errorf("tradeoffer IEconGetTradeOffers http.Get: http status %v", resp.Status)
	}

	defer resp.Body.Close()

	tosResp := &ieconGetTradeOffersResponse{}
	err = json.NewDecoder(resp.Body).Decode(tosResp)

	if err != nil {
		return nil, fmt.Errorf("tradeoffer IEconGetTradeOffers Decode: error %v", err)
	}

	return &tosResp.Response.CEconTradeOffers, nil
}

type ieconGetTradeOfferResponse struct {
	Response struct {
		Offer        CEconTradeOffer
		Descriptions []ItemDescription
	}
}

// ItemDescription represents the details about the items unique w classid instanceid
type ItemDescription struct {
	AppID          uint   `json:"appid"`
	ClassID        uint64 `json:"classid,string"`
	InstanceID     uint64 `json:"instanceid,string"`
	MarketHashName string `json:"market_hash_name"`
	IconURL        string `json:"icon_url"`
	NameColor      string `json:"name_color"`
	Name           string `json:"name"`
}

func findMarketHashName(itemD []ItemDescription, appID uint, classID, instanceID uint64) string {
	for _, description := range itemD {
		if description.AppID == appID &&
			description.ClassID == classID &&
			description.InstanceID == instanceID {
			return description.MarketHashName
		}
	}

	return ""
}

// IEconGetTradeOffer retrieves details about a specific tradeoffer
func IEconGetTradeOffer(baseSteamAPIURL string, apiKey string, tradeOfferID uint64) (
	*CEconTradeOffer, error,
) {

	querystring := url.Values{}
	querystring.Add("key", apiKey)
	querystring.Add("format", "json")
	querystring.Add("tradeofferid", strconv.FormatUint(tradeOfferID, 10))
	querystring.Add("language", "en")

	resp, err := http.Get(baseSteamAPIURL + "/IEconService/GetTradeOffer/v1?" + querystring.Encode())
	if err != nil {
		return nil, fmt.Errorf("tradeoffer IEconGetTradeOffer http.Get: error %v", err)
	}
	if resp.StatusCode != http.StatusOK {
		body, errBody := ioutil.ReadAll(resp.Body)
		return nil,
			fmt.Errorf("tradeoffer IEconGetTradeOffer: steam responded with a status %d with the message: %s (%v)",
				resp.StatusCode,
				body,
				errBody,
			)
	}

	defer resp.Body.Close()

	toResp := ieconGetTradeOfferResponse{}
	err = json.NewDecoder(resp.Body).Decode(&toResp)

	if err != nil {
		body, _ := ioutil.ReadAll(resp.Body)
		return nil, fmt.Errorf("tradeoffer IEconGetTradeOffer Decode(%s): error %v", body, err)
	}

	// If the state is 0, it means there is a mistake
	if toResp.Response.Offer.State == 0 {
		body, errBody := ioutil.ReadAll(resp.Body)
		return nil,
			fmt.Errorf("tradeoffer IEconGetTradeOffer: steam responded with a status %d with the message: %s (%v)",
				resp.StatusCode,
				body,
				errBody,
			)
	}

	for giveIndex, asset := range toResp.Response.Offer.ToGive {
		toResp.Response.Offer.ToGive[giveIndex].MarketHashName =
			findMarketHashName(toResp.Response.Descriptions, asset.AppID, asset.ClassID, asset.InstanceID)
	}

	for receiveIndex, asset := range toResp.Response.Offer.ToReceive {
		toResp.Response.Offer.ToReceive[receiveIndex].MarketHashName =
			findMarketHashName(toResp.Response.Descriptions, asset.AppID, asset.ClassID, asset.InstanceID)
	}

	return &toResp.Response.Offer, nil
}

// IEconActionTradeOffer declines a TO created by someone else
func IEconActionTradeOffer(baseSteamAPIURL string, action string, apiKey string, tradeOfferID uint64) error {

	if action != "Decline" && action != "Cancel" {
		return fmt.Errorf("tradeoffer IEconActionTradeOffer doesn't support %v action", action)
	}
	querystring := url.Values{}
	querystring.Add("key", apiKey)
	querystring.Add("tradeofferid", strconv.FormatUint(tradeOfferID, 10))

	resp, err := http.Get(
		baseSteamAPIURL + "/IEconService/" + action + "TradeOffer/v0001?" + querystring.Encode())

	if err != nil {
		return fmt.Errorf("tradeoffer IEconGetTradeOffer http.Get: error %v", err)
	}
	if resp.StatusCode != http.StatusOK {
		body, errBody := ioutil.ReadAll(resp.Body)
		return fmt.Errorf("tradeoffer IEcon%sTradeOffer: steam responded with a status %d with the message: %s (%v)",
			action,
			resp.StatusCode,
			body,
			errBody,
		)
	}

	err = resp.Body.Close()

	if err != nil {
		return fmt.Errorf("tradeoffer IEcon%sTradeOffer resp.Body.Close(): error %v", action, err)
	}

	return nil

}

func boolToStr(b bool) string {
	if b {
		return "1"
	}

	return "0"

}
<|endoftext|>


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

package git

import (
	"fmt"
	"os"
	"path/filepath"
	"sort"
	"strconv"
	"strings"
)

// EntryMode the type of the object in the git tree
type EntryMode int

// There are only a few file modes in Git. They look like unix file modes, but they can only be
// one of these.
const (
	// EntryModeBlob
	EntryModeBlob EntryMode = 0100644
	// EntryModeExec
	EntryModeExec EntryMode = 0100755
	// EntryModeSymlink
	EntryModeSymlink EntryMode = 0120000
	// EntryModeCommit
	EntryModeCommit EntryMode = 0160000
	// EntryModeTree
	EntryModeTree EntryMode = 0040000
)

// TreeEntry the leaf in the git tree
type TreeEntry struct {
	ID   SHA1
	Type ObjectType

	mode EntryMode
	name string

	ptree *Tree

	commited bool

	size  int64
	sized bool
}

// Name returns the name of the entry
func (te *TreeEntry) Name() string {
	return te.name
}

// Size returns the size of the entry
func (te *TreeEntry) Size() int64 {
	if te.IsDir() {
		return 0
	} else if te.sized {
		return te.size
	}

	stdout, err := NewCommand("cat-file", "-s", te.ID.String()).RunInDir(te.ptree.repo.Path)
	if err != nil {
		return 0
	}

	te.sized = true
	te.size, _ = strconv.ParseInt(strings.TrimSpace(stdout), 10, 64)
	return te.size
}

// IsSubModule if the entry is a sub module
func (te *TreeEntry) IsSubModule() bool {
	return te.mode == EntryModeCommit
}

// IsDir if the entry is a sub dir
func (te *TreeEntry) IsDir() bool {
	return te.mode == EntryModeTree
}

// IsLink if the entry is a symlink
func (te *TreeEntry) IsLink() bool {
	return te.mode == EntryModeSymlink
}

// Blob retrun the blob object the entry
func (te *TreeEntry) Blob() *Blob {
	return &Blob{
		repo:      te.ptree.repo,
		TreeEntry: te,
	}
}

// GetSubJumpablePathName return the full path of subdirectory jumpable ( contains only one directory )
func (te *TreeEntry) GetSubJumpablePathName() string {
	if te.IsSubModule() || !te.IsDir() {
		return ""
	}
	tree, err := te.ptree.SubTree(te.name)
	if err != nil {
		return te.name
	}
	entries, _ := tree.ListEntries()
	if len(entries) == 1 && entries[0].IsDir() {
		name := entries[0].GetSubJumpablePathName()
		if name != "" {
			return te.name + "/" + name
		}
	}
	return te.name
}

// Entries a list of entry
type Entries []*TreeEntry

var sorter = []func(t1, t2 *TreeEntry) bool{
	func(t1, t2 *TreeEntry) bool {
		return (t1.IsDir() || t1.IsSubModule()) && !t2.IsDir() && !t2.IsSubModule()
	},
	func(t1, t2 *TreeEntry) bool {
		return t1.name < t2.name
	},
}

func (tes Entries) Len() int      { return len(tes) }
func (tes Entries) Swap(i, j int) { tes[i], tes[j] = tes[j], tes[i] }
func (tes Entries) Less(i, j int) bool {
	t1, t2 := tes[i], tes[j]
	var k int
	for k = 0; k < len(sorter)-1; k++ {
		s := sorter[k]
		switch {
		case s(t1, t2):
			return true
		case s(t2, t1):
			return false
		}
	}
	return sorter[k](t1, t2)
}

// Sort sort the list of entry
func (tes Entries) Sort() {
	sort.Sort(tes)
}

// getCommitInfoState transient state for getting commit info for entries
type getCommitInfoState struct {
	entries        map[string]*TreeEntry // map from filepath to entry
	commits        map[string]*Commit    // map from entry name to commit
	lastCommitHash string
	lastCommit     *Commit
	treePath       string
	headCommit     *Commit
	nextSearchSize int // next number of commits to search for
}

func initGetCommitInfoState(entries Entries, headCommit *Commit, treePath string) *getCommitInfoState {
	entriesByPath := make(map[string]*TreeEntry, len(entries))
	for _, entry := range entries {
		entriesByPath[filepath.Join(treePath, entry.Name())] = entry
	}
	return &getCommitInfoState{
		entries:        entriesByPath,
		commits:        make(map[string]*Commit, len(entriesByPath)),
		treePath:       treePath,
		headCommit:     headCommit,
		nextSearchSize: 16,
	}
}

// GetCommitsInfo gets information of all commits that are corresponding to these entries
func (tes Entries) GetCommitsInfo(commit *Commit, treePath string) ([][]interface{}, error) {
	state := initGetCommitInfoState(tes, commit, treePath)
	if err := getCommitsInfo(state); err != nil {
		return nil, err
	}

	commitsInfo := make([][]interface{}, len(tes))
	for i, entry := range tes {
		commit = state.commits[filepath.Join(treePath, entry.Name())]
		switch entry.Type {
		case ObjectCommit:
			subModuleURL := ""
			if subModule, err := state.headCommit.GetSubModule(entry.Name()); err != nil {
				return nil, err
			} else if subModule != nil {
				subModuleURL = subModule.URL
			}
			subModuleFile := NewSubModuleFile(commit, subModuleURL, entry.ID.String())
			commitsInfo[i] = []interface{}{entry, subModuleFile}
		default:
			commitsInfo[i] = []interface{}{entry, commit}
		}
	}
	return commitsInfo, nil
}

func (state *getCommitInfoState) nextCommit(hash string) {
	state.lastCommitHash = hash
	state.lastCommit = nil
}

func (state *getCommitInfoState) commit() (*Commit, error) {
	var err error
	if state.lastCommit == nil {
		state.lastCommit, err = state.headCommit.repo.GetCommit(state.lastCommitHash)
	}
	return state.lastCommit, err
}

func (state *getCommitInfoState) update(path string) error {
	relPath, err := filepath.Rel(state.treePath, path)
	if err != nil {
		return nil
	}
	var entryPath string
	if index := strings.IndexRune(relPath, os.PathSeparator); index >= 0 {
		entryPath = filepath.Join(state.treePath, relPath[:index])
	} else {
		entryPath = path
	}
	if _, ok := state.entries[entryPath]; !ok {
		return nil
	} else if _, ok := state.commits[entryPath]; ok {
		return nil
	}
	state.commits[entryPath], err = state.commit()
	return err
}

func getCommitsInfo(state *getCommitInfoState) error {
	for len(state.entries) > len(state.commits) {
		if err := getNextCommitInfos(state); err != nil {
			return err
		}
	}
	return nil
}

func getNextCommitInfos(state *getCommitInfoState) error {
	logOutput, err := logCommand(state.lastCommitHash, state).RunInDir(state.headCommit.repo.Path)
	if err != nil {
		return err
	}
	lines := strings.Split(logOutput, "\n")
	i := 0
	for i < len(lines) {
		state.nextCommit(lines[i])
		i++
		for ; i < len(lines); i++ {
			path := lines[i]
			if path == "" {
				break
			}
			if path[0] == '"' {
				path, err = strconv.Unquote(path)
				if err != nil {
					return fmt.Errorf("Unquote: %v", err)
				}
			}
			state.update(path)
		}
		i++ // skip blank line
		if len(state.entries) == len(state.commits) {
			break
		}
	}
	return nil
}

func logCommand(exclusiveStartHash string, state *getCommitInfoState) *Command {
	var commitHash string
	if len(exclusiveStartHash) == 0 {
		commitHash = state.headCommit.ID.String()
	} else {
		commitHash = exclusiveStartHash + "^"
	}
	var command *Command
	numRemainingEntries := len(state.entries) - len(state.commits)
	if numRemainingEntries < 32 {
		searchSize := (numRemainingEntries + 1) / 2
		command = NewCommand("log", prettyLogFormat, "--name-only",
			"-"+strconv.Itoa(searchSize), commitHash, "--")
		for path := range state.entries {
			if _, ok := state.commits[path]; !ok {
				command.AddArguments(path)
			}
		}
	} else {
		command = NewCommand("log", prettyLogFormat, "--name-only",
			"-"+strconv.Itoa(state.nextSearchSize), commitHash, "--", state.treePath)
	}
	state.nextSearchSize += state.nextSearchSize
	return command
}
<commit_msg>Fix Windows path bug in GetCommitInfos (#67)<commit_after>// Copyright 2015 The Gogs Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package git

import (
	"fmt"
	"path"
	"sort"
	"strconv"
	"strings"
)

// EntryMode the type of the object in the git tree
type EntryMode int

// There are only a few file modes in Git. They look like unix file modes, but they can only be
// one of these.
const (
	// EntryModeBlob
	EntryModeBlob EntryMode = 0100644
	// EntryModeExec
	EntryModeExec EntryMode = 0100755
	// EntryModeSymlink
	EntryModeSymlink EntryMode = 0120000
	// EntryModeCommit
	EntryModeCommit EntryMode = 0160000
	// EntryModeTree
	EntryModeTree EntryMode = 0040000
)

// TreeEntry the leaf in the git tree
type TreeEntry struct {
	ID   SHA1
	Type ObjectType

	mode EntryMode
	name string

	ptree *Tree

	commited bool

	size  int64
	sized bool
}

// Name returns the name of the entry
func (te *TreeEntry) Name() string {
	return te.name
}

// Size returns the size of the entry
func (te *TreeEntry) Size() int64 {
	if te.IsDir() {
		return 0
	} else if te.sized {
		return te.size
	}

	stdout, err := NewCommand("cat-file", "-s", te.ID.String()).RunInDir(te.ptree.repo.Path)
	if err != nil {
		return 0
	}

	te.sized = true
	te.size, _ = strconv.ParseInt(strings.TrimSpace(stdout), 10, 64)
	return te.size
}

// IsSubModule if the entry is a sub module
func (te *TreeEntry) IsSubModule() bool {
	return te.mode == EntryModeCommit
}

// IsDir if the entry is a sub dir
func (te *TreeEntry) IsDir() bool {
	return te.mode == EntryModeTree
}

// IsLink if the entry is a symlink
func (te *TreeEntry) IsLink() bool {
	return te.mode == EntryModeSymlink
}

// Blob retrun the blob object the entry
func (te *TreeEntry) Blob() *Blob {
	return &Blob{
		repo:      te.ptree.repo,
		TreeEntry: te,
	}
}

// GetSubJumpablePathName return the full path of subdirectory jumpable ( contains only one directory )
func (te *TreeEntry) GetSubJumpablePathName() string {
	if te.IsSubModule() || !te.IsDir() {
		return ""
	}
	tree, err := te.ptree.SubTree(te.name)
	if err != nil {
		return te.name
	}
	entries, _ := tree.ListEntries()
	if len(entries) == 1 && entries[0].IsDir() {
		name := entries[0].GetSubJumpablePathName()
		if name != "" {
			return te.name + "/" + name
		}
	}
	return te.name
}

// Entries a list of entry
type Entries []*TreeEntry

var sorter = []func(t1, t2 *TreeEntry) bool{
	func(t1, t2 *TreeEntry) bool {
		return (t1.IsDir() || t1.IsSubModule()) && !t2.IsDir() && !t2.IsSubModule()
	},
	func(t1, t2 *TreeEntry) bool {
		return t1.name < t2.name
	},
}

func (tes Entries) Len() int      { return len(tes) }
func (tes Entries) Swap(i, j int) { tes[i], tes[j] = tes[j], tes[i] }
func (tes Entries) Less(i, j int) bool {
	t1, t2 := tes[i], tes[j]
	var k int
	for k = 0; k < len(sorter)-1; k++ {
		s := sorter[k]
		switch {
		case s(t1, t2):
			return true
		case s(t2, t1):
			return false
		}
	}
	return sorter[k](t1, t2)
}

// Sort sort the list of entry
func (tes Entries) Sort() {
	sort.Sort(tes)
}

// getCommitInfoState transient state for getting commit info for entries
type getCommitInfoState struct {
	entries        map[string]*TreeEntry // map from filepath to entry
	commits        map[string]*Commit    // map from filepath to commit
	lastCommitHash string
	lastCommit     *Commit
	treePath       string
	headCommit     *Commit
	nextSearchSize int // next number of commits to search for
}

func initGetCommitInfoState(entries Entries, headCommit *Commit, treePath string) *getCommitInfoState {
	entriesByPath := make(map[string]*TreeEntry, len(entries))
	for _, entry := range entries {
		entriesByPath[path.Join(treePath, entry.Name())] = entry
	}
	if treePath = path.Clean(treePath); treePath == "." {
		treePath = ""
	}
	return &getCommitInfoState{
		entries:        entriesByPath,
		commits:        make(map[string]*Commit, len(entriesByPath)),
		treePath:       treePath,
		headCommit:     headCommit,
		nextSearchSize: 16,
	}
}

// GetCommitsInfo gets information of all commits that are corresponding to these entries
func (tes Entries) GetCommitsInfo(commit *Commit, treePath string) ([][]interface{}, error) {
	state := initGetCommitInfoState(tes, commit, treePath)
	if err := getCommitsInfo(state); err != nil {
		return nil, err
	}

	commitsInfo := make([][]interface{}, len(tes))
	for i, entry := range tes {
		commit = state.commits[path.Join(treePath, entry.Name())]
		switch entry.Type {
		case ObjectCommit:
			subModuleURL := ""
			if subModule, err := state.headCommit.GetSubModule(entry.Name()); err != nil {
				return nil, err
			} else if subModule != nil {
				subModuleURL = subModule.URL
			}
			subModuleFile := NewSubModuleFile(commit, subModuleURL, entry.ID.String())
			commitsInfo[i] = []interface{}{entry, subModuleFile}
		default:
			commitsInfo[i] = []interface{}{entry, commit}
		}
	}
	return commitsInfo, nil
}

func (state *getCommitInfoState) nextCommit(hash string) {
	state.lastCommitHash = hash
	state.lastCommit = nil
}

func (state *getCommitInfoState) commit() (*Commit, error) {
	var err error
	if state.lastCommit == nil {
		state.lastCommit, err = state.headCommit.repo.GetCommit(state.lastCommitHash)
	}
	return state.lastCommit, err
}

func (state *getCommitInfoState) update(entryPath string) error {
	var entryNameStartIndex int
	if len(state.treePath) > 0 {
		entryNameStartIndex = len(state.treePath) + 1
	}

	if index := strings.IndexByte(entryPath[entryNameStartIndex:], '/'); index >= 0 {
		entryPath = entryPath[:entryNameStartIndex+index]
	}

	if _, ok := state.entries[entryPath]; !ok {
		return nil
	} else if _, ok := state.commits[entryPath]; ok {
		return nil
	}

	var err error
	state.commits[entryPath], err = state.commit()
	return err
}

func getCommitsInfo(state *getCommitInfoState) error {
	for len(state.entries) > len(state.commits) {
		if err := getNextCommitInfos(state); err != nil {
			return err
		}
	}
	return nil
}

func getNextCommitInfos(state *getCommitInfoState) error {
	logOutput, err := logCommand(state.lastCommitHash, state).RunInDir(state.headCommit.repo.Path)
	if err != nil {
		return err
	}
	lines := strings.Split(logOutput, "\n")
	i := 0
	for i < len(lines) {
		state.nextCommit(lines[i])
		i++
		for ; i < len(lines); i++ {
			entryPath := lines[i]
			if entryPath == "" {
				break
			}
			if entryPath[0] == '"' {
				entryPath, err = strconv.Unquote(entryPath)
				if err != nil {
					return fmt.Errorf("Unquote: %v", err)
				}
			}
			state.update(entryPath)
		}
		i++ // skip blank line
		if len(state.entries) == len(state.commits) {
			break
		}
	}
	return nil
}

func logCommand(exclusiveStartHash string, state *getCommitInfoState) *Command {
	var commitHash string
	if len(exclusiveStartHash) == 0 {
		commitHash = state.headCommit.ID.String()
	} else {
		commitHash = exclusiveStartHash + "^"
	}
	var command *Command
	numRemainingEntries := len(state.entries) - len(state.commits)
	if numRemainingEntries < 32 {
		searchSize := (numRemainingEntries + 1) / 2
		command = NewCommand("log", prettyLogFormat, "--name-only",
			"-"+strconv.Itoa(searchSize), commitHash, "--")
		for entryPath := range state.entries {
			if _, ok := state.commits[entryPath]; !ok {
				command.AddArguments(entryPath)
			}
		}
	} else {
		command = NewCommand("log", prettyLogFormat, "--name-only",
			"-"+strconv.Itoa(state.nextSearchSize), commitHash, "--", state.treePath)
	}
	state.nextSearchSize += state.nextSearchSize
	return command
}
<|endoftext|>


<commit_before>package swarm

import (
	"common"
	"crypto/sha256"
	"time"
)

type State interface {
	HandleTransaction(t common.Transaction)
	HandleBlock(b *Block)
}

func newBlockChain(Host string, Id string, StorageMapping map[string]interface{}) (b *BlockChain) {
	b = new(BlockChain)
	b.Id = Id
	b.StorageMapping = StorageMapping
	b.outgoingTransactions = make(chan common.NetworkObject)
	return
}

func NewBlockChain(Host string, Id string, StorageMapping map[string]interface{}) (b *BlockChain) {
	b = newBlockChain(Host, Id, StorageMapping)
	b.state = NewStateSwarmInformed(b)
	return
}

func JoinBlockChain(Host string, Id string) (b *BlockChain) {
	b = newBlockChain(Host, Id, make(map[string]interface{}))
	//b.state = NewStateSwarmJoin(b)
	return
}

//List of states
// SwarmInformed - Swarm member shave been told to join swarm
// SwarmConnected - Swarm Members have succesfully formed a block
// SwarmLive - Swarm has sent a signal to the parent blockchain saying it is
//             alive and is in the steady state
// SwarmJoin - We are joining an already alive swarm
// SwarmDied - The swarm has died, terminate

type StateSwarmInformed struct {
	//Map of hosts seen to number of times they have failed to generate a block
	//Used for both host alive tracking & host block generation tracking
	hostsseen map[string]int

	// How many times we have broadcast that we are alive, we use a two stage
	// process where we broadcast, and then broadcast again when we have seen
	// enough nodes up to form a majority
	broadcastcount uint

	// This state has two phases, the learning phase where it listens for new
	// hosts and then the commit stage where it listens for a block that
	// is correct according to its knowledge and then votes for it.
	learning bool

	chain    *BlockChain
	blockgen <-chan time.Time
	block    *Block
}

func NewStateSwarmInformed(chain *BlockChain) (s *StateSwarmInformed) {
	s = new(StateSwarmInformed)
	s.chain = chain
	s.blockgen = time.Tick(5 * time.Second)

	s.learning = true

	go s.broadcastLife()
	go s.checkBlockGen()
	return
}

func (s *StateSwarmInformed) blockCompiler() (compiler string) {

	hosts := make([]string, 0, len(s.hostsseen))

	//Pull all hosts who we haven't seen skipping a block
	for host, skipped := range s.hostsseen {
		if skipped != 0 {
			continue
		}
		hosts = append(hosts, host)
	}

	//Check if we should be the block generator
	compiler = common.RendezvousHash(sha256.New(), hosts, s.chain.Host)
	return
}

func (s *StateSwarmInformed) checkBlockGen() {

	var compiler string

	for _ = range s.blockgen {

		if s.learning {
			s.learning = false
			continue
		}

		if len(s.chain.BlockHistory) == 0 {
			s.hostsseen[compiler] += 1
		}

		//Dont't try to generate a block if we don't have a majority of hosts
		if len(s.hostsseen) < 128 {
			continue
			//Should actually switch to state swarmdied / join
		}

		compiler = s.blockCompiler()

		if compiler == s.chain.Host {

			id, err := common.RandomString(8)
			if err != nil {
				panic("checkBlockGenRandom" + err.Error())
			}
			b := &Block{id, s.chain.Id, s.chain.Host, nil, nil, nil}
			b.StorageMapping = make(map[string]interface{})
			for host, _ := range s.hostsseen {
				b.StorageMapping[host] = nil
			}

			s.chain.outgoingTransactions <- common.BlockNetworkObject(b)
		}
	}
}

func (s *StateSwarmInformed) broadcastLife() {
	s.broadcastcount += 1
	s.chain.outgoingTransactions <- common.TransactionNetworkObject(
		NewNodeAlive(s.chain.Host, s.chain.Id))
}

func (s *StateSwarmInformed) HandleTransaction(t common.Transaction) {
	switch n := t.(type) {
	case *NodeAlive:
		if !s.learning {
			return
		}

		s.hostsseen[n.Node] = 0
		// Resend hostsseen count once you have seen a majority of hosts
		if len(s.hostsseen) > 128 && s.broadcastcount < 2 {
			s.broadcastLife()
		}
	default:
		return
	}
}

func (s *StateSwarmInformed) HandleBlock(b *Block) {

	// If the learning timeout hasn't expired, don't accept blocks
	if s.learning {
		return
	}

	// All blocks in this state should be generated by the ideal host
	if b.Compiler != s.blockCompiler() {
		return
	}

	// We are looking for a block to generate a heartbeat for
	if len(s.chain.BlockHistory) == 0 {
		s.chain.AddBlock(b)

		if _, ok := b.StorageMapping[s.chain.Host]; ok {
			//Generate heartbeat for block
		}
	}

	// We're looking for the block with heartbeats to figure out if we're in
	// it
	if len(s.chain.BlockHistory) == 1 {
		if _, ok := b.StorageMapping[s.chain.Host]; ok {
			//If we're in the block switch to signal mode
			//s.chain.state = NewStateSwarmConnected()
		} else {
			//Join the swarm
			//s.chain.state = NewStateSwarmJoin()

		}
	}

}
<commit_msg>Change State Interface to Return State from handleBlock<commit_after>package swarm

import (
	"common"
	"crypto/sha256"
	"time"
)

type State interface {
	HandleTransaction(t common.Transaction)
	HandleBlock(b *Block) State
}

func newBlockChain(Host string, Id string, StorageMapping map[string]interface{}) (b *BlockChain) {
	b = new(BlockChain)
	b.Id = Id
	b.StorageMapping = StorageMapping
	b.outgoingTransactions = make(chan common.NetworkObject)
	return
}

func NewBlockChain(Host string, Id string, StorageMapping map[string]interface{}) (b *BlockChain) {
	b = newBlockChain(Host, Id, StorageMapping)
	b.state = NewStateSwarmInformed(b)
	return
}

func JoinBlockChain(Host string, Id string) (b *BlockChain) {
	b = newBlockChain(Host, Id, make(map[string]interface{}))
	//b.state = NewStateSwarmJoin(b)
	return
}

//List of states
// SwarmInformed - Swarm member shave been told to join swarm
// SwarmConnected - Swarm Members have succesfully formed a block
// SwarmLive - Swarm has sent a signal to the parent blockchain saying it is
//             alive and is in the steady state
// SwarmJoin - We are joining an already alive swarm
// SwarmDied - The swarm has died, terminate

type StateSwarmInformed struct {
	//Map of hosts seen to number of times they have failed to generate a block
	//Used for both host alive tracking & host block generation tracking
	hostsseen map[string]int

	// How many times we have broadcast that we are alive, we use a two stage
	// process where we broadcast, and then broadcast again when we have seen
	// enough nodes up to form a majority
	broadcastcount uint

	// This state has two phases, the learning phase where it listens for new
	// hosts and then the commit stage where it listens for a block that
	// is correct according to its knowledge and then votes for it.
	learning bool

	chain    *BlockChain
	blockgen <-chan time.Time
	block    *Block
}

func NewStateSwarmInformed(chain *BlockChain) (s *StateSwarmInformed) {
	s = new(StateSwarmInformed)
	s.chain = chain
	s.blockgen = time.Tick(5 * time.Second)

	s.learning = true

	go s.broadcastLife()
	go s.checkBlockGen()
	return
}

func (s *StateSwarmInformed) blockCompiler() (compiler string) {

	hosts := make([]string, 0, len(s.hostsseen))

	//Pull all hosts who we haven't seen skipping a block
	for host, skipped := range s.hostsseen {
		if skipped != 0 {
			continue
		}
		hosts = append(hosts, host)
	}

	//Check if we should be the block generator
	compiler = common.RendezvousHash(sha256.New(), hosts, s.chain.Host)
	return
}

func (s *StateSwarmInformed) checkBlockGen() {

	var compiler string

	for _ = range s.blockgen {

		if s.learning {
			s.learning = false
			continue
		}

		if len(s.chain.BlockHistory) == 0 {
			s.hostsseen[compiler] += 1
		}

		//Dont't try to generate a block if we don't have a majority of hosts
		if len(s.hostsseen) < 128 {
			continue
			//Should actually switch to state swarmdied / join
		}

		compiler = s.blockCompiler()

		if compiler == s.chain.Host {

			id, err := common.RandomString(8)
			if err != nil {
				panic("checkBlockGenRandom" + err.Error())
			}
			b := &Block{id, s.chain.Id, s.chain.Host, nil, nil, nil}
			b.StorageMapping = make(map[string]interface{})
			for host, _ := range s.hostsseen {
				b.StorageMapping[host] = nil
			}

			s.chain.outgoingTransactions <- common.BlockNetworkObject(b)
		}
	}
}

func (s *StateSwarmInformed) broadcastLife() {
	s.broadcastcount += 1
	s.chain.outgoingTransactions <- common.TransactionNetworkObject(
		NewNodeAlive(s.chain.Host, s.chain.Id))
}

func (s *StateSwarmInformed) HandleTransaction(t common.Transaction) {
	switch n := t.(type) {
	case *NodeAlive:
		if !s.learning {
			return
		}

		s.hostsseen[n.Node] = 0
		// Resend hostsseen count once you have seen a majority of hosts
		if len(s.hostsseen) > 128 && s.broadcastcount < 2 {
			s.broadcastLife()
		}
	default:
		return
	}
}

func (s *StateSwarmInformed) HandleBlock(b *Block) State {

	// If the learning timeout hasn't expired, don't accept blocks
	if s.learning {
		return s
	}

	// All blocks in this state should be generated by the ideal host
	if b.Compiler != s.blockCompiler() {
		return s
	}

	// We are looking for a block to generate a heartbeat for
	if len(s.chain.BlockHistory) == 0 {
		s.chain.AddBlock(b)

		if _, ok := b.StorageMapping[s.chain.Host]; ok {
			//Generate heartbeat for block
		}
	}

	// We're looking for the block with heartbeats to figure out if we're in
	// it
	if len(s.chain.BlockHistory) == 1 {
		if _, ok := b.StorageMapping[s.chain.Host]; ok {
			//If we're in the block switch to signal mode
			//s.chain.state = NewStateSwarmConnected()
		} else {
			//Join the swarm
			//s.chain.state = NewStateSwarmJoin()

		}
	}

	return s

}
<|endoftext|>


<commit_before>package main

import (
	"errors"
	"fmt"

	pb "github.com/khyew/grpc-talk"
	context "golang.org/x/net/context"
	"google.golang.org/grpc/peer"
)

// Hello is an implementation of the gRPC-defined rpc method
// It responds to clients with an appropriate reply for their hellos
func (h *HelloServer) Hello(ctx context.Context, request *pb.HelloRequest) (*pb.HelloResponse, error) {
	pr, _ := peer.FromContext(ctx)
	name := request.GetName()

	if name == "" {
		fmt.Printf("Received a hello from %v but it contained no name; responding with an error\n", pr.Addr)
		return nil, errors.New("Didn't receive a name")
	}

	fmt.Printf("Received a hello from %v. Sending a kind reply :)\n", pr.Addr)
	response := &pb.HelloResponse{
		Reply: fmt.Sprintf("Hello %v! Nice to meet you :)", name),
	}

	return response, nil
}

// HelloServer is an implementation of the demo protobuf-defined server interface
type HelloServer struct{}
<commit_msg>cleaned up error messages in server<commit_after>package main

import (
	"errors"
	"fmt"

	pb "github.com/khyew/grpc-talk"
	context "golang.org/x/net/context"
	"google.golang.org/grpc/peer"
)

// Hello is an implementation of the gRPC-defined rpc method
// It responds to clients with an appropriate reply for their hellos
func (h *HelloServer) Hello(ctx context.Context, request *pb.HelloRequest) (*pb.HelloResponse, error) {
	pr, _ := peer.FromContext(ctx)
	name := request.GetName()

	if name == "" {
		fmt.Printf("Received a hello from %v but it contained no name; responding with an error\n", pr.Addr)
		return nil, errors.New("Didn't receive a name")
	}

	fmt.Printf("Received a hello from %v at %v. Sending a kind reply :)\n", name, pr.Addr)
	response := &pb.HelloResponse{
		Reply: fmt.Sprintf("Hello %v! Nice to meet you :)", name),
	}

	return response, nil
}

// HelloServer is an implementation of the demo protobuf-defined server interface
type HelloServer struct{}
<|endoftext|>


<commit_before>// Copyright 2015 The rkt Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import (
	"fmt"
	"os"
	"testing"

	"github.com/coreos/rkt/tests/testutils"
)

var expectedResults = []string{
	"prestart OK",
	"main OK",
	"sidekick OK",
	"poststop OK",
}

func TestAceValidator(t *testing.T) {
	ctx := testutils.NewRktRunCtx()
	defer ctx.Cleanup()

	if err := ctx.LaunchMDS(); err != nil {
		t.Fatalf("Cannot launch metadata service: %v", err)
	}

	aceMain := os.Getenv("RKT_ACE_MAIN_IMAGE")
	if aceMain == "" {
		panic("empty RKT_ACE_MAIN_IMAGE env var")
	}
	aceSidekick := os.Getenv("RKT_ACE_SIDEKICK_IMAGE")
	if aceSidekick == "" {
		panic("empty RKT_ACE_SIDEKICK_IMAGE env var")
	}

	rktArgs := fmt.Sprintf("--debug --insecure-skip-verify run --mds-register --volume database,kind=empty %s %s",
		aceMain, aceSidekick)
	rktCmd := fmt.Sprintf("%s %s", ctx.Cmd(), rktArgs)

	child := spawnOrFail(t, rktCmd)
	defer waitOrFail(t, child, true)

	for _, e := range expectedResults {
		if err := expectWithOutput(child, e); err != nil {
			t.Fatalf("Expected %q but not found: %v", e, err)
		}
	}
}
<commit_msg>functional tests: Simplify getting values from env<commit_after>// Copyright 2015 The rkt Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import (
	"fmt"
	"testing"

	"github.com/coreos/rkt/tests/testutils"
)

var expectedResults = []string{
	"prestart OK",
	"main OK",
	"sidekick OK",
	"poststop OK",
}

func TestAceValidator(t *testing.T) {
	ctx := testutils.NewRktRunCtx()
	defer ctx.Cleanup()

	if err := ctx.LaunchMDS(); err != nil {
		t.Fatalf("Cannot launch metadata service: %v", err)
	}

	aceMain := testutils.GetValueFromEnvOrPanic("RKT_ACE_MAIN_IMAGE")
	aceSidekick := testutils.GetValueFromEnvOrPanic("RKT_ACE_SIDEKICK_IMAGE")

	rktArgs := fmt.Sprintf("--debug --insecure-skip-verify run --mds-register --volume database,kind=empty %s %s",
		aceMain, aceSidekick)
	rktCmd := fmt.Sprintf("%s %s", ctx.Cmd(), rktArgs)

	child := spawnOrFail(t, rktCmd)
	defer waitOrFail(t, child, true)

	for _, e := range expectedResults {
		if err := expectWithOutput(child, e); err != nil {
			t.Fatalf("Expected %q but not found: %v", e, err)
		}
	}
}
<|endoftext|>