fix: solana subscription jsonParsed mode, program data scheme, update miner stake in MinerBase

This commit is contained in:
bahadylbekov 2020-11-13 18:36:01 +03:00
parent 2551affae0
commit d99443d9f0
9 changed files with 127 additions and 37 deletions

View File

@ -6,6 +6,7 @@ import (
"github.com/libp2p/go-libp2p-core/peer"
"github.com/Secured-Finance/dione/ethclient"
"github.com/Secured-Finance/dione/types"
"github.com/ethereum/go-ethereum/common"
"github.com/filecoin-project/go-state-types/crypto"
@ -57,6 +58,27 @@ func NewMiningBase() *MiningBase {
}
}
func (m *MinerBase) UpdateStake(c *ethclient.EthereumClient, miner common.Address) error {
mStake, err := c.GetMinerStake(miner)
if err != nil {
logrus.Warn("Can't get miner stake", err)
return err
}
nStake, err := c.GetTotalStake()
if err != nil {
logrus.Warn("Can't get miner stake", err)
return err
}
m.MinerStake = *mStake
m.NetworkStake = *nStake
return nil
}
// Start, Stop mining functions
func (m *Miner) MineTask(ctx context.Context, base *MiningBase, mb *MinerBase) (*types.DioneTask, error) {

View File

@ -205,25 +205,3 @@ func (c *EthereumClient) SubmitRequestAnswer(reqID *big.Int, data string, callba
return nil
}
// Getting total stake in DioneStaking contract, this function could
// be used for storing the total stake and veryfing the stake tokens
// on new tasks
func (c *EthereumClient) GetTotalStake() (*big.Int, error) {
totalStake, err := c.dioneStaking.TotalStake()
if err != nil {
return nil, err
}
return totalStake, nil
}
// Getting miner stake in DioneStaking contract, this function could
// be used for storing the miner's stake and veryfing the stake tokens
// on new tasks
func (c *EthereumClient) GetMinerStake(minerAddress common.Address) (*big.Int, error) {
minerStake, err := c.dioneStaking.MinerStake(minerAddress)
if err != nil {
return nil, err
}
return minerStake, nil
}

36
ethclient/stake.go Normal file
View File

@ -0,0 +1,36 @@
package ethclient
import (
"github.com/Secured-Finance/dione/types"
"github.com/ethereum/go-ethereum/common"
)
// Getting total stake in DioneStaking contract, this function could
// be used for storing the total stake and veryfing the stake tokens
// on new tasks
func (c *EthereumClient) GetTotalStake() (*types.BigInt, error) {
var b types.BigInt
totalStake, err := c.dioneStaking.TotalStake()
if err != nil {
return nil, err
}
b.Int = totalStake
return &b, nil
}
// Getting miner stake in DioneStaking contract, this function could
// be used for storing the miner's stake and veryfing the stake tokens
// on new tasks
func (c *EthereumClient) GetMinerStake(minerAddress common.Address) (*types.BigInt, error) {
var b types.BigInt
minerStake, err := c.dioneStaking.MinerStake(minerAddress)
if err != nil {
return nil, err
}
b.Int = minerStake
return &b, nil
}

1
go.sum
View File

@ -1204,6 +1204,7 @@ golang.org/x/net v0.0.0-20200602114024-627f9648deb9/go.mod h1:qpuaurCH72eLCgpAm/
golang.org/x/net v0.0.0-20200625001655-4c5254603344/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
golang.org/x/net v0.0.0-20200707034311-ab3426394381 h1:VXak5I6aEWmAXeQjA+QSZzlgNrpq9mjcfDemuexIKsU=
golang.org/x/net v0.0.0-20200707034311-ab3426394381/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
golang.org/x/net v0.0.0-20201016165138-7b1cca2348c0 h1:5kGOVHlq0euqwzgTC9Vu15p6fV1Wi0ArVi8da2urnVg=
golang.org/x/net v0.0.0-20201016165138-7b1cca2348c0/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=

View File

@ -5,15 +5,16 @@ import "net/http"
type RequestBody struct {
Jsonrpc string `json:"jsonrpc"`
Method string `json:"method"`
Params []string `json:"params"`
Params []interface{} `json:"params"`
ID int `json:"id"`
}
func NewRequestBody(method string) *RequestBody {
var i []interface{}
return &RequestBody{
Jsonrpc: "2.0",
Method: method,
Params: []string{},
Params: i,
ID: 0,
}
}

View File

@ -6,7 +6,7 @@ import (
"log"
"github.com/Secured-Finance/dione/rpc"
types "github.com/Secured-Finance/dione/solana/types"
"github.com/Secured-Finance/dione/solana/types"
ws "github.com/dgrr/fastws"
"github.com/shengdoushi/base58"
"github.com/sirupsen/logrus"
@ -20,6 +20,16 @@ type SolanaClient struct {
ws string
}
type SubParams struct {
Encoding string `json:"encoding"`
}
func NewSubParam(encoding string) *SubParams {
return &SubParams{
Encoding: encoding,
}
}
// NewSolanaClient creates a new solana client structure.
func NewSolanaClient() *SolanaClient {
return &SolanaClient{
@ -60,8 +70,10 @@ func (c *SolanaClient) subsctibeOnProgram(programID string) {
requestBody := rpc.NewRequestBody("programSubscribe")
requestBody.Params = append(requestBody.Params, programID)
p := NewSubParam("jsonParsed")
requestBody.Params = append(requestBody.Params, p)
body, err := json.Marshal(requestBody)
logrus.Info(string(body))
if err != nil {
logrus.Errorf("Couldn't unmarshal parameters to request body %v", err)
}
@ -82,7 +94,6 @@ func (c *SolanaClient) subsctibeOnProgram(programID string) {
}
json.Unmarshal(msg, &parsedSub)
logrus.Info("Subscription: ", parsedSub)
// TODO: 1) Unmarshal base58 data to program structure in order to read data
// 2) Save data from oracle event in redis cache
// 3) Start mining of Solana oracle event
}

42
solana/types/program.go Normal file
View File

@ -0,0 +1,42 @@
package types
// SOLANA PROGRAM SCHEME
type Data struct {
Parsed Parsed `json:"parsed"`
Program string `json:"program"`
Space int `json:"space"`
}
type AuthorizedVoters struct {
AuthorizedVoter string `json:"authorizedVoter"`
Epoch int `json:"epoch"`
}
type EpochCredits struct {
Credits string `json:"credits"`
Epoch int `json:"epoch"`
PreviousCredits string `json:"previousCredits"`
}
type LastTimestamp struct {
Slot int `json:"slot"`
Timestamp int `json:"timestamp"`
}
type Votes struct {
ConfirmationCount int `json:"confirmationCount"`
Slot int `json:"slot"`
}
type Info struct {
AuthorizedVoters []AuthorizedVoters `json:"authorizedVoters"`
AuthorizedWithdrawer string `json:"authorizedWithdrawer"`
Commission int `json:"commission"`
EpochCredits []EpochCredits `json:"epochCredits"`
LastTimestamp LastTimestamp `json:"lastTimestamp"`
NodePubkey string `json:"nodePubkey"`
PriorVoters []interface{} `json:"priorVoters"`
RootSlot int `json:"rootSlot"`
Votes []Votes `json:"votes"`
}
type Parsed struct {
Info Info `json:"info"`
Type string `json:"type"`
}

View File

@ -26,7 +26,7 @@ type Value struct {
}
type Account struct {
Data string `json:"data"`
Data Data `json:"data"`
Executable bool `json:"executable"`
Lamports int `json:"lamports"`
Owner string `json:"owner"`

View File

@ -1,7 +1,6 @@
package store
import (
"math/big"
"time"
"github.com/Secured-Finance/dione/ethclient"
@ -13,15 +12,15 @@ import (
type DioneStakeInfo struct {
ID int
MinerStake *big.Int
TotalStake *big.Int
MinerStake *types.BigInt
TotalStake *types.BigInt
MinerAddress string
MinerEthWallet string
Timestamp time.Time
Ethereum *ethclient.EthereumClient
}
func NewDioneStakeInfo(minerStake, totalStake *big.Int, minerWallet, minerEthWallet string, ethereumClient *ethclient.EthereumClient) *DioneStakeInfo {
func NewDioneStakeInfo(minerStake, totalStake *types.BigInt, minerWallet, minerEthWallet string, ethereumClient *ethclient.EthereumClient) *DioneStakeInfo {
return &DioneStakeInfo{
MinerStake: minerStake,
TotalStake: totalStake,
@ -89,8 +88,8 @@ func (s *Store) GetLastStakeInfo(wallet, ethWallet string) (*DioneStakeInfo, err
func (s *DioneStakeInfo) Validate() error {
return validation.ValidateStruct(
s,
validation.Field(&s.MinerStake, validation.Required, validation.By(types.ValidateBigInt(s.MinerStake))),
validation.Field(&s.TotalStake, validation.Required, validation.By(types.ValidateBigInt(s.TotalStake))),
validation.Field(&s.MinerStake, validation.Required, validation.By(types.ValidateBigInt(s.MinerStake.Int))),
validation.Field(&s.TotalStake, validation.Required, validation.By(types.ValidateBigInt(s.TotalStake.Int))),
validation.Field(&s.MinerAddress, validation.Required),
validation.Field(&s.MinerEthWallet, validation.Required),
)