forked from evmos/ethermint
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of https://github.com/b2network/b2-node
- Loading branch information
Showing
9 changed files
with
177 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package bitcoin | ||
|
||
import ( | ||
"context" | ||
"database/sql" | ||
"fmt" | ||
"time" | ||
) | ||
|
||
func openDB(cfg StateConfig) (*sql.DB, error) { | ||
dsn := fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=disable", | ||
cfg.Host, cfg.Port, cfg.User, cfg.Pass, cfg.DBName) | ||
db, err := sql.Open("postgres", dsn) | ||
if err != nil { | ||
return nil, err | ||
} | ||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) | ||
defer cancel() | ||
|
||
err = db.PingContext(ctx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return db, nil | ||
} | ||
|
||
type VerifiedBatch struct { | ||
BatchNum int64 | ||
TxHash string | ||
Aggregator string | ||
StateRoot string | ||
BlockNum int64 | ||
IsTrusted bool | ||
} | ||
|
||
func GetStateRoot(cfg StateConfig, index int64) ([]*VerifiedBatch, error) { | ||
db, err := openDB(cfg) | ||
if err != nil { | ||
return nil, fmt.Errorf("openDB err:%w", err) | ||
} | ||
userSQL := "select state_root, max(block_num) block_num from state.verified_batch where block_num > $1 and " + | ||
" is_trusted=true group by state_root order by block_num desc" | ||
rows, err := db.Query(userSQL, index) | ||
if err != nil { | ||
return nil, fmt.Errorf("vin parse err:%w", err) | ||
} | ||
var batchs []*VerifiedBatch | ||
|
||
for rows.Next() { | ||
var stateRoot string | ||
var blockNum int64 | ||
err = rows.Scan(&stateRoot, &blockNum) | ||
if err != nil { | ||
return nil, fmt.Errorf("vin parse err:%w", err) | ||
} | ||
batch := &VerifiedBatch{ | ||
StateRoot: stateRoot, | ||
BlockNum: blockNum, | ||
} | ||
batchs = append(batchs, batch) | ||
} | ||
return batchs, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package bitcoin_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/evmos/ethermint/bitcoin" | ||
_ "github.com/lib/pq" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestLocalGetStateRoot(t *testing.T) { | ||
cfg := bitcoin.StateConfig{ | ||
Host: "localhost", | ||
Port: 5432, | ||
User: "state_user", | ||
Pass: "state_password", | ||
DBName: "state_db", | ||
} | ||
items, err := bitcoin.GetStateRoot(cfg, 1) | ||
require.NoError(t, err) | ||
for _, item := range items { | ||
require.Equal(t, int64(1299), item.BlockNum) | ||
require.Equal(t, "0x1cc9e812fdad14a03f6e3c8563393d0e0c155dbd1d54361f41b33da46b087294", item.StateRoot) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters