Skip to content

Commit

Permalink
feat: add EthSyncing api
Browse files Browse the repository at this point in the history
  • Loading branch information
LinZexiao committed Aug 29, 2023
1 parent 88e1013 commit 9f94caa
Show file tree
Hide file tree
Showing 11 changed files with 103 additions and 3 deletions.
2 changes: 1 addition & 1 deletion app/node/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ func (b *Builder) build(ctx context.Context) (*Node, error) {
if err != nil {
return nil, err
}
if nd.eth, err = eth.NewEthSubModule(ctx, b.repo.Config(), nd.chain, nd.mpool, sqlitePath); err != nil {
if nd.eth, err = eth.NewEthSubModule(ctx, b.repo.Config(), nd.chain, nd.mpool, sqlitePath, nd.syncer.API()); err != nil {
return nil, err
}

Expand Down
1 change: 1 addition & 0 deletions app/node/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ func aliasETHAPI(rpcServer *jsonrpc.RPCServer) {
rpcServer.AliasMethod("eth_getStorageAt", "Filecoin.EthGetStorageAt")
rpcServer.AliasMethod("eth_getBalance", "Filecoin.EthGetBalance")
rpcServer.AliasMethod("eth_chainId", "Filecoin.EthChainId")
rpcServer.AliasMethod("eth_syncing", "Filecoin.EthSyncing")
rpcServer.AliasMethod("eth_feeHistory", "Filecoin.EthFeeHistory")
rpcServer.AliasMethod("eth_protocolVersion", "Filecoin.EthProtocolVersion")
rpcServer.AliasMethod("eth_maxPriorityFeePerGas", "Filecoin.EthMaxPriorityFeePerGas")
Expand Down
4 changes: 4 additions & 0 deletions app/submodule/eth/dummy.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ func (e *ethAPIDummy) EthChainId(ctx context.Context) (types.EthUint64, error) {
return 0, ErrModuleDisabled
}

func (e *ethAPIDummy) EthSyncing(ctx context.Context) (types.EthSyncingResult, error) {
return types.EthSyncingResult{}, ErrModuleDisabled
}

func (e *ethAPIDummy) NetVersion(ctx context.Context) (string, error) {
return "", ErrModuleDisabled
}
Expand Down
37 changes: 37 additions & 0 deletions app/submodule/eth/eth_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -684,6 +684,43 @@ func (a *ethAPI) EthChainId(ctx context.Context) (types.EthUint64, error) {
return types.EthUint64(types2.Eip155ChainID), nil
}

func (a *ethAPI) EthSyncing(ctx context.Context) (types.EthSyncingResult, error) {
state, err := a.em.syncAPI.SyncState(ctx)
if err != nil {
return types.EthSyncingResult{}, fmt.Errorf("failed calling SyncState: %w", err)
}

if len(state.ActiveSyncs) == 0 {
return types.EthSyncingResult{}, errors.New("no active syncs, try again")
}

working := -1
for i, ss := range state.ActiveSyncs {
if ss.Stage == types.StageIdle {
continue
}
working = i

}
if working == -1 {
working = len(state.ActiveSyncs) - 1
}

ss := state.ActiveSyncs[working]
if ss.Base == nil || ss.Target == nil {
return types.EthSyncingResult{}, errors.New("missing syncing information, try again")
}

res := types.EthSyncingResult{
DoneSync: ss.Stage == types.StageSyncComplete,
CurrentBlock: types.EthUint64(ss.Height),
StartingBlock: types.EthUint64(ss.Base.Height()),
HighestBlock: types.EthUint64(ss.Target.Height()),
}

return res, nil
}

func (a *ethAPI) EthFeeHistory(ctx context.Context, p jsonrpc.RawParams) (types.EthFeeHistory, error) {
params, err := jsonrpc.DecodeParams[types.EthFeeHistoryParams](p)
if err != nil {
Expand Down
7 changes: 5 additions & 2 deletions app/submodule/eth/eth_submodule.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ func NewEthSubModule(ctx context.Context,
chainModule *chain.ChainSubmodule,
mpoolModule *mpool.MessagePoolSubmodule,
sqlitePath string,
syncAPI v1api.ISyncer,
) (*EthSubModule, error) {
ctx, cancel := context.WithCancel(ctx)
em := &EthSubModule{
Expand All @@ -27,6 +28,7 @@ func NewEthSubModule(ctx context.Context,
sqlitePath: sqlitePath,
ctx: ctx,
cancel: cancel,
syncAPI: syncAPI,
}
ee, err := newEthEventAPI(ctx, em)
if err != nil {
Expand Down Expand Up @@ -66,8 +68,9 @@ type EthSubModule struct { // nolint
ethEventAPI *ethEventAPI
ethAPIAdapter ethAPIAdapter

ctx context.Context
cancel context.CancelFunc
ctx context.Context
cancel context.CancelFunc
syncAPI v1api.ISyncer
}

func (em *EthSubModule) Start(_ context.Context) error {
Expand Down
24 changes: 24 additions & 0 deletions venus-shared/actors/types/eth.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,30 @@ func (c *EthCall) UnmarshalJSON(b []byte) error {
return nil
}

type EthSyncingResult struct {
DoneSync bool
StartingBlock EthUint64
CurrentBlock EthUint64
HighestBlock EthUint64
}

func (sr EthSyncingResult) MarshalJSON() ([]byte, error) {
if sr.DoneSync {
// when done syncing, the json response should be '"result": false'
return []byte("false"), nil
}

// need to do an anonymous struct to avoid infinite recursion
return json.Marshal(&struct {
StartingBlock EthUint64 `json:"startingblock"`
CurrentBlock EthUint64 `json:"currentblock"`
HighestBlock EthUint64 `json:"highestblock"`
}{
StartingBlock: sr.StartingBlock,
CurrentBlock: sr.CurrentBlock,
HighestBlock: sr.HighestBlock})
}

const (
EthAddressLength = 20
EthHashLength = 32
Expand Down
1 change: 1 addition & 0 deletions venus-shared/api/chain/v1/eth.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ type IETH interface {
EthGetStorageAt(ctx context.Context, address types.EthAddress, position types.EthBytes, blkParam types.EthBlockNumberOrHash) (types.EthBytes, error) //perm:read
EthGetBalance(ctx context.Context, address types.EthAddress, blkParam types.EthBlockNumberOrHash) (types.EthBigInt, error) //perm:read
EthChainId(ctx context.Context) (types.EthUint64, error) //perm:read
EthSyncing(ctx context.Context) (types.EthSyncingResult, error) //perm:read
NetVersion(ctx context.Context) (string, error) //perm:read
NetListening(ctx context.Context) (bool, error) //perm:read
EthProtocolVersion(ctx context.Context) (types.EthUint64, error) //perm:read
Expand Down
10 changes: 10 additions & 0 deletions venus-shared/api/chain/v1/method.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ curl http://<ip>:<port>/rpc/v1 -X POST -H "Content-Type: application/json" -H "
* [EthMaxPriorityFeePerGas](#ethmaxpriorityfeepergas)
* [EthProtocolVersion](#ethprotocolversion)
* [EthSendRawTransaction](#ethsendrawtransaction)
* [EthSyncing](#ethsyncing)
* [FilecoinAddressToEthAddress](#filecoinaddresstoethaddress)
* [NetListening](#netlistening)
* [NetVersion](#netversion)
Expand Down Expand Up @@ -2943,6 +2944,15 @@ Inputs:

Response: `"0x0707070707070707070707070707070707070707070707070707070707070707"`

### EthSyncing


Perms: read

Inputs: `[]`

Response: `false`

### FilecoinAddressToEthAddress
FilecoinAddressToEthAddress converts an f410 or f0 Filecoin Address to an EthAddress

Expand Down
15 changes: 15 additions & 0 deletions venus-shared/api/chain/v1/mock/mock_fullnode.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions venus-shared/api/chain/v1/proxy_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions venus-shared/types/eth.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 9f94caa

Please sign in to comment.