Skip to content
This repository has been archived by the owner on Mar 21, 2024. It is now read-only.

Commit

Permalink
Merge pull request #606 from Bytom/dev
Browse files Browse the repository at this point in the history
merge dev
  • Loading branch information
wliyongfeng authored Apr 12, 2018
2 parents fa4a9dd + 5a2882b commit f7709ca
Show file tree
Hide file tree
Showing 80 changed files with 3,924 additions and 1,046 deletions.
10 changes: 8 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,11 @@ all: test target release-all

bytomd:
@echo "Building bytomd to cmd/bytomd/bytomd"
@go build -ldflags "-X github.com/bytom/version.GitCommit=`git rev-parse HEAD`" \
-o cmd/bytomd/bytomd cmd/bytomd/main.go
go build $(BUILD_FLAGS) -o cmd/bytomd/bytomd cmd/bytomd/main.go

bytomcli:
@echo "Building bytomcli to cmd/bytomcli/bytomcli"
go build $(BUILD_FLAGS) -o cmd/bytomcli/bytomcli cmd/bytomcli/main.go

target:
mkdir -p $@
Expand Down Expand Up @@ -104,4 +107,7 @@ test:
benchmark:
go test -bench $(PACKAGES)

functional-tests:
@go test -v -timeout=30m -tags=functional ./test

.PHONY: all target release-all clean test benchmark
25 changes: 10 additions & 15 deletions account/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,7 @@ func (m *Manager) Create(ctx context.Context, xpubs []chainkd.XPub, quorum int,
return nil, ErrDuplicateAlias
}

nextAccountIndex := m.getNextXpubsIndex(xpubs)

signer, err := signers.Create("account", xpubs, quorum, nextAccountIndex)
signer, err := signers.Create("account", xpubs, quorum, m.getNextXpubsIndex(xpubs))
id := signers.IDGenerate()
if err != nil {
return nil, errors.Wrap(err)
Expand Down Expand Up @@ -265,20 +263,20 @@ func (m *Manager) GetAliasByID(id string) string {
}

// CreateAddress generate an address for the select account
func (m *Manager) CreateAddress(ctx context.Context, accountID string, change bool) (cp *CtrlProgram, err error) {
func (m *Manager) CreateAddress(ctx context.Context, accountID string) (cp *CtrlProgram, err error) {
account, err := m.findByID(ctx, accountID)
if err != nil {
return nil, err
}
return m.createAddress(ctx, account, change)
return m.createAddress(ctx, account)
}

// CreateAddress generate an address for the select account
func (m *Manager) createAddress(ctx context.Context, account *Account, change bool) (cp *CtrlProgram, err error) {
func (m *Manager) createAddress(ctx context.Context, account *Account) (cp *CtrlProgram, err error) {
if len(account.XPubs) == 1 {
cp, err = m.createP2PKH(ctx, account, change)
cp, err = m.createP2PKH(ctx, account)
} else {
cp, err = m.createP2SH(ctx, account, change)
cp, err = m.createP2SH(ctx, account)
}
if err != nil {
return nil, err
Expand All @@ -290,7 +288,7 @@ func (m *Manager) createAddress(ctx context.Context, account *Account, change bo
return cp, nil
}

func (m *Manager) createP2PKH(ctx context.Context, account *Account, change bool) (*CtrlProgram, error) {
func (m *Manager) createP2PKH(ctx context.Context, account *Account) (*CtrlProgram, error) {
idx := m.getNextXpubsIndex(account.Signer.XPubs)
path := signers.Path(account.Signer, signers.AccountKeySpace, idx)
derivedXPubs := chainkd.DeriveXPubs(account.XPubs, path)
Expand All @@ -313,11 +311,10 @@ func (m *Manager) createP2PKH(ctx context.Context, account *Account, change bool
Address: address.EncodeAddress(),
KeyIndex: idx,
ControlProgram: control,
Change: change,
}, nil
}

func (m *Manager) createP2SH(ctx context.Context, account *Account, change bool) (*CtrlProgram, error) {
func (m *Manager) createP2SH(ctx context.Context, account *Account) (*CtrlProgram, error) {
idx := m.getNextXpubsIndex(account.Signer.XPubs)
path := signers.Path(account.Signer, signers.AccountKeySpace, idx)
derivedXPubs := chainkd.DeriveXPubs(account.XPubs, path)
Expand All @@ -344,7 +341,6 @@ func (m *Manager) createP2SH(ctx context.Context, account *Account, change bool)
Address: address.EncodeAddress(),
KeyIndex: idx,
ControlProgram: control,
Change: change,
}, nil
}

Expand All @@ -354,7 +350,6 @@ type CtrlProgram struct {
Address string
KeyIndex uint64
ControlProgram []byte
Change bool
}

func (m *Manager) insertAccountControlProgram(ctx context.Context, progs ...*CtrlProgram) error {
Expand Down Expand Up @@ -398,7 +393,7 @@ func (m *Manager) GetCoinbaseControlProgram() ([]byte, error) {
return nil, err
}

program, err := m.createAddress(nil, account, false)
program, err := m.createAddress(nil, account)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -455,7 +450,7 @@ func (m *Manager) ListAccounts(id string) ([]*Account, error) {

// ListControlProgram return all the local control program
func (m *Manager) ListControlProgram() ([]*CtrlProgram, error) {
cps := []*CtrlProgram{}
var cps []*CtrlProgram
cpIter := m.db.IteratorPrefix([]byte(accountCPPrefix))
defer cpIter.Release()

Expand Down
37 changes: 1 addition & 36 deletions account/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func (a *spendAction) Build(ctx context.Context, b *txbuilder.TemplateBuilder) e
}

if res.Change > 0 {
acp, err := a.accounts.CreateAddress(ctx, a.AccountID, true)
acp, err := a.accounts.CreateAddress(ctx, a.AccountID)
if err != nil {
return errors.Wrap(err, "creating control program")
}
Expand Down Expand Up @@ -182,41 +182,6 @@ func UtxoToInputs(signer *signers.Signer, u *UTXO) (*types.TxInput, *txbuilder.S
return txInput, sigInst, nil
}

//DecodeControlAction unmarshal JSON-encoded data of control action
func (m *Manager) DecodeControlAction(data []byte) (txbuilder.Action, error) {
a := &controlAction{accounts: m}
err := json.Unmarshal(data, a)
return a, err
}

type controlAction struct {
accounts *Manager
bc.AssetAmount
AccountID string `json:"account_id"`
}

func (a *controlAction) Build(ctx context.Context, b *txbuilder.TemplateBuilder) error {
var missing []string
if a.AccountID == "" {
missing = append(missing, "account_id")
}
if a.AssetId.IsZero() {
missing = append(missing, "asset_id")
}
if len(missing) > 0 {
return txbuilder.MissingFieldsError(missing...)
}

// Produce a control program, but don't insert it into the database yet.
acp, err := a.accounts.CreateAddress(ctx, a.AccountID, false)
if err != nil {
return err
}
a.accounts.insertControlProgramDelayed(ctx, b, acp)

return b.AddOutput(types.NewTxOutput(*a.AssetId, a.Amount, acp.ControlProgram))
}

// insertControlProgramDelayed takes a template builder and an account
// control program that hasn't been inserted to the database yet. It
// registers callbacks on the TemplateBuilder so that all of the template's
Expand Down
4 changes: 2 additions & 2 deletions account/reserve.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ func (re *reserver) reserveUTXO(ctx context.Context, out bc.Hash, exp time.Time,
}

//u.ValidHeight > 0 means coinbase utxo
if u.ValidHeight > 0 && u.ValidHeight > re.c.Height() {
if u.ValidHeight > 0 && u.ValidHeight > re.c.BestBlockHeight() {
return nil, errors.WithDetail(ErrMatchUTXO, "this coinbase utxo is immature")
}

Expand Down Expand Up @@ -257,7 +257,7 @@ func (re *reserver) source(src source) *sourceReserver {
db: re.db,
src: src,
reserved: make(map[bc.Hash]uint64),
currentHeight: re.c.Height,
currentHeight: re.c.BestBlockHeight,
}
re.sources[src] = sr
return sr
Expand Down
2 changes: 1 addition & 1 deletion account/reserve_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func TestCancelReservation(t *testing.T) {
t.Fatal(err)
}

controlProg, err := accountManager.CreateAddress(nil, testAccount.ID, false)
controlProg, err := accountManager.CreateAddress(nil, testAccount.ID)
if err != nil {
t.Fatal(err)
}
Expand Down
6 changes: 3 additions & 3 deletions api/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,20 +108,20 @@ func (a *API) listAddresses(ctx context.Context, ins struct {
}) Response {
accountID := ins.AccountID
if ins.AccountAlias != "" {
account, err := a.wallet.AccountMgr.FindByAlias(ctx, ins.AccountAlias)
acc, err := a.wallet.AccountMgr.FindByAlias(ctx, ins.AccountAlias)
if err != nil {
return NewErrorResponse(err)
}

accountID = account.ID
accountID = acc.ID
}

cps, err := a.wallet.AccountMgr.ListControlProgram()
if err != nil {
return NewErrorResponse(err)
}

addresses := []*addressResp{}
var addresses []*addressResp
for _, cp := range cps {
if cp.Address == "" || (len(accountID) != 0 && strings.Compare(accountID, cp.AccountID) != 0) {
continue
Expand Down
2 changes: 2 additions & 0 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ func (a *API) buildHandler() {
m.Handle("/build-transaction", jsonHandler(a.build))
m.Handle("/sign-transaction", jsonHandler(a.pseudohsmSignTemplates))
m.Handle("/submit-transaction", jsonHandler(a.submit))
// TODO remove this api, separate sign and submit process
m.Handle("/sign-submit-transaction", jsonHandler(a.signSubmit))
m.Handle("/get-transaction", jsonHandler(a.getTransaction))
m.Handle("/list-transactions", jsonHandler(a.listTransactions))
Expand Down Expand Up @@ -231,6 +232,7 @@ func (a *API) buildHandler() {
m.Handle("/gas-rate", jsonHandler(a.gasRate))
m.Handle("/get-work", jsonHandler(a.getWork))
m.Handle("/submit-work", jsonHandler(a.submitWork))
m.Handle("/set-mining", jsonHandler(a.setMining))

handler := latencyHandler(m, walletEnable)
handler = maxBytesHandler(handler) // TODO(tessr): consider moving this to non-core specific mux
Expand Down
7 changes: 3 additions & 4 deletions api/block_retrieve.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
chainjson "github.com/bytom/encoding/json"
"github.com/bytom/protocol/bc"
"github.com/bytom/protocol/bc/types"
"github.com/bytom/wallet"
)

// return best block hash
Expand Down Expand Up @@ -120,10 +119,10 @@ func (a *API) getBlock(ins GetBlockReq) Response {
}

for i := range orig.Inputs {
tx.Inputs = append(tx.Inputs, wallet.BuildAnnotatedInput(orig, uint32(i)))
tx.Inputs = append(tx.Inputs, a.wallet.BuildAnnotatedInput(orig, uint32(i)))
}
for i := range orig.Outputs {
tx.Outputs = append(tx.Outputs, wallet.BuildAnnotatedOutput(orig, i))
tx.Outputs = append(tx.Outputs, a.wallet.BuildAnnotatedOutput(orig, i))
}
resp.Transactions = append(resp.Transactions, tx)
}
Expand Down Expand Up @@ -162,6 +161,6 @@ func (a *API) getBlockTransactionsCountByHeight(height uint64) Response {

// return current block count
func (a *API) getBlockCount() Response {
blockHeight := map[string]uint64{"block_count": a.chain.Height()}
blockHeight := map[string]uint64{"block_count": a.chain.BestBlockHeight()}
return NewSuccessResponse(blockHeight)
}
26 changes: 26 additions & 0 deletions api/miner.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package api
import (
"context"

"github.com/bytom/errors"
"github.com/bytom/protocol/bc"
"github.com/bytom/protocol/bc/types"
)
Expand Down Expand Up @@ -67,3 +68,28 @@ func (a *API) GetWork() (*GetWorkResp, error) {
func (a *API) SubmitWork(bh *types.BlockHeader) error {
return a.miningPool.SubmitWork(bh)
}

func (a *API) setMining(in struct {
IsMining bool `json:"is_mining"`
}) Response {
if in.IsMining {
return a.startMining()
}
return a.stopMining()
}

func (a *API) startMining() Response {
a.cpuMiner.Start()
if !a.IsMining() {
return NewErrorResponse(errors.New("Failed to start mining"))
}
return NewSuccessResponse("")
}

func (a *API) stopMining() Response {
a.cpuMiner.Stop()
if a.IsMining() {
return NewErrorResponse(errors.New("Failed to stop mining"))
}
return NewSuccessResponse("")
}
2 changes: 1 addition & 1 deletion api/nodeinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func (a *API) GetNodeInfo() *NetInfo {
Syncing: a.sync.BlockKeeper().IsCaughtUp(),
Mining: a.cpuMiner.IsMining(),
PeerCount: len(a.sync.Switch().Peers().List()),
CurrentBlock: a.chain.Height(),
CurrentBlock: a.chain.BestBlockHeight(),
}
_, info.HighestBlock = a.sync.Peers().BestPeer()
if info.CurrentBlock > info.HighestBlock {
Expand Down
2 changes: 1 addition & 1 deletion api/receivers.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func (a *API) createAccountReceiver(ctx context.Context, ins struct {
accountID = account.ID
}

program, err := a.wallet.AccountMgr.CreateAddress(ctx, accountID, false)
program, err := a.wallet.AccountMgr.CreateAddress(ctx, accountID)
if err != nil {
return NewErrorResponse(err)
}
Expand Down
11 changes: 7 additions & 4 deletions api/transact.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

log "github.com/sirupsen/logrus"

"github.com/bytom/blockchain/pseudohsm"
"github.com/bytom/blockchain/txbuilder"
"github.com/bytom/errors"
"github.com/bytom/net/http/reqid"
Expand All @@ -21,8 +22,6 @@ var defaultTxTTL = 5 * time.Minute
func (a *API) actionDecoder(action string) (func([]byte) (txbuilder.Action, error), bool) {
var decoder func([]byte) (txbuilder.Action, error)
switch action {
case "control_account":
decoder = a.wallet.AccountMgr.DecodeControlAction
case "control_address":
decoder = txbuilder.DecodeControlAddressAction
case "control_program":
Expand Down Expand Up @@ -184,13 +183,17 @@ func (a *API) submit(ctx context.Context, ins struct {

// POST /sign-submit-transaction
func (a *API) signSubmit(ctx context.Context, x struct {
Password []string `json:"password"`
Password string `json:"password"`
Txs txbuilder.Template `json:"transaction"`
}) Response {
if err := txbuilder.Sign(ctx, &x.Txs, nil, x.Password[0], a.pseudohsmSignTemplate); err != nil {
if err := txbuilder.Sign(ctx, &x.Txs, nil, x.Password, a.pseudohsmSignTemplate); err != nil {
log.WithField("build err", err).Error("fail on sign transaction.")
return NewErrorResponse(err)
}

if signCount, complete := txbuilder.SignInfo(&x.Txs); !complete && signCount == 0 {
return NewErrorResponse(pseudohsm.ErrLoadKey)
}
log.Info("Sign Transaction complete.")

txID, err := a.submitSingle(nil, &x.Txs)
Expand Down
4 changes: 3 additions & 1 deletion blockchain/query/annotated.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ type AnnotatedInput struct {
AssetDefinition *json.RawMessage `json:"asset_definition"`
Amount uint64 `json:"amount"`
IssuanceProgram chainjson.HexBytes `json:"issuance_program,omitempty"`
ControlProgram chainjson.HexBytes `json:"-"`
ControlProgram chainjson.HexBytes `json:"control_program,omitempty"`
Address string `json:"address,omitempty"`
SpentOutputID *bc.Hash `json:"spent_output_id,omitempty"`
AccountID string `json:"account_id,omitempty"`
AccountAlias string `json:"account_alias,omitempty"`
Expand All @@ -49,6 +50,7 @@ type AnnotatedOutput struct {
AccountID string `json:"account_id,omitempty"`
AccountAlias string `json:"account_alias,omitempty"`
ControlProgram chainjson.HexBytes `json:"control_program"`
Address string `json:"address,omitempty"`
}

//AnnotatedAccount means an annotated account.
Expand Down
10 changes: 3 additions & 7 deletions blockchain/rpc/types/responses.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import (
"strings"
"time"

"github.com/bytom/p2p"
"github.com/bytom/protocol/bc"
"github.com/bytom/types"
"github.com/tendermint/go-crypto"
"github.com/tendermint/go-wire/data"

"github.com/bytom/p2p"
"github.com/bytom/protocol/bc"
)

type BlockNonce [8]byte
Expand All @@ -17,10 +17,6 @@ type ResultBlockchainInfo struct {
LastHeight uint64 `json:"last_height"`
}

type ResultGenesis struct {
Genesis *types.GenesisDoc `json:"genesis"`
}

type ResultBlock struct {
}

Expand Down
Loading

0 comments on commit f7709ca

Please sign in to comment.