Skip to content

Commit

Permalink
add rpc api ft_forkStatus (#463)
Browse files Browse the repository at this point in the history
  • Loading branch information
erickyan86 authored Sep 3, 2019
1 parent 40fe6f9 commit 6df696f
Show file tree
Hide file tree
Showing 11 changed files with 52 additions and 16 deletions.
6 changes: 3 additions & 3 deletions accountmanager/accountmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ type CreateAccountAction struct {
Description string `json:"description,omitempty"`
}

type UpdateAccountAction struct {
type UpdataAccountAction struct {
Founder common.Name `json:"founder,omitempty"`
}

Expand Down Expand Up @@ -392,7 +392,7 @@ func (am *AccountManager) CreateAccount(fromName common.Name, accountName common
// }

//UpdateAccount update the pubkey of the account
func (am *AccountManager) UpdateAccount(accountName common.Name, accountAction *UpdateAccountAction) error {
func (am *AccountManager) UpdateAccount(accountName common.Name, accountAction *UpdataAccountAction) error {
acct, err := am.GetAccountByName(accountName)
if acct == nil {
return ErrAccountNotExist
Expand Down Expand Up @@ -1397,7 +1397,7 @@ func (am *AccountManager) process(accountManagerContext *types.AccountManagerCon
internalActions = append(internalActions, internalAction)
}
case types.UpdateAccount:
var acct UpdateAccountAction
var acct UpdataAccountAction
err := rlp.DecodeBytes(action.Data(), &acct)
if err != nil {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion accountmanager/accountmanager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1569,7 +1569,7 @@ func TestAccountManager_Process(t *testing.T) {
if err != nil {
panic("rlp payload err")
}
aa1 := &UpdateAccountAction{
aa1 := &UpdataAccountAction{
Founder: common.Name(""),
}

Expand Down
8 changes: 7 additions & 1 deletion blockchain/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -1175,11 +1175,17 @@ func (bc *BlockChain) GetHeaderByNumber(number uint64) *types.Header {
// Config retrieves the blockchain's chain configuration.
func (bc *BlockChain) Config() *params.ChainConfig { return bc.chainConfig }

// ForkUpdate .
// ForkUpdate update fork status.
func (bc *BlockChain) ForkUpdate(block *types.Block, statedb *state.StateDB) error {
return bc.fcontroller.update(block, statedb, bc.GetHeaderByNumber)
}

// ForkStatus returns current fork status.
func (bc *BlockChain) ForkStatus(statedb *state.StateDB) (*ForkConfig, ForkInfo, error) {
info, err := bc.fcontroller.getForkInfo(statedb)
return bc.fcontroller.cfg, info, err
}

// Export writes the active chain to the given writer.
func (bc *BlockChain) Export(w io.Writer) error {
return bc.ExportN(w, uint64(0), bc.CurrentBlock().NumberU64())
Expand Down
2 changes: 1 addition & 1 deletion blockchain/forkcontroller.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ func (fc *ForkController) checkForkID(header *types.Header, state *state.StateDB
if curForkID, _, err := fc.currentForkID(state); err != nil {
return err
} else if header.CurForkID() != curForkID || header.NextForkID() < curForkID {
return fmt.Errorf("invild header curForkID: %v, header nextForkID: %v,actual curForkID %v, header hash: %v, header number: %v",
return fmt.Errorf("invalid header curForkID: %v, header nextForkID: %v,actual curForkID %v, header hash: %v, header number: %v",
header.CurForkID(), header.NextForkID(), curForkID, header.Hash().Hex(), header.Number.Uint64())
}
return nil
Expand Down
14 changes: 9 additions & 5 deletions ftservice/apibackend.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

"github.com/ethereum/go-ethereum/common/math"
"github.com/fractalplatform/fractal/accountmanager"
"github.com/fractalplatform/fractal/blockchain"
"github.com/fractalplatform/fractal/common"
"github.com/fractalplatform/fractal/consensus"
"github.com/fractalplatform/fractal/feemanager"
Expand All @@ -39,7 +40,7 @@ import (
"github.com/fractalplatform/fractal/utils/fdb"
)

// APIBackend implements ftserviceapi.Backend for full nodes
// APIBackend implements ftservice api.Backend for full nodes
type APIBackend struct {
ftservice *FtService
gpo *gasprice.Oracle
Expand Down Expand Up @@ -236,18 +237,21 @@ func (b *APIBackend) GetEVM(ctx context.Context, account *accountmanager.Account
account.AddAccountBalanceByID(from, assetID, math.MaxBig256)
vmError := func() error { return nil }

evmcontext := &processor.EvmContext{
evmContext := &processor.EvmContext{
ChainContext: b.ftservice.BlockChain(),
EngineContext: b.ftservice.Engine(),
}

context := processor.NewEVMContext(from, to, assetID, gasPrice, header, evmcontext, nil)
context := processor.NewEVMContext(from, to, assetID, gasPrice, header, evmContext, nil)
return vm.NewEVM(context, account, state, b.ChainConfig(), vmCfg), vmError, nil
}

func (b *APIBackend) SetGasPrice(gasPrice *big.Int) bool {
b.ftservice.SetGasPrice(gasPrice)
return true
return b.ftservice.SetGasPrice(gasPrice)
}

func (b *APIBackend) ForkStatus(statedb *state.StateDB) (*blockchain.ForkConfig, blockchain.ForkInfo, error) {
return b.ftservice.BlockChain().ForkStatus(statedb)
}

func (b *APIBackend) GetAccountManager() (*accountmanager.AccountManager, error) {
Expand Down
2 changes: 0 additions & 2 deletions rpcapi/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,9 +208,7 @@ func (api *AccountAPI) GetNonce(accountName common.Name) (uint64, error) {
if err != nil {
return 0, err
}

return acct.GetNonce(accountName)

}

//GetAssetInfoByName
Expand Down
2 changes: 2 additions & 0 deletions rpcapi/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"math/big"

"github.com/fractalplatform/fractal/accountmanager"
"github.com/fractalplatform/fractal/blockchain"
"github.com/fractalplatform/fractal/common"
"github.com/fractalplatform/fractal/consensus"
"github.com/fractalplatform/fractal/debug"
Expand Down Expand Up @@ -59,6 +60,7 @@ type Backend interface {
GetDetailTxByFilter(ctx context.Context, filterFn func(common.Name) bool, blockNr, lookbackNum uint64) []*types.DetailTx
GetTxsByFilter(ctx context.Context, filterFn func(common.Name) bool, blockNr, lookbackNum uint64) *types.AccountTxs
GetBadBlocks(ctx context.Context) ([]*types.Block, error)
ForkStatus(statedb *state.StateDB) (*blockchain.ForkConfig, blockchain.ForkInfo, error)
SetStatePruning(enable bool) (bool, uint64)

// TxPool
Expand Down
26 changes: 26 additions & 0 deletions rpcapi/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -387,3 +387,29 @@ func (s *PrivateBlockChainAPI) SetStatePruning(enable bool) types.BlockState {
prestatus, number := s.b.SetStatePruning(enable)
return types.BlockState{PreStatePruning: prestatus, CurrentNumber: number}
}

type RPCForkStatus struct {
Count uint64 `json:"count"`
Percentage uint64 `json:"percentage"`
CurID uint64 `json:"curID"`
NexID uint64 `json:"nextID"`
CurIDBlockCount uint64 `json:"curIDBlockCount"`
NextIDBlockCount uint64 `json:"nextIDBlockCount"`
}

func (s *PrivateBlockChainAPI) ForkStatus(ctx context.Context) (*RPCForkStatus, error) {
state, _, err := s.b.StateAndHeaderByNumber(ctx, rpc.LatestBlockNumber)
if state == nil || err != nil {
return nil, err
}
cfg, status, err := s.b.ForkStatus(state)
if err != nil {
return nil, err
}
return &RPCForkStatus{Count: cfg.ForkBlockNum,
Percentage: cfg.Forkpercentage,
CurID: status.CurForkID,
NexID: status.NextForkID,
CurIDBlockCount: status.CurForkIDBlockNum,
NextIDBlockCount: status.NextForkIDBlockNum}, nil
}
2 changes: 1 addition & 1 deletion sdk/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func (acc *Account) CreateAccount(to common.Name, value *big.Int, id uint64, gas
}

// UpdateAccount update accout
func (acc *Account) UpdateAccount(to common.Name, value *big.Int, id uint64, gas uint64, newacct *accountmanager.UpdateAccountAction) (hash common.Hash, err error) {
func (acc *Account) UpdateAccount(to common.Name, value *big.Int, id uint64, gas uint64, newacct *accountmanager.UpdataAccountAction) (hash common.Hash, err error) {
nonce := acc.nonce
if nonce == math.MaxUint64 {
nonce, err = acc.api.AccountNonce(acc.name.String())
Expand Down
2 changes: 1 addition & 1 deletion sdk/account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func TestCreateAccount(t *testing.T) {
func TestUpdateAccount(t *testing.T) {
Convey("UpdateAccount", t, func() {
act := NewAccount(api, common.StrToName(name), priv, chainCfg.SysTokenID, math.MaxUint64, true, chainCfg.ChainID)
hash, err := act.UpdateAccount(common.StrToName(chainCfg.AccountName), big.NewInt(0), chainCfg.SysTokenID, gas, &accountmanager.UpdateAccountAction{
hash, err := act.UpdateAccount(common.StrToName(chainCfg.AccountName), big.NewInt(0), chainCfg.SysTokenID, gas, &accountmanager.UpdataAccountAction{
Founder: common.StrToName(name),
})
So(err, ShouldBeNil)
Expand Down
2 changes: 1 addition & 1 deletion sdk/test/sample.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ func sampleIncreaseAsset() *TTX {
AssetID: chainCfg.SysTokenID,
Value: big.NewInt(0),
Payload: &accountmanager.IncAsset{
AssetId: 1,
AssetID: 1,
Amount: new(big.Int).Mul(big.NewInt(100000000000), big.NewInt(1e18)),
To: "sampleact",
},
Expand Down

0 comments on commit 6df696f

Please sign in to comment.