Skip to content

Commit

Permalink
Merge a846d1e into 3708334
Browse files Browse the repository at this point in the history
  • Loading branch information
elvis88 committed May 23, 2019
2 parents 3708334 + a846d1e commit 4685d61
Show file tree
Hide file tree
Showing 12 changed files with 750 additions and 209 deletions.
78 changes: 49 additions & 29 deletions consensus/dpos/dpos.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,42 +240,62 @@ func (dpos *Dpos) Finalize(chain consensus.IChainReader, header *types.Header, t
return nil, err
}
candidate.Counter += dpos.config.BlockFrequency
if coffset < poffset {
coffset += dpos.config.CandidateScheduleSize
}
for coffset--; coffset != poffset; coffset-- {
name := pstate.ActivatedCandidateSchedule[coffset]
replace := false
for index, roffset := range pstate.OffCandidateSchedule {
if roffset == coffset {
replace = true
name = pstate.ActivatedCandidateSchedule[dpos.config.CandidateScheduleSize+uint64(index)]
timestamp := header.Time.Uint64()
if header.Time.Uint64()-parent.Time.Uint64() != dpos.config.blockInterval() {
epcho := dpos.config.epoch(parent.Time.Uint64())
for ; ; timestamp = header.Time.Uint64() - dpos.config.blockInterval() {
if dpos.config.epoch(timestamp) == epcho {
if offset := dpos.config.getoffset(timestamp); offset != coffset {
coffset = offset + 1
}
break
}
}
pcandidate, err := sys.GetCandidate(name)
if err != nil {
return nil, err
}
pcandidate.Counter += dpos.config.BlockFrequency
if err := sys.SetCandidate(pcandidate); err != nil {
return nil, err
}
if dpos.config.epoch(timestamp) == dpos.config.epoch(parent.Time.Uint64()) {
if coffset < poffset {
coffset += dpos.config.CandidateScheduleSize
}

if !replace {
// replace
opcandidate, err := sys.GetCandidateByEpcho(pstate.Epcho, name)
if err != nil {
return nil, err
}
acnt := pcandidate.ActualCounter - opcandidate.ActualCounter
scnt := pcandidate.Counter - opcandidate.Counter
if scnt-acnt > dpos.config.maxMissing() && uint64(len(pstate.OffCandidateSchedule)) <= dpos.config.BackupScheduleSize {
pstate.OffCandidateSchedule = append(pstate.OffCandidateSchedule, coffset)
if err := sys.SetState(pstate); err != nil {
log.Info("candidate replace yes or not", "prev offset", poffset, "next offset", coffset, "num", header.Number)
for coffset--; coffset != poffset; coffset-- {
index := coffset % dpos.config.CandidateScheduleSize
if index < uint64(len(pstate.ActivatedCandidateSchedule)) {
name := pstate.ActivatedCandidateSchedule[index]
replace := false
for index, roffset := range pstate.OffCandidateSchedule {
if roffset == coffset {
replace = true
name = pstate.ActivatedCandidateSchedule[dpos.config.CandidateScheduleSize+uint64(index)]
break
}
}
pcandidate, err := sys.GetCandidate(name)
if err != nil {
return nil, err
}
pcandidate.Counter += dpos.config.BlockFrequency
if err := sys.SetCandidate(pcandidate); err != nil {
return nil, err
}

if !replace {
// replace
opcandidate, err := sys.GetCandidateByEpcho(pstate.Epcho, name)
if err != nil {
return nil, err
}
acnt := pcandidate.ActualCounter - opcandidate.ActualCounter
scnt := pcandidate.Counter - opcandidate.Counter
log.Info("candidate replace", "prev should", opcandidate.Counter, "prev actual", opcandidate.ActualCounter, "next should", pcandidate.Counter, "next actual", pcandidate.ActualCounter, "max", dpos.config.maxMissing(), "num", header.Number)
if scnt-acnt > dpos.config.maxMissing() && uint64(len(pstate.OffCandidateSchedule)) < uint64(len(pstate.ActivatedCandidateSchedule))-dpos.config.CandidateScheduleSize {
pstate.OffCandidateSchedule = append(pstate.OffCandidateSchedule, coffset)
if err := sys.SetState(pstate); err != nil {
return nil, err
}
}
}
}

}
}
}
Expand Down
103 changes: 101 additions & 2 deletions sdk/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"time"

"github.com/fractalplatform/fractal/accountmanager"
"github.com/fractalplatform/fractal/asset"
"github.com/fractalplatform/fractal/common"
"github.com/fractalplatform/fractal/consensus/dpos"
"github.com/fractalplatform/fractal/crypto"
Expand Down Expand Up @@ -169,6 +168,56 @@ func (acc *Account) UpdateAccount(to common.Name, value *big.Int, id uint64, gas
return
}

// UpdateAccountAuthor update accout
func (acc *Account) UpdateAccountAuthor(to common.Name, value *big.Int, id uint64, gas uint64, newacct *accountmanager.AccountAuthorAction) (hash common.Hash, err error) {
nonce := acc.nonce
if nonce == math.MaxUint64 {
nonce, err = acc.api.AccountNonce(acc.name.String())
if err != nil {
return
}
}

bts, _ := rlp.EncodeToBytes(newacct)
action := types.NewAction(types.UpdateAccountAuthor, acc.name, to, nonce, id, gas, value, bts, nil)
tx := types.NewTransaction(acc.feeid, acc.gasprice, []*types.Action{action}...)
key := types.MakeKeyPair(acc.priv, []uint64{0})
err = types.SignActionWithMultiKey(action, tx, types.NewSigner(acc.chainID), []*types.KeyPair{key})
if err != nil {
return
}
rawtx, _ := rlp.EncodeToBytes(tx)
checked := acc.checked || acc.nonce == math.MaxUint64
var checkedfunc func() error
if checked {
// before
checkedfunc, err = acc.checkUpdateAccount(action)
if err != nil {
return
}
}
hash, err = acc.api.SendRawTransaction(rawtx)
if err != nil {
return
}
if checked {
//after
err = acc.utilReceipt(hash, timeout)
if err != nil {
return
}
err = checkedfunc()
if err != nil {
return
}
}

if acc.nonce != math.MaxUint64 {
acc.nonce++
}
return
}

// Transfer transfer tokens
func (acc *Account) Transfer(to common.Name, value *big.Int, id uint64, gas uint64) (hash common.Hash, err error) {
nonce := acc.nonce
Expand Down Expand Up @@ -425,7 +474,7 @@ func (acc *Account) DestroyAsset(to common.Name, value *big.Int, id uint64, gas
}

// SetAssetOwner update asset owner
func (acc *Account) SetAssetOwner(to common.Name, value *big.Int, id uint64, gas uint64, asset *asset.AssetObject) (hash common.Hash, err error) {
func (acc *Account) SetAssetOwner(to common.Name, value *big.Int, id uint64, gas uint64, asset *accountmanager.UpdateAssetOwner) (hash common.Hash, err error) {
nonce := acc.nonce
if nonce == math.MaxUint64 {
nonce, err = acc.api.AccountNonce(acc.name.String())
Expand Down Expand Up @@ -627,6 +676,56 @@ func (acc *Account) UnRegCandidate(to common.Name, value *big.Int, id uint64, ga
return
}

// RefundCandidate refund cadiate
func (acc *Account) RefundCandidate(to common.Name, value *big.Int, id uint64, gas uint64) (hash common.Hash, err error) {
nonce := acc.nonce
if nonce == math.MaxUint64 {
nonce, err = acc.api.AccountNonce(acc.name.String())
if err != nil {
return
}
}

action := types.NewAction(types.RefundCandidate, acc.name, to, nonce, id, gas, value, nil, nil)
tx := types.NewTransaction(acc.feeid, big.NewInt(1e10), []*types.Action{action}...)
key := types.MakeKeyPair(acc.priv, []uint64{0})

err = types.SignActionWithMultiKey(action, tx, types.NewSigner(acc.chainID), []*types.KeyPair{key})
if err != nil {
panic(err)
}
rawtx, _ := rlp.EncodeToBytes(tx)
checked := acc.checked || acc.nonce == math.MaxUint64
var checkedfunc func() error
if checked {
// before
checkedfunc, err = acc.chekRefoundProdoucer(action)
if err != nil {
return
}
}
hash, err = acc.api.SendRawTransaction(rawtx)
if err != nil {
return
}
if checked {
// after
err = acc.utilReceipt(hash, timeout)
if err != nil {
return
}
err = checkedfunc()
if err != nil {
return
}
}

if acc.nonce != math.MaxUint64 {
acc.nonce++
}
return
}

// VoteCandidate vote cadiate
func (acc *Account) VoteCandidate(to common.Name, value *big.Int, id uint64, gas uint64, arg *dpos.VoteCandidate) (hash common.Hash, err error) {
nonce := acc.nonce
Expand Down

0 comments on commit 4685d61

Please sign in to comment.