Skip to content

Commit

Permalink
wip explorer
Browse files Browse the repository at this point in the history
  • Loading branch information
hanchon committed Aug 29, 2024
1 parent b95ebab commit 00d8841
Show file tree
Hide file tree
Showing 8 changed files with 251 additions and 42 deletions.
5 changes: 3 additions & 2 deletions cmd/playground/explorer/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"

"github.com/hanchon/hanchond/playground/evmos"
"github.com/hanchon/hanchond/playground/explorer"
"github.com/hanchon/hanchond/playground/sql"
"github.com/spf13/cobra"
)
Expand All @@ -25,8 +26,8 @@ var startCmd = &cobra.Command{
// TODO: move the newFromDB to cosmos daemon
e := evmos.NewEvmosFromDB(queries, nodeID)

fmt.Println(e.Ports.P8545)
fmt.Println(e.Ports.P1317)
ex := explorer.NewLocalExplorerClient(e.Ports.P8545, e.Ports.P1317, e.HomeDir)
ex.ProcessBlocks()
},
}

Expand Down
12 changes: 12 additions & 0 deletions lib/converter/hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,25 @@ import (
"crypto/sha256"
"encoding/base64"
"encoding/hex"

"golang.org/x/crypto/sha3"
)

func GenerateCosmosTxHash(txBytes []byte) string {
hash := sha256.Sum256(txBytes)
return hex.EncodeToString(hash[:])
}

func GenerateEthTxHash(txBytes []byte) (string, error) {
hash := sha3.NewLegacyKeccak256()
_, err := hash.Write(txBytes)
if err != nil {
return "", err
}
buf := hash.Sum(nil)
return hex.EncodeToString(buf), nil
}

func GenerateCosmosTxHashWithBase64(txInBase64 string) (string, error) {
txBytes, err := base64.StdEncoding.DecodeString(txInBase64)
if err != nil {
Expand Down
28 changes: 28 additions & 0 deletions playground/explorer/database.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package explorer

import (
"context"
"sync"

"github.com/hanchon/hanchond/playground/explorer/database"
)

type Database struct {
queries *database.Queries
mutex *sync.Mutex
ctx context.Context
}

func NewDatabase(ctx context.Context, queries *database.Queries) *Database {
return &Database{
queries: queries,
mutex: &sync.Mutex{},
ctx: ctx,
}
}

func (d *Database) GetLatestBlock() (database.Block, error) {
d.mutex.Lock()
defer d.mutex.Unlock()
return d.queries.GetLatestBlock(d.ctx)
}
19 changes: 17 additions & 2 deletions playground/explorer/database/explorerquery.sql
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,23 @@ DELETE FROM blocks WHERE id = ?;

-- name: InsertBlock :one
INSERT INTO blocks(
height, time, txcount, totalValue, proposer, gasused, gaslimit, basefee, hash, parenthash
height, txcount, hash, parenthash
) VALUES (
?, ?, ?, ?, ?, ?, ?, ?, ?, ?
?, ?, ?, ?
)
RETURNING id;

-- name: InsertTransaction :one
INSERT INTO transactions(
cosmoshash, ethhash, content, sender, blockheight
) VALUES (
?, ?, ?, ?, ?
)
RETURNING id;

-- name: GetTransactions :many
SELECT * FROM transactions;

-- name: GetLimitedTransactions :many
SELECT * FROM transactions ORDER BY id DESC LIMIT ?;

122 changes: 101 additions & 21 deletions playground/explorer/database/explorerquery.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 13 additions & 6 deletions playground/explorer/database/explorerschema.sql
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
CREATE TABLE IF NOT EXISTS blocks(
id BIGSERIAL NOT NULL PRIMARY KEY,
height BIGINT UNIQUE NOT NULL,
time TIMESTAMP WITHOUT TIME ZONE NOT NULL,
txcount INTEGER NOT NULL,
totalValue NUMERIC(65, 0) NOT NULL,
proposer TEXT NOT NULL,
gasused NUMERIC(65, 0) NOT NULL,
gaslimit NUMERIC(65, 0) NOT NULL,
basefee NUMERIC(65, 0) NOT NULL,
hash TEXT NOT NULL,
parenthash TEXT NOT NULL
);

CREATE INDEX IF NOT EXISTS blocksindex on blocks (height);

CREATE TABLE IF NOT EXISTS transactions(
id BIGSERIAL NOT NULL PRIMARY KEY,
cosmoshash TEXT NOT NULL,
ethhash TEXT NOT NULL,
content TEXT NOT NULL,
sender TEXT NOT NULL,
blockheight BIGINT NOT NULL REFERENCES blocks(height) ON DELETE CASCADE
);


CREATE INDEX IF NOT EXISTS cosmoshash on transactions (cosmoshash);
CREATE INDEX IF NOT EXISTS ethhash on transactions (ethhash);
15 changes: 9 additions & 6 deletions playground/explorer/database/models.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 00d8841

Please sign in to comment.