Skip to content

Commit

Permalink
Merge pull request #74 from make-software/feat-2-0/migration-guide
Browse files Browse the repository at this point in the history
Merge migration guide an changelog
  • Loading branch information
davidatwhiletrue authored Jul 12, 2024
2 parents 0d9f7ec + 8ce1bba commit 7431909
Show file tree
Hide file tree
Showing 2 changed files with 291 additions and 0 deletions.
49 changes: 49 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,54 @@ All notable changes to this project will be documented in this file. The format
[comment]: <> (Fixed: any bug fixes)
[comment]: <> (Security: in case of vulnerabilities)

## [3.0.0-beta1]

This version is compatible with Casper node v2.0.0-rc3 and Casper node v1.5.6.

### Added

* New `GetEntity()` method added to the RPC client.
* New `QueryBalanceDetails()` method added to the RPC client.
* New `PutTransaction()` and `GetTransaction()` methods added to the RPC client.
* New `TransactionV1` class to model the new type of transactions in Condor.
* New global state keys added to the SDK: `AddressableEntityKey`, `BalanceHoldKey`, `BidAddrKey`, `BlockGlobalAddrKey`, `ByteCodeKey`, `EntryPointKey`, `MessageKey`, `NamedKeyKey` and `PackageKey`.
* New `AddressableEntity` class added. It may represent an account, a stored smart contract, or a system smart contract.
* New properties in the `StoredValue` class that can be retrieved with `QueryGlobalState()` method: `BidKind`, `Unbonding`, `AddressableEntity`, `Package`, `ByteCode`, `MessageTopicSummary`, `Message`, `NamedKey`, `Reservation`, and `EntryPoint`.
* New classes to represent data from the global state: `BidKind`, `UnbondingPurse`, `Package` and `ByteCode`.
* New `Message` class to contain data for native events included in the `TransactionAccepted` event from the SSE channel.
* Added `TransactionAccepted`, `TransactionProcessed`, and `TransactionExpired` events to the list of events emitted by a node through the SSE interface.

### Changed

* The `Block` class has changed to abstract the developer from the versioned block records returned from the network in Condor version. Refer to the migration guide for more information.
* For blocks produced in Casper 2.0, the `Block` instance contains the previous switch block hash in the `LastSwitchBlockHash` property.
* Ther `EraEnd` class has changed to abstract the developer from the versioned records returned from the network. More info in the migration guide.
* The `Transfer` class contained in the response for a `GetBlockTransfers()` call or in a `ExecutionResult` object has changed to abstract from the versioned transfer records returned from the network. Refer to the migration guide for more information.
* The input argument for the `QueryBalance()` method in the RPC client is any object from a class implementing the `IPurseIdentifier` interface. `PublicKey`, `AccountHashKey`, `URef`, and `AddressableEntity` classes implement this interface.
* The type to refer to block heights is now `ulong` across all the SDK. In previous version there was a mix of `ulong` and `int`.
* When using the `GetNodeStatus()` method with a Casper 2.0 node, the response contains the hash for the latest switch block in the `LatestSwitchBlockHash` property.
* `GetDeploy()` response has changed and now contains a `ExecutionInfo`object when the deploy has been processed instead a list of `ExecutionResult` objects. The execution info itself contains block information and a result object.
* Starting with this version of the SDK, only public keys are checksummed with the CEP-57 standard. The rest of the keys and hashes are not checksummed anymore.
* In the `StoredValue` class, `Transfer` has been renamed to `LegacyTransfer`.
* `DeployApproval` class has been renamed to `Approval` and is used for `Deploy` as well as for the new `TransactionV1` model.
* `ActionThresholds` class has now new `UpgradeManagement` property.
* The `EntryPoint` class has new entry point types and a new property `EntryPointPayment`. Both apply when working with Casper 2.0 only.
* `Step` event from SSE contains a list of `Transform` objects instead of a `ExecutionEffect` instance.
* `FinalitySignature` event contains `BlockHeight` and `ChainNameHash` value when connected to a Casper 2.0 node.
* `DeployProcessed` event from SSE contains a `ExecutionResultV1` object instead of a `ExecutionResult` object.

### Deprecated

* `Proposer.isSystem` property is marked as Obsolete. Use `IsSystem` with capital `I` instead.

### Removed

* `GetAccountBalance()` method in the RPC client has been removed. Use `QueryBalance()` instead. `GetBalance()` method exists to use `state_get_balance` from the RPC interface if needed.

### Security

* BouncyCastle package updated to 2.4.0 version.

## [2.3.0]

### Added
Expand Down Expand Up @@ -83,6 +131,7 @@ This new type permits to parse correctly the value `"00"` used for system blocks
### Added
* Initial release of Casper .NET SDK.

[3.0.0-beta1]: https://github.com/make-software/casper-net-sdk/releases/tag/v3.0.0-beta1
[2.3.0]: https://github.com/make-software/casper-net-sdk/releases/tag/v2.3.0
[2.2.0]: https://github.com/make-software/casper-net-sdk/releases/tag/v2.2.0
[2.1.0]: https://github.com/make-software/casper-net-sdk/releases/tag/v2.1.0
Expand Down
242 changes: 242 additions & 0 deletions Docs/Articles/CondorMigrationGuide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,242 @@
# Migration from Casper .NET SDK v2.x to v3.0

To migrate your application from Casper .NET SDK v2.x to Casper .NET SDK v3.0, you’ll need to make several changes to your code. The good news is that version 3 of this SDK keeps compatibility with Casper v1.5.6 nodes; therefore, once you update your application, it will work before and after the Condor upgrade.

This guide outlines the changes necessary for your application. However, it's worth noting that it doesn't replace other Condor-related documents introducing the new concepts in Casper 2.0 or migration guides. We strongly advise the reader to understand these new concepts first, as they will significantly aid in grasping the changes.

## Blocks

With Condor, produced blocks are stored in a new format that extends the information contained compared to old blocks. Blocks produced before the upgrade keep their original format. Thus, the SDK implements `BlockV1` and `BlocV2` classes to handle old and new block formats, respectively.

To facilitate handling different versions, the SDK also implements the type `Block`, which can represent either a V1 or V2 type in the network. This is the type obtained by default in the RPC queries and the SSE channel and contains all the data you may want to query:

```csharp
public class Block
{
public int Version { get; init; }
public string Hash { get; init; }
public string AccumulatedSeed { get; init; }
public ulong EraId { get; init; }
public ulong Height { get; init; }
public string ParentHash { get; init; }
public string ProtocolVersion { get; init; }
public bool RandomBit { get; init; }
public string StateRootHash { get; init; }
public string Timestamp { get; init; }
public EraEnd EraEnd { get; init; }
public UInt16 CurrentGasPrice { get; init; }
public Proposer Proposer { get; init; }
public string LastSwitchBlockHash { get; init; }
public List<BlockTransaction> Transactions { get; init; }
public List<List<UInt16>> RewardedSignatures { get; init; }
}
```

Note that `Block` does not have a header or body parts.

Also, some properties may have a `null` value if they’re not part of the versioned block. For example, `LastSwitchBlockHas` is present only for V2 blocks and `null` for V1 blocks.

### Recovering the versioned block object

If, for any reason, you need to work with the original format of the block, you can cast a Block into BlockV1 or BlockV2. For example:

```csharp
if (block.Version == 2) {
var blockv2 = (BlockV2)block;
// ...
} else if (block.Version == 1) {
var blockv1 = (BlockV1)block;
// ...
}
```

### EraEnd

EraEnd structure also differs for switch blocks produced before and after Condor. In most cases, you can just use the EraEnd object from the common Block class. But again, if necessary, you can recover an EraEndV1 object from a Version 1 Block instance:

```csharp
if (block.Version == 1) {
var blockv1 = (BlockV1)block;
var eraEnd = blockv1.Header.EraEnd;
// ...
}
```

### Transactions included in a block

Blocks produced on Casper v1.x contain a list of deploys that differentiate between native transfers and all other types of deploys. On Condor, transactions (this includes legacy deploys too) have a category field that determines the processing lane to which the transaction is sent. The chainspec of the network determines the properties of each lane:

1. Maximum transaction size in bytes for a given transaction in a certain lane.
2. Maximum args length size in bytes for a given transaction in a certain lane.
3. Transaction gas limit size in motes for a given transaction in a certain lane.
4. The maximum number of transactions the lane can contain for one block.

The categories (i.e., lanes) defined so far are:

1. Mint (native CSPR transfers) transactions
2. Auction (native interaction with the Auction contract) transactions
3. Install/Upgrade transactions
4. Large transactions
5. Medium transactions
6. Small transactions

A `Block` contains a list of transactions:

```csharp
public List<BlockTransaction> Transactions { get; init; }
```

The category, version (either a legacy Deploy or the new TransactionV1 type), and the hash are provided for each transaction.

`Deploy`s in V1 blocks are categorized as `Mint` for native transfer deploys and `Large` for all the rest. The same happens for legacy deploys sent to a Casper v2.x node.

### Block height type

The type to represent block height is now `ulong` everywhere. `int` was used in some methods or types in the previous
version. That's not the case with Casper .NET SDK v3.

## Transfers

The `Transfer` class contained in the response for a `GetBlockTransfers()` call or in a `ExecutionResult` object is also a versioned object. It contains all the information related to the transfer.

If you need to recover the original format, cast an instance to a `TransferV1` or `TransferV2` object:

```csharp
var response = await rpcClient.GetBlockTransfers();
var result = response.Parse();
foreach(var transfer in result.Transfers)
{
if (transfer.Version == 1) {
var transferv1 = (TransferV1)transfer;
// ...
}
}
```

## Account/Contract Merge

On Condor, accounts and contracts are stored with the new type AddressableEntity. The EntityKind property in this type permits knowing whether the record is an Account, a stored SmartContract, or a System contract.

### GetEntity RPC method

Use the new method GetEntity(IEntityIdentifier) in the RPC interface to retrieve a record. PublicKey, AccountHashKey, and AddressableEntityKey implement the IEntityIdentiifer interface and can be used to retrieve an AddressableEntity from the network. While PublicKey and AccountHashKeyare known types from Casper v1.x, the AddressableEntitykey is new, but the developer must become familiar with it to work with Condor. Some examples of this key are:

```
entity-account-2f3fb80d362ad0a922f446915a259c9aaec9ba99292b3e50ff2359c458007309
entity-contract-a5cf5917505ef60a6f0df395dd19e86a0f075d00f2e6ce49f5aa0e18f6e26f5d
entity-system-a1b5f200a58533875ef83cb98de14f128342b34162cbc14d4f41f3ccbc451dc3
```

### Legacy accounts

Existing accounts are migrated either during the Condor upgrade or when they interact with the network for the first time after the upgrade. The exact time depends on the `chainspec` agreed for the Condor upgrade. Hash values for the `AccountHashKey` and the `EntityKey` are shared and remain unchanged for migrated accounts.

Non-migrated accounts can also be retrieved using the new `GetEntity` method. In this case, the response contains the account info in the `LegacyAccount` property instead of the `Entity` property.

Alternatively, the legacy method `GetAccountInfo` also works for non-migrated accounts.

### Contract information

Like accounts, contract records have also been migrated to the new `AddressableEntity`. After the migration, only the `GetEntity` method can be used to retrieve contract information. Hash values for contracts and packages remain unchanged for the migrated records.

To retrieve information about the contract package, use the `QueryGlobalState` method with the `PackageKey` of the contract.

### Backwards compatibility

When using the SDK v3 with a Casper v1.x network, only GetAccountInfo can be used to retrieve account information. For contract and package information use QueryGlobalState.

## Balances

The new `NoFee` mode in Condor introduces the concept of a ‘balance hold’. In this mode of operation, for each transaction, the network holds the amount paid for its execution in the paying purse. Thus, entities have a total balance and an available balance, where the available balance is equal to the total balance minus the balance holds.

To get detailed information on an entity balance, use the new method `QueryBalanceDetails(IPurseIdentifier)` with a purse identifier. `PublicKey`, `AccountHashKey`, `AddressableEntityKey`, and `URef` keys implement the `IPurseIdentifier` interface and can be used to retrieve the balance details for an entity.

`GetAccountBalance` has been renamed to `GetBalance` since this method can be used for any type of entity, not only for accounts.

## Deploys and Transactions

Condor introduces a new transaction model to support advanced use cases. While `Deploy`s continue to work in Condor, this type is deprecated, and Casper recommends switching to the new `TransactionV1` type.

Similar to the `DeployTemplates` class, which provides deploy templates for most common use cases, we plan to implement a TransactionV1Templates class for the new transaction model in the next release of this SDK.

Use the new method `PutTransaction` to send a `TransactionV1` to the network. Use the new `GetTransaction` method to retrieve an accepted transaction. Both methods can also be used to send and retrieve a Deploy. For unprocessed transactions, the ExecutionInfo in the response is `null`. Upon processing, this property contains all information about the execution, including cost, payments, errors (if any), and execution effects.


### GetDeploy()

Response from the `GetDeploy()` method has changed. Instead of a list of `ExecutionResult` objects, it now returns an instance of `ExecutionInfo` for a processed deploy. This instance contains block information and a results object.

### Payments and costs

For a transaction (old and new versions) processed in Condor, the execution results object contains three properties related to the gas consumed and the CSPR tokens paid:

- `limit`: The maximum allowed gas limit for the transaction.
- `consumed`: Gas consumed executing the transaction.
- `cost`: CSPR paid/held for the transaction.

In the NoFee model, the user does not specify any amount for payment. Instead, he must use the Fixed pricing mode with a gas price tolerance. The network chainspec defines the gas limit for each transaction category (see the ‘Transactions included in a block’ section above). Then, the network determines a gas price for each era based on the previous era's load. This price works as a multiplier for the consumed gas and relates to the gas price tolerance specified in the transaction. The transaction won't be processed if the tolerance is lower than the gas price. The gas consumed in a transaction must always be lower than the limit, or it will fail with an out-of-gas error. Finally, the cost in the no-fee model is the limit multiplied by the gas price.

### Execution results

The `ExecutionResult` class is a versioned object. Deploys processed before the Condor upgrade are stored in the global state as `ExecutionResultV1`records, and deploys and transactions processed after the Condor upgrade are stored as `ExecutionResultV2` records.

For a processed transaction, the `GetTransaction` method always returns an `ExecutionResult` instance regardless of the version. It has the same fields as `ExecutionResultV2`. The user can obtain the original structure by casting this instance to the correct version:

```csharp
if (executionResult.Version == 2) {
var executionResultV2 = (ExecutionResultV2)executionResult;
// ...
} else if (executionResult.Version == 1) {
var executionResultV1 = (ExecutionResultV1)executionResult;
// ...
}
```

The Effect property contains a list of Transforms that modify the global state due to the transaction execution. Note that the ExecutionEffect type in Casper v1.x, which also contained a list of operations in addition to the transforms, has been removed in Condor execution results.

## Auction contract

The auction contract also has changed in Condor. If your application tracks validator bids, rewards, and delegators, you must rework how network responses are parsed and interpreted. A complete description of the changes cannot covered in this guide.

## Server Sent Events

The ServerEventsClient class can listen to an event stream from a v1.x node and from v2.x.

### Blocks

The BlockedAdded event contains a Block object. The original block structure, BlockV1 or BlockV2, can be obtained with the cast operator, as described above.

### Transactions

On Condor, the `DeployAccepted`, `DeployProcessed`, and `DeployExpired` events are replaced with the equivalent `TransactionAccepted`, `TransactionProcessed`, and `TransactionExpired` events. These are emitted for both `Deploy` and `TransactionV1` types of transactions.

A `TransactionAccepted` event contains a `Transaction` property with either a `Deploy` or a `TransactionV1`. `TransactionProcessed` and `TransactionExpired` events contain a `TransactionHash` property with either a deploy hash or a transaction version 1 hash.

### Finality signatures

The `FinalitySignature` event contains an instance of the versioned `FinalitySignature` class. Version 2 of this type is an extension that contains all properties in version 1 plus the block height and the chain name hash.

The user can obtain the original structure by casting this instance to the correct version:

```csharp
if (finalitySignature.Version == 2) {
var finalitySignatureV2 = (FinalitySignatureV2)finalitySignature;
// ...
} else if (finalitySignature.Version == 1) {
var finalitySignatureV1 = (FinalitySignatureV1)finalitySignature;
// ...
}
```

## Other changes

### Last switch block hash

For a Condor network, the `GetNodeStatus` method can be used to get the latest switch block hash. The response contains the `LatestSwitchBlockHash` property with this value.

Also, for blocks produced in Casper 2.0, the `Block` instance contains the previous switch block hash in the `LastSwitchBlockHash` property.

### Checksums (CEP-57)

On SDK v3 only public keys are checksummed with the CEP-57 standard. The rest of the keys and hashes are not checksummed anymore.

0 comments on commit 7431909

Please sign in to comment.