FromBytes parses a Block from a byte slice, by unmarshaling and converting a RawBlock protobuf.
(bits []byte)
| 123 | // FromBytes parses a Block from a byte slice, by unmarshaling and |
| 124 | // converting a RawBlock protobuf. |
| 125 | func (b *Block) FromBytes(bits []byte) error { |
| 126 | var rb RawBlock |
| 127 | err := proto.Unmarshal(bits, &rb) |
| 128 | if err != nil { |
| 129 | return err |
| 130 | } |
| 131 | txs := make([]*Tx, len(rb.Transactions)) |
| 132 | var eg errgroup.Group |
| 133 | for i := range rb.Transactions { |
| 134 | i := i |
| 135 | eg.Go(func() error { |
| 136 | tx, err := NewTx(rb.Transactions[i].Program, rb.Transactions[i].Version, rb.Transactions[i].Runlimit) |
| 137 | if err != nil { |
| 138 | return err |
| 139 | } |
| 140 | if !tx.Finalized { |
| 141 | return txvm.ErrUnfinalized |
| 142 | } |
| 143 | txs[i] = tx |
| 144 | return nil |
| 145 | }) |
| 146 | } |
| 147 | err = eg.Wait() |
| 148 | if err != nil { |
| 149 | return err |
| 150 | } |
| 151 | b.UnsignedBlock = &UnsignedBlock{ |
| 152 | BlockHeader: rb.Header, |
| 153 | Transactions: txs, |
| 154 | } |
| 155 | for _, arg := range rb.Arguments { |
| 156 | switch arg.Type { |
| 157 | case DataType_BYTES: |
| 158 | b.Arguments = append(b.Arguments, arg.Bytes) |
| 159 | case DataType_INT: |
| 160 | b.Arguments = append(b.Arguments, arg.Int) |
| 161 | case DataType_TUPLE: |
| 162 | b.Arguments = append(b.Arguments, arg.Tuple) |
| 163 | } |
| 164 | } |
| 165 | return nil |
| 166 | } |
| 167 | |
| 168 | // Bytes encodes the Block as a byte slice, by converting it to a |
| 169 | // RawBlock protobuf and marshaling that. |