Skip to content

Commit

Permalink
Merge pull request #116 from Fairblock/feat/keep-encrypted-tx
Browse files Browse the repository at this point in the history
Update to keep encrypted tx & update its processed height
  • Loading branch information
p0p3yee authored Mar 4, 2024
2 parents 85a3c94 + 1fc5fd4 commit 69757d8
Show file tree
Hide file tree
Showing 6 changed files with 165 additions and 35 deletions.
2 changes: 2 additions & 0 deletions proto/fairyring/pep/encrypted_tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ message EncryptedTx {
string data = 3;
string creator = 4;
cosmos.base.v1beta1.Coin chargedGas = 5;
uint64 processedAtChainHeight = 6;
bool expired = 7;
}

message EncryptedTxArray {
Expand Down
19 changes: 19 additions & 0 deletions scripts/tests/pep.sh
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,25 @@ if [ "$RESULT" != "$AGG_KEY_HEIGHT" ]; then
exit 1
fi

FIRST_ENCRYPTED_TX_HEIGHT=$($BINARY query pep list-encrypted-tx --node $CHAIN2_NODE -o json | jq '.encryptedTxArray[0].encryptedTx[0].processedAtChainHeight')
SECOND_ENCRYPTED_TX_HEIGHT=$($BINARY query pep list-encrypted-tx --node $CHAIN2_NODE -o json | jq '.encryptedTxArray[0].encryptedTx[1].processedAtChainHeight')

echo "First Encrypted tx processed at height: $FIRST_ENCRYPTED_TX_HEIGHT, 2nd one processed at: $SECOND_ENCRYPTED_TX_HEIGHT"

FIRST_EVENT=$(curl -s http://localhost:26657/block_results?height=$FIRST_ENCRYPTED_TX_HEIGHT | jq '.result.begin_block_events[] | select(.type == "reverted-encrypted-tx") | .attributes[] | select(.key == "reason") | .value')
if [[ "$FIRST_EVENT" != *"insufficient fees"* ]]; then
echo "ERROR: Pep module expected first encrypted tx failed with reason insufficient fee, got: $FIRST_EVENT instead"
exit 1
fi
echo "First Encrypted TX Failed with Reason: $FIRST_EVENT as expected."

SECOND_EVENT=$(curl -s http://localhost:26657/block_results?height=$SECOND_ENCRYPTED_TX_HEIGHT | jq '.result.begin_block_events[] | select(.type == "executed-encrypted-tx") | .attributes[] | select(.key == "events") | .value')
if [[ "$SECOND_EVENT" != *"coin_received"* ]]; then
echo "ERROR: Pep module expected second encrypted tx succeeded with events, got: $SECOND_EVENT instead"
exit 1
fi
echo "Second Encrypted TX succeeded with Events: $(echo $SECOND_EVENT | jq) as expected."

echo ""
echo "###########################################################"
echo "# SUCCESSFULLY TESTED #"
Expand Down
30 changes: 30 additions & 0 deletions x/pep/keeper/encrypted_tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,36 @@ func (k Keeper) SetEncryptedTx(
), parsedEncryptedTxArr)
}

func (k Keeper) SetEncryptedTxProcessedHeight(
ctx sdk.Context,
height uint64,
index uint64,
processedHeight uint64,
) {
arr := k.GetEncryptedTxAllFromHeight(ctx, height)

if index >= uint64(len(arr.EncryptedTx)) {
return
}

arr.EncryptedTx[index].ProcessedAtChainHeight = processedHeight

k.SetEncryptedTx(ctx, height, arr)
}

func (k Keeper) SetAllEncryptedTxExpired(
ctx sdk.Context,
height uint64,
) {
arr := k.GetEncryptedTxAllFromHeight(ctx, height)

for i := range arr.EncryptedTx {
arr.EncryptedTx[i].Expired = true
}

k.SetEncryptedTx(ctx, height, arr)
}

// GetEncryptedTx returns a encryptedTx from its index
func (k Keeper) GetEncryptedTx(
ctx sdk.Context,
Expand Down
10 changes: 6 additions & 4 deletions x/pep/keeper/msg_server_submit_encrypted_tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,12 @@ func (k msgServer) SubmitEncryptedTx(goCtx context.Context, msg *types.MsgSubmit
}

encryptedTx := types.EncryptedTx{
TargetHeight: msg.TargetBlockHeight,
Data: msg.Data,
Creator: msg.Creator,
ChargedGas: &minGas,
TargetHeight: msg.TargetBlockHeight,
Data: msg.Data,
Creator: msg.Creator,
ChargedGas: &minGas,
ProcessedAtChainHeight: 0,
Expired: false,
}

txIndex := k.AppendEncryptedTx(ctx, encryptedTx)
Expand Down
7 changes: 3 additions & 4 deletions x/pep/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,8 +286,8 @@ func (am AppModule) BeginBlock(ctx sdk.Context, _ abci.RequestBeginBlock) {
am.keeper.Logger(ctx).Error(fmt.Sprintf("Decryption key not found for block height: %d, Removing all the encrypted txs...", h))
encryptedTxs := am.keeper.GetEncryptedTxAllFromHeight(ctx, h)
if len(encryptedTxs.EncryptedTx) > 0 {
am.keeper.RemoveAllEncryptedTxFromHeight(ctx, h)
am.keeper.Logger(ctx).Info(fmt.Sprintf("Removed total %d encrypted txs at block %d", len(encryptedTxs.EncryptedTx), h))
am.keeper.SetAllEncryptedTxExpired(ctx, h)
am.keeper.Logger(ctx).Info(fmt.Sprintf("Updated total %d encrypted txs at block %d to expired", len(encryptedTxs.EncryptedTx), h))
indexes := make([]string, len(encryptedTxs.EncryptedTx))
for _, v := range encryptedTxs.EncryptedTx {
indexes = append(indexes, strconv.FormatUint(v.Index, 10))
Expand Down Expand Up @@ -344,6 +344,7 @@ func (am AppModule) BeginBlock(ctx sdk.Context, _ abci.RequestBeginBlock) {

for _, eachTx := range arr.EncryptedTx {
startConsumedGas := ctx.GasMeter().GasConsumed()
am.keeper.SetEncryptedTxProcessedHeight(ctx, eachTx.TargetHeight, eachTx.Index, uint64(ctx.BlockHeight()))
if currentNonce, found := am.keeper.GetPepNonce(ctx, eachTx.Creator); found && currentNonce.Nonce == math.MaxUint64 {
am.processFailedEncryptedTx(ctx, eachTx, "invalid pep nonce", startConsumedGas)
continue
Expand Down Expand Up @@ -585,8 +586,6 @@ func (am AppModule) BeginBlock(ctx sdk.Context, _ abci.RequestBeginBlock) {

telemetry.IncrCounter(1, types.KeyTotalSuccessEncryptedTx)
}

am.keeper.RemoveAllEncryptedTxFromHeight(ctx, h)
}

}
Expand Down
132 changes: 105 additions & 27 deletions x/pep/types/encrypted_tx.pb.go

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

0 comments on commit 69757d8

Please sign in to comment.