当前位置:网站首页>Amino encoding protocol
Amino encoding protocol
2022-06-13 07:57:00 【Boiled soybeans in brine】
amino Is the object encoding specification . It is Proto3 Subset , With interface support extension
amino And Proto3 Largely compatible with ( But with Proto2 Are not compatible )
amino The goal of the agreement
- Parity is divided into logical objects and persistent objects through support interfaces .
- Have unique / Deterministic value coding .
- Binary bytes must be decoded using a pattern .
- Patterns must be upgradeable .
- No pattern , Enough structures must be parsed .
- Encoder and decoder logic must be fairly simple .
- Serialization must be reasonably compact .
- Sufficient compatibility must be maintained JSON Format ( But not with JSON Make a general conversion )
Amino vs JSON
JavaScript Object Notation(JSON) Easy to read , Reasonable structure , Very suitable for Javascript interoperate , But it's inefficient . Why does it exist Protobuf3,BER,RLP, Because we need to be more compact , A more efficient binary coding standard .Amino For complex objects ( For example, embedded objects ) Provides effective binary encoding , These objects are naturally integrated with your favorite modern programming languages . Besides ,Amino With full compatibility JSON code
Amino vs Protobuf3
Amino Hope to become Protobuf4
- Protobuf3 Interface not supported . It supports
oneof, As a union type , But in Modern Languages ( for example C ++ class ,Java Interface / class ,Go Interface / The implementation and Rust characteristic ) Can not be well converted to “ Interface ” and “ Realization ”
If Protobuf Interface support , The user of the externally defined schema file will be able to support the specific type of the interface defined by the caller . contrary ,oneofProtobuf3 The functional requirements of oneof The specific type is pre declared in the definition of the field
Amino Detailed rules for the
Interface
Amino It is a coding library that can handle interfaces . It's through every “ The specific type ” The byte prefix was added before .
The specific type is the non interface type that implements the registered interface . Not all types need to be registered as concrete types - Only if they will be stored in the interface type field ( Or a list of interface elements ) In the middle of the day , You need to register them . The specific types of interface registration and implementation shall be carried out during program initialization , To detect any problems
Registration type
To encode and decode the interface , You must register with them ,codec.RegisterInterface
amino.RegisterInterface((*MyInterface1)(nil), nil)
amino.RegisterInterface((*MyInterface2)(nil), nil)
amino.RegisterConcrete(MyStruct1{
}, "com.tendermint/MyStruct1", nil)
amino.RegisterConcrete(MyStruct2{
}, "com.tendermint/MyStruct2", nil)
amino.RegisterConcrete(&MyStruct3{
}, "anythingcangoinhereifitsunique", nil)
Prefix bytes to identify specific types
All registered specific types are previously 4 Bytes ( be called “ Prefix bytes ”) Encoding , Even if it is not in the interface field / The same is true of elements . such ,Amino To ensure that ( almost ) Concrete types always have the same canonical representation . The first byte of prefix byte cannot be zero byte
When you register 1024 A concrete type that implements the same interface , The likelihood of conflict is about 0.01%
Prefix bytes never start with zero bytes , Therefore, ambiguous bytes are represented by 0x00 Transference .
4 Prefix bytes always precede the binary encoding of a specific type
Calculate prefix and ambiguous bytes
To calculate the disambiguation bytes , We take hash := sha256(concreteTypeName) And delete leading 0x00 byte .
> hash := sha256("com.tendermint.consensus/MyConcreteName")
> hex.EncodeBytes(hash) // 0x{00 00 A8 FC 54 00 00 00 BB 9C 83 DD ...} (example)
In the example above ,hash There are two leading 0x00 byte , So we delete it .
> rest = dropLeadingZeroBytes(hash) // 0x{A8 FC 54 00 00 00 BB 9C 83 DD ...}
> disamb = rest[0:3]
> rest = dropLeadingZeroBytes(rest[3:])
> prefix = rest[0:4]
Unsupported type
Floating point numbers
Floating point type is not recommended , because They are usually uncertain . If you need to use them , Please use field label amino:"unsafe".
边栏推荐
- [log4j2 log framework] sensitive character filtering
- JMeter common commands
- Selenium reports an error deprecationwarning: executable_ path has been deprecated, please pass in a Service object
- [MySQL] the most complete MySQL monitoring project
- Success logarithm of leetcode spells and potions
- [deep learning]: introduction to pytorch to project practice (XII) convolutional neural network: padding and stride
- Effective Go - The Go Programming Language
- 免费文件服务器储存技术
- MySQL summary
- 3. deploy the fabric2.2 cluster (use the official demo)
猜你喜欢

Index push down (ICP) for mysql5.6
![[redis problem] record a big key problem handling](/img/ad/787d5911fac2cc2105e439d906b67f.jpg)
[redis problem] record a big key problem handling

Openharmony notes ----------- (I)

Clickhouse column basic data type description

20 | pipeline oriented instruction design (Part 1): Modern CPU with multi-purpose

SolidWorks修改工程图中文字字体的方法

ES6 deleting an attribute of an object

基于paddlepaddle的新冠肺炎识别

Redis learning journey - cache exceptions (CACHE penetration, cache avalanche, cache breakdown)

母婴用品批发行业使用管理软件提高效率 实现降本增效
随机推荐
[deep learning]: introduction to pytorch to project practice (XII) convolutional neural network: padding and stride
21 | 面向流水线的指令设计(下):奔腾4是怎么失败的?
Logback log framework learning and problems
Redis master-slave replication - mentality detection mechanism
IIS中的网站访问excel
Practice makes sense -- your byte alignment and stack cognition may be wrong
适合生鲜批发行业的几种精准接单方式
[MySQL change master error] slave is not configured or failed to initialize properly
[pytorch] pytorch0.4.0 installation tutorial and GPU configuration collection (including test code)
17. how to understand multi version concurrency control and read / write sets of fabric smart contracts? (vernacular version)
【PYTORCH】Expected object of type torch. xxxTensor but found type torch. cuda. xxxTensor(torch0.4.0)
2021-10-20
6. fabric2.2 stop clustering and delete data (use the official demo)
[MySQL] methods for troubleshooting lock related problems
[problem record] taberror: inconsistent use of tabs and spaces in indentation
Classification of databases
【Emgu.CV】Emgu. CV. Example\ocr operation reports an error system IO. Filenotfoundexception: "failed to load file or assembly" system.drawing.common "
OpenHarmony笔记-----------(一)
22 | adventure and prediction (I): hazard is both "danger" and "opportunity"
Hashtable source code analysis