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

update codes #46

Merged
merged 1 commit into from
Oct 17, 2024
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
43,806 changes: 3,454 additions & 40,352 deletions docs/static/openapi.yml

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion hypergrid-aide/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ require (
github.com/mostynb/zstdpool-freelist v0.0.0-20201229113212-927304c0c3b1 // indirect
github.com/mr-tron/base58 v1.2.0 // indirect
github.com/mtibben/percent v0.2.1 // indirect
github.com/near/borsh-go v0.3.1
github.com/oasisprotocol/curve25519-voi v0.0.0-20230904125328-1f23a7beb09a // indirect
github.com/oklog/run v1.1.0 // indirect
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
Expand Down Expand Up @@ -179,4 +180,4 @@ require (
nhooyr.io/websocket v1.8.6 // indirect
pgregory.net/rapid v1.1.0 // indirect
sigs.k8s.io/yaml v1.4.0 // indirect
)
)
4 changes: 3 additions & 1 deletion hypergrid-aide/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,8 @@ github.com/nats-io/nats.go v1.9.1/go.mod h1:ZjDU1L/7fJ09jvUSRVBR2e7+RnLiiIQyqyzE
github.com/nats-io/nkeys v0.1.0/go.mod h1:xpnFELMwJABBLVhffcfd1MZx6VsNRFpEugbxziKVo7w=
github.com/nats-io/nkeys v0.1.3/go.mod h1:xpnFELMwJABBLVhffcfd1MZx6VsNRFpEugbxziKVo7w=
github.com/nats-io/nuid v1.0.1/go.mod h1:19wcPz3Ph3q0Jbyiqsd0kePYG7A95tJPxeL+1OSON2c=
github.com/near/borsh-go v0.3.1 h1:ukNbhJlPKxfua0/nIuMZhggSU8zvtRP/VyC25LLqPUA=
github.com/near/borsh-go v0.3.1/go.mod h1:NeMochZp7jN/pYFuxLkrZtmLqbADmnp/y1+/dL+AsyQ=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A=
github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE=
Expand Down Expand Up @@ -1035,4 +1037,4 @@ pgregory.net/rapid v1.1.0/go.mod h1:PY5XlDGj0+V1FCq0o192FdRhpKHGTRIWBgqjDBTrq04=
sigs.k8s.io/yaml v1.1.0/go.mod h1:UJmg0vDUVViEyp3mgSv9WPwZCDxu4rQW1olrI1uml+o=
sigs.k8s.io/yaml v1.4.0 h1:Mk1wCc2gy/F0THH0TAp1QYyJNzRm2KCLy3o5ASXVI5E=
sigs.k8s.io/yaml v1.4.0/go.mod h1:Ejl7/uTz7PSA4eKMyQCUTnhZYNmLIl+5c2lQPGR2BPY=
sourcegraph.com/sourcegraph/appdash v0.0.0-20190731080439-ebfcffb1b5c0/go.mod h1:hI742Nqp5OhwiqlzhgfbWU4mW4yO10fP+LoT9WOswdU=
sourcegraph.com/sourcegraph/appdash v0.0.0-20190731080439-ebfcffb1b5c0/go.mod h1:hI742Nqp5OhwiqlzhgfbWU4mW4yO10fP+LoT9WOswdU=
157 changes: 75 additions & 82 deletions hypergrid-aide/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"fmt"
"io"
"log"
"os"
"strconv"
Expand All @@ -12,60 +13,105 @@ import (
// Importing the general purpose Cosmos blockchain client
"github.com/ignite/cli/v28/ignite/pkg/cosmosaccount"
"github.com/ignite/cli/v28/ignite/pkg/cosmosclient"
"gopkg.in/yaml.v3"
// Importing the types package of your blog blockchain
)

const SOLANA_RPC_ENDPOINT = "http://localhost:8899" //"https://devnet1.sonic.game" //
const COSMOS_RPC_ENDPOINT = "http://172.31.10.244:26657"
const COSMOS_ADDRESS_PREFIX = "cosmos"
const COSMOS_HOME = ".hypergrid-ssn"
const COSMOS_KEY = "my_key"
const COSMOS_GAS = "100000000"
// Default values for the global variables
var SOLANA_RPC_ENDPOINT = "http://localhost:8899"
var SOLANA_BASELAYER_RPC = "https://api.devnet.solana.com"
var SOLANA_PRIVATE_KEY = "~/.config/solana/id.json"
var COSMOS_RPC_ENDPOINT = "http://172.31.10.244:26657"
var COSMOS_ADDRESS_PREFIX = "cosmos"
var COSMOS_HOME = ".hypergrid-ssn"
var COSMOS_KEY = "my_key"
var COSMOS_GAS = "100000000"

const AIDE_GET_BLOCKS_COUNT_LIMIT = uint64(200)

// read variables from yaml file
func readVariablesFromYaml(filename string) {
// Open the file
file, err := os.Open(filename)
if err != nil {
log.Println(err)
return
}
defer file.Close()

// Read the file
data, err := io.ReadAll(file)
if err != nil {
log.Println(err)
return
}

// Unmarshal the YAML
var params map[string]interface{}
err = yaml.Unmarshal(data, &params)
if err != nil {
log.Println(err)
return
}

// Print the params
log.Println(params)

// Set the global variables
SOLANA_RPC_ENDPOINT = params["solana_rpc"].(string)
SOLANA_BASELAYER_RPC = params["solana_baselayer_rpc"].(string)
SOLANA_PRIVATE_KEY = params["solana_private_key"].(string)
COSMOS_RPC_ENDPOINT = params["cosmos_rpc"].(string)
COSMOS_ADDRESS_PREFIX = params["cosmos_address_prefix"].(string)
COSMOS_HOME = params["cosmos_home"].(string)
COSMOS_KEY = params["cosmos_key"].(string)
COSMOS_GAS = params["cosmos_gas"].(string)

tools.COSMOS_ADDRESS_PREFIX = COSMOS_ADDRESS_PREFIX
}

func SendGridBlockFees(cosmos tools.CosmosClient, solana tools.SolanaClient, account cosmosaccount.Account, gridId string, limit uint64) {
first_available_slot, err := solana.GetFirstBlock()
if err != nil {
log.Fatal(err)
}
fmt.Println("first_available_slot: ", first_available_slot)
log.Println("first_available_slot: ", first_available_slot)

last_sent_slot, err := tools.GetLastSentSlot()
if err != nil {
log.Fatal(err)
}
fmt.Println("last_sent_slot: ", last_sent_slot)
log.Println("last_sent_slot: ", last_sent_slot)
//choose the max of last_sent_slot and first_available_slot - 1
start_slot := last_sent_slot + 1
if last_sent_slot < first_available_slot {
start_slot = first_available_slot
}

fmt.Println("start_slot: ", start_slot)
log.Println("start_slot: ", start_slot)
blocks, latest_slot, err := solana.GetBlocks(start_slot, limit)
fmt.Println("start_slot2: ", start_slot)
log.Println("start_slot2: ", start_slot)
if err != nil {
fmt.Println("GetBlocks fail")
log.Println("GetBlocks fail")
log.Fatal(err)
}
fmt.Println("blocks: ", len(blocks))
log.Println("blocks: ", len(blocks))
if len(blocks) > 0 {
fmt.Println("SendGridBlockFees")
log.Println("SendGridBlockFees")
resp, err_send := cosmos.SendGridBlockFees(account, gridId, blocks)
if err_send != nil {
log.Fatal(err_send)
fmt.Println("SendGridBlockFees fail")
log.Println("SendGridBlockFees fail")
} else {
fmt.Println("SendGridBlockFees success")
log.Println("SendGridBlockFees success")
last_sent_slot = latest_slot //blocks[len(blocks)-1].Slot
_, err = tools.SetLastSentSlot(last_sent_slot)
if err != nil {
log.Fatal(err)
}

}
fmt.Print("MsgCreateGridTxFee:", resp)
log.Print("MsgCreateGridTxFee:", resp)
} else {
last_sent_slot = latest_slot
_, err = tools.SetLastSentSlot(last_sent_slot)
Expand All @@ -74,33 +120,24 @@ func SendGridBlockFees(cosmos tools.CosmosClient, solana tools.SolanaClient, acc
}

}

// queryResp, err := cosmos.QueryAllGridBlockFees()
// if err != nil {
// log.Fatal(err)
// }

// // Print response from querying all the posts
// fmt.Print("\n\nAll grid tx fee:\n\n")
// fmt.Println(queryResp)
}

func SendGridInbox(cosmos tools.CosmosClient, solana tools.SolanaClient, account cosmosaccount.Account, gridId string) {
func SendGridInbox(solana tools.SolanaClient) {
block, err := solana.GetLastBlock()
if err != nil {
log.Fatal(err)
}

cosmos.SendGridInbox(account, gridId, block)
tools.SendTxInbox(SOLANA_BASELAYER_RPC, block.Slot, block.Blockhash)
}

func SyncStateAccount(cosmos tools.CosmosClient, account cosmosaccount.Account, source string, pubkey string) {
res, err := cosmos.SyncStateAccount(account, source, pubkey)
func SyncStateAccount(cosmos tools.CosmosClient, account cosmosaccount.Account, source string, pubkey string, version string) {
res, err := cosmos.SyncStateAccount(account, source, pubkey, version)
if err != nil {
log.Fatal(err)
}
fmt.Print("SyncStateAccount:\n\n")
fmt.Println(res)
log.Print("SyncStateAccount:\n\n")
log.Println(res)
}

func main() {
Expand All @@ -117,15 +154,19 @@ func main() {
// os.Exit(1)
}

//read variables from yaml file
readVariablesFromYaml(home + "/.hypergrid-aide.yaml")

command := args[1]
switch command {
case "sync":
if len(args) < 5 {
fmt.Println("Usage: hypergrid-aide sync <source> <pubkey>")
fmt.Println("Usage: hypergrid-aide sync <source> <pubkey> <version>")
os.Exit(1)
}
source := args[2]
pubkey := args[3]
version := args[4]
cosmos := tools.NewCosmosClient(
cosmosclient.WithNodeAddress(COSMOS_RPC_ENDPOINT),
cosmosclient.WithAddressPrefix(COSMOS_ADDRESS_PREFIX),
Expand All @@ -136,26 +177,11 @@ func main() {
if err != nil {
log.Fatal(err)
}
SyncStateAccount(*cosmos, account, source, pubkey)
SyncStateAccount(*cosmos, account, source, pubkey, version)
// break
case "inbox":
cosmos := tools.NewCosmosClient(
cosmosclient.WithNodeAddress(COSMOS_RPC_ENDPOINT),
cosmosclient.WithAddressPrefix(COSMOS_ADDRESS_PREFIX),
cosmosclient.WithHome(home+"/"+COSMOS_HOME),
cosmosclient.WithGas(COSMOS_GAS),
)
solana := tools.NewSolanaClient(SOLANA_RPC_ENDPOINT)
account, err := cosmos.Account(COSMOS_KEY)
if err != nil {
log.Fatal(err)
}
resp, err := solana.GetIdentity()
if err != nil {
log.Fatal(err)
}
gridId := resp.Identity.String()
SendGridInbox(*cosmos, *solana, account, gridId)
SendGridInbox(*solana)
// break
case "block":
limit := AIDE_GET_BLOCKS_COUNT_LIMIT
Expand Down Expand Up @@ -188,37 +214,4 @@ func main() {
default:
fmt.Println("Usage: hypergrid-aide <command>")
}

// fmt.Println("Hypergrid Aide")

// cosmos := tools.NewCosmosClient(
// cosmosclient.WithNodeAddress(COSMOS_RPC_ENDPOINT),
// cosmosclient.WithAddressPrefix(COSMOS_ADDRESS_PREFIX),
// cosmosclient.WithHome(COSMOS_HOME),
// cosmosclient.WithGas("100000000"),
// )

// solana := tools.NewSolanaClient(SOLANA_RPC_ENDPOINT)

// // Account `alice` was initialized during `ignite chain serve`
// accountName := COSMOS_KEY

// // Get account from the keyring
// account, err := cosmos.Account(accountName)
// if err != nil {
// log.Fatal(err)
// }

// resp, err := solana.GetIdentity()
// if err != nil {
// log.Fatal(err)
// }

// gridId := resp.Identity.String()
// fmt.Println("Grid ID: ", gridId)

// SendGridBlockFees(*cosmos, *solana, account, gridId)

// SendGridInbox(*cosmos, *solana, account, gridId)

}
Loading
Loading