-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathmsg_server_submit_batch_metadata.go
More file actions
106 lines (88 loc) · 3.97 KB
/
msg_server_submit_batch_metadata.go
File metadata and controls
106 lines (88 loc) · 3.97 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
package keeper
import (
"context"
"strconv"
"cosmossdk.io/store/prefix"
rolluptypes "github.com/airchains-network/junction/x/rollup/types"
"github.com/cosmos/cosmos-sdk/runtime"
sdk "github.com/cosmos/cosmos-sdk/types"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
func (k msgServer) SubmitBatchMetadata(goCtx context.Context, msg *rolluptypes.MsgSubmitBatchMetadata) (*rolluptypes.MsgSubmitBatchMetadataResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
storeAdapter := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx))
batchNo := msg.BatchNo
rollupId := msg.RollupId
daName := msg.DaName
daCommitment := msg.DaCommitment
daBlockHash := msg.DaHash
daPointer := msg.DaPointer
daNamespace := msg.DaNamespace
batchStorePath, batchKeyByte := k.GetRollupBatchDbStoreKeys(rollupId, batchNo)
batchDataStore := prefix.NewStore(storeAdapter, rolluptypes.KeyPrefix(batchStorePath))
// Check if the batch metadata already exists
if batchDataStore.Has(batchKeyByte) {
return nil, status.Error(codes.AlreadyExists, "batch metadata already exists")
}
var previousMerkleRootHash string
if batchNo == 0 {
return nil, status.Error(codes.InvalidArgument, "batch number must be greater than 0")
}
if batchNo < 2 {
previousMerkleRootHash = "initial_batch_no_previous_hash" // This indicates that there is no previous Merkle root hash for the initial batch
} else {
var previousBatch rolluptypes.Batch
_, previousBatchNo := k.GetRollupBatchDbStoreKeys(rollupId, batchNo-1)
previousBatchBytes := batchDataStore.Get(previousBatchNo)
k.cdc.MustUnmarshal(previousBatchBytes, &previousBatch)
previousMerkleRootHash = previousBatch.MerkleRootHash
}
// Create a new batch metadata entry
var batchMetadata = rolluptypes.Batch{
Submitter: msg.Creator,
RollupId: rollupId,
BatchNo: batchNo,
MerkleRootHash: "",
PreviousMerkleRootHash: previousMerkleRootHash,
ZkProof: nil,
PublicWitness: nil,
Timestamp: ctx.BlockTime().String(),
DaName: daName,
DaCommitment: daCommitment,
DaHash: daBlockHash,
DaPointer: daPointer,
DaNamespace: daNamespace,
IsFinalized: false,
}
// Marshal and store the batch metadata
batchDataBytes := k.cdc.MustMarshal(&batchMetadata)
batchDataStore.Set(batchKeyByte, batchDataBytes)
// updating rollup metadata
rollupDataStore := prefix.NewStore(storeAdapter, rolluptypes.KeyPrefix(rolluptypes.RollupDataKey))
rollupBytes := rollupDataStore.Get([]byte(rollupId))
var rollup rolluptypes.RollupMetadata
k.cdc.MustUnmarshal(rollupBytes, &rollup)
rollup.RollupLatestBatchNo = batchNo
rollup.DaLatestHash = daBlockHash
rollupBytes = k.cdc.MustMarshal(&rollup)
rollupDataStore.Set([]byte(rollupId), rollupBytes)
// Emit an event for the new batch metadata
ctx.EventManager().EmitEvent(sdk.NewEvent(
rolluptypes.EventTypeBatchMetadataSubmitted,
sdk.NewAttribute(rolluptypes.AttributeKeySubmitter, batchMetadata.Submitter),
sdk.NewAttribute(rolluptypes.AttributeKeyBatchNo, strconv.FormatUint(batchMetadata.BatchNo, 10)),
sdk.NewAttribute(rolluptypes.AttributeKeyTimestamp, batchMetadata.Timestamp),
sdk.NewAttribute(rolluptypes.AttributeKeyRollupId, batchMetadata.RollupId),
sdk.NewAttribute(rolluptypes.AttributeKeyDaName, batchMetadata.DaName),
sdk.NewAttribute(rolluptypes.AttributeKeyDaCommitment, batchMetadata.DaCommitment),
sdk.NewAttribute(rolluptypes.AttributeKeyDaHash, batchMetadata.DaHash),
sdk.NewAttribute(rolluptypes.AttributeKeyDaPointer, batchMetadata.DaPointer),
sdk.NewAttribute(rolluptypes.AttributeKeyDaNamespace, batchMetadata.DaNamespace),
sdk.NewAttribute(rolluptypes.AttributeKeyPreviousMerkleRootHash, batchMetadata.PreviousMerkleRootHash),
sdk.NewAttribute(rolluptypes.AttributeKeyIsFinalized, strconv.FormatBool(batchMetadata.IsFinalized)),
))
return &rolluptypes.MsgSubmitBatchMetadataResponse{
Status: true,
}, nil
}