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: unfork cometbft, cosmos-sdk, ibc-go #16

Merged
merged 40 commits into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
2a634d1
init
hoank101 Nov 2, 2024
9962526
add PreBlocker
hoank101 Nov 2, 2024
7a918a8
add abci test
TropicalDog17 Nov 4, 2024
079e813
add custom mempool
hoank101 Nov 9, 2024
c66a6f7
add mempool_priority
hoank101 Nov 16, 2024
ae69440
add more test
hoank101 Nov 16, 2024
b9f8a09
update test
hoank101 Nov 16, 2024
9fe05ff
update test
hoank101 Nov 18, 2024
a2b2dbb
update test
hoank101 Nov 18, 2024
7c9655c
add fifo mempool by sender nonce
hoank101 Nov 22, 2024
348b0b1
update mempool
hoank101 Nov 22, 2024
e4c99bd
update test
hoank101 Nov 22, 2024
17edbff
bet
hoank101 Nov 22, 2024
3b91598
bet
hoank101 Nov 22, 2024
a0e8ff5
bet
hoank101 Nov 22, 2024
0c23b00
update the mempool
hoank101 Nov 23, 2024
8f9aacc
update the mempool
hoank101 Nov 23, 2024
bd6a56f
update mempool
hoank101 Nov 24, 2024
54ead04
update mempool
hoank101 Nov 24, 2024
f3fdb95
bet
hoank101 Nov 24, 2024
c02272d
bet
hoank101 Nov 24, 2024
7289b31
feat: unfork cosmos sdk (#17)
kien6034 Nov 24, 2024
16a79b2
upgrade cometbft to v0.37.13
hoank101 Nov 24, 2024
7a6027a
Merge branch 'develop' of https://github.com/orbitorg/core into feat/…
hoank101 Nov 25, 2024
760062b
bet
hoank101 Nov 25, 2024
1875d99
test: add test override config cache size (#30)
tnv1 Nov 27, 2024
077e7e9
fix: handle totalPower is zero (#31)
tnv1 Nov 28, 2024
2ac4141
[ibc] move check memo and receiver length logic to antehandler (#28)
kien6034 Nov 28, 2024
fb782fb
test: add test BlockHeightMiddleware (#34)
tnv1 Nov 28, 2024
668e387
update script
hoank101 Nov 28, 2024
aa7fe0e
bet
hoank101 Nov 28, 2024
35311c2
downgrade to go1.20
TropicalDog17 Nov 28, 2024
0775a78
pin taskgroup 0.6.0 for go < 1.21
TropicalDog17 Nov 28, 2024
2433f89
fix
TropicalDog17 Nov 28, 2024
3e4a438
lint
TropicalDog17 Nov 28, 2024
ae86d70
bet
TropicalDog17 Nov 28, 2024
05f1392
fix test
TropicalDog17 Nov 28, 2024
343479f
feat: add upgrade handle (#35)
hoank101 Nov 28, 2024
508dfb9
update script
hoank101 Dec 1, 2024
1039e51
bet
TropicalDog17 Dec 2, 2024
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
71 changes: 24 additions & 47 deletions app/app.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package app

import (
"cosmossdk.io/math"
"encoding/json"
"fmt"
"github.com/classic-terra/core/v3/app/mempool"
signer_extraction "github.com/skip-mev/block-sdk/adapters/signer_extraction_adapter"
"github.com/skip-mev/block-sdk/block"
blockbase "github.com/skip-mev/block-sdk/block/base"
"io"
stdlog "log"
"net/http"
Expand Down Expand Up @@ -43,8 +48,6 @@ import (

"github.com/classic-terra/core/v3/app/keepers"
terraappparams "github.com/classic-terra/core/v3/app/params"
anteauth "github.com/classic-terra/core/v3/custom/auth/ante"

// upgrades
"github.com/classic-terra/core/v3/app/upgrades"
v2 "github.com/classic-terra/core/v3/app/upgrades/v2"
Expand Down Expand Up @@ -149,7 +152,6 @@ func NewTerraApp(
txConfig := encodingConfig.TxConfig

invCheckPeriod := cast.ToUint(appOpts.Get(server.FlagInvCheckPeriod))

bApp := baseapp.NewBaseApp(appName, logger, db, txConfig.TxDecoder(), baseAppOptions...)
bApp.SetCommitMultiStoreTracer(traceStore)
bApp.SetVersion(version.Version)
Expand Down Expand Up @@ -261,9 +263,27 @@ func NewTerraApp(
panic(err)
}

signerExtractor := signer_extraction.NewDefaultAdapter()
defaultLaneConfig := blockbase.LaneConfig{
Logger: app.Logger(),
TxEncoder: app.txConfig.TxEncoder(),
TxDecoder: app.txConfig.TxDecoder(),
MaxBlockSpace: math.LegacyZeroDec(),
MaxTxs: 0,
SignerExtractor: signerExtractor,
}

defaultLane := mempool.NewDefaultLane(defaultLaneConfig)
lanes := []block.Lane{defaultLane}
mempool, err := block.NewLanedMempool(app.Logger(), lanes)
if err != nil {
panic(err)
}

app.SetMempool(mempool)

app.SetAnteHandler(anteHandler)
app.SetPostHandler(postHandler)
app.SetPrepareProposal(app.prepareProposal)
app.SetEndBlocker(app.EndBlocker)

// must be before Loading version
Expand Down Expand Up @@ -307,49 +327,6 @@ func (app *TerraApp) BeginBlocker(ctx sdk.Context, req abci.RequestBeginBlock) a
return app.mm.BeginBlock(ctx, req)
}

func (app *TerraApp) prepareProposal(ctx sdk.Context, req abci.RequestPrepareProposal) abci.ResponsePrepareProposal {
// get current txs
currentTxs := req.Txs

// Create slices for oracle and non-oracle txs
oracleTxs := make([][]byte, 0)
otherTxs := make([][]byte, 0)

// Track seen txs to prevent duplicates
seenTxs := make(map[string]bool)

// Separate oracle and non-oracle txs
for _, tx := range currentTxs {
// Skip if we've seen this tx before
txHash := string(tx)
if seenTxs[txHash] {
continue
}
seenTxs[txHash] = true

// Decode the transaction
txDecoder := app.txConfig.TxDecoder()
decodedTx, err := txDecoder(tx)
if err != nil {
continue
}

// Check if it's an oracle tx
if anteauth.IsOracleTx(decodedTx.GetMsgs()) {
oracleTxs = append(oracleTxs, tx)
} else {
otherTxs = append(otherTxs, tx)
}
}

// Combine txs with oracle txs first
orderedTxs := append(oracleTxs, otherTxs...)

return abci.ResponsePrepareProposal{
Txs: orderedTxs,
}
}

// EndBlocker application updates every end block
func (app *TerraApp) EndBlocker(ctx sdk.Context, req abci.RequestEndBlock) abci.ResponseEndBlock {
return app.mm.EndBlock(ctx, req)
Expand Down
36 changes: 36 additions & 0 deletions app/mempool/default.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package mempool

import (
"github.com/skip-mev/block-sdk/block"
blockbase "github.com/skip-mev/block-sdk/block/base"
)

const (
// DefaultName defines the name of the priority lane.
DefaultName = "default"
)

// NewDefaultLane defines a default lane implementation. The default lane orders
// transactions by the transaction fees. The default lane accepts any transaction
// that should not be ignored (as defined by the IgnoreList in the LaneConfig).
// The default lane builds and verifies blocks in a similar fashion to how the
// CometBFT/Tendermint consensus engine builds and verifies blocks pre SDK version
// 0.47.0.
func NewDefaultLane(cfg blockbase.LaneConfig) block.Lane {
lane := &blockbase.BaseLane{}
proposalHandler := blockbase.NewDefaultProposalHandler(lane)

_lane, err := blockbase.NewBaseLane(
hoank101 marked this conversation as resolved.
Show resolved Hide resolved
cfg,
DefaultName,
blockbase.WithMempool(NewMempool(blockbase.NewDefaultTxPriority(), cfg.SignerExtractor, cfg.MaxTxs)),
blockbase.WithPrepareLaneHandler(proposalHandler.PrepareLaneHandler()),
blockbase.WithProcessLaneHandler(proposalHandler.ProcessLaneHandler()),
)
if err != nil {
panic(err)
}

*lane = *_lane
return lane
}
142 changes: 142 additions & 0 deletions app/mempool/mempool.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
package mempool

import (
"context"
"errors"
"fmt"

anteauth "github.com/classic-terra/core/v3/custom/auth/ante"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkmempool "github.com/cosmos/cosmos-sdk/types/mempool"
signer_extraction "github.com/skip-mev/block-sdk/adapters/signer_extraction_adapter"
blockbase "github.com/skip-mev/block-sdk/block/base"
)

type (
txKey struct {
nonce uint64
sender string
}

// Mempool defines a mempool that orders transactions based on the
// txPriority. The mempool is a wrapper on top of the SDK's Priority Nonce mempool.
// It include's additional helper functions that allow users to determine if a
// transaction is already in the mempool and to compare the priority of two
// transactions.
Mempool[C comparable] struct {
// index defines an index of transactions.
index sdkmempool.Mempool

// signerExtractor defines the signer extraction adapter that allows us to
// extract the signer from a transaction.
extractor signer_extraction.Adapter

// txCache is a map of all transactions in the mempool. It is used
// to quickly check if a transaction is already in the mempool.
txCache map[txKey]struct{}
}
)

// NewMempool returns a new Mempool.
func NewMempool[C comparable](txPriority blockbase.TxPriority[C], extractor signer_extraction.Adapter, maxTx int) *Mempool[C] {
return &Mempool[C]{
index: blockbase.NewPriorityMempool(
blockbase.PriorityNonceMempoolConfig[C]{
TxPriority: txPriority,
MaxTx: maxTx,
},
extractor,
),
extractor: extractor,
txCache: make(map[txKey]struct{}),
}
}

// Priority returns the priority of the transaction.
func (cm *Mempool[C]) Priority(ctx sdk.Context, tx sdk.Tx) any {
msgs := tx.GetMsgs()
fmt.Println("hoank get Priority")
isOracleTx := anteauth.IsOracleTx(msgs)
if isOracleTx {
return 2 // Higher priority for oracle transactions
}
return 1 // Normal priority for other transactions
}

// CountTx returns the number of transactions in the mempool.
func (cm *Mempool[C]) CountTx() int {
return cm.index.CountTx()
}

// Select returns an iterator of all transactions in the mempool. NOTE: If you
// remove a transaction from the mempool while iterating over the transactions,
// the iterator will not be aware of the removal and will continue to iterate
// over the removed transaction. Be sure to reset the iterator if you remove a transaction.
func (cm *Mempool[C]) Select(ctx context.Context, txs [][]byte) sdkmempool.Iterator {
return cm.index.Select(ctx, txs)
}

// Compare return 0 to ignore priority check in ProcessLaneHandler.
func (cm *Mempool[C]) Compare(ctx sdk.Context, this sdk.Tx, other sdk.Tx) (int, error) {
thisPriority := cm.Priority(ctx, this).(int)
otherPriority := cm.Priority(ctx, other).(int)
fmt.Println("hoank compare", thisPriority, otherPriority)
return otherPriority - thisPriority, nil
}

// Contains returns true if the transaction is contained in the mempool.
func (cm *Mempool[C]) Contains(tx sdk.Tx) bool {
if key, err := cm.getTxKey(tx); err != nil {
return false
} else {
if _, ok := cm.txCache[key]; ok {
return true
} else {
return false
}
}
}
hoank101 marked this conversation as resolved.
Show resolved Hide resolved

// Insert inserts a transaction into the mempool.
func (cm *Mempool[C]) Insert(ctx context.Context, tx sdk.Tx) error {
if err := cm.index.Insert(ctx, tx); err != nil {
return fmt.Errorf("failed to insert tx into auction index: %w", err)
hoank101 marked this conversation as resolved.
Show resolved Hide resolved
}

if key, err := cm.getTxKey(tx); err != nil {
return err
} else {
cm.txCache[key] = struct{}{}
}

return nil
}

// Remove removes a transaction from the mempool.
func (cm *Mempool[C]) Remove(tx sdk.Tx) error {
if err := cm.index.Remove(tx); err != nil && !errors.Is(err, sdkmempool.ErrTxNotFound) {
return fmt.Errorf("failed to remove transaction from the mempool: %w", err)
}

if key, err := cm.getTxKey(tx); err != nil {
return err
} else {
delete(cm.txCache, key)
}

return nil
}

func (cm *Mempool[C]) getTxKey(tx sdk.Tx) (txKey, error) {
signers, err := cm.extractor.GetSigners(tx)
if err != nil {
return txKey{}, err
}
if len(signers) == 0 {
return txKey{}, fmt.Errorf("attempted to remove a tx with no signatures")
}
sig := signers[0]
sender := sig.Signer.String()
nonce := sig.Sequence
return txKey{nonce, sender}, nil
}
Loading