How to use serializeData method of prog Package

Best Syzkaller code snippet using prog.serializeData

instruction.go

Source:instruction.go Github

copy

Full Screen

1package bridgeprog2import (3 "crypto/sha256"4 "github.com/near/borsh-go"5 "github.com/stafiprotocol/solana-go-sdk/common"6 "github.com/stafiprotocol/solana-go-sdk/types"7)8type Instruction [8]byte9type Event [8]byte10var (11 InstructionCreateBridge Instruction12 InstructionCreateMintProposal Instruction13 InstructionApproveMintProposal Instruction14 InstructionSetOwners Instruction15 InstructionChangeThreshold Instruction16 InstructionSetResourceId Instruction17 InstructionTransferOut Instruction18 InstructionSetFeeReceiver Instruction19 InstructionSetFeeAmount Instruction20 InstructionSetSupportChainIds Instruction21 EventTransferOut Event22 ProgramLogPrefix = "Program log: "23 EventTransferOutPrefix = ProgramLogPrefix + "7arrB4Lk4L"24)25func init() {26 createBridgeHash := sha256.Sum256([]byte("global:create_bridge"))27 copy(InstructionCreateBridge[:], createBridgeHash[:8])28 createMintProposalHash := sha256.Sum256([]byte("global:create_mint_proposal"))29 copy(InstructionCreateMintProposal[:], createMintProposalHash[:8])30 approveMintProposalHash := sha256.Sum256([]byte("global:approve_mint_proposal"))31 copy(InstructionApproveMintProposal[:], approveMintProposalHash[:8])32 setOwnersHash := sha256.Sum256([]byte("global:set_owners"))33 copy(InstructionSetOwners[:], setOwnersHash[:8])34 changeThresholdHash := sha256.Sum256([]byte("global:change_threshold"))35 copy(InstructionChangeThreshold[:], changeThresholdHash[:8])36 setResourceIdHash := sha256.Sum256([]byte("global:set_resource_id"))37 copy(InstructionSetResourceId[:], setResourceIdHash[:8])38 transferOutHash := sha256.Sum256([]byte("global:transfer_out"))39 copy(InstructionTransferOut[:], transferOutHash[:8])40 eventTransferOutHash := sha256.Sum256([]byte("event:EventTransferOut"))41 copy(EventTransferOut[:], eventTransferOutHash[:8])42 setFeeReceiverHash := sha256.Sum256([]byte("global:set_fee_receiver"))43 copy(InstructionSetFeeReceiver[:], setFeeReceiverHash[:8])44 setFeeAmountHash := sha256.Sum256([]byte("global:set_fee_amount"))45 copy(InstructionSetFeeAmount[:], setFeeAmountHash[:8])46 setSupportChainIdsHash := sha256.Sum256([]byte("global:set_support_chain_ids"))47 copy(InstructionSetSupportChainIds[:], setSupportChainIdsHash[:8])48}49func CreateBridge(50 bridgeProgramID,51 bridgeAccount common.PublicKey,52 owners []common.PublicKey,53 threshold uint64,54 nonce uint8,55 supportChainIds []uint8,56 resourceIdToMint map[[32]byte]common.PublicKey,57 admin,58 feeReceiver common.PublicKey,59 feeAmounts map[uint8]uint64) types.Instruction {60 data, err := borsh.Serialize(struct {61 Instruction Instruction62 Owners []common.PublicKey63 Threshold uint6464 Nonce uint865 SupportChainIds []uint866 ResourceIdToTokenProg map[[32]byte]common.PublicKey67 Admin common.PublicKey68 FeeReceiver common.PublicKey69 FeeAmounts map[uint8]uint6470 }{71 Instruction: InstructionCreateBridge,72 Owners: owners,73 Threshold: threshold,74 Nonce: nonce,75 SupportChainIds: supportChainIds,76 ResourceIdToTokenProg: resourceIdToMint,77 Admin: admin,78 FeeReceiver: feeReceiver,79 FeeAmounts: feeAmounts,80 })81 if err != nil {82 panic(err)83 }84 return types.Instruction{85 ProgramID: bridgeProgramID,86 Accounts: []types.AccountMeta{87 {PubKey: bridgeAccount, IsSigner: false, IsWritable: true},88 {PubKey: common.SysVarRentPubkey, IsSigner: false, IsWritable: false},89 },90 Data: data,91 }92}93func ChangeThreshold(94 bridgeProgramID,95 bridgeAccount,96 adminAccount common.PublicKey,97 threshold uint64) types.Instruction {98 data, err := common.SerializeData(struct {99 Instruction Instruction100 Threshold uint64101 }{102 Instruction: InstructionChangeThreshold,103 Threshold: threshold,104 })105 if err != nil {106 panic(err)107 }108 accounts := []types.AccountMeta{109 {PubKey: bridgeAccount, IsSigner: false, IsWritable: true},110 {PubKey: adminAccount, IsSigner: true, IsWritable: false},111 }112 return types.Instruction{113 ProgramID: bridgeProgramID,114 Accounts: accounts,115 Data: data,116 }117}118func SetResourceId(119 bridgeProgramID,120 bridgeAccount,121 adminAccount common.PublicKey,122 resourceId [32]byte,123 mint common.PublicKey,124) types.Instruction {125 data, err := common.SerializeData(struct {126 Instruction Instruction127 ResourceId [32]byte128 Mint common.PublicKey129 }{130 Instruction: InstructionSetResourceId,131 ResourceId: resourceId,132 Mint: mint,133 })134 if err != nil {135 panic(err)136 }137 accounts := []types.AccountMeta{138 {PubKey: bridgeAccount, IsSigner: false, IsWritable: true},139 {PubKey: adminAccount, IsSigner: true, IsWritable: false},140 }141 return types.Instruction{142 ProgramID: bridgeProgramID,143 Accounts: accounts,144 Data: data,145 }146}147func SetSupportChainIds(148 bridgeProgramID,149 bridgeAccount,150 adminAccount common.PublicKey,151 chainIds []uint8,152) types.Instruction {153 data, err := common.SerializeData(struct {154 Instruction Instruction155 ChainIds []uint8156 }{157 Instruction: InstructionSetSupportChainIds,158 ChainIds: chainIds,159 })160 if err != nil {161 panic(err)162 }163 accounts := []types.AccountMeta{164 {PubKey: bridgeAccount, IsSigner: false, IsWritable: true},165 {PubKey: adminAccount, IsSigner: true, IsWritable: false},166 }167 return types.Instruction{168 ProgramID: bridgeProgramID,169 Accounts: accounts,170 Data: data,171 }172}173func SetOwners(174 bridgeProgramID,175 bridgeAccount,176 adminAccount common.PublicKey,177 owners []common.PublicKey,178) types.Instruction {179 data, err := common.SerializeData(struct {180 Instruction Instruction181 Owners []common.PublicKey182 }{183 Instruction: InstructionSetOwners,184 Owners: owners,185 })186 if err != nil {187 panic(err)188 }189 accounts := []types.AccountMeta{190 {PubKey: bridgeAccount, IsSigner: false, IsWritable: true},191 {PubKey: adminAccount, IsSigner: true, IsWritable: false},192 }193 return types.Instruction{194 ProgramID: bridgeProgramID,195 Accounts: accounts,196 Data: data,197 }198}199func SetFeeReceiver(200 bridgeProgramID,201 bridgeAccount,202 adminAccount common.PublicKey,203 feeReceiver common.PublicKey,204) types.Instruction {205 data, err := common.SerializeData(struct {206 Instruction Instruction207 FeeReceiver common.PublicKey208 }{209 Instruction: InstructionSetFeeReceiver,210 FeeReceiver: feeReceiver,211 })212 if err != nil {213 panic(err)214 }215 accounts := []types.AccountMeta{216 {PubKey: bridgeAccount, IsSigner: false, IsWritable: true},217 {PubKey: adminAccount, IsSigner: true, IsWritable: false},218 }219 return types.Instruction{220 ProgramID: bridgeProgramID,221 Accounts: accounts,222 Data: data,223 }224}225func SetFeeAmount(226 bridgeProgramID,227 bridgeAccount,228 adminAccount common.PublicKey,229 destChainId uint8,230 amount uint64,231) types.Instruction {232 data, err := common.SerializeData(struct {233 Instruction Instruction234 DestChainId uint8235 Amount uint64236 }{237 Instruction: InstructionSetFeeAmount,238 DestChainId: destChainId,239 Amount: amount,240 })241 if err != nil {242 panic(err)243 }244 accounts := []types.AccountMeta{245 {PubKey: bridgeAccount, IsSigner: false, IsWritable: true},246 {PubKey: adminAccount, IsSigner: true, IsWritable: false},247 }248 return types.Instruction{249 ProgramID: bridgeProgramID,250 Accounts: accounts,251 Data: data,252 }253}254type ProposalUsedAccount struct {255 Pubkey common.PublicKey256 IsSigner bool257 IsWritable bool258}259func CreateMintProposal(260 bridgeProgramID,261 bridgeAccount,262 proposalAccount,263 toAccount,264 proposerAccount common.PublicKey,265 resourceId [32]byte,266 amount uint64,267 tokenProgram common.PublicKey,268) types.Instruction {269 data, err := common.SerializeData(struct {270 Instruction Instruction271 ResourceId [32]byte272 Amount uint64273 TokenProgram common.PublicKey274 }{275 Instruction: InstructionCreateMintProposal,276 ResourceId: resourceId,277 Amount: amount,278 TokenProgram: tokenProgram,279 })280 if err != nil {281 panic(err)282 }283 return types.Instruction{284 ProgramID: bridgeProgramID,285 Accounts: []types.AccountMeta{286 {PubKey: bridgeAccount, IsSigner: false, IsWritable: false},287 {PubKey: proposalAccount, IsSigner: false, IsWritable: true},288 {PubKey: toAccount, IsSigner: false, IsWritable: false},289 {PubKey: proposerAccount, IsSigner: true, IsWritable: false},290 {PubKey: common.SysVarRentPubkey, IsSigner: false, IsWritable: false},291 },292 Data: data,293 }294}295func ApproveMintProposal(296 bridgeProgramID,297 bridgeAccount,298 multiSiner,299 proposalAccount,300 approverAccount,301 mintAccount,302 toAccount,303 tokenProgram common.PublicKey,304) types.Instruction {305 data, err := common.SerializeData(struct {306 Instruction Instruction307 }{308 Instruction: InstructionApproveMintProposal,309 })310 if err != nil {311 panic(err)312 }313 accounts := []types.AccountMeta{314 {PubKey: bridgeAccount, IsSigner: false, IsWritable: false},315 {PubKey: multiSiner, IsSigner: false, IsWritable: false},316 {PubKey: proposalAccount, IsSigner: false, IsWritable: true},317 {PubKey: approverAccount, IsSigner: true, IsWritable: false},318 {PubKey: mintAccount, IsSigner: false, IsWritable: true},319 {PubKey: toAccount, IsSigner: false, IsWritable: true},320 {PubKey: tokenProgram, IsSigner: false, IsWritable: false},321 }322 return types.Instruction{323 ProgramID: bridgeProgramID,324 Accounts: accounts,325 Data: data,326 }327}328func TransferOut(329 bridgeProgramID,330 bridgeAccount,331 authorityAccount,332 mintAccount,333 fromAccount,334 feeReciever,335 tokenProgram,336 systemProgram common.PublicKey,337 amount uint64,338 receiver []byte,339 destChainId uint8,340) types.Instruction {341 data, err := common.SerializeData(struct {342 Instruction Instruction343 Amount uint64344 Receiver []byte345 DestChainId uint8346 }{347 Instruction: InstructionTransferOut,348 Amount: amount,349 Receiver: receiver,350 DestChainId: destChainId,351 })352 if err != nil {353 panic(err)354 }355 accounts := []types.AccountMeta{356 {PubKey: bridgeAccount, IsSigner: false, IsWritable: true},357 {PubKey: authorityAccount, IsSigner: true, IsWritable: false},358 {PubKey: mintAccount, IsSigner: false, IsWritable: true},359 {PubKey: fromAccount, IsSigner: false, IsWritable: true},360 {PubKey: feeReciever, IsSigner: false, IsWritable: true},361 {PubKey: tokenProgram, IsSigner: false, IsWritable: false},362 {PubKey: systemProgram, IsSigner: false, IsWritable: false},363 }364 return types.Instruction{365 ProgramID: bridgeProgramID,366 Accounts: accounts,367 Data: data,368 }369}...

Full Screen

Full Screen

manager.go

Source:manager.go Github

copy

Full Screen

...79func (o *CkpManager) Start() error {80 o.Lock()81 defer o.Unlock()82 // Save checkpoint info at start, helps to check whether have problems in storage.83 buf, err := o.serializeData()84 if err != nil {85 return err86 }87 err = o.storage.Save(buf)88 if err != nil {89 log.Errorf("save checkpoint error: %s", err)90 return err91 }92 var ctx context.Context93 ctx, o.cancel = context.WithCancel(context.Background())94 go o.run(ctx)95 return nil96}97// Clean any ckp in the o.ckps that is not in use98func (o *CkpManager) clean() {99 var serverID uint32100 oneServerID := true101 for name, ckp := range o.ckps {102 if _, ok := o.reg[name]; !ok {103 delete(o.ckps, name)104 continue105 }106 if serverID == 0 {107 serverID = ckp.ServerID108 continue109 }110 if serverID != 0 && ckp.ServerID != serverID {111 oneServerID = false112 }113 }114 if oneServerID {115 // All Checkpointer(sink)'s server_id are same, not need alignedProgress116 o.alignedProgress = nil117 }118}119func (o *CkpManager) SetAlignedProgress(p prog.Progress) error {120 o.Lock()121 defer o.Unlock()122 o.alignedProgress = &Progress{}123 o.alignedProgress.SetProgress(p)124 buf, err := o.serializeData()125 if err != nil {126 return err127 }128 err = o.storage.Save(buf)129 if err != nil {130 log.Errorf("save checkpoint error: %s", err)131 return err132 }133 return nil134}135func (o *CkpManager) GetMinProgress() prog.Progress {136 // If using gtid, we can not compare by gitd_set, because each Progress's gtid_set may137 // neither contain each other. And, we can not compare by file&pos when server_ids138 // of each Progress may be different. So, we use last aligned Progress as min Progress,139 // when server_ids of each Progress may be different140 o.Lock()141 defer o.Unlock()142 // Clean any ckp that is not used by any sink143 o.clean()144 var minProgress prog.Progress145 for _, ckp := range o.ckps {146 progress := ckp.GetProgress()147 if progress.IsZero() {148 continue149 }150 if minProgress.IsZero() {151 minProgress = progress152 continue153 }154 if progress.Pos.ServerID != minProgress.Pos.ServerID {155 // If Progress's server_id is not equal, we use last aligned Progress156 // as min Progress157 log.Warn("different server_id found in ckps, will use last aligned progress as min progress")158 if o.alignedProgress == nil {159 log.Panicf("last aligned progress is nil")160 }161 return o.alignedProgress.GetProgress()162 }163 if progress.Compare(&minProgress) < 0 {164 minProgress = progress165 }166 }167 return minProgress168}169func (o *CkpManager) GetCheckpoint(name string) *Checkpoint {170 o.Lock()171 defer o.Unlock()172 var ckp *Checkpoint173 ckp, ok := o.ckps[name]174 if !ok {175 log.Warnf("checkpoint name not found: %s", name)176 return NewCheckpoint()177 }178 return ckp179}180func (o *CkpManager) serializeData() ([]byte, error) {181 data := &Data{Time: time.Now(), AlignedProgress: o.alignedProgress, Ckps: o.ckps}182 buf, err := json.Marshal(data)183 if err != nil {184 o.Unlock()185 log.Errorf("marshal checkpoint content error: %s", err)186 return buf, err187 }188 return buf, nil189}190// WaitUntil wait all sinks to reach a specified progress191func (o *CkpManager) WaitUntil(ctx context.Context, progress prog.Progress) error {192 for {193 allDone := true194 select {195 case <-ctx.Done():196 return ctx.Err()197 default:198 o.Lock()199 for name, ckper := range o.reg {200 ckp := ckper.Checkpoint()201 o.ckps[name] = ckp202 p := ckp.GetProgress()203 // If server_id is different, we treat it as not done, need to wait204 done := p.Position().ServerID == progress.Position().ServerID &&205 p.Compare(&progress) >= 0206 if !done {207 allDone = false208 break209 }210 }211 if allDone {212 log.Infof("wait util all sinks to catch progress succeed: %s", progress)213 buf, err := o.serializeData()214 if err != nil {215 o.Unlock()216 return err217 }218 err = o.storage.Save(buf)219 o.Unlock()220 return err221 }222 o.Unlock()223 log.Infof("wait util all sinks to catch progress: %s", progress)224 time.Sleep(100 * time.Millisecond)225 }226 }227 return nil228}229func (o *CkpManager) RegisterCheckpointer(name string, ckper Checkpointer) error {230 o.Lock()231 defer o.Unlock()232 if o.reg[name] != nil {233 return fmt.Errorf("checkpointer exists already: %s", name)234 }235 o.reg[name] = ckper236 o.ckps[name] = ckper.Checkpoint()237 return nil238}239func (o *CkpManager) Err() <-chan error {240 return o.errCh241}242func (o *CkpManager) Close() error {243 log.Warnf("closing ckp manager")244 if o.cancel != nil {245 o.cancel()246 for range o.errCh {247 // drain248 }249 }250 if o.storage != nil {251 o.storage.Close()252 }253 return nil254}255func (o *CkpManager) run(ctx context.Context) {256 defer close(o.errCh)257 ticker := time.NewTicker(time.Duration(o.cfg.Interval) * time.Second)258 tick := 0259 for {260 select {261 case <-ticker.C:262 o.Lock()263 for name, ckper := range o.reg {264 ckp := ckper.Checkpoint()265 o.ckps[name] = ckp266 }267 buf, err := o.serializeData()268 if err != nil {269 o.Unlock()270 o.errCh <- err271 return272 }273 if tick%50 == 0 {274 // Print checkpoint info into log every 50 tick275 log.Infof("checkpoint info: %s", buf)276 }277 err = o.storage.Save(buf)278 o.Unlock()279 if err != nil {280 log.Errorf("save checkpoint error: %s", err)281 o.errCh <- err...

Full Screen

Full Screen

serializeData

Using AI Code Generation

copy

Full Screen

1import (2type prog struct {3}4func (p prog) serializeData() string {5 data, err := json.Marshal(p)6 if err != nil {7 fmt.Println(err)8 }9 return string(data)10}11func main() {12 p := prog{13 }14 fmt.Println(p.serializeData())15}16{"Name":"Naveen","Age":25}17Method 2: Using MarshalIndent()18MarshalIndent() is similar to Marshal() but it returns a JSON with indentation. The method signature of MarshalIndent() is as follows:19func MarshalIndent(v interface{}, prefix, indent string) ([]byte, error)20import (21type prog struct {22}23func main() {24 p := prog{25 }26 data, err := json.MarshalIndent(p, "", " ")27 if err != nil {28 fmt.Println(err)29 }30 fmt.Println(string(data))31}32{33}34import

Full Screen

Full Screen

serializeData

Using AI Code Generation

copy

Full Screen

1import "prog"2func main() {3 p.SerializeData()4}5type Prog struct {6}7func (p *Prog) SerializeData() {8}9type Prog struct {10}11func (p *Prog) ParseData() {12}13import "testing"14func TestParseData(t *testing.T) {15}16import "testing"17func TestSerializeData(t *testing.T) {18}19import "fmt"20var (21func main() {22 fmt.Println(a + b)23}

Full Screen

Full Screen

serializeData

Using AI Code Generation

copy

Full Screen

1import "fmt"2func main() {3 p.serializeData()4 fmt.Println("p.name =", p.name)5 fmt.Println("p.age =", p.age)6 fmt.Println("p.salary =", p.salary)7 fmt.Println("p.active =", p.active)8}

Full Screen

Full Screen

serializeData

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 p := prog.Prog{4 }5 prog.SerializeData(p)6}

Full Screen

Full Screen

serializeData

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 p := prog{}4 reader := bufio.NewReader(os.Stdin)5 fmt.Print("Enter your name: ")6 name, _ := reader.ReadString('7 p.setName(name)8 fmt.Print("Enter your age: ")9 fmt.Scan(&age)10 p.setAge(age)11 p.serializeData()12}13import (14func main() {15 p := prog{}16 p.deserializeData()17 fmt.Println("Name:", p.getName())18 fmt.Println("Age:", p.getAge())19}20import (21func main() {22 p := prog{}23 reader := bufio.NewReader(os.Stdin)24 fmt.Print("Enter your name: ")25 name, _ := reader.ReadString('26 p.setName(name)27 fmt.Print("Enter your age: ")28 fmt.Scan(&age)29 p.setAge(age)30 p.saveData()31}

Full Screen

Full Screen

serializeData

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 progObj := prog.Prog{}4 dataObj := prog.Data{}5 serializedData := progObj.SerializeData(dataObj)6 fmt.Println("Serialized Data:", serializedData)7}8Serialized Data: {"Name":"Anil","Age":40,"Salary":100000}9Your name to display (optional):

Full Screen

Full Screen

serializeData

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 var data interface{}4 fmt.Println("Original Data:", data)5 serializedData := serializeData(data)6 fmt.Println("Serialized Data:", serializedData)7 deserializedData := deserializeData(serializedData)8 fmt.Println("Deserialized Data:", deserializedData)9}10func serializeData(data interface{}) []byte {11 serializedData := reflect.ValueOf(data).Bytes()12}13func deserializeData(serializedData []byte) interface{} {14 deserializedData := reflect.ValueOf(serializedData).Interface()15}

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Syzkaller automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used method in

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful