forked from gitchain/gitchain
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rut.go
65 lines (54 loc) · 1.41 KB
/
rut.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
// Reference Update Transaction (RUT)
package transaction
import (
"encoding/gob"
"encoding/hex"
"encoding/json"
"fmt"
"github.com/gitchain/gitchain/repository"
"github.com/gitchain/gitchain/types"
)
func init() {
gob.Register(&ReferenceUpdate{})
}
const (
REFERENCE_UPDATE_VERSION = 1
)
type ReferenceUpdate struct {
Version uint32
Repository string
Ref string
Old repository.Ref
New repository.Ref
}
func (tx *ReferenceUpdate) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]interface{}{
"Type": "Reference Update Tranasction",
"Version": tx.Version,
"Repository": tx.Repository,
"Ref": tx.Ref,
"Old": hex.EncodeToString(tx.Old),
"New": hex.EncodeToString(tx.New),
})
}
func NewReferenceUpdate(repository, ref string, old, new repository.Ref) *ReferenceUpdate {
return &ReferenceUpdate{
Version: REFERENCE_UPDATE_VERSION,
Repository: repository,
Ref: ref,
Old: old,
New: new}
}
func (txn *ReferenceUpdate) Valid() bool {
return (txn.Version == REFERENCE_UPDATE_VERSION && len(txn.Repository) > 0 &&
len(txn.Ref) > 0)
}
func (txn *ReferenceUpdate) Encode() ([]byte, error) {
return encode(txn)
}
func (txn *ReferenceUpdate) Hash() types.Hash {
return hash(txn)
}
func (txn *ReferenceUpdate) String() string {
return fmt.Sprintf("RUT %s %s %s:%s", txn.Repository, txn.Ref, txn.Old, txn.New)
}