Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add RPC endpoints to support Trustwallet staking #1475

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions builtin/plugins/dposv3/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,10 @@ func loadDelegationList(ctx contract.StaticContext) (DelegationList, error) {
return DefaultNoCache.loadDelegationList(ctx)
}

func LoadDelegationList(ctx contract.StaticContext) (DelegationList, error) {
return DefaultNoCache.loadDelegationList(ctx)
}

func (c *CachedDposStorage) loadDelegationList(ctx contract.StaticContext) (DelegationList, error) {
if c.EnableCaching && len(c.delegations) > 0 {
return c.delegations, nil
Expand Down
19 changes: 19 additions & 0 deletions e2e/dpos-downtime.toml
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,25 @@
Condition = "contains"
Expected = ["2000000000000000000"]

[[TestCases]]
RunCmd = "{{ $.LoomPath }} dpos3 update-candidate-info loomX test-trustwallet-endpoint loomX.io 350 -k {{index $.NodePrivKeyPathList 1}}"

# wait for 5 blocks so that the offline validator delegations get slashed
[[TestCases]]
RunCmd = "wait_for_block_height_to_increase 0 3"

[[TestCases]]
RunCmd = "/usr/bin/curl -X GET --data {\"jsonrpc\":\"2.0\",\"id\":83} {{index $.NodeProxyAppAddressList 0}}/query/getvalidators"
All = true
Condition = "contains"
Expected = [
"\"jsonrpc\": \"2.0\",",
"\"id\": ",
"\"name\": \"loomX\",",
"\"description\": \"test-trustwallet-endpoint\",",
"\"website\": \"loomX.io\",",
]

# Node 1 staked 1.25M in order to become a validator, if the node was properly
# slashed, the node's self-delegation should now be a bit less than 1.25M so the
# figure "1250000000000000000000000" should not be present in the response.
Expand Down
52 changes: 52 additions & 0 deletions rpc/instrumenting.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/loomnetwork/go-loom/plugin/types"
"github.com/loomnetwork/loomchain/config"
"github.com/loomnetwork/loomchain/rpc/eth"
"github.com/loomnetwork/loomchain/rpc/trustwallet"
"github.com/loomnetwork/loomchain/vm"
rpctypes "github.com/tendermint/tendermint/rpc/lib/types"
)
Expand Down Expand Up @@ -558,3 +559,54 @@ func (m InstrumentingMiddleware) EthGetTransactionCount(
resp, err = m.next.EthGetTransactionCount(local, block)
return
}

// Trust wallet
func (m InstrumentingMiddleware) GetValidators() (*trustwallet.JsonGetValidators, error) {
var err error
defer func(begin time.Time) {
lvs := []string{"method", "GetValidators", "error", fmt.Sprint(err != nil)}
m.requestCount.With(lvs...).Add(1)
m.requestLatency.With(lvs...).Observe(time.Since(begin).Seconds())
}(time.Now())

return m.next.GetValidators()
}

func (m InstrumentingMiddleware) ListDelegations(
address string,
) (*trustwallet.JsonListDelegation, error) {
var err error
defer func(begin time.Time) {
lvs := []string{"method", "ListDelegations", "error", fmt.Sprint(err != nil)}
m.requestCount.With(lvs...).Add(1)
m.requestLatency.With(lvs...).Observe(time.Since(begin).Seconds())
}(time.Now())

return m.next.ListDelegations(address)
}

func (m InstrumentingMiddleware) GetAccountInfo(
address string,
) (*trustwallet.JsonAccountInfo, error) {
var err error
defer func(begin time.Time) {
lvs := []string{"method", "GetAccountInfo", "error", fmt.Sprint(err != nil)}
m.requestCount.With(lvs...).Add(1)
m.requestLatency.With(lvs...).Observe(time.Since(begin).Seconds())
}(time.Now())

return m.next.GetAccountInfo(address)
}

func (m InstrumentingMiddleware) GetRewards(
address string,
) (*trustwallet.JsonGetRewards, error) {
var err error
defer func(begin time.Time) {
lvs := []string{"method", "GetRewards", "error", fmt.Sprint(err != nil)}
m.requestCount.With(lvs...).Add(1)
m.requestLatency.With(lvs...).Observe(time.Since(begin).Seconds())
}(time.Now())

return m.next.GetRewards(address)
}
29 changes: 29 additions & 0 deletions rpc/mock_query_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/loomnetwork/loomchain/config"
"github.com/loomnetwork/loomchain/rpc/eth"
"github.com/loomnetwork/loomchain/rpc/trustwallet"
"github.com/loomnetwork/loomchain/vm"
)

Expand Down Expand Up @@ -361,3 +362,31 @@ func (m *MockQueryService) EvmUnSubscribe(id string) (bool, error) {
m.MethodsCalled = append([]string{"EvmUnSubscribe"}, m.MethodsCalled...)
return true, nil
}

func (m *MockQueryService) GetValidators() (*trustwallet.JsonGetValidators, error) {
m.mutex.Lock()
defer m.mutex.Unlock()
m.MethodsCalled = append([]string{"GetValidators"}, m.MethodsCalled...)
return nil, nil
}

func (m *MockQueryService) ListDelegations(address string) (*trustwallet.JsonListDelegation, error) {
m.mutex.Lock()
defer m.mutex.Unlock()
m.MethodsCalled = append([]string{"ListDelegations"}, m.MethodsCalled...)
return nil, nil
}

func (m *MockQueryService) GetAccountInfo(address string) (*trustwallet.JsonAccountInfo, error) {
m.mutex.Lock()
defer m.mutex.Unlock()
m.MethodsCalled = append([]string{"GetAccountInfo"}, m.MethodsCalled...)
return nil, nil
}

func (m *MockQueryService) GetRewards(address string) (*trustwallet.JsonGetRewards, error) {
m.mutex.Lock()
defer m.mutex.Unlock()
m.MethodsCalled = append([]string{"GetRewards"}, m.MethodsCalled...)
return nil, nil
}
Loading