-
Notifications
You must be signed in to change notification settings - Fork 330
/
Copy pathactionindex.go
65 lines (55 loc) · 1.77 KB
/
actionindex.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Copyright (c) 2019 IoTeX Foundation
// This source code is provided 'as is' and no warranties are given as to title or non-infringement, merchantability
// or fitness for purpose and, to the extent permitted by law, all liability for your use of the code is disclaimed.
// This source code is governed by Apache License 2.0 that can be found in the LICENSE file.
package blockindex
import (
"github.com/pkg/errors"
"google.golang.org/protobuf/proto"
"github.com/iotexproject/iotex-core/v2/blockindex/indexpb"
"github.com/iotexproject/iotex-core/v2/pkg/util/byteutil"
)
// ActionIndex change private to public for mock Indexer
type ActionIndex struct {
blkHeight uint64
// txNumber is the %-th of action in a block
// txNumber starts from 1
// 0 means no txNumber for backward compatibility
txNumber uint32
}
// Height returns the block height of action
func (a *ActionIndex) BlockHeight() uint64 {
return a.blkHeight
}
// TxNumber returns the transaction number of action
func (a *ActionIndex) TxNumber() uint32 {
return a.txNumber
}
// Serialize into byte stream
func (a *ActionIndex) Serialize() []byte {
return byteutil.Must(proto.Marshal(a.toProto()))
}
// Deserialize from byte stream
func (a *ActionIndex) Deserialize(buf []byte) error {
pb := &indexpb.ActionIndex{}
if err := proto.Unmarshal(buf, pb); err != nil {
return err
}
return a.fromProto(pb)
}
// toProto converts to protobuf
func (a *ActionIndex) toProto() *indexpb.ActionIndex {
return &indexpb.ActionIndex{
BlkHeight: a.blkHeight,
TxNumber: a.txNumber,
}
}
// fromProto converts from protobuf
func (a *ActionIndex) fromProto(pbIndex *indexpb.ActionIndex) error {
if pbIndex == nil {
return errors.New("empty protobuf")
}
a.blkHeight = pbIndex.BlkHeight
a.txNumber = pbIndex.TxNumber
return nil
}