solana-improvement-documents/proposals/0047-last-hard-fork-syscall.md

94 lines
3.7 KiB
Markdown
Raw Normal View History

---
2023-04-15 13:27:07 -07:00
simd: '0047'
title: Syscall to get the last hardfork
authors:
- Godmode Galactus (Mango Markets)
category: Standard
type: Core
status: Draft
created: 2023-04-15
---
## Summary
2023-04-17 08:15:10 -07:00
Create a new syscall which can be used to get the last hard fork slot.
`fn sol_get_last_hardfork_slot() -> Slot`
## Motivation
2023-04-17 08:15:10 -07:00
In Solana, when the cluster cannot reach consensus, it is currently restarted
using `ledger-tool` with a hard fork on a slot. All participating nodes need to
restart their validator specifying the hard fork slot. Once all nodes
participating in the restart effort on the hard fork exceed 80% of the total
stakes, the restart is successful, and the cluster continues from the hard fork
slot. So hard fork is a tool to intentionally fork off the nodes not
participating in the restart effort.
2023-04-15 13:39:08 -07:00
2023-04-18 01:16:32 -07:00
Program developers may find it useful to know that a hard fork has recently
2023-04-15 13:39:08 -07:00
occurred. This information can help them prevent arbitrage and liquidation
caused by outdated oracle price account states. However, the cluster's restart
process takes several hours; in that time, the world can change, causing asset
2023-04-18 01:16:32 -07:00
prices to fluctuate. As the cluster updates the state of all accounts, malicious
actors could take advantage of the delay by conducting trades using outdated
prices, anticipating that they will soon catch up to the actual prices of
real-world assets. Knowing that hard fork has been done recently, programs can
manage these cases more appropriately.
## Alternatives Considered
2023-04-15 13:39:08 -07:00
No alternate considerations; we need to have the value of last hard fork slot
2023-04-18 01:16:32 -07:00
while executing the program to correctly treat this case. We cannot have an
account because then it should be updated just after the restart is successful,
which will add complexity. The best way is to create a new syscall to get this
2023-04-17 08:15:10 -07:00
information during execution of transaction, which will help us get last
hardfork slot without any interface changes for the instruction.
## Detailed Design
2023-04-17 08:15:10 -07:00
### Creation of a new syscall
2023-04-17 08:15:10 -07:00
The implementation of this syscall is pretty straitforward. In solana client all
the hardforks for a cluster are stored in the bank structure. The last hard fork
slot can be retrieved and then stored in invoke context, so that the executing
2023-04-18 01:16:32 -07:00
program can access it.
2023-04-17 08:15:10 -07:00
For other clients, we have to get the last hard fork slot information and make
2023-04-18 01:16:32 -07:00
it accessible to the runtime of the program. If there is no hard fork done yet
on the cluster we consider that the first hard for is at Slot `0`.
2023-04-17 08:15:10 -07:00
### Overview of changes for solana client
2023-04-17 08:15:10 -07:00
The hardfork data is available in the `Bank`. The structure `HardForks` contains
a vector with all the previous hard forks. The vector is also sorted when we
register a slot. So the last element of the vector is usually the last slot at
which hard fork was done. We can get the last hard fork slot from the bank and
pass it to invoke context structure. We can register new syscall in the file
`definitions.rs` where other syscalls are defined.
2023-04-17 08:15:10 -07:00
```rust
define_syscall!(fn sol_get_last_hardfork_slot() -> Slot);
```
2023-04-17 08:15:10 -07:00
We can then return the data of last hardfork slot passed to the invoke context
in this functions implementation.
## Impact
2023-04-18 01:16:32 -07:00
Programs will start using this new syscall to correctly address the security
2023-04-15 13:39:08 -07:00
concerns during network restart. This will increase the reliability of solana
2023-04-18 01:16:32 -07:00
cluster as whole and make programs developers more confident to handle edge
cases during such extreme events.
2023-04-18 01:16:32 -07:00
As the method is syscall the developers do not need to pass any new
2023-04-17 08:15:10 -07:00
accounts or sysvars to the instruction to use this feature.
## Security Consideration
None
## Backwards Compatibility
2023-04-18 01:16:32 -07:00
The programs using the new syscall could not be used on solana version which
does not implement this feature. Existing programs which do not use this feature
are not impacted at all.