Skip to content

Commit

Permalink
unify comet prefix for all cometbft imports
Browse files Browse the repository at this point in the history
this includes two trivial oneliner refactors to avoid some imports:
 * NewFilePV(secp.GenPrivKey()) -> GenFilePV()
 * NodePvKeyToAddress dropped, inlined in caller
  • Loading branch information
altergui committed Jan 9, 2024
1 parent dcfe566 commit b9beb0f
Show file tree
Hide file tree
Showing 23 changed files with 188 additions and 198 deletions.
6 changes: 3 additions & 3 deletions api/api_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"encoding/json"
"time"

tmtypes "github.com/cometbft/cometbft/types"
comettypes "github.com/cometbft/cometbft/types"
"github.com/google/uuid"
"go.vocdoni.io/dvote/types"
"go.vocdoni.io/dvote/vochain/indexer/indexertypes"
Expand Down Expand Up @@ -331,6 +331,6 @@ func CensusTypeToOrigin(ctype CensusTypeDescription) (models.CensusOrigin, []byt
}

type Block struct {
tmtypes.Block `json:",inline"`
Hash types.HexBytes `json:"hash" `
comettypes.Block `json:",inline"`
Hash types.HexBytes `json:"hash" `
}
6 changes: 3 additions & 3 deletions api/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"strconv"
"time"

tmtypes "github.com/cometbft/cometbft/types"
comettypes "github.com/cometbft/cometbft/types"
"go.vocdoni.io/dvote/crypto/zk/circuit"
"go.vocdoni.io/dvote/httprouter"
"go.vocdoni.io/dvote/httprouter/apirest"
Expand Down Expand Up @@ -753,7 +753,7 @@ func (a *API) chainBlockHandler(_ *apirest.APIdata, ctx *httprouter.HTTPContext)
return ErrBlockNotFound
}
block := &Block{
Block: tmtypes.Block{
Block: comettypes.Block{
Header: tmblock.Header,
Data: tmblock.Data,
Evidence: tmblock.Evidence,
Expand Down Expand Up @@ -788,7 +788,7 @@ func (a *API) chainBlockByHashHandler(_ *apirest.APIdata, ctx *httprouter.HTTPCo
return ErrBlockNotFound
}
block := &Block{
Block: tmtypes.Block{
Block: comettypes.Block{
Header: tmblock.Header,
Data: tmblock.Data,
Evidence: tmblock.Evidence,
Expand Down
8 changes: 4 additions & 4 deletions api/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
"math/big"
"reflect"

cmtpool "github.com/cometbft/cometbft/mempool"
ctypes "github.com/cometbft/cometbft/rpc/core/types"
cometpool "github.com/cometbft/cometbft/mempool"
cometcoretypes "github.com/cometbft/cometbft/rpc/core/types"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
"github.com/iancoleman/strcase"
Expand Down Expand Up @@ -44,10 +44,10 @@ func (a *API) electionSummary(pi *indexertypes.Process) ElectionSummary {
}

// sendTx wraps a.vocapp.SendTx(). If an error is returned, it's wrapped into an apirest.APIerror
func (a *API) sendTx(tx []byte) (*ctypes.ResultBroadcastTx, error) {
func (a *API) sendTx(tx []byte) (*cometcoretypes.ResultBroadcastTx, error) {
resp, err := a.vocapp.SendTx(tx)
switch {
case errors.As(err, &cmtpool.ErrMempoolIsFull{}):
case errors.As(err, &cometpool.ErrMempoolIsFull{}):
return nil, ErrVochainOverloaded.WithErr(err)
case err != nil:
return nil, ErrVochainSendTxFailed.WithErr(err)
Expand Down
6 changes: 2 additions & 4 deletions test/statedb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import (
"math/rand"
"testing"

secp "github.com/cometbft/cometbft/crypto/secp256k1"
"github.com/cometbft/cometbft/privval"
cometprivval "github.com/cometbft/cometbft/privval"
qt "github.com/frankban/quicktest"
"go.vocdoni.io/proto/build/go/models"

Expand All @@ -20,8 +19,7 @@ func TestAddValidator(t *testing.T) {
s := testcommon.NewVochainStateWithValidators(t)
rint := rand.Int()
tmp := t.TempDir()
val := privval.NewFilePV(
secp.GenPrivKey(),
val := cometprivval.GenFilePV(
fmt.Sprintf("%s/vochainBenchmark_keyfile%d", tmp, rint),
fmt.Sprintf("%s/vochainBenchmark_statefile%d", tmp, rint),
)
Expand Down
22 changes: 11 additions & 11 deletions test/testcommon/testutil/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"sync/atomic"
"time"

tmtypes "github.com/cometbft/cometbft/types"
comettypes "github.com/cometbft/cometbft/types"
"go.vocdoni.io/dvote/log"
)

Expand Down Expand Up @@ -37,9 +37,9 @@ func (b *MockBlockStore) NewBlock(height int64) {
panic(fmt.Sprintf("height is not the expected one (got:%d expected:%d)", height, count))
}
log.Infow("new block", "height", height)
b.set(height, &tmtypes.Block{
Header: tmtypes.Header{Height: height, Time: time.Now(), ChainID: "test"},
Data: tmtypes.Data{Txs: make([]tmtypes.Tx, 0)}},
b.set(height, &comettypes.Block{
Header: comettypes.Header{Height: height, Time: time.Now(), ChainID: "test"},
Data: comettypes.Data{Txs: make([]comettypes.Tx, 0)}},
)
}

Expand All @@ -48,26 +48,26 @@ func (b *MockBlockStore) EndBlock() int64 {
return b.height.Add(1)
}

func (b *MockBlockStore) Get(height int64) *tmtypes.Block {
func (b *MockBlockStore) Get(height int64) *comettypes.Block {
val, ok := b.blockByHeight.Load(height)
if !ok {
return nil
}
return val.(*tmtypes.Block)
return val.(*comettypes.Block)
}

func (b *MockBlockStore) GetByHash(hash []byte) *tmtypes.Block {
var block *tmtypes.Block
func (b *MockBlockStore) GetByHash(hash []byte) *comettypes.Block {
var block *comettypes.Block
b.blockByHeight.Range(func(key, value any) bool {
if bytes.Equal(value.(*tmtypes.Block).Hash().Bytes(), hash) {
block = value.(*tmtypes.Block)
if bytes.Equal(value.(*comettypes.Block).Hash().Bytes(), hash) {
block = value.(*comettypes.Block)
return false
}
return true
})
return block
}

func (b *MockBlockStore) set(height int64, block *tmtypes.Block) {
func (b *MockBlockStore) set(height int64, block *comettypes.Block) {
b.blockByHeight.Store(height, block)
}
11 changes: 4 additions & 7 deletions test/testcommon/vochain.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import (
"strconv"
"testing"

secp "github.com/cometbft/cometbft/crypto/secp256k1"
"github.com/cometbft/cometbft/privval"
cometprivval "github.com/cometbft/cometbft/privval"

"go.vocdoni.io/dvote/db"
"go.vocdoni.io/dvote/test/testcommon/testutil"
Expand Down Expand Up @@ -61,16 +60,14 @@ func NewVochainState(tb testing.TB) *state.State {

func NewVochainStateWithValidators(tb testing.TB) *state.State {
s := NewVochainState(tb)
vals := make([]*privval.FilePV, 2)
vals := make([]*cometprivval.FilePV, 2)
rint := rand.Int()
vals[0] = privval.NewFilePV(
secp.GenPrivKey(),
vals[0] = cometprivval.GenFilePV(
"/tmp/"+strconv.Itoa(rint),
"/tmp/"+strconv.Itoa(rint),
)
rint = rand.Int()
vals[1] = privval.NewFilePV(
secp.GenPrivKey(),
vals[1] = cometprivval.GenFilePV(
"/tmp/"+strconv.Itoa(rint),
"/tmp/"+strconv.Itoa(rint),
)
Expand Down
4 changes: 2 additions & 2 deletions vochain/account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"math/rand"
"testing"

abcitypes "github.com/cometbft/cometbft/abci/types"
cometabcitypes "github.com/cometbft/cometbft/abci/types"
"github.com/ethereum/go-ethereum/common"
qt "github.com/frankban/quicktest"

Expand Down Expand Up @@ -900,7 +900,7 @@ func sendTx(app *BaseApplication, signer *ethereum.SignKeys, stx *models.SignedT
return err
}

cktx := new(abcitypes.CheckTxRequest)
cktx := new(cometabcitypes.CheckTxRequest)
cktx.Tx = stxBytes
cktxresp, _ := app.CheckTx(context.Background(), cktx)
if cktxresp.Code != 0 {
Expand Down
42 changes: 21 additions & 21 deletions vochain/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import (
"sync/atomic"
"time"

abcitypes "github.com/cometbft/cometbft/abci/types"
tmnode "github.com/cometbft/cometbft/node"
tmcli "github.com/cometbft/cometbft/rpc/client/local"
ctypes "github.com/cometbft/cometbft/rpc/core/types"
tmtypes "github.com/cometbft/cometbft/types"
cometabcitypes "github.com/cometbft/cometbft/abci/types"
cometnode "github.com/cometbft/cometbft/node"
cometcli "github.com/cometbft/cometbft/rpc/client/local"
cometcoretypes "github.com/cometbft/cometbft/rpc/core/types"
comettypes "github.com/cometbft/cometbft/types"
ethcommon "github.com/ethereum/go-ethereum/common"
lru "github.com/hashicorp/golang-lru/v2"
"go.vocdoni.io/dvote/config"
Expand Down Expand Up @@ -43,16 +43,16 @@ const (
var (
// ErrTransactionNotFound is returned when the transaction is not found in the blockstore.
ErrTransactionNotFound = fmt.Errorf("transaction not found")
// Ensure that BaseApplication implements abcitypes.Application.
_ abcitypes.Application = (*BaseApplication)(nil)
// Ensure that BaseApplication implements cometabcitypes.Application.
_ cometabcitypes.Application = (*BaseApplication)(nil)
)

// BaseApplication reflects the ABCI application implementation.
type BaseApplication struct {
State *vstate.State
Istc *ist.Controller
Node *tmnode.Node
NodeClient *tmcli.Local
Node *cometnode.Node
NodeClient *cometcli.Local
NodeAddress ethcommon.Address
TransactionHandler *transaction.TransactionHandler
isSynchronizingFn func() bool
Expand All @@ -61,14 +61,14 @@ type BaseApplication struct {
isSynchronizing atomic.Bool

// Callback blockchain functions
fnGetBlockByHeight func(height int64) *tmtypes.Block
fnGetBlockByHash func(hash []byte) *tmtypes.Block
fnSendTx func(tx []byte) (*ctypes.ResultBroadcastTx, error)
fnGetBlockByHeight func(height int64) *comettypes.Block
fnGetBlockByHash func(hash []byte) *comettypes.Block
fnSendTx func(tx []byte) (*cometcoretypes.ResultBroadcastTx, error)
fnGetTx func(height uint32, txIndex int32) (*models.SignedTx, error)
fnGetTxHash func(height uint32, txIndex int32) (*models.SignedTx, []byte, error)
fnMempoolSize func() int
fnMempoolPrune func(txKey [32]byte) error
blockCache *lru.Cache[int64, *tmtypes.Block]
blockCache *lru.Cache[int64, *comettypes.Block]
// txLReferences is a map indexed by hashed transactions and storing the height where the transaction
// was seen frist time and the number of attempts failed for including it into a block.
txReferences sync.Map
Expand All @@ -79,11 +79,11 @@ type BaseApplication struct {
// endBlockTimestamp is the last block end timestamp calculated from local time.
endBlockTimestamp atomic.Int64
// startBlockTimestamp is the current block timestamp from tendermint's
// abcitypes.RequestBeginBlock.Header.Time
// cometabcitypes.RequestBeginBlock.Header.Time
startBlockTimestamp atomic.Int64
chainID string
dataDir string
genesisInfo *tmtypes.GenesisDoc
genesisInfo *comettypes.GenesisDoc

// lastDeliverTxResponse is used to store the last DeliverTxResponse, so validators
// can skip block re-execution on FinalizeBlock call.
Expand Down Expand Up @@ -144,7 +144,7 @@ func NewBaseApplication(vochainCfg *config.VochainCfg) (*BaseApplication, error)
return nil, fmt.Errorf("cannot load zk circuit: %w", err)
}

blockCache, err := lru.New[int64, *tmtypes.Block](32)
blockCache, err := lru.New[int64, *comettypes.Block](32)
if err != nil {
return nil, err
}
Expand All @@ -154,7 +154,7 @@ func NewBaseApplication(vochainCfg *config.VochainCfg) (*BaseApplication, error)
TransactionHandler: transactionHandler,
blockCache: blockCache,
dataDir: vochainCfg.DataDir,
genesisInfo: &tmtypes.GenesisDoc{},
genesisInfo: &comettypes.GenesisDoc{},
}, nil
}

Expand Down Expand Up @@ -282,7 +282,7 @@ func (app *BaseApplication) endBlock(t time.Time, h uint32) {
// GetBlockByHeight retrieves a full block indexed by its height.
// This method uses an LRU cache for the blocks so in general it is more
// convenient for high load operations than GetBlockByHash(), which does not use cache.
func (app *BaseApplication) GetBlockByHeight(height int64) *tmtypes.Block {
func (app *BaseApplication) GetBlockByHeight(height int64) *comettypes.Block {
if app.fnGetBlockByHeight == nil {
log.Errorw(fmt.Errorf("method not assigned"), "getBlockByHeight")
return nil
Expand All @@ -306,7 +306,7 @@ func (app *BaseApplication) GetBlockByHeight(height int64) *tmtypes.Block {
}

// GetBlockByHash retreies a full block indexed by its Hash
func (app *BaseApplication) GetBlockByHash(hash []byte) *tmtypes.Block {
func (app *BaseApplication) GetBlockByHash(hash []byte) *comettypes.Block {
if app.fnGetBlockByHash == nil {
log.Errorw(fmt.Errorf("method not assigned"), "getBlockByHash")
return nil
Expand All @@ -325,7 +325,7 @@ func (app *BaseApplication) GetTxHash(height uint32, txIndex int32) (*models.Sig
}

// SendTx sends a transaction to the mempool (sync)
func (app *BaseApplication) SendTx(tx []byte) (*ctypes.ResultBroadcastTx, error) {
func (app *BaseApplication) SendTx(tx []byte) (*cometcoretypes.ResultBroadcastTx, error) {
if app.fnSendTx == nil {
log.Errorw(fmt.Errorf("method not assigned"), "sendTx")
return nil, nil
Expand Down Expand Up @@ -355,7 +355,7 @@ func (app *BaseApplication) MempoolDeleteTx(txID [32]byte) {
}

// Genesis returns the tendermint genesis information
func (app *BaseApplication) Genesis() *tmtypes.GenesisDoc {
func (app *BaseApplication) Genesis() *comettypes.GenesisDoc {
return app.genesisInfo
}

Expand Down
24 changes: 12 additions & 12 deletions vochain/appsetup.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import (
"context"
"fmt"

tmcli "github.com/cometbft/cometbft/rpc/client/local"
ctypes "github.com/cometbft/cometbft/rpc/core/types"
tmtypes "github.com/cometbft/cometbft/types"
cometcli "github.com/cometbft/cometbft/rpc/client/local"
cometcoretypes "github.com/cometbft/cometbft/rpc/core/types"
comettypes "github.com/cometbft/cometbft/types"
"go.vocdoni.io/dvote/config"
"go.vocdoni.io/dvote/log"
"go.vocdoni.io/proto/build/go/models"
Expand All @@ -22,8 +22,8 @@ func (app *BaseApplication) SetNode(vochaincfg *config.VochainCfg, genesis []byt
if vochaincfg.IsSeedNode {
return nil
}
// Note that tmcli.New logs any error rather than returning it.
app.NodeClient = tmcli.New(app.Node)
// Note that cometcli.New logs any error rather than returning it.
app.NodeClient = cometcli.New(app.Node)
nodeGenesis, err := app.NodeClient.Genesis(context.TODO())
if err != nil {
return err
Expand All @@ -35,7 +35,7 @@ func (app *BaseApplication) SetNode(vochaincfg *config.VochainCfg, genesis []byt
// SetDefaultMethods assigns fnGetBlockByHash, fnGetBlockByHeight, fnSendTx to use the
// BlockStore from app.Node to load blocks. Assumes app.Node has been set.
func (app *BaseApplication) SetDefaultMethods() {
app.SetFnGetBlockByHash(func(hash []byte) *tmtypes.Block {
app.SetFnGetBlockByHash(func(hash []byte) *comettypes.Block {
resblock, err := app.NodeClient.BlockByHash(context.Background(), hash)
if err != nil {
log.Warnf("cannot fetch block by hash: %v", err)
Expand All @@ -44,7 +44,7 @@ func (app *BaseApplication) SetDefaultMethods() {
return resblock.Block
})

app.SetFnGetBlockByHeight(func(height int64) *tmtypes.Block {
app.SetFnGetBlockByHeight(func(height int64) *comettypes.Block {
resblock, err := app.NodeClient.Block(context.Background(), &height)
if err != nil {
log.Warnf("cannot fetch block by height: %v", err)
Expand All @@ -60,7 +60,7 @@ func (app *BaseApplication) SetDefaultMethods() {
return app.Node.Mempool().Size()
})
app.SetFnMempoolPrune(app.fnMempoolRemoveTxTendermint)
app.SetFnSendTx(func(tx []byte) (*ctypes.ResultBroadcastTx, error) {
app.SetFnSendTx(func(tx []byte) (*cometcoretypes.ResultBroadcastTx, error) {
result, err := app.NodeClient.BroadcastTxSync(context.Background(), tx)
log.Debugw("broadcast tx",
"size", len(tx),
Expand Down Expand Up @@ -108,17 +108,17 @@ func (app *BaseApplication) getTxHashTendermint(height uint32, txIndex int32) (*
}

// SetFnGetBlockByHash sets the getter for blocks by hash
func (app *BaseApplication) SetFnGetBlockByHash(fn func(hash []byte) *tmtypes.Block) {
func (app *BaseApplication) SetFnGetBlockByHash(fn func(hash []byte) *comettypes.Block) {
app.fnGetBlockByHash = fn
}

// SetFnGetBlockByHeight sets the getter for blocks by height
func (app *BaseApplication) SetFnGetBlockByHeight(fn func(height int64) *tmtypes.Block) {
func (app *BaseApplication) SetFnGetBlockByHeight(fn func(height int64) *comettypes.Block) {
app.fnGetBlockByHeight = fn
}

// SetFnSendTx sets the sendTx method
func (app *BaseApplication) SetFnSendTx(fn func(tx []byte) (*ctypes.ResultBroadcastTx, error)) {
func (app *BaseApplication) SetFnSendTx(fn func(tx []byte) (*cometcoretypes.ResultBroadcastTx, error)) {
app.fnSendTx = fn
}

Expand Down Expand Up @@ -149,7 +149,7 @@ func (app *BaseApplication) SetFnMempoolPrune(fn func([32]byte) error) {

// fnMempoolRemoveTxTendermint removes a transaction (identifier by its vochain.TxKey() hash)
// from the Tendermint mempool.
func (app *BaseApplication) fnMempoolRemoveTxTendermint(txKey [tmtypes.TxKeySize]byte) error {
func (app *BaseApplication) fnMempoolRemoveTxTendermint(txKey [comettypes.TxKeySize]byte) error {
if app.Node == nil {
log.Errorw(fmt.Errorf("method not assigned"), "mempoolRemoveTxTendermint")
return nil
Expand Down
Loading

0 comments on commit b9beb0f

Please sign in to comment.