github.com/stafiprotocol/go-substrate-rpc-client@v1.4.7/types/digest_item.go (about) 1 // Go Substrate RPC Client (GSRPC) provides APIs and types around Polkadot and any Substrate-based chain RPC calls 2 // 3 // Copyright 2020 Stafi Protocol 4 // 5 // Licensed under the Apache License, Version 2.0 (the "License"); 6 // you may not use this file except in compliance with the License. 7 // You may obtain a copy of the License at 8 // 9 // http://www.apache.org/licenses/LICENSE-2.0 10 // 11 // Unless required by applicable law or agreed to in writing, software 12 // distributed under the License is distributed on an "AS IS" BASIS, 13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 // See the License for the specific language governing permissions and 15 // limitations under the License. 16 17 package types 18 19 import "github.com/stafiprotocol/go-substrate-rpc-client/pkg/scale" 20 21 // DigestItem specifies the item in the logs of a digest 22 type DigestItem struct { 23 IsOther bool 24 AsOther Bytes // 0 25 IsAuthoritiesChange bool 26 AsAuthoritiesChange []AuthorityID // 1 27 IsChangesTrieRoot bool 28 AsChangesTrieRoot Hash // 2 29 IsSealV0 bool 30 AsSealV0 SealV0 // 3 31 IsConsensus bool 32 AsConsensus Consensus // 4 33 IsSeal bool 34 AsSeal Seal // 5 35 IsPreRuntime bool 36 AsPreRuntime PreRuntime // 6 37 } 38 39 func (m *DigestItem) Decode(decoder scale.Decoder) error { 40 b, err := decoder.ReadOneByte() 41 42 if err != nil { 43 return err 44 } 45 46 switch b { 47 case 0: 48 m.IsOther = true 49 err = decoder.Decode(&m.AsOther) 50 case 1: 51 m.IsAuthoritiesChange = true 52 err = decoder.Decode(&m.AsAuthoritiesChange) 53 case 2: 54 m.IsChangesTrieRoot = true 55 err = decoder.Decode(&m.AsChangesTrieRoot) 56 case 3: 57 m.IsSealV0 = true 58 err = decoder.Decode(&m.AsSealV0) 59 case 4: 60 m.IsConsensus = true 61 err = decoder.Decode(&m.AsConsensus) 62 case 5: 63 m.IsSeal = true 64 err = decoder.Decode(&m.AsSeal) 65 case 6: 66 m.IsPreRuntime = true 67 err = decoder.Decode(&m.AsPreRuntime) 68 } 69 70 if err != nil { 71 return err 72 } 73 74 return nil 75 } 76 77 func (m DigestItem) Encode(encoder scale.Encoder) error { 78 var err1, err2 error 79 switch { 80 case m.IsOther: 81 err1 = encoder.PushByte(0) 82 err2 = encoder.Encode(m.AsOther) 83 case m.IsAuthoritiesChange: 84 err1 = encoder.PushByte(1) 85 err2 = encoder.Encode(m.AsAuthoritiesChange) 86 case m.IsChangesTrieRoot: 87 err1 = encoder.PushByte(2) 88 err2 = encoder.Encode(m.AsChangesTrieRoot) 89 case m.IsSealV0: 90 err1 = encoder.PushByte(3) 91 err2 = encoder.Encode(m.AsSealV0) 92 case m.IsConsensus: 93 err1 = encoder.PushByte(4) 94 err2 = encoder.Encode(m.AsConsensus) 95 case m.IsSeal: 96 err1 = encoder.PushByte(5) 97 err2 = encoder.Encode(m.AsSeal) 98 case m.IsPreRuntime: 99 err1 = encoder.PushByte(6) 100 err2 = encoder.Encode(m.AsPreRuntime) 101 } 102 103 if err1 != nil { 104 return err1 105 } 106 if err2 != nil { 107 return err2 108 } 109 110 return nil 111 } 112 113 // AuthorityID represents a public key (an 32 byte array) 114 type AuthorityID [32]byte 115 116 // NewAuthorityID creates a new AuthorityID type 117 func NewAuthorityID(b [32]byte) AuthorityID { 118 return AuthorityID(b) 119 } 120 121 type SealV0 struct { 122 Signer U64 123 Signature Signature 124 } 125 126 type Seal struct { 127 ConsensusEngineID ConsensusEngineID 128 Bytes Bytes 129 } 130 131 // ConsensusEngineID is a 4-byte identifier (actually a [u8; 4]) identifying the engine, e.g. for Aura it would be 132 // [b'a', b'u', b'r', b'a'] 133 type ConsensusEngineID U32 134 135 type Consensus struct { 136 ConsensusEngineID ConsensusEngineID 137 Bytes Bytes 138 } 139 140 type PreRuntime struct { 141 ConsensusEngineID ConsensusEngineID 142 Bytes Bytes 143 }