github.com/MetalBlockchain/metalgo@v1.11.9/snow/choices/test_decidable.go (about) 1 // Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved. 2 // See the file LICENSE for licensing terms. 3 4 package choices 5 6 import ( 7 "context" 8 "fmt" 9 10 "github.com/MetalBlockchain/metalgo/ids" 11 ) 12 13 var _ Decidable = (*TestDecidable)(nil) 14 15 // TestDecidable is a test Decidable 16 type TestDecidable struct { 17 IDV ids.ID 18 AcceptV, RejectV error 19 StatusV Status 20 } 21 22 func (d *TestDecidable) ID() ids.ID { 23 return d.IDV 24 } 25 26 func (d *TestDecidable) Accept(context.Context) error { 27 switch d.StatusV { 28 case Unknown, Rejected: 29 return fmt.Errorf("invalid state transition from %s to %s", 30 d.StatusV, Accepted) 31 default: 32 d.StatusV = Accepted 33 return d.AcceptV 34 } 35 } 36 37 func (d *TestDecidable) Reject(context.Context) error { 38 switch d.StatusV { 39 case Unknown, Accepted: 40 return fmt.Errorf("invalid state transition from %s to %s", 41 d.StatusV, Rejected) 42 default: 43 d.StatusV = Rejected 44 return d.RejectV 45 } 46 } 47 48 func (d *TestDecidable) Status() Status { 49 return d.StatusV 50 } 51 52 func (d *TestDecidable) SetStatus(status Status) { 53 d.StatusV = status 54 }