Skip to content

Commit

Permalink
Fix CheckTx and time
Browse files Browse the repository at this point in the history
  • Loading branch information
p-offtermatt committed Nov 21, 2023
1 parent 0de939c commit 72c25a2
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 25 deletions.
22 changes: 6 additions & 16 deletions cometmock/abci_client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
cometlog "github.com/cometbft/cometbft/libs/log"
cmtmath "github.com/cometbft/cometbft/libs/math"
cmtproto "github.com/cometbft/cometbft/proto/tendermint/types"
ctypes "github.com/cometbft/cometbft/rpc/core/types"
"github.com/cometbft/cometbft/state"
blockindexkv "github.com/cometbft/cometbft/state/indexer/block/kv"
"github.com/cometbft/cometbft/state/txindex"
Expand Down Expand Up @@ -86,22 +85,19 @@ type AbciClient struct {
// When transaction FreshTxQueue[i] is included, it will be removed from the FreshTxQueue,
// and the result will be sent to ResponseChannelQueue[i].
//
FreshTxQueue []types.Tx
StaleTxQueue []types.Tx
ResponseChannelQueue []chan *ctypes.ResultBroadcastTxCommit
FreshTxQueue []types.Tx
StaleTxQueue []types.Tx
}

func (a *AbciClient) QueueTx(tx types.Tx, responseChannel chan *ctypes.ResultBroadcastTxCommit) {
func (a *AbciClient) QueueTx(tx types.Tx) {
// lock the block mutex so txs are not queued while a block is being run
blockMutex.Lock()
a.FreshTxQueue = append(a.FreshTxQueue, tx)
a.ResponseChannelQueue = append(a.ResponseChannelQueue, responseChannel)
blockMutex.Unlock()
}

func (a *AbciClient) ClearTxs() {
a.FreshTxQueue = make([]types.Tx, 0)
a.ResponseChannelQueue = make([]chan *ctypes.ResultBroadcastTxCommit, 0)
a.StaleTxQueue = make([]types.Tx, 0)
}

Expand Down Expand Up @@ -259,7 +255,6 @@ func NewAbciClient(
ErrorOnUnequalResponses: errorOnUnequalResponses,
signingStatus: signingStatus,
FreshTxQueue: make([]types.Tx, 0),
ResponseChannelQueue: make([]chan *ctypes.ResultBroadcastTxCommit, 0),
}
}

Expand Down Expand Up @@ -934,14 +929,6 @@ func (a *AbciClient) runBlock_helper(
for index, tx := range a.FreshTxQueue {
txBytes := []byte(tx)
resCheckTx, err := a.SendCheckTx(abcitypes.CheckTxType_New, &txBytes)
response := ctypes.ResultBroadcastTxCommit{
CheckTx: *resCheckTx,
// empty result, because we currently do not support BroadcastTxCommit, so it will not be used
TxResult: abcitypes.ExecTxResult{},
Hash: tx.Hash(),
Height: newHeight,
}
a.ResponseChannelQueue[index] <- &response
if err != nil {
return fmt.Errorf("error from CheckTx: %v", err)
}
Expand Down Expand Up @@ -1024,6 +1011,9 @@ func (a *AbciClient) runBlock_helper(
evidences,
)

// set the block time to the time passed as argument
block.Time = blockTime

// clear the tx queues
a.ClearTxs()

Expand Down
6 changes: 3 additions & 3 deletions cometmock/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func GetMockPVsFromNodeHomes(nodeHomes []string) []types.PrivValidator {
func main() {
logger := cometlog.NewTMLogger(cometlog.NewSyncWriter(os.Stdout))

argumentString := "[--block-time=value] [--auto-tx=<value>] <app-addresses> <genesis-file> <cometmock-listen-address> <node-homes> <abci-connection-mode>"
argumentString := "[--block-time=value] [--auto-tx=<value>] [--block-production-interval=<value>] [--starting-timestamp=<value>] [--starting-timestamp-from-genesis=<value>] <app-addresses> <genesis-file> <cometmock-listen-address> <node-homes> <abci-connection-mode>"

app := &cli.App{
Name: "cometmock",
Expand Down Expand Up @@ -95,7 +95,7 @@ advancing blocks or broadcasting transactions.`,
The timestamp to use for the first block, given in milliseconds since the unix epoch.
If this is < 0, the current system time is used.
If this is >= 0, the system time is ignored and this timestamp is used for the first block instead.`,
Value: 1000,
Value: -1,
},
&cli.BoolFlag{
Name: "starting-timestamp-from-genesis",
Expand Down Expand Up @@ -166,7 +166,7 @@ or the system time between creating the genesis request and producing the first

// read block time from args
blockTime := time.Duration(c.Int64("block-time")) * time.Millisecond
fmt.Printf("Block time: %d\n", blockTime)
fmt.Printf("Block time: %d\n", blockTime.Milliseconds())

clientMap := make(map[string]abci_client.AbciCounterpartyClient)

Expand Down
18 changes: 12 additions & 6 deletions cometmock/rpc_server/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
cmtquery "github.com/cometbft/cometbft/libs/pubsub/query"
"github.com/cometbft/cometbft/p2p"

abcitypes "github.com/cometbft/cometbft/abci/types"
ctypes "github.com/cometbft/cometbft/rpc/core/types"
rpc "github.com/cometbft/cometbft/rpc/jsonrpc/server"
rpctypes "github.com/cometbft/cometbft/rpc/jsonrpc/types"
Expand Down Expand Up @@ -484,17 +485,22 @@ func BroadcastTx(tx *types.Tx) (*ctypes.ResultBroadcastTxCommit, error) {
abci_client.GlobalClient.Logger.Info(
"BroadcastTxs called", "tx", tx)

responseChan := make(chan *ctypes.ResultBroadcastTxCommit)
abci_client.GlobalClient.QueueTx(*tx, responseChan)
txBytes := []byte(*tx)
checkTxResponse, err := abci_client.GlobalClient.SendCheckTx(abcitypes.CheckTxType_New, &txBytes)
if err != nil {
return nil, err
}
abci_client.GlobalClient.QueueTx(*tx)

if abci_client.GlobalClient.AutoIncludeTx {
go abci_client.GlobalClient.RunBlock()
}

response := <-responseChan

// TODO: fill the return value if necessary
return response, nil
return &ctypes.ResultBroadcastTxCommit{
CheckTx: *checkTxResponse,
Hash: tx.Hash(),
Height: abci_client.GlobalClient.CurState.LastBlockHeight,
}, err
}

func ABCIInfo(ctx *rpctypes.Context) (*ctypes.ResultABCIInfo, error) {
Expand Down

0 comments on commit 72c25a2

Please sign in to comment.