github.com/mavryk-network/mvgo@v1.19.9/rpc/smart_rollup.go (about)

     1  // Copyright (c) 2023 Blockwatch Data Inc.
     2  // Author: alex@blockwatch.cc
     3  
     4  package rpc
     5  
     6  import (
     7  	"encoding/json"
     8  	"fmt"
     9  
    10  	"github.com/mavryk-network/mvgo/mavryk"
    11  	"github.com/mavryk-network/mvgo/micheline"
    12  )
    13  
    14  // Ensure SmartRollup types implement the TypedOperation interface.
    15  var (
    16  	_ TypedOperation = (*SmartRollupOriginate)(nil)
    17  	_ TypedOperation = (*SmartRollupAddMessages)(nil)
    18  	_ TypedOperation = (*SmartRollupCement)(nil)
    19  	_ TypedOperation = (*SmartRollupPublish)(nil)
    20  	_ TypedOperation = (*SmartRollupRefute)(nil)
    21  	_ TypedOperation = (*SmartRollupTimeout)(nil)
    22  	_ TypedOperation = (*SmartRollupExecuteOutboxMessage)(nil)
    23  	_ TypedOperation = (*SmartRollupRecoverBond)(nil)
    24  )
    25  
    26  type SmartRollupResult struct {
    27  	Address          *mavryk.Address               `json:"address,omitempty"`            // v016, smart_rollup_originate
    28  	Size             *mavryk.Z                     `json:"size,omitempty"`               // v016, smart_rollup_originate
    29  	InboxLevel       int64                         `json:"inbox_level,omitempty"`        // v016, smart_rollup_cement
    30  	StakedHash       *mavryk.SmartRollupCommitHash `json:"staked_hash,omitempty"`        // v016, smart_rollup_publish
    31  	PublishedAtLevel int64                         `json:"published_at_level,omitempty"` // v016, smart_rollup_publish
    32  	GameStatus       *GameStatus                   `json:"game_status,omitempty"`        // v016, smart_rollup_refute, smart_rollup_timeout
    33  	Commitment       *mavryk.SmartRollupCommitHash `json:"commitment_hash,omitempty"`    // v017, smart_rollup_cement
    34  }
    35  
    36  type SmartRollupOriginate struct {
    37  	Manager
    38  	PvmKind          mavryk.PvmKind  `json:"pvm_kind"`
    39  	Kernel           mavryk.HexBytes `json:"kernel"`
    40  	OriginationProof mavryk.HexBytes `json:"origination_proof"`
    41  	ParametersTy     micheline.Prim  `json:"parameters_ty"`
    42  }
    43  
    44  type SmartRollupAddMessages struct {
    45  	Manager
    46  	Messages []mavryk.HexBytes `json:"message"`
    47  }
    48  
    49  type SmartRollupCement struct {
    50  	Manager
    51  	Rollup     mavryk.Address                `json:"rollup"`
    52  	Commitment *mavryk.SmartRollupCommitHash `json:"commitment,omitempty"` // deprecated in v17
    53  }
    54  
    55  type SmartRollupCommitment struct {
    56  	CompressedState mavryk.SmartRollupStateHash  `json:"compressed_state"`
    57  	InboxLevel      int64                        `json:"inbox_level"`
    58  	Predecessor     mavryk.SmartRollupCommitHash `json:"predecessor"`
    59  	NumberOfTicks   mavryk.Z                     `json:"number_of_ticks"`
    60  }
    61  
    62  type SmartRollupPublish struct {
    63  	Manager
    64  	Rollup     mavryk.Address        `json:"rollup"`
    65  	Commitment SmartRollupCommitment `json:"commitment"`
    66  }
    67  
    68  type SmartRollupRefute struct {
    69  	Manager
    70  	Rollup     mavryk.Address        `json:"rollup"`
    71  	Opponent   mavryk.Address        `json:"opponent"`
    72  	Refutation SmartRollupRefutation `json:"refutation"`
    73  }
    74  
    75  type SmartRollupRefutation struct {
    76  	Kind         string                        `json:"refutation_kind"`
    77  	PlayerHash   *mavryk.SmartRollupCommitHash `json:"player_commitment_hash,omitempty"`
    78  	OpponentHash *mavryk.SmartRollupCommitHash `json:"opponent_commitment_hash,omitempty"`
    79  	Choice       *mavryk.Z                     `json:"choice,omitempty"`
    80  	Step         *SmartRollupRefuteStep        `json:"step,omitempty"`
    81  }
    82  
    83  // Step can either be
    84  //
    85  // - []SmartRollupTick
    86  // - SmartRollupInputProof
    87  // - smth else?
    88  //
    89  // There is no indication in the outer parts of the refutation struct that
    90  // suggests how to decode this.
    91  type SmartRollupRefuteStep struct {
    92  	Ticks []SmartRollupTick
    93  	Proof *SmartRollupProof
    94  }
    95  
    96  type SmartRollupProof struct {
    97  	PvmStep    mavryk.HexBytes        `json:"pvm_step,omitempty"`
    98  	InputProof *SmartRollupInputProof `json:"input_proof,omitempty"`
    99  }
   100  
   101  func (s *SmartRollupRefuteStep) UnmarshalJSON(buf []byte) error {
   102  	if len(buf) == 0 {
   103  		return nil
   104  	}
   105  	switch buf[0] {
   106  	case '[':
   107  		s.Ticks = make([]SmartRollupTick, 0)
   108  		return json.Unmarshal(buf, &s.Ticks)
   109  	case '{':
   110  		s.Proof = &SmartRollupProof{}
   111  		return json.Unmarshal(buf, s.Proof)
   112  	default:
   113  		return fmt.Errorf("Invalid refute step data %q", string(buf))
   114  	}
   115  }
   116  
   117  func (s SmartRollupRefuteStep) MarshalJSON() ([]byte, error) {
   118  	if s.Ticks != nil {
   119  		return json.Marshal(s.Ticks)
   120  	}
   121  	if s.Proof != nil {
   122  		return json.Marshal(s.Proof)
   123  	}
   124  	return nil, nil
   125  }
   126  
   127  type SmartRollupTick struct {
   128  	State mavryk.SmartRollupStateHash `json:"state"`
   129  	Tick  mavryk.Z                    `json:"tick"`
   130  }
   131  
   132  type SmartRollupInputProof struct {
   133  	Kind    string          `json:"input_proof_kind"`
   134  	Level   int64           `json:"level"`
   135  	Counter mavryk.Z        `json:"message_counter"`
   136  	Proof   mavryk.HexBytes `json:"serialized_proof"`
   137  }
   138  
   139  type SmartRollupTimeout struct {
   140  	Manager
   141  	Rollup  mavryk.Address `json:"rollup"`
   142  	Stakers struct {
   143  		Alice mavryk.Address `json:"alice"`
   144  		Bob   mavryk.Address `json:"bob"`
   145  	} `json:"stakers"`
   146  }
   147  
   148  type SmartRollupExecuteOutboxMessage struct {
   149  	Manager
   150  	Rollup             mavryk.Address               `json:"rollup"`
   151  	CementedCommitment mavryk.SmartRollupCommitHash `json:"cemented_commitment"`
   152  	OutputProof        mavryk.HexBytes              `json:"output_proof"`
   153  }
   154  
   155  type SmartRollupRecoverBond struct {
   156  	Manager
   157  	Rollup mavryk.Address `json:"rollup"`
   158  	Staker mavryk.Address `json:"staker"`
   159  }
   160  
   161  type GameStatus struct {
   162  	Status string          `json:"status,omitempty"`
   163  	Kind   string          `json:"kind,omitempty"`
   164  	Reason string          `json:"reason,omitempty"`
   165  	Player *mavryk.Address `json:"player,omitempty"`
   166  }
   167  
   168  func (s *GameStatus) UnmarshalJSON(buf []byte) error {
   169  	if len(buf) == 0 {
   170  		return nil
   171  	}
   172  	switch buf[0] {
   173  	case '"':
   174  		s.Status = string(buf[1 : len(buf)-1])
   175  	case '{':
   176  		type alias *GameStatus
   177  		type wrapper struct {
   178  			S alias `json:"result"`
   179  		}
   180  		a := wrapper{alias(s)}
   181  		_ = json.Unmarshal(buf, &a)
   182  	default:
   183  		return fmt.Errorf("Invalid game status data %q", string(buf))
   184  	}
   185  	return nil
   186  }