Skip to content

Latest commit

 

History

History
103 lines (79 loc) · 3.94 KB

File metadata and controls

103 lines (79 loc) · 3.94 KB
title actions material
StorageMap & Serialisation
checkAnswer
hints
editor
language startingCode answer
c#
...... // Implement Helper here ...... public static BigInteger GenerateAlien (string alienName, byte[] owner) { if (owner.Length != 20 && owner.Length != 33) throw new InvalidOperationException ("The parameter owner should be a 20-byte address or a 33-byte public key"); if (!Runtime.CheckWitness (owner)) return 0; uint xna = FindXna (RandomNumber ()); Alien someAlien = new Alien { Xna = xna, AlienName = alienName, Id = updateCounter () }; // Implement (2) here // Implement (3) here Runtime.Notify (someAlien.Id, "created"); return someAlien.Id; } ......
...... using Helper = Neo.SmartContract.Framework.Helper; ...... public static BigInteger GenerateAlien (string alienName, byte[] owner) { if (owner.Length != 20 && owner.Length != 33) throw new InvalidOperationException ("The parameter owner should be a 20-byte address or a 33-byte public key"); if (!Runtime.CheckWitness (owner)) return 0; uint xna = FindXna (RandomNumber ()); Alien someAlien = new Alien { Xna = xna, AlienName = alienName, Id = updateCounter () }; StorageMap alienMap = Storage.CurrentContext.CreateMap (nameof (alienMap)); alienMap.Put (someAlien.Id.ToByteArray (), Helper.Serialize (someAlien)); Runtime.Notify (someAlien.Id, "created"); return someAlien.Id; } ......

Now that we are familiar with storing simple values, our goal now is to store all the Alien objects that we have created. Since the contract will create many aliens, we will need:

  1. A data structure that can contain a large collection of key-value pairs, and differentiate between each alien.
  2. A way to serialise objects into byte[] type that can be saved into storage

Creating and Using StorageMap

A Map data structure is a collection of key-value pairs, where each element has a unique key. StorageMap is a type provided by Neo framework to read and write data in a Map structure. It can be initialised like this:

StorageMap someMap = Storage.CurrentContext.CreateMap (nameof (someMap));

Note that the CreateMap () constructor is a member of the StorageContext class.

It has similar methods to the Storage class:

  • someMap.Get (key): returns the byte[] corresponding to the key
  • someMap.Put (key, value): Puts value into the corresponding key.
  • someMap.Delete (key)

The same as Storage, StorageMap methods accept string and byte[] as keys, and string, byte[] and BigInteger as values.

For saving Aliens we have created, we can construct a dedicated map, use each Alien's Id property as keys.

Note that BigInteger type has built-in ToByteArray method.

Serialising and Deserialising Objects

Neo provides an auxiliary class to help with conversion from objects to and from byte[]. They can be intialised at the start of a contract like this:

using Helper = Neo.SmartContract.Framework.Helper;

Here is an example of serialising and deserialisng objects with Helper:

// Serialise
byte[] value = Helper.Serialize (myCar); 

// Deserialise
Car obj = Helper.Deserialize (value) as Car; // Explicitly casts the deserialised object as type Car

Instructions

  1. Initiate Helper at the start of the file.
  2. In GenerateAlien (), Create a StorageMap type called alienMap
  3. Deserialise the newly created Alien, convert this Alien's Id to byte[], save the data with Id as the key, and Alien as the value. (try to do it in one line)