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

Commit

Permalink
Dev (#61)
Browse files Browse the repository at this point in the history
* Specifiy the branches of CI

It's enough to test on master and dev branch. Furthermore, no need to
clone dev branch particularly in script part

* Fix a bug(#46)

* Close(#41)

* Replace the basic log statement with logrus

* Finish the basic log replacement

* Add block process to build accountutxos db  (#53)

* Add block process to build accountutxos db

* Update 544bf9b

* Update init pin.Store

* Rename some variables

* Update gofmt

* Modify io_test to not support bytom/log && Add logrus in glide.lock

* Replace the basic log in p2p (#60)

* Account bind key && Asset bind key (#59)

* Add bindAccount && bindAsset function in bytomcli

* Add signTransactions in bytomcli
  • Loading branch information
gguoss authored Oct 20, 2017
1 parent ddd0e8b commit 94fa4ba
Show file tree
Hide file tree
Showing 33 changed files with 959 additions and 468 deletions.
20 changes: 13 additions & 7 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
language: go
dist: trusty
sudo: false

go:
- 1.8.3
- 1.9
- tip
- 1.8.3
- 1.9
- tip

branches:
only:
- master
- dev

script:
- git clone -b dev https://github.com/Bytom/bytom.git $GOPATH/src/github.com/bytom
- cd $GOPATH/src/github.com/bytom
- make install
- make test
- git clone https://github.com/Bytom/bytom.git $GOPATH/src/github.com/bytom
- cd $GOPATH/src/github.com/bytom
- make install
- make test
132 changes: 73 additions & 59 deletions blockchain/account/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,23 @@
package account

import (
"context"
// stdsql "database/sql"
"encoding/json"
"fmt"
"sync"
"time"
"context"
"encoding/json"

"github.com/bytom/log"
"github.com/bytom/errors"
"github.com/bytom/protocol"
"github.com/bytom/blockchain/pin"
"github.com/golang/groupcache/lru"
//"github.com/lib/pq"

// "chain/core/pin"
"github.com/bytom/crypto/sha3pool"
"github.com/bytom/protocol/vm/vmutil"
"github.com/bytom/blockchain/signers"
"github.com/bytom/blockchain/txbuilder"
"github.com/bytom/crypto/ed25519/chainkd"
// "chain/database/pg"
"github.com/bytom/errors"
"github.com/bytom/log"
"github.com/bytom/protocol"
"github.com/bytom/protocol/vm/vmutil"

dbm "github.com/tendermint/tmlibs/db"
)

Expand All @@ -31,12 +29,12 @@ var (
ErrBadIdentifier = errors.New("either ID or alias must be specified, and not both")
)

func NewManager(db dbm.DB, chain *protocol.Chain /*, pinStore *pin.Store*/) *Manager {
func NewManager(db dbm.DB, chain *protocol.Chain , pinStore *pin.Store) *Manager {
return &Manager{
db: db,
chain: chain,
utxoDB: newReserver(db, chain /*, pinStore*/),
// pinStore: pinStore,
utxoDB: newReserver(db, chain),
pinStore: pinStore,
cache: lru.New(maxAccountCache),
aliasCache: lru.New(maxAccountCache),
delayedACPs: make(map[*txbuilder.TemplateBuilder][]*controlProgram),
Expand All @@ -49,7 +47,7 @@ type Manager struct {
chain *protocol.Chain
utxoDB *reserver
indexer Saver
// pinStore *pin.Store
pinStore *pin.Store

cacheMu sync.Mutex
cache *lru.Cache
Expand Down Expand Up @@ -102,7 +100,7 @@ func (m *Manager) Create(ctx context.Context, xpubs []chainkd.XPub, quorum int,
return nil, errors.Wrap(err)
}

account_id := []byte(accountSigner.ID)
account_id := json.RawMessage(accountSigner.ID)
account := &Account{
Signer: accountSigner,
Alias: alias,
Expand All @@ -114,8 +112,8 @@ func (m *Manager) Create(ctx context.Context, xpubs []chainkd.XPub, quorum int,
return nil, errors.Wrap(err, "failed marshal account")
}
if len(acc) > 0 {
m.db.Set(account_id, json.RawMessage(acc))
m.db.Set([]byte(alias), account_id)
m.db.Set(account_id, acc)
m.db.Set(json.RawMessage("ali"+alias), account_id)
}

err = m.indexAnnotatedAccount(ctx, account)
Expand All @@ -138,7 +136,7 @@ func (m *Manager) UpdateTags(ctx context.Context, id, alias *string, tags map[st
if alias != nil {
key_id = m.db.Get([]byte(*alias))
} else {
key_id = []byte(*id)
key_id = json.RawMessage(*id)
}

bytes := m.db.Get(key_id)
Expand Down Expand Up @@ -173,7 +171,7 @@ func (m *Manager) UpdateTags(ctx context.Context, id, alias *string, tags map[st

} else {

m.db.Set(key_id, json.RawMessage(acc))
m.db.Set(key_id, acc)
return nil
}

Expand Down Expand Up @@ -214,14 +212,23 @@ func (m *Manager) findByID(ctx context.Context, id string) (*signers.Signer, err
if ok {
return cached.(*signers.Signer), nil
}
account, err := signers.Find(ctx, m.db, "account", id)

bytes := m.db.Get(json.RawMessage(id))
if bytes == nil {
return nil,errors.New("not find this account.")
}

var account Account
err := json.Unmarshal(bytes, &account)
if err != nil {
return nil, err
return nil,errors.New("failed unmarshal this account.")
}


m.cacheMu.Lock()
m.cache.Add(id, account)
m.cache.Add(id, account.Signer)
m.cacheMu.Unlock()
return account, nil
return account.Signer, nil
}

type controlProgram struct {
Expand Down Expand Up @@ -273,31 +280,40 @@ func (m *Manager) CreateControlProgram(ctx context.Context, accountID string, ch
return cp.controlProgram, nil
}

type ControlProgram struct {
AccountID string
KeyIndex uint64
ControlProgram []byte
Change bool
ExpiresAt time.Time
}

func (m *Manager) insertAccountControlProgram(ctx context.Context, progs ...*controlProgram) error {
/*const q = `
INSERT INTO account_control_programs (signer_id, key_index, control_program, change, expires_at)
SELECT unnest($1::text[]), unnest($2::bigint[]), unnest($3::bytea[]), unnest($4::boolean[]),
unnest($5::timestamp with time zone[])
`
var (
accountIDs pq.StringArray
keyIndexes pq.Int64Array
controlProgs pq.ByteaArray
change pq.BoolArray
expirations []stdsql.NullString
)

var b32 [32]byte
for _, p := range progs {
accountIDs = append(accountIDs, p.accountID)
keyIndexes = append(keyIndexes, int64(p.keyIndex))
controlProgs = append(controlProgs, p.controlProgram)
change = append(change, p.change)
expirations = append(expirations, stdsql.NullString{
String: p.expiresAt.Format(time.RFC3339),
Valid: !p.expiresAt.IsZero(),
})
}*/

// _, err := m.dbm.ExecContext(ctx, q, accountIDs, keyIndexes, controlProgs, change, pq.Array(expirations))

acp, err := json.Marshal(&struct{
AccountID string
KeyIndex uint64
ControlProgram []byte
Change bool
ExpiresAt time.Time}{
AccountID: p.accountID,
KeyIndex: p.keyIndex,
ControlProgram: p.controlProgram,
Change: p.change,
ExpiresAt: p.expiresAt})

if err != nil {
return errors.Wrap(err, "failed marshal controlProgram")
}
if len(acp) > 0 {
sha3pool.Sum256(b32[:], p.controlProgram)
m.db.Set(json.RawMessage("acp"+string(b32[:])), acp)
}
}

return errors.Wrap(nil)
}

Expand All @@ -306,15 +322,14 @@ func (m *Manager) nextIndex(ctx context.Context) (uint64, error) {
defer m.acpMu.Unlock()

if m.acpIndexNext >= m.acpIndexCap {
/*var cap uint64
const incrby = 10000 // account_control_program_seq increments by 10,000
const q = `SELECT nextval('account_control_program_seq')`
err := m.db.QueryRowContext(ctx, q).Scan(&cap)
if err != nil {
return 0, errors.Wrap(err, "scan")

const incrby = 10000 // start 1,increments by 10,000
if(m.acpIndexCap <= incrby){
m.acpIndexCap = incrby + 1
}else{
m.acpIndexCap += incrby
}
m.acpIndexCap = cap
m.acpIndexNext = cap - incrby*/
m.acpIndexNext = m.acpIndexCap - incrby
}

n := m.acpIndexNext
Expand All @@ -327,12 +342,11 @@ func (m *Manager) QueryAll(ctx context.Context) (interface{}, error) {

iter := m.db.Iterator()
for iter.Next() {
value := string(iter.Value())
if value[:3] == "acc" {
key := string(iter.Key())
if key[:3] != "acc" {
continue
}
ret = append(ret, value)
//log.Printf(ctx,"%s\t", value)
ret = append(ret, string(iter.Value()))
}

return ret, nil
Expand Down
16 changes: 11 additions & 5 deletions blockchain/account/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,17 @@ func (a *spendUTXOAction) Build(ctx context.Context, b *txbuilder.TemplateBuilde
}
b.OnRollback(canceler(ctx, a.accounts, res.ID))

acct, err := a.accounts.findByID(ctx, res.Source.AccountID)
if err != nil {
return err
var acct *signers.Signer
if res.Source.AccountID == ""{
//TODO coinbase
acct = &signers.Signer{}
}else{
acct, err = a.accounts.findByID(ctx, res.Source.AccountID)
if err != nil {
return err
}
}

txInput, sigInst, err := utxoToInputs(ctx, acct, res.UTXOs[0], a.ReferenceData)
if err != nil {
return err
Expand Down Expand Up @@ -231,7 +238,6 @@ func (m *Manager) insertControlProgramDelayed(ctx context.Context, b *txbuilder.
if len(acps) == 0 {
return nil
}
// return m.insertAccountControlProgram(ctx, acps...)
return nil
return m.insertAccountControlProgram(ctx, acps...)
})
}
Loading

0 comments on commit 94fa4ba

Please sign in to comment.