syntax = "proto3";

package aea.valory.abci.v0_1_0;

message AbciMessage{

  // Custom Types
  message CheckTxType{
    enum _CheckTxType {
      NEW            = 0;
      RECHECK        = 1;
    }
    _CheckTxType type = 1;
  }

  message ConsensusParams{
    message Duration {
      int64 seconds = 1;
      int32 nanos = 2;
    }
    message BlockParams {
      int64 max_bytes = 1;
      int64 max_gas = 2;
    }
    message EvidenceParams {
      int64 max_age_num_blocks = 1;
      Duration max_age_duration = 2;
      int64 max_bytes = 3;
    }
    message ValidatorParams {
      repeated string pub_key_types = 1;
    }
    message VersionParams {
      uint64 app_version = 1;
    }
    BlockParams block = 1;
    EvidenceParams evidence = 2;
    ValidatorParams validator = 3;
    VersionParams version = 4;
  }

  message Events{
    message EventAttribute {
      bytes key   = 1;
      bytes value = 2;
      bool  index = 3;  // nondeterministic
    }
    message Event {
      string                  type       = 1;
      repeated EventAttribute attributes = 2;
    }
    repeated Event events = 1;
  }

  message Evidences{
    message Evidence {
      enum EvidenceType {
        UNKNOWN             = 0;
        DUPLICATE_VOTE      = 1;
        LIGHT_CLIENT_ATTACK = 2;
      }
      EvidenceType type = 1;
      // The offending validator
      LastCommitInfo.Validator validator = 2;
      // The height when the offense occurred
      int64 height = 3;
      // The corresponding time where the offense occurred
      Timestamp time = 4;
      // Total voting power of the validator set in case the ABCI application does
      // not store historical validators.
      // https://github.com/tendermint/tendermint/issues/4581
      int64 total_voting_power = 5;
    }
    repeated Evidence byzantine_validators = 1;
  }

  message Header{
    message ConsensusVersion {
      uint64 block = 1;
      uint64 app   = 2;
    }
    message BlockID {
      bytes         hash            = 1;
      PartSetHeader part_set_header = 2;
    }
    message PartSetHeader {
      uint32 total = 1;
      bytes  hash  = 2;
    }
    ConsensusVersion             version  = 1;
    string                       chain_id = 2;
    int64                        height   = 3;
    Timestamp                    time     = 4;
        
    // prev block info
    BlockID last_block_id = 5;
        
    // hashes of block data
    bytes last_commit_hash = 6;  // commit from validators from the last block
    bytes data_hash        = 7;  // transactions
        
    // hashes from the app output from the prev block
    bytes validators_hash      = 8;   // validators for the current block
    bytes next_validators_hash = 9;   // validators for the next block
    bytes consensus_hash       = 10;  // consensus params for current block
    bytes app_hash             = 11;  // state after txs from the previous block
    bytes last_results_hash    = 12;  // root hash of all results from the txs from the previous block
        
    // consensus info
    bytes evidence_hash    = 13;  // evidence included in the block
    bytes proposer_address = 14;  // original proposer of the block
  }

  message LastCommitInfo{
    message Validator {
      bytes address = 1;  // The first 20 bytes of SHA256(public key)
      // PubKey pub_key = 2 [(gogoproto.nullable)=false];
      int64 power = 3;  // The voting power
    }
    message VoteInfo {
      Validator validator         = 1;
      bool      signed_last_block = 2;
    }
    int32             round = 1;
    repeated VoteInfo votes = 2;
  }

  message ProofOps{
    message ProofOp {
      string type = 1;
      bytes  key  = 2;
      bytes  data = 3;
    }
    repeated ProofOp ops = 1;
  }

  message Result{
    enum ResultType {
      UNKNOWN       = 0;  // Unknown result, abort all snapshot restoration
      ACCEPT        = 1;  // Snapshot accepted, apply chunks
      ABORT         = 2;  // Abort all snapshot restoration
      REJECT        = 3;  // Reject this specific snapshot, try others
      REJECT_FORMAT = 4;  // Reject all snapshots of this format, try others
      REJECT_SENDER = 5;  // Reject all snapshots from the sender(s), try others
    }
    ResultType result_type = 1;
  }

  message SnapShots{
    repeated Snapshot snapshots = 1;
  }

  message Snapshot{
    // State Sync Types
    uint64 height   = 1;  // The height at which the snapshot was taken
    uint32 format   = 2;  // The application-specific snapshot format
    uint32 chunks   = 3;  // Number of chunks in the snapshot
    bytes  hash     = 4;  // Arbitrary snapshot hash, equal only if identical
    bytes  metadata = 5;  // Arbitrary application metadata
  }

  message Timestamp{
    // Represents seconds of UTC time since Unix epoch
    // 1970-01-01T00:00:00Z. Must be from 0001-01-01T00:00:00Z to
    // 9999-12-31T23:59:59Z inclusive.
    int64 seconds = 1;
    // Non-negative fractions of a second at nanosecond resolution. Negative
    // second values with fractions must still have non-negative nanos values
    // that count forward in time. Must be from 0 to 999,999,999
    // inclusive.
    int32 nanos = 2;
  }

  message ValidatorUpdates{
    message PublicKey {
      oneof sum {
        bytes ed25519   = 1;
        bytes secp256k1 = 2;
      }
    }
    message ValidatorUpdate {
      PublicKey pub_key = 1;
      int64 power = 2;
    }
    repeated ValidatorUpdate validators = 1;
  }


  // Performatives and contents
  message Request_Echo_Performative{
    string message = 1;
  }

  message Request_Flush_Performative{
  }

  message Request_Info_Performative{
    string version = 1;
    int32 block_version = 2;
    int32 p2p_version = 3;
  }

  message Request_Set_Option_Performative{
    string option_key = 1;
    string option_value = 2;
  }

  message Request_Init_Chain_Performative{
    Timestamp time = 1;
    string chain_id = 2;
    ConsensusParams consensus_params = 3;
    bool consensus_params_is_set = 4;
    ValidatorUpdates validators = 5;
    bytes app_state_bytes = 6;
    int32 initial_height = 7;
  }

  message Request_Query_Performative{
    bytes query_data = 1;
    string path = 2;
    int32 height = 3;
    bool prove = 4;
  }

  message Request_Begin_Block_Performative{
    bytes hash = 1;
    Header header = 2;
    LastCommitInfo last_commit_info = 3;
    Evidences byzantine_validators = 4;
  }

  message Request_Check_Tx_Performative{
    bytes tx = 1;
    CheckTxType type = 2;
  }

  message Request_Deliver_Tx_Performative{
    bytes tx = 1;
  }

  message Request_End_Block_Performative{
    int32 height = 1;
  }

  message Request_Commit_Performative{
  }

  message Request_List_Snapshots_Performative{
  }

  message Request_Offer_Snapshot_Performative{
    Snapshot snapshot = 1;
    bytes app_hash = 2;
  }

  message Request_Load_Snapshot_Chunk_Performative{
    int32 height = 1;
    int32 format = 2;
    int32 chunk_index = 3;
  }

  message Request_Apply_Snapshot_Chunk_Performative{
    int32 index = 1;
    bytes chunk = 2;
    string chunk_sender = 3;
  }

  message Response_Exception_Performative{
    string error = 1;
  }

  message Response_Echo_Performative{
    string message = 1;
  }

  message Response_Flush_Performative{
  }

  message Response_Info_Performative{
    string info_data = 1;
    string version = 2;
    int32 app_version = 3;
    int32 last_block_height = 4;
    bytes last_block_app_hash = 5;
  }

  message Response_Set_Option_Performative{
    int32 code = 1;
    string log = 2;
    string info = 3;
  }

  message Response_Init_Chain_Performative{
    ConsensusParams consensus_params = 1;
    bool consensus_params_is_set = 2;
    ValidatorUpdates validators = 3;
    bytes app_hash = 4;
  }

  message Response_Query_Performative{
    int32 code = 1;
    string log = 2;
    string info = 3;
    int32 index = 4;
    bytes key = 5;
    bytes value = 6;
    ProofOps proof_ops = 7;
    int32 height = 8;
    string codespace = 9;
  }

  message Response_Begin_Block_Performative{
    Events events = 1;
  }

  message Response_Check_Tx_Performative{
    int32 code = 1;
    bytes data = 2;
    string log = 3;
    string info = 4;
    int32 gas_wanted = 5;
    int32 gas_used = 6;
    Events events = 7;
    string codespace = 8;
  }

  message Response_Deliver_Tx_Performative{
    int32 code = 1;
    bytes data = 2;
    string log = 3;
    string info = 4;
    int32 gas_wanted = 5;
    int32 gas_used = 6;
    Events events = 7;
    string codespace = 8;
  }

  message Response_End_Block_Performative{
    ValidatorUpdates validator_updates = 1;
    ConsensusParams consensus_param_updates = 2;
    bool consensus_param_updates_is_set = 3;
    Events events = 4;
  }

  message Response_Commit_Performative{
    bytes data = 1;
    int32 retain_height = 2;
  }

  message Response_List_Snapshots_Performative{
    SnapShots snapshots = 1;
  }

  message Response_Offer_Snapshot_Performative{
    Result result = 1;
  }

  message Response_Load_Snapshot_Chunk_Performative{
    bytes chunk = 1;
  }

  message Response_Apply_Snapshot_Chunk_Performative{
    Result result = 1;
    repeated int32 refetch_chunks = 2;
    repeated string reject_senders = 3;
  }

  message Dummy_Performative{
    ConsensusParams dummy_consensus_params = 1;
  }


  oneof performative{
    Dummy_Performative dummy = 5;
    Request_Apply_Snapshot_Chunk_Performative request_apply_snapshot_chunk = 6;
    Request_Begin_Block_Performative request_begin_block = 7;
    Request_Check_Tx_Performative request_check_tx = 8;
    Request_Commit_Performative request_commit = 9;
    Request_Deliver_Tx_Performative request_deliver_tx = 10;
    Request_Echo_Performative request_echo = 11;
    Request_End_Block_Performative request_end_block = 12;
    Request_Flush_Performative request_flush = 13;
    Request_Info_Performative request_info = 14;
    Request_Init_Chain_Performative request_init_chain = 15;
    Request_List_Snapshots_Performative request_list_snapshots = 16;
    Request_Load_Snapshot_Chunk_Performative request_load_snapshot_chunk = 17;
    Request_Offer_Snapshot_Performative request_offer_snapshot = 18;
    Request_Query_Performative request_query = 19;
    Request_Set_Option_Performative request_set_option = 20;
    Response_Apply_Snapshot_Chunk_Performative response_apply_snapshot_chunk = 21;
    Response_Begin_Block_Performative response_begin_block = 22;
    Response_Check_Tx_Performative response_check_tx = 23;
    Response_Commit_Performative response_commit = 24;
    Response_Deliver_Tx_Performative response_deliver_tx = 25;
    Response_Echo_Performative response_echo = 26;
    Response_End_Block_Performative response_end_block = 27;
    Response_Exception_Performative response_exception = 28;
    Response_Flush_Performative response_flush = 29;
    Response_Info_Performative response_info = 30;
    Response_Init_Chain_Performative response_init_chain = 31;
    Response_List_Snapshots_Performative response_list_snapshots = 32;
    Response_Load_Snapshot_Chunk_Performative response_load_snapshot_chunk = 33;
    Response_Offer_Snapshot_Performative response_offer_snapshot = 34;
    Response_Query_Performative response_query = 35;
    Response_Set_Option_Performative response_set_option = 36;
  }
}
