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

feat: update greenfield to the latest version #329

Merged
merged 2 commits into from
Apr 19, 2023
Merged
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
2 changes: 1 addition & 1 deletion .github/workflows/e2e-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ on:

env:
GreenfileldTag: v0.0.10
GreenfileldIntegrationTestBranch: gnfd-sp
GreenfileldIntegrationTestBranch: fynn/new_sdk_test
GOPRIVATE: github.com/bnb-chain
GH_ACCESS_TOKEN: ${{ secrets.GH_ACCESS_TOKEN }}
MYSQL_USER: root
Expand Down
9 changes: 4 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ go 1.18

replace (
cosmossdk.io/math => github.com/bnb-chain/greenfield-cosmos-sdk/math v0.0.0-20230228075616-68ac309b432c
github.com/bnb-chain/greenfield => github.com/bnb-chain/greenfield v0.1.0
github.com/bnb-chain/greenfield => github.com/bnb-chain/greenfield v0.0.12-0.20230417120203-7b2a32d58918
github.com/confio/ics23/go => github.com/cosmos/cosmos-sdk/ics23/go v0.8.0
github.com/cosmos/cosmos-sdk => github.com/bnb-chain/greenfield-cosmos-sdk v0.0.13
github.com/forbole/juno/v4 => github.com/bnb-chain/juno/v4 v4.0.0-20230407031340-bda71c92f7e2
github.com/forbole/juno/v4 => github.com/bnb-chain/juno/v4 v4.0.0-20230418063138-cfe377950b33
github.com/gogo/protobuf => github.com/regen-network/protobuf v1.3.3-alpha.regen.1
github.com/tendermint/tendermint => github.com/bnb-chain/gnfd-tendermint v0.0.3
)
Expand All @@ -16,8 +16,7 @@ require (
cosmossdk.io/math v1.0.0-beta.6
github.com/aws/aws-sdk-go v1.44.159
github.com/bnb-chain/greenfield v0.1.0
github.com/bnb-chain/greenfield-common/go v0.0.0-20230327055559-264d5271f271
github.com/bnb-chain/greenfield-go-sdk v0.0.8
github.com/bnb-chain/greenfield-common/go v0.0.0-20230407104542-ed19e3666522
github.com/bytedance/gopkg v0.0.0-20221122125632-68358b8ecec6
github.com/cloudflare/cfssl v1.6.3
github.com/consensys/gnark-crypto v0.7.0
Expand Down Expand Up @@ -101,7 +100,7 @@ require (
github.com/dustin/go-humanize v1.0.1-0.20200219035652-afde56e7acac // indirect
github.com/dvsekhvalnov/jose2go v1.5.0 // indirect
github.com/elastic/gosigar v0.14.2 // indirect
github.com/evmos/ethermint v0.6.1-0.20220919141022-34226aa7b1fa // indirect
github.com/evmos/ethermint v0.6.1-0.20220919141022-34226aa7b1fa
github.com/felixge/httpsnoop v1.0.3 // indirect
github.com/ferranbt/fastssz v0.0.0-20210905181407-59cf6761a7d5 // indirect
github.com/flynn/noise v1.0.0 // indirect
Expand Down
14 changes: 6 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -208,18 +208,16 @@ github.com/blakesmith/ar v0.0.0-20190502131153-809d4375e1fb/go.mod h1:PkYb9DJNAw
github.com/bmizerany/pat v0.0.0-20170815010413-6226ea591a40/go.mod h1:8rLXio+WjiTceGBHIoTvn60HIbs7Hm7bcHjyrSqYB9c=
github.com/bnb-chain/gnfd-tendermint v0.0.3 h1:RjEBQZJW7krM4DFxZbIasNNO7K2uohcmL9ULT84qllk=
github.com/bnb-chain/gnfd-tendermint v0.0.3/go.mod h1:/v9z9F6cq0+f7EGG92lYSLBcPYQDILoK91X8YM28hWo=
github.com/bnb-chain/greenfield v0.1.0 h1:H2Xe45HcZ7ts6OBKKE9wiTUmiQg2yCSf/X8XPt1P6V4=
github.com/bnb-chain/greenfield v0.1.0/go.mod h1:7344f+97RhnkZPOIE/uHdF5u6Awwyhfb/TgIA6nQhXA=
github.com/bnb-chain/greenfield-common/go v0.0.0-20230327055559-264d5271f271 h1:KcI/UzJe+/79pSyISTpT5iHXgQga7kRZl+aNSLfNWBk=
github.com/bnb-chain/greenfield-common/go v0.0.0-20230327055559-264d5271f271/go.mod h1:gGaPRDSjK1QH/lMjZ5wfhbP4SfrUUKX6Q72x/8uehxo=
github.com/bnb-chain/greenfield v0.0.12-0.20230417120203-7b2a32d58918 h1:moehHscmSGZXSeqUNoJZkU9rVv3FWGn9Q/EOFdwZQfk=
github.com/bnb-chain/greenfield v0.0.12-0.20230417120203-7b2a32d58918/go.mod h1:7344f+97RhnkZPOIE/uHdF5u6Awwyhfb/TgIA6nQhXA=
github.com/bnb-chain/greenfield-common/go v0.0.0-20230407104542-ed19e3666522 h1:n3R7Ybzu9XlCHl63QT14XwqaSuGff9U+g3ZJic2frOM=
github.com/bnb-chain/greenfield-common/go v0.0.0-20230407104542-ed19e3666522/go.mod h1:gGaPRDSjK1QH/lMjZ5wfhbP4SfrUUKX6Q72x/8uehxo=
github.com/bnb-chain/greenfield-cosmos-sdk v0.0.13 h1:xm9qqrOjrGfX4imXxxruHIpuUUUAFbdtM/TXPXRc5zo=
github.com/bnb-chain/greenfield-cosmos-sdk v0.0.13/go.mod h1:rvAY7ga/AakZWyYkA1zAsNtvKpdoyRFZTqF4MhFYzZ8=
github.com/bnb-chain/greenfield-cosmos-sdk/math v0.0.0-20230228075616-68ac309b432c h1:BLmdYaj7Dx0YOhfk77+KPPJSMCwpQl6f4Y30+801bf0=
github.com/bnb-chain/greenfield-cosmos-sdk/math v0.0.0-20230228075616-68ac309b432c/go.mod h1:u/MXvf8wbUbCsAEyQSSYXXMsczAsFX48e2D6JI86T4o=
github.com/bnb-chain/greenfield-go-sdk v0.0.8 h1:eBhhdek0o9NnRVAeDOVJVyP3y3a/ym5ZuZWEbV5Nrco=
github.com/bnb-chain/greenfield-go-sdk v0.0.8/go.mod h1:J2zKMxU4th2PxADbcKhI7m9jOrIpuwoqE6LvZFG6tMo=
github.com/bnb-chain/juno/v4 v4.0.0-20230407031340-bda71c92f7e2 h1:LHFQyu4ukkpnu2VIyxlWaFP64hNWonZ6m/dNIkO7OwQ=
github.com/bnb-chain/juno/v4 v4.0.0-20230407031340-bda71c92f7e2/go.mod h1:Tp0Ag6bPt7Vcv8na01mA17X6vgQA6MY4VE0sztXZEmM=
github.com/bnb-chain/juno/v4 v4.0.0-20230418063138-cfe377950b33 h1:uD3jpCCavGcbEMpQbllld0sxR3OG2QqZ53ZOA8LwtFQ=
github.com/bnb-chain/juno/v4 v4.0.0-20230418063138-cfe377950b33/go.mod h1:3z4aiyd+ajj1PFfkT137SyVmtDuWo/6aObYvr38Fj1U=
github.com/boltdb/bolt v1.3.1/go.mod h1:clJnj/oiGkjum5o1McbSZDSLxVThjynRyGBgiAx27Ps=
github.com/bradfitz/go-smtpd v0.0.0-20170404230938-deb6d6237625/go.mod h1:HYsPBTaaSFSlLx/70C2HPIMNZpVV8+vt/A+FMnYP11g=
github.com/bradfitz/gomemcache v0.0.0-20170208213004-1952afaa557d/go.mod h1:PmM6Mmwb0LSuEubjR8N7PtNe1KxZLtOUHtbeikc5h60=
Expand Down
42 changes: 18 additions & 24 deletions pkg/greenfield/greenfield.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,8 @@ import (
"sync"
"time"

"github.com/bnb-chain/greenfield-go-sdk/client/chain"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"

"github.com/bnb-chain/greenfield-storage-provider/pkg/log"
chainClient "github.com/bnb-chain/greenfield/sdk/client"
)

const (
Expand All @@ -22,17 +19,15 @@ const (

// GreenfieldClient the greenfield chain client, only use to query.
type GreenfieldClient struct {
// TODO: polish it by new sdk version
gnfdCompositeClients *chain.GnfdCompositeClients
gnfdCompositeClient *chain.GnfdCompositeClient
currentHeight int64
updatedAt time.Time
Provider []string
chainClient *chainClient.GreenfieldClient
currentHeight int64
updatedAt time.Time
Provider []string
}

// GnfdCompositeClient return the greenfield chain client
func (client *GreenfieldClient) GnfdCompositeClient() *chain.GnfdCompositeClient {
return client.gnfdCompositeClient
// GnfdClient return the greenfield chain client
func (client *GreenfieldClient) GnfdClient() *chainClient.GreenfieldClient {
return client.chainClient
}

// Greenfield is an encapsulation of greenfield chain go sdk which supports for more query request
Expand All @@ -51,12 +46,14 @@ func NewGreenfield(cfg *GreenfieldChainConfig) (*Greenfield, error) {
}
var clients []*GreenfieldClient
for _, config := range cfg.NodeAddr {
cc, err := chainClient.NewGreenfieldClient(config.TendermintAddresses[0], cfg.ChainID)
if err != nil {
return nil, err
}
client := &GreenfieldClient{
Provider: config.GreenfieldAddresses,
gnfdCompositeClients: chain.NewGnfdCompositClients(config.GreenfieldAddresses, config.TendermintAddresses,
cfg.ChainID, chain.WithGrpcDialOption(grpc.WithTransportCredentials(insecure.NewCredentials()))),
Provider: config.GreenfieldAddresses,
chainClient: cc,
}
client.gnfdCompositeClient = client.gnfdCompositeClients.GetClient()
clients = append(clients, client)
}
greenfield := &Greenfield{
Expand Down Expand Up @@ -97,23 +94,20 @@ func (greenfield *Greenfield) updateClient() {
select {
case <-ticker.C:
var (
maxHeight int64
maxHeight uint64
maxHeightClient = greenfield.getCurrentClient()
)
for _, client := range greenfield.backUpClients {
gnfdCompositeClient := client.gnfdCompositeClients.GetClient()
status, err := gnfdCompositeClient.RpcClient.TmClient.Status(context.Background())
currentHeight, err := greenfield.GetCurrentHeight(context.Background())
if err != nil {
log.Errorw("get status failed", "node_addr", client.Provider, "error", err)
log.Errorw("get latest block height failed", "node_addr", client.Provider, "error", err)
continue
}
currentHeight := status.SyncInfo.LatestBlockHeight
if currentHeight > maxHeight {
maxHeight = currentHeight
maxHeightClient = client
client.gnfdCompositeClient = gnfdCompositeClient
}
client.currentHeight = currentHeight
client.currentHeight = (int64)(currentHeight)
client.updatedAt = time.Now()
}
if maxHeightClient != greenfield.getCurrentClient() {
Expand Down
29 changes: 13 additions & 16 deletions pkg/greenfield/query_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
permissiontypes "github.com/bnb-chain/greenfield/x/permission/types"
sptypes "github.com/bnb-chain/greenfield/x/sp/types"
storagetypes "github.com/bnb-chain/greenfield/x/storage/types"
"github.com/cosmos/cosmos-sdk/client/grpc/tmservice"
"github.com/cosmos/cosmos-sdk/types/query"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"

Expand All @@ -19,21 +20,17 @@ import (

// GetCurrentHeight the block height sub one as the stable height.
func (greenfield *Greenfield) GetCurrentHeight(ctx context.Context) (uint64, error) {
status, err := greenfield.getCurrentClient().GnfdCompositeClient().RpcClient.TmClient.Status(ctx)
resp, err := greenfield.client.chainClient.TmClient.GetLatestBlock(ctx, &tmservice.GetLatestBlockRequest{})
if err != nil {
log.Errorw("failed to query status", "error", err)
log.Errorw("get latest block height failed", "node_addr", greenfield.client.Provider, "error", err)
return 0, err
}
height := status.SyncInfo.LatestBlockHeight
if height > 0 {
height = height - 1
}
return uint64(height), nil
return (uint64)(resp.SdkBlock.Header.Height), nil
}

// HasAccount returns an indication of the existence of address.
func (greenfield *Greenfield) HasAccount(ctx context.Context, address string) (bool, error) {
client := greenfield.getCurrentClient().GnfdCompositeClient()
client := greenfield.getCurrentClient().GnfdClient()
resp, err := client.Account(ctx, &authtypes.QueryAccountRequest{Address: address})
if err != nil {
log.Errorw("failed to query account", "address", address, "error", err)
Expand All @@ -44,7 +41,7 @@ func (greenfield *Greenfield) HasAccount(ctx context.Context, address string) (b

// QuerySPInfo returns the list of storage provider info.
func (greenfield *Greenfield) QuerySPInfo(ctx context.Context) ([]*sptypes.StorageProvider, error) {
client := greenfield.getCurrentClient().GnfdCompositeClient()
client := greenfield.getCurrentClient().GnfdClient()
var spInfos []*sptypes.StorageProvider
resp, err := client.StorageProviders(ctx, &sptypes.QueryStorageProvidersRequest{
Pagination: &query.PageRequest{
Expand All @@ -64,7 +61,7 @@ func (greenfield *Greenfield) QuerySPInfo(ctx context.Context) ([]*sptypes.Stora

// QueryStorageParams returns storage params
func (greenfield *Greenfield) QueryStorageParams(ctx context.Context) (params *storagetypes.Params, err error) {
client := greenfield.getCurrentClient().GnfdCompositeClient()
client := greenfield.getCurrentClient().GnfdClient()
resp, err := client.StorageQueryClient.Params(ctx, &storagetypes.QueryParamsRequest{})
if err != nil {
log.Errorw("failed to query storage params", "error", err)
Expand All @@ -75,7 +72,7 @@ func (greenfield *Greenfield) QueryStorageParams(ctx context.Context) (params *s

// QueryBucketInfo return the bucket info by name.
func (greenfield *Greenfield) QueryBucketInfo(ctx context.Context, bucket string) (*storagetypes.BucketInfo, error) {
client := greenfield.getCurrentClient().GnfdCompositeClient()
client := greenfield.getCurrentClient().GnfdClient()
resp, err := client.HeadBucket(ctx, &storagetypes.QueryHeadBucketRequest{BucketName: bucket})
if errors.Is(err, storagetypes.ErrNoSuchBucket) {
return nil, merrors.ErrNoSuchBucket
Expand All @@ -89,7 +86,7 @@ func (greenfield *Greenfield) QueryBucketInfo(ctx context.Context, bucket string

// QueryObjectInfo return the object info by name.
func (greenfield *Greenfield) QueryObjectInfo(ctx context.Context, bucket, object string) (*storagetypes.ObjectInfo, error) {
client := greenfield.getCurrentClient().GnfdCompositeClient()
client := greenfield.getCurrentClient().GnfdClient()
resp, err := client.HeadObject(ctx, &storagetypes.QueryHeadObjectRequest{
BucketName: bucket,
ObjectName: object,
Expand All @@ -103,7 +100,7 @@ func (greenfield *Greenfield) QueryObjectInfo(ctx context.Context, bucket, objec

// QueryObjectInfoByID return the object info by name.
func (greenfield *Greenfield) QueryObjectInfoByID(ctx context.Context, objectID string) (*storagetypes.ObjectInfo, error) {
client := greenfield.getCurrentClient().GnfdCompositeClient()
client := greenfield.getCurrentClient().GnfdClient()
resp, err := client.HeadObjectById(ctx, &storagetypes.QueryHeadObjectByIdRequest{
ObjectId: objectID,
})
Expand Down Expand Up @@ -158,7 +155,7 @@ func (greenfield *Greenfield) ListenObjectSeal(ctx context.Context, bucket, obje

// QueryStreamRecord return the steam record info by account.
func (greenfield *Greenfield) QueryStreamRecord(ctx context.Context, account string) (*paymenttypes.StreamRecord, error) {
client := greenfield.getCurrentClient().GnfdCompositeClient()
client := greenfield.getCurrentClient().GnfdClient()
resp, err := client.StreamRecord(ctx, &paymenttypes.QueryGetStreamRecordRequest{
Account: account,
})
Expand All @@ -171,7 +168,7 @@ func (greenfield *Greenfield) QueryStreamRecord(ctx context.Context, account str

// VerifyGetObjectPermission verify get object permission.
func (greenfield *Greenfield) VerifyGetObjectPermission(ctx context.Context, account, bucket, object string) (bool, error) {
client := greenfield.getCurrentClient().GnfdCompositeClient()
client := greenfield.getCurrentClient().GnfdClient()
resp, err := client.VerifyPermission(ctx, &storagetypes.QueryVerifyPermissionRequest{
Operator: account,
BucketName: bucket,
Expand All @@ -191,7 +188,7 @@ func (greenfield *Greenfield) VerifyGetObjectPermission(ctx context.Context, acc
// VerifyPutObjectPermission verify put object permission.
func (greenfield *Greenfield) VerifyPutObjectPermission(ctx context.Context, account, bucket, object string) (bool, error) {
_ = object
client := greenfield.getCurrentClient().GnfdCompositeClient()
client := greenfield.getCurrentClient().GnfdClient()
resp, err := client.VerifyPermission(ctx, &storagetypes.QueryVerifyPermissionRequest{
Operator: account,
BucketName: bucket,
Expand Down
16 changes: 8 additions & 8 deletions pkg/p2p/types/message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ func Test_verifyPingMsgSignature(t *testing.T) {
pingMsg := &Ping{
SpOperatorAddress: km.GetAddr().String(),
}
sigs, err := km.GetPrivKey().Sign(pingMsg.GetSignBytes())
sigs, err := km.Sign(pingMsg.GetSignBytes())
assert.NoError(t, err)
pingMsg.Signature = sigs
valid := km.GetPrivKey().PubKey().VerifySignature(pingMsg.GetSignBytes(), sigs)
valid := km.PubKey().VerifySignature(pingMsg.GetSignBytes(), sigs)
assert.True(t, valid)
err = VerifySignature(pingMsg.GetSpOperatorAddress(), pingMsg.GetSignBytes(), pingMsg.GetSignature())
assert.NoError(t, err)
Expand All @@ -43,10 +43,10 @@ func Test_verifyPongMsgSignature(t *testing.T) {
},
SpOperatorAddress: km.GetAddr().String(),
}
sigs, err := km.GetPrivKey().Sign(pongMsg.GetSignBytes())
sigs, err := km.Sign(pongMsg.GetSignBytes())
assert.NoError(t, err)
pongMsg.Signature = sigs
valid := km.GetPrivKey().PubKey().VerifySignature(pongMsg.GetSignBytes(), sigs)
valid := km.PubKey().VerifySignature(pongMsg.GetSignBytes(), sigs)
assert.True(t, valid)
err = VerifySignature(pongMsg.GetSpOperatorAddress(), pongMsg.GetSignBytes(), pongMsg.GetSignature())
assert.NoError(t, err)
Expand All @@ -62,10 +62,10 @@ func Test_verifyGetApprovalRequestMsgSignature(t *testing.T) {
},
SpOperatorAddress: km.GetAddr().String(),
}
sigs, err := km.GetPrivKey().Sign(sdk.Keccak256(approvalReqMsg.GetSignBytes()))
sigs, err := km.Sign(sdk.Keccak256(approvalReqMsg.GetSignBytes()))
approvalReqMsg.Signature = sigs
assert.NoError(t, err)
valid := km.GetPrivKey().PubKey().VerifySignature(approvalReqMsg.GetSignBytes(), sigs)
valid := km.PubKey().VerifySignature(approvalReqMsg.GetSignBytes(), sigs)
assert.True(t, valid)
err = VerifySignature(approvalReqMsg.GetSpOperatorAddress(), approvalReqMsg.GetSignBytes(), approvalReqMsg.GetSignature())
assert.NoError(t, err)
Expand All @@ -81,10 +81,10 @@ func Test_verifyGetApprovalResponseMsgSignature(t *testing.T) {
},
SpOperatorAddress: km.GetAddr().String(),
}
sigs, err := km.GetPrivKey().Sign(approvalRspMsg.GetSignBytes())
sigs, err := km.Sign(approvalRspMsg.GetSignBytes())
approvalRspMsg.Signature = sigs
assert.NoError(t, err)
valid := km.GetPrivKey().PubKey().VerifySignature(approvalRspMsg.GetSignBytes(), sigs)
valid := km.PubKey().VerifySignature(approvalRspMsg.GetSignBytes(), sigs)
assert.True(t, valid)
err = VerifySignature(approvalRspMsg.GetSpOperatorAddress(), approvalRspMsg.GetSignBytes(), approvalRspMsg.GetSignature())
assert.NoError(t, err)
Expand Down
29 changes: 28 additions & 1 deletion service/blocksyncer/block_syncer_indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ func (i *Impl) HandleTx(tx *types.Tx) {
}

// HandleMessage accepts the transaction and handles messages contained inside the transaction.
func (i *Impl) HandleMessage(index int, msg sdk.Msg, tx *types.Tx) {
func (i *Impl) HandleMessage(block *coretypes.ResultBlock, index int, msg sdk.Msg, tx *types.Tx) {
log.Info("HandleMessage")
}

Expand All @@ -181,3 +181,30 @@ func (i *Impl) Processed(ctx context.Context, height uint64) (bool, error) {
log.Infof("epoch height:%d, cur height: %d", ep.BlockHeight, height)
return ep.BlockHeight > int64(height), nil
}

func (i *Impl) ExportEventsByTxs(ctx context.Context, block *coretypes.ResultBlock, txs []*types.Tx) error {
for _, tx := range txs {
txHash := common.HexToHash(tx.TxHash)
for _, event := range tx.Events {
i.HandleEvent(ctx, block, txHash, sdk.Event(event))
}
}
return nil
}

// GetBlockRecordNum returns total number of blocks stored in database.
func (i *Impl) GetBlockRecordNum(_ context.Context) int64 {
return 1
}

// GetLastBlockRecordHeight returns the last block height stored inside the database
func (i *Impl) GetLastBlockRecordHeight(ctx context.Context) (uint64, error) {
var lastBlockRecordHeight uint64
currentEpoch, err := i.DB.GetEpoch(ctx)
if err == nil {
lastBlockRecordHeight = 0
} else {
lastBlockRecordHeight = uint64(currentEpoch.BlockHeight)
}
return lastBlockRecordHeight, err
}
Loading