code.vegaprotocol.io/vega@v0.79.0/core/examples/nullchain/timeforward.go (about)

     1  // Copyright (C) 2023 Gobalsky Labs Limited
     2  //
     3  // This program is free software: you can redistribute it and/or modify
     4  // it under the terms of the GNU Affero General Public License as
     5  // published by the Free Software Foundation, either version 3 of the
     6  // License, or (at your option) any later version.
     7  //
     8  // This program is distributed in the hope that it will be useful,
     9  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    10  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    11  // GNU Affero General Public License for more details.
    12  //
    13  // You should have received a copy of the GNU Affero General Public License
    14  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    15  
    16  package nullchain
    17  
    18  import (
    19  	"bytes"
    20  	"encoding/json"
    21  	"errors"
    22  	"fmt"
    23  	"io/ioutil"
    24  	"net/http"
    25  	"time"
    26  
    27  	config "code.vegaprotocol.io/vega/core/examples/nullchain/config"
    28  )
    29  
    30  var ErrTimeForward = errors.New("time forward failed")
    31  
    32  func move(raw string) error {
    33  	values := map[string]string{"forward": raw}
    34  
    35  	jsonValue, _ := json.Marshal(values)
    36  
    37  	r, err := http.Post(config.TimeforwardAddress, "application/json", bytes.NewBuffer(jsonValue))
    38  	if err != nil {
    39  		return fmt.Errorf("time forward failed: %w", err)
    40  	}
    41  	defer r.Body.Close()
    42  
    43  	if r.StatusCode == http.StatusOK {
    44  		return nil
    45  	}
    46  
    47  	data, err := ioutil.ReadAll(r.Body)
    48  	if err != nil {
    49  		return fmt.Errorf("time forward failed: %w", err)
    50  	}
    51  	return fmt.Errorf("%w: %s", ErrTimeForward, string(data))
    52  }
    53  
    54  func MoveByDuration(d time.Duration) error {
    55  	return move(d.String())
    56  }
    57  
    58  func MoveToDate(t time.Time) error {
    59  	return move(t.Format(time.RFC3339))
    60  }