Skip to content

Commit

Permalink
Merge pull request #40 from mirrorworld-universe/wsw
Browse files Browse the repository at this point in the history
fixing:  (HAL-07) INCONSISTENT LOGGING MECHANISM
  • Loading branch information
ZeneDeLuca authored Oct 8, 2024
2 parents ca9335a + e08311e commit aa0c2e6
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 50 deletions.
24 changes: 12 additions & 12 deletions hypergrid-aide/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,43 +29,43 @@ func SendGridBlockFees(cosmos tools.CosmosClient, solana tools.SolanaClient, acc
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 Down Expand Up @@ -99,8 +99,8 @@ func SyncStateAccount(cosmos tools.CosmosClient, account cosmosaccount.Account,
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 Down
33 changes: 16 additions & 17 deletions hypergrid-aide/tools/cosmos.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package tools

import (
"context"
"fmt"
"hypergridssn/x/hypergridssn/types"
"log"
"strconv"
Expand All @@ -21,7 +20,7 @@ type CosmosClient struct {
}

func NewCosmosClient(options ...cosmosclient.Option) *CosmosClient {
fmt.Println("NewCosmosClient")
log.Println("NewCosmosClient")
// Create a Cosmos client instance
ctx := context.Background()
cosmos, err := cosmosclient.New(ctx, options...)
Expand All @@ -44,7 +43,7 @@ func (c *CosmosClient) SendGridBlockFees(account cosmosaccount.Account, gridId s
if err != nil {
log.Fatal(err)
}
fmt.Println("Account: ", address)
log.Println("Account: ", address)

items := []*types.GridBlockFeeItem{}
for _, block := range blocks {
Expand Down Expand Up @@ -73,8 +72,8 @@ func (c *CosmosClient) SendGridBlockFees(account cosmosaccount.Account, gridId s
return nil, err
}
// Print response from broadcasting a transaction
fmt.Print("MsgCreateGridTxFee:\n\n")
fmt.Println(txResp)
log.Print("MsgCreateGridTxFee:\n\n")
log.Println(txResp)
return &txResp, nil
}
return nil, nil
Expand All @@ -85,7 +84,7 @@ func (c *CosmosClient) SendGridInbox(account cosmosaccount.Account, gridId strin
if err != nil {
log.Fatal(err)
}
fmt.Println("Account: ", address)
log.Println("Account: ", address)

// Define a message to create a grid inbox
msg := types.MsgCreateGridInbox{
Expand All @@ -103,8 +102,8 @@ func (c *CosmosClient) SendGridInbox(account cosmosaccount.Account, gridId strin
return nil, err
}
// Print response from broadcasting a transaction
fmt.Print("MsgCreateGridTxFee:\n\n")
fmt.Println(txResp)
log.Print("MsgCreateGridTxFee:\n\n")
log.Println(txResp)

return &txResp, nil
}
Expand All @@ -114,7 +113,7 @@ func (c *CosmosClient) SyncStateAccount(account cosmosaccount.Account, source st
if err != nil {
log.Fatal(err)
}
fmt.Println("Account: ", address)
log.Println("Account: ", address)

// Define a message to create a grid inbox
msg := types.MsgCreateSolanaAccount{
Expand All @@ -132,8 +131,8 @@ func (c *CosmosClient) SyncStateAccount(account cosmosaccount.Account, source st
return nil, err
}
// Print response from broadcasting a transaction
fmt.Print("MsgCreateSolanaAccount:\n\n")
fmt.Println(txResp)
log.Print("MsgCreateSolanaAccount:\n\n")
log.Println(txResp)

return &txResp, nil
}
Expand All @@ -152,8 +151,8 @@ func (c *CosmosClient) QueryAllGridBlockFees() (*types.QueryAllGridBlockFeeRespo
}

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

Expand All @@ -171,8 +170,8 @@ func (c *CosmosClient) QueryGridBlockFee(_id uint64) (*types.QueryGetGridBlockFe
}

// Print response from querying all the posts
fmt.Print("\n\nGet grid tx fee:\n\n")
fmt.Println(queryResp)
log.Print("\n\nGet grid tx fee:\n\n")
log.Println(queryResp)
return queryResp, err
}

Expand All @@ -187,7 +186,7 @@ func (c *CosmosClient) QueryAllHypergridNodes() (*types.QueryAllHypergridNodeRes
}

// Print response from querying all the posts
fmt.Print("\n\nAll Hypergrid Nodes:\n\n")
fmt.Println(queryResp)
log.Print("\n\nAll Hypergrid Nodes:\n\n")
log.Println(queryResp)
return queryResp, err
}
23 changes: 12 additions & 11 deletions hypergrid-aide/tools/solana.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package tools
import (
"context"
"fmt"
"log"
"os"

"github.com/davecgh/go-spew/spew"
Expand All @@ -29,7 +30,7 @@ type SolanaClient struct {
}

func NewSolanaClient(endpoint string) *SolanaClient {
fmt.Println("NewSolanaClient")
log.Println("NewSolanaClient")
return &SolanaClient{
Endpoint: endpoint,
Client: rpc.New(endpoint),
Expand Down Expand Up @@ -84,7 +85,7 @@ func (s *SolanaClient) GetLastBlock() (SolanaBlock, error) {
}

func (s *SolanaClient) GetBlocks(start_slot uint64, limit uint64) ([]SolanaBlock, uint64, error) {
fmt.Println("GetBlocks start_slot: ", start_slot)
log.Println("GetBlocks start_slot: ", start_slot)
resp, err := s.Client.GetBlocksWithLimit(context.TODO(), start_slot, limit, rpc.CommitmentFinalized)

if err != nil {
Expand All @@ -96,7 +97,7 @@ func (s *SolanaClient) GetBlocks(start_slot uint64, limit uint64) ([]SolanaBlock
rewards := true
latest_slot := uint64(0)
for _, block := range *resp {
fmt.Println("block: ", block)
log.Println("block: ", block)
latest_slot = block
resp2, err := s.Client.GetBlockWithOpts(context.TODO(), block, &rpc.GetBlockOpts{
// Encoding: solana.EncodingJSONParsed,
Expand All @@ -105,11 +106,11 @@ func (s *SolanaClient) GetBlocks(start_slot uint64, limit uint64) ([]SolanaBlock
Rewards: &rewards,
})
if err != nil {
fmt.Println("error: ", err.Error())
log.Println("error: ", err.Error())
continue
}

fmt.Println("blockhash: ", resp2.Blockhash.String())
log.Println("blockhash: ", resp2.Blockhash.String())

Fee := uint64(0)
voteFee := uint64(0)
Expand All @@ -128,8 +129,8 @@ func (s *SolanaClient) GetBlocks(start_slot uint64, limit uint64) ([]SolanaBlock
Fee += tx.Meta.Fee
}
}
fmt.Println("voteFee: ", voteFee)
fmt.Println("Fee: ", Fee)
log.Println("voteFee: ", voteFee)
log.Println("Fee: ", Fee)

// // Calculate the fee
// Rewards := uint64(0)
Expand Down Expand Up @@ -193,8 +194,8 @@ func (s *SolanaClient) SendTransaction(programID string) (*solana.Signature, err
if err != nil {
panic(err)
}
fmt.Println("accountFrom private key:", accountFrom)
fmt.Println("accountFrom public key:", accountFrom.PublicKey())
log.Println("accountFrom private key:", accountFrom)
log.Println("accountFrom public key:", accountFrom.PublicKey())

recent, err := s.Client.GetRecentBlockhash(context.TODO(), rpc.CommitmentFinalized)
if err != nil {
Expand Down Expand Up @@ -229,7 +230,7 @@ func (s *SolanaClient) SendTransaction(programID string) (*solana.Signature, err
},
)
if err != nil {
fmt.Println(fmt.Errorf("unable to sign transaction: %w", err))
log.Println(fmt.Errorf("unable to sign transaction: %w", err))
return nil, err
}
spew.Dump(tx)
Expand All @@ -249,7 +250,7 @@ func (s *SolanaClient) SendTransaction(programID string) (*solana.Signature, err
func (s *SolanaClient) GetTransaction(signature string) (*rpc.GetTransactionResult, error) {
txhash, err := solana.SignatureFromBase58(signature)
if err != nil {
fmt.Println(fmt.Errorf("unable to sign transaction: %w", err))
log.Println(fmt.Errorf("unable to sign transaction: %w", err))
return nil, err
}
return s.Client.GetTransaction(context.TODO(), txhash, &rpc.GetTransactionOpts{
Expand Down
11 changes: 5 additions & 6 deletions hypergrid-aide/tools/sys.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package tools

import (
"fmt"
"io"
"log"
"os"
Expand Down Expand Up @@ -34,24 +33,24 @@ func GetLastSentSlot() (uint64, error) {

fd, err := io.ReadAll(f)
if err != nil {
fmt.Println("read to fd fail", err)
log.Println("read to fd fail", err)
return 0, err
}
last_sent_slot, err := strconv.ParseUint(string(fd), 10, 64) // 将fd从[]byte转换为string,然后转换为int
if err != nil {
fmt.Println("convert fd to int fail", err)
log.Println("convert fd to int fail", err)
return 0, err

}
fmt.Println("read last_sent_slot: ", last_sent_slot)
log.Println("read last_sent_slot: ", last_sent_slot)
return last_sent_slot, nil
}
fmt.Println("file not exist")
log.Println("file not exist")
return 0, nil
}

func SetLastSentSlot(slot uint64) (bool, error) {
fmt.Println("SetLastSentSlot: ", slot)
log.Println("SetLastSentSlot: ", slot)
if !CheckFileExist(AIDE_CONFIG_FILE) {

f, err := os.Create(AIDE_CONFIG_FILE)
Expand Down
9 changes: 5 additions & 4 deletions tools/solana.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"crypto/sha256"
"encoding/binary"
"fmt"
"log"
"os"
"strings"

Expand Down Expand Up @@ -154,8 +155,8 @@ func getLocalPrivateKey() (solana.PrivateKey, error) {
// panic(err)
return nil, err
}
fmt.Println("accountFrom private key:", accountFrom)
fmt.Println("accountFrom public key:", accountFrom.PublicKey())
log.Println("accountFrom private key:", accountFrom)
log.Println("accountFrom public key:", accountFrom.PublicKey())

return accountFrom, nil
}
Expand Down Expand Up @@ -334,7 +335,7 @@ func SendTxInbox(rpcUrl string, slot uint64, hash string) (*solana.Signature, *s
return nil, nil, err
}
data_key := data_account.PublicKey()
fmt.Println("data_account:", data_key)
log.Println("data_account:", data_key)

signer, err := getLocalPrivateKey()
if err != nil {
Expand All @@ -355,7 +356,7 @@ func SendTxInbox(rpcUrl string, slot uint64, hash string) (*solana.Signature, *s
// panic(err)
return nil, nil, err
}
fmt.Println("signature: ", sig)
log.Println("signature: ", sig)

return sig, &data_key, nil
}
Expand Down

0 comments on commit aa0c2e6

Please sign in to comment.