Skip to content

Commit

Permalink
add api isAutoBuyTicket, startAutoBuyTicket and stopAutoBuyTicket
Browse files Browse the repository at this point in the history
  • Loading branch information
zongyuan committed Jul 18, 2019
1 parent 3846065 commit 5ca9053
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 38 deletions.
15 changes: 3 additions & 12 deletions cmd/efsn/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,9 @@ import (
"strings"
"time"

"github.com/elastic/gosigar"
"github.com/FusionFoundation/efsn/accounts"
"github.com/FusionFoundation/efsn/accounts/keystore"
"github.com/FusionFoundation/efsn/cmd/utils"
"github.com/FusionFoundation/efsn/common"
"github.com/FusionFoundation/efsn/console"
"github.com/FusionFoundation/efsn/eth"
"github.com/FusionFoundation/efsn/ethclient"
Expand All @@ -41,6 +39,7 @@ import (
"github.com/FusionFoundation/efsn/log"
"github.com/FusionFoundation/efsn/metrics"
"github.com/FusionFoundation/efsn/node"
"github.com/elastic/gosigar"
"gopkg.in/urfave/cli.v1"
)

Expand Down Expand Up @@ -333,16 +332,8 @@ func startNode(ctx *cli.Context, stack *node.Node) {
}
}
}()
// Start auto buy tickets if enabled
if ctx.GlobalBool(utils.AutoBuyTicketsEnabledFlag.Name) {
// use first account
if unlocks != nil && passwords != nil {
common.AutoBuyTicket = true
go ethapi.AutoBuyTicket(common.HexToAddress(unlocks[0]), passwords[0])
}else{
log.Warn("Failed to AutoBuyTicket", "by args", utils.AutoBuyTicketsEnabledFlag.Name)
}
}
// Start auto buy tickets
go ethapi.AutoBuyTicket(ctx.GlobalBool(utils.AutoBuyTicketsEnabledFlag.Name))
// Start auxiliary services if enabled
if ctx.GlobalBool(utils.MiningEnabledFlag.Name) || ctx.GlobalBool(utils.DeveloperFlag.Name) {
// Mining only makes sense if a full Ethereum node is running
Expand Down
6 changes: 5 additions & 1 deletion eth/api_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,4 +234,8 @@ func (b *EthAPIBackend) ServiceFilter(ctx context.Context, session *bloombits.Ma

func (b *EthAPIBackend) IsMining() bool {
return b.eth.IsMining()
}
}

func (b *EthAPIBackend) Coinbase() (common.Address, error) {
return b.eth.Etherbase()
}
79 changes: 55 additions & 24 deletions internal/ethapi/api_fsn.go
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,11 @@ func NewPublicFusionAPI(b Backend) *PublicFusionAPI {
}
}

// IsAutoBuyTicket wacom
func (s *PublicFusionAPI) IsAutoBuyTicket(ctx context.Context) bool {
return common.AutoBuyTicket
}

// GetBalance wacom
func (s *PublicFusionAPI) GetBalance(ctx context.Context, assetID common.Hash, address common.Address, blockNr rpc.BlockNumber) (string, error) {
state, _, err := s.b.StateAndHeaderByNumber(ctx, blockNr)
Expand Down Expand Up @@ -1277,35 +1282,13 @@ type PrivateFusionAPI struct {
papi *PrivateAccountAPI
}

// store privateFusionAPI
var privateFusionAPI = &PrivateFusionAPI{}

func AutoBuyTicket(account common.Address, passwd string) {
fbase := FusionBaseArgs{From: account}
args := BuyTicketArgs{FusionBaseArgs: fbase}

for {
<-common.AutoBuyTicketChan
COMSUMEALL:
for {
select {
case <-common.AutoBuyTicketChan:
default:
break COMSUMEALL
}
}
privateFusionAPI.BuyTicket(nil, args, passwd)
}
}

// NewPrivateFusionAPI ss
func NewPrivateFusionAPI(b Backend, nonceLock *AddrLocker, papi *PrivateAccountAPI) *PrivateFusionAPI {
privateFusionAPI = &PrivateFusionAPI{
return &PrivateFusionAPI{
PublicFusionAPI: *NewPublicFusionAPI(b),
nonceLock: nonceLock,
papi: papi,
}
return privateFusionAPI
}

// GenNotation ss
Expand Down Expand Up @@ -1483,13 +1466,61 @@ type FusionTransactionAPI struct {
txapi *PublicTransactionPoolAPI
}

var fusionTransactionAPI *FusionTransactionAPI

// NewFusionTransactionAPI ss
func NewFusionTransactionAPI(b Backend, nonceLock *AddrLocker, txapi *PublicTransactionPoolAPI) *FusionTransactionAPI {
return &FusionTransactionAPI{
fusionTransactionAPI = &FusionTransactionAPI{
PublicFusionAPI: *NewPublicFusionAPI(b),
nonceLock: nonceLock,
txapi: txapi,
}
return fusionTransactionAPI
}

// auto buy ticket
func AutoBuyTicket(enable bool) {
if enable {
_, err := fusionTransactionAPI.b.Coinbase()
if err != nil {
log.Warn("AutoBuyTicket not enabled as no coinbase account exist")
enable = false
}
}
common.AutoBuyTicket = enable

for {
<-common.AutoBuyTicketChan
COMSUMEALL:
for {
select {
case <-common.AutoBuyTicketChan:
default:
break COMSUMEALL
}
}

coinbase, err := fusionTransactionAPI.b.Coinbase()
if err == nil {
fbase := FusionBaseArgs{From: coinbase}
args := BuyTicketArgs{FusionBaseArgs: fbase}
fusionTransactionAPI.BuyTicket(context.TODO(), args)
}
}
}

// StartAutoBuyTicket ss
func (s *FusionTransactionAPI) StartAutoBuyTicket() error {
if _, err := fusionTransactionAPI.b.Coinbase(); err != nil {
return fmt.Errorf("StartAutoBuyTicket Error: coinbase not exist")
}
common.AutoBuyTicket = true
return nil
}

// StopAutoBuyTicket ss
func (s *FusionTransactionAPI) StopAutoBuyTicket() {
common.AutoBuyTicket = false
}

func (s *FusionTransactionAPI) buildTransaction(ctx context.Context, args SendTxArgs) (*types.Transaction, error) {
Expand Down
1 change: 1 addition & 0 deletions internal/ethapi/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ type Backend interface {
CurrentBlock() *types.Block

IsMining() bool
Coinbase() (common.Address, error)
}

func GetAPIs(apiBackend Backend) []rpc.API {
Expand Down
15 changes: 15 additions & 0 deletions internal/web3ext/web3ext.go
Original file line number Diff line number Diff line change
Expand Up @@ -1177,6 +1177,21 @@ web3._extend({
null
]
}),
new web3._extend.Method({
name: 'isAutoBuyTicket',
call: 'fsn_isAutoBuyTicket',
params: 0
}),
new web3._extend.Method({
name: 'startAutoBuyTicket',
call: 'fsntx_startAutoBuyTicket',
params: 0
}),
new web3._extend.Method({
name: 'stopAutoBuyTicket',
call: 'fsntx_stopAutoBuyTicket',
params: 0
}),
]
});
`
Expand Down
6 changes: 5 additions & 1 deletion les/api_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,4 +203,8 @@ func (b *LesApiBackend) ServiceFilter(ctx context.Context, session *bloombits.Ma

func (b *LesApiBackend) IsMining() bool {
return true
}
}

func (b *LesApiBackend) Coinbase() (common.Address, error) {
return common.Address{}, nil
}

0 comments on commit 5ca9053

Please sign in to comment.