-
Notifications
You must be signed in to change notification settings - Fork 68
/
bridgectrl.go
93 lines (81 loc) · 2.62 KB
/
bridgectrl.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package bridgectrl
import (
"context"
"math"
"github.com/0xPolygonHermez/zkevm-bridge-service/etherman"
"github.com/0xPolygonHermez/zkevm-bridge-service/log"
"github.com/0xPolygonHermez/zkevm-bridge-service/utils/gerror"
"github.com/jackc/pgx/v4"
)
const (
// KeyLen is the length of key and value in the Merkle Tree
KeyLen = 32
)
// BridgeController struct
type BridgeController struct {
exitTrees []*MerkleTree
rollupsTree *MerkleTree
networkIDs map[uint]uint8
}
// NewBridgeController creates new BridgeController.
func NewBridgeController(ctx context.Context, cfg Config, networks []uint, mtStore interface{}) (*BridgeController, error) {
var (
networkIDs = make(map[uint]uint8)
exitTrees []*MerkleTree
)
for i, network := range networks {
networkIDs[network] = uint8(i)
mt, err := NewMerkleTree(ctx, mtStore.(merkleTreeStore), cfg.Height, network)
if err != nil {
return nil, err
}
exitTrees = append(exitTrees, mt)
}
rollupsTree, err := NewMerkleTree(ctx, mtStore.(merkleTreeStore), cfg.Height, math.MaxInt32)
if err != nil {
log.Error("error creating rollupsTree. Error: ", err)
return nil, err
}
return &BridgeController{
exitTrees: exitTrees,
rollupsTree: rollupsTree,
networkIDs: networkIDs,
}, nil
}
func (bt *BridgeController) GetNetworkID(networkID uint) (uint8, error) {
tID, found := bt.networkIDs[networkID]
if !found {
return 0, gerror.ErrNetworkNotRegister
}
return tID, nil
}
// AddDeposit adds deposit information to the bridge tree.
func (bt *BridgeController) AddDeposit(ctx context.Context, deposit *etherman.Deposit, depositID uint64, dbTx pgx.Tx) error {
leaf := hashDeposit(deposit)
tID, err := bt.GetNetworkID(deposit.NetworkID)
if err != nil {
return err
}
return bt.exitTrees[tID].addLeaf(ctx, depositID, leaf, deposit.DepositCount, dbTx)
}
// ReorgMT reorg the specific merkle tree.
func (bt *BridgeController) ReorgMT(ctx context.Context, depositCount uint, networkID uint, dbTx pgx.Tx) error {
tID, err := bt.GetNetworkID(networkID)
if err != nil {
return err
}
return bt.exitTrees[tID].resetLeaf(ctx, depositCount, dbTx)
}
// GetExitRoot returns the dedicated merkle tree's root.
// only use for the test purpose
func (bt *BridgeController) GetExitRoot(ctx context.Context, networkID int, dbTx pgx.Tx) ([]byte, error) {
return bt.exitTrees[networkID].getRoot(ctx, dbTx)
}
func (bt *BridgeController) AddRollupExitLeaf(ctx context.Context, rollupLeaf etherman.RollupExitLeaf, dbTx pgx.Tx) error {
err := bt.rollupsTree.addRollupExitLeaf(ctx, rollupLeaf, dbTx)
if err != nil {
log.Error("error adding rollupleaf. Error: ", err)
return err
}
return nil
}